Skip to content
This repository was archived by the owner on Sep 25, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 77 additions & 76 deletions src/org/minima/system/network/minidapps/NanoDAPPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
public class NanoDAPPServer extends NanoHTTPD{

DAPPManager mDAPPManager;

public NanoDAPPServer(int zPort, DAPPManager zDAPPManager) {
super(zPort);

mDAPPManager = zDAPPManager;
}

Expand All @@ -43,144 +43,144 @@ public Response serve(IHTTPSession session) {
try {
//GET or POST
Method method = session.getMethod();

//What are they looking for..
String fileRequested = session.getUri();
//MinimaLogger.log("GET "+fileRequested);

//Quick clean
if(fileRequested.endsWith("/")) {
fileRequested = fileRequested.concat("index.html");
}
if(fileRequested.startsWith("/")) {
fileRequested = fileRequested.substring(1);
}

//GET or POST
if(Method.GET.equals(method)) {
//Any parameters
Map<String, List<String>> params = session.getParameters();

//Is there an uninstall..
String uninst = "";
List<String> uninstall = params.get("uninstall");
if(uninstall != null) {
uninst = uninstall.get(0);
}

//Are we uninstalling a MiniDAPP
if(fileRequested.equals("index.html") && !uninst.equals("")) {
//UNINSTALL the DAPP
File appfolder = new File(mDAPPManager.getMiniDAPPSFolder(),uninst);

//Delete the app root..
BackupManager.deleteFileOrFolder(appfolder);

//Recalculate the MINIDAPPS
mDAPPManager.recalculateMiniDAPPS();

//Return the main index page..
String page = new String(indexhtml.returnData(),StandardCharsets.UTF_8);
String newpage = page.replace("######", createMiniDAPPList());
return getOKResponse(newpage.getBytes(), "text/html");
}

//Otherwise lets see..
if(fileRequested.endsWith("/minima.js") || fileRequested.equals("minima.js")) {
//MinimaLogger.log("MINIMA.JS REQUESTED!");
return getOKResponse(mDAPPManager.getMinimaJS() , "text/javascript");

}else if(fileRequested.startsWith("minidapps/")) {
//Send the MiniDAPP!
String fullfile = mDAPPManager.getMiniDAPPSFolder()+"/"+fileRequested.substring(10);
byte[] file = getFileBytes(fullfile);

if(file.length>0) {
return getOKResponse(file, getContentType(fullfile));
}else {
return getNotFoundResponse();
}

}else {
if(fileRequested.equals("index.html")) {
String page = new String(indexhtml.returnData(),StandardCharsets.UTF_8);
String newpage = page.replace("######", createMiniDAPPList());
return getOKResponse(newpage.getBytes(), "text/html");

}else if(fileRequested.equals("css/minidapps.css")) {
return getOKResponse(minidappscss.returnData(), "text/css");

}else if(fileRequested.equals("favicon.ico")) {
return getOKResponse(faviconico.returnData(), "image/ico");

}else if(fileRequested.equals("help.html")) {
return getOKResponse(helphtml.returnData(), "text/html");

}else if(fileRequested.equals("icon.png")) {
return getOKResponse(iconpng.returnData(), "image/png");

}else if(fileRequested.equals("installdapp.html")) {
return getOKResponse(installdapphtml.returnData(), "text/html");

}else if(fileRequested.equals("tile-grey.jpeg")) {
return getOKResponse(tilegreyjpeg.returnData(), "image/jpeg");

}else {
//Not found..
return getNotFoundResponse();
}
return getNotFoundResponse();
}
}

}else if(Method.POST.equals(method)) {
//Only on the Install DAPP page..
if(fileRequested.equals("installdapp.html")) {
//get the file..
Map<String, String> files = new HashMap<String, String>();
session.parseBody(files);

//Get the File..
String minidappfile = files.get("minidapp");

//Load the file..
byte[] file = getFileBytes(minidappfile);

//Create a MiniData Object..
MiniData dapp = new MiniData(file);

//POST it..
Message msg = new Message(DAPPManager.DAPP_INSTALL);
msg.addObject("minidapp", dapp);
mDAPPManager.PostMessage(msg);

return getOKResponse(installdapphtml.returnData(), "text/html");
}else if(fileRequested.startsWith("minidapps/")) {
String fullfile = mDAPPManager.getMiniDAPPSFolder()+"/"+fileRequested.substring(10);
byte[] file = getFileBytes(fullfile);

if(file.length>0) {
return getOKResponse(file, getContentType(fullfile));
}else {
return getNotFoundResponse();
}
}
}

return getNotFoundResponse();

} catch (Exception ioe) {
return getInternalErrorResponse("INTERNAL ERROR");
}
}

protected Response getOKResponse(byte[] zHTML, String zContentType) {
Response resp = Response.newFixedLengthResponse(Status.OK, zContentType, zHTML);
resp.addHeader("Server", "HTTP RPC Server from Minima v0.88");
resp.addHeader("Date", new Date().toString());
// resp.addHeader("Access-Control-Allow-Origin", "*");

return resp;
}

protected Response getForbiddenResponse(String s) {
return Response.newFixedLengthResponse(Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "FORBIDDEN: " + s);
}
Expand All @@ -192,19 +192,19 @@ protected Response getInternalErrorResponse(String s) {
protected Response getNotFoundResponse() {
return Response.newFixedLengthResponse(Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "Error 404, file not found.");
}


public String createMiniDAPPList() throws Exception {
StringBuilder list = new StringBuilder();

JSONArray alldapps = mDAPPManager.getMiniDAPPS();

list.append("<table width=100%>");

int len = alldapps.size();
for(int i=0;i<len;i++) {
JSONObject app = (JSONObject) alldapps.get(i);

//Now do it..
String root = (String) app.get("root");
String approot = (String) app.get("approot");
Expand All @@ -213,29 +213,30 @@ public String createMiniDAPPList() throws Exception {
String backg = root+"/"+(String) app.get("background");
String icon = root+"/"+(String) app.get("icon");
String webpage = root+"/index.html";

String openpage = "_"+name;

//Now do it..
list.append("<tr><td>" +
" <table style='background-size:100%;background-image: url("+backg+");' width=100% height=100 class=minidapp>" +
" <tr>" +
" <td style='cursor:pointer;' rowspan=2 onclick=\"window.open('"+webpage+"', '"+openpage+"');\">" +
" <img src='"+icon+"' height=100>" +
" </td>" +
" <td width=100% class='minidappdescription'>" +
" <div style='position:relative'>" +
" <div onclick='uninstallDAPP(\""+name+"\",\""+approot+"\");' style='color:red;cursor:pointer;position:absolute;right:10;top:10'>UNINSTALL</div>" +
" <br>" +
" <div onclick=\"window.open('"+webpage+"','"+openpage+"');\" style='cursor:pointer;font-size:18'><b>"+name.toUpperCase()+"</b></div>" +
" <br><div onclick=\"window.open('"+webpage+"','"+openpage+"');\" style='cursor:pointer;font-size:12'>"+desc+"</div>" +
list.append("<tr><td>" +
" <table style='background-size:100%;background-image: url("+backg+");' width=100% height=100 class=minidapp>" +
" <tr>" +
" <td style='cursor:pointer;' rowspan=2 onclick=\"window.open('"+webpage+"', '"+openpage+"');\">" +
" <img src='"+icon+"' height=100>" +
" </td>" +
" <td width=100% class='minidappdescription'>" +
" <div style='position:relative'>" +
" <div onclick='reInstallDAPP(\""+name+"\",\""+approot+"\");' style='color:blue;cursor:pointer;position:absolute;right:100;top:10'>REINSTALL</div>" +
" <div onclick='uninstallDAPP(\""+name+"\",\""+approot+"\");' style='color:red;cursor:pointer;position:absolute;right:10;top:10'>UNINSTALL</div>" +
" <br>" +
" <div onclick=\"window.open('"+webpage+"','"+openpage+"');\" style='cursor:pointer;font-size:18'><b>"+name.toUpperCase()+"</b></div>" +
" <br><div onclick=\"window.open('"+webpage+"','"+openpage+"');\" style='cursor:pointer;font-size:12'>"+desc+"</div>" +
" </div>"+
" </td>" +
" </tr>" +
" </table>" +
" </td>" +
" </tr>" +
" </table>" +
" </td></tr>");
}

if(len == 0) {
list.append("<tr><td style='text-align:center;'><br><br><b>NO DAPPS INSTALLED YET..</b>"
+ "<br><br><br>"
Expand All @@ -246,44 +247,44 @@ public String createMiniDAPPList() throws Exception {
+ "Go to <a href='http://mifi.minima.global/' target='_blank'>http://mifi.minima.global/</a> to find MiniDAPPs"
+ "</td></tr>");
}

list.append("</table>");

return list.toString();
}

public byte[] getFileBytes(String zFile) throws IOException {
File ff = new File(zFile);

long size = ff.length();
byte[] ret = new byte[(int) size];

try {
FileInputStream fis = new FileInputStream(zFile);
BufferedInputStream bis = new BufferedInputStream(fis);

bis.read(ret);

bis.close();
fis.close();

} catch (IOException e) {
e.printStackTrace();
}
}

return ret;
}

public static String getContentType(String zFile) {

String ending;
int dot = zFile.lastIndexOf(".");
if(dot != -1) {
ending = zFile.substring(dot+1);
}else {
return "text/plain";
}

if(ending.equals("html")) {
return "text/html";
}else if(ending.equals("htm")) {
Expand All @@ -296,7 +297,7 @@ public static String getContentType(String zFile) {
return "text/plain";
}else if(ending.equals("xml")) {
return "text/xml";

}else if(ending.equals("jpg")) {
return "image/jpeg";
}else if(ending.equals("jpeg")) {
Expand All @@ -309,18 +310,18 @@ public static String getContentType(String zFile) {
return "image/svg+xml";
}else if(ending.equals("ico")) {
return "image/ico";

}else if(ending.equals("zip")) {
return "application/zip";
}else if(ending.equals("pdf")) {
return "application/pdf";

}else if(ending.equals("mp3")) {
return "audio/mp3";
}else if(ending.equals("wav")) {
return "audio/wav";
}

return "text/plain";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class indexhtml {
public static byte[] FINAL_ARRAY = null;
static {
//0 - 1000
HEXDATA[0] = new MiniData("0x3C68746D6C3E0A3C686561643E0A0A093C6C696E6B2072656C3D227374796C6573686565742220747970653D22746578742F6373732220687265663D226373732F6D696E6964617070732E637373223E0A090A093C212D2D20203C6D657461206E616D653D2276696577706F72742220636F6E74656E743D2277696474683D3730302C20696E697469616C2D7363616C653D312C206D6178696D756D2D7363616C653D31223E202D2D3E0A0A3C2F686561643E0A0A3C626F6479207374796C653D226261636B67726F756E642D696D6167653A2075726C2874696C652D677265792E6A706567293B223E0A0A3C73637269707420747970653D22746578742F6A617661736372697074223E0A0A092F2F5761697420666F7220746865207061676520746F206C6F61642E2E0A0977696E646F772E6164644576656E744C697374656E657228226C6F6164222C2066756E6374696F6E28297B0A0909636F6E737420696E707574456C656D656E74203D20646F63756D656E742E676574456C656D656E7442794964282266696C652D696E70757422293B0A0909696E707574456C656D656E742E6164644576656E744C697374656E657228226368616E6765222C2068616E646C6546696C65732C2066616C7365293B0A09090A090966756E6374696F6E2068616E646C6546696C65732829207B0A09092020646F63756D656E742E676574456C656D656E74427949642827696E7374616C6C666F726D27292E7375626D697428293B0A09097D0A097D293B0A090A0A0966756E6374696F6E20696E7374616C6C4441505028297B0A09092F2F4F70656E20612066696C65206469616C6F672E2E0A0909646F63756D656E742E676574456C656D656E7442794964282766696C652D696E70757427292E636C69636B28293B0A097D0A090A0966756E6374696F6E20756E696E7374616C6C44415050286E616D652C20726F6F74297B0A0909696628636F6E6669726D282241726520796F75207375726520796F75207769736820746F20756E696E7374616C6C20222B6E616D652B22203F2229297B0A0909092F2F556E696E7374616C6C207468697320726F6F74206170702E2E0A09090977696E646F772E6C6F636174696F6E2E687265663D22696E6465782E68746D6C3F756E696E7374616C6C3D222B726F6F743B0A09097D0A097D0A0A3C2F7363726970743E0A0A3C7461626C6520616C69676E3D2263656E74657222207374796C653D2270616464696E673A33303B77696474683A3730303B6865696768743A313030253B223E0A3C74723E0A3C74643E0A090A093C212D2D204D41494E205449544C45202D2D3E0A093C7461626C6520636C6173733D226D61696E7469746C6522207769").getData();
HEXDATA[0] = new MiniData("0x3c68746d6c3e0a3c686561643e0a0a093c6c696e6b2072656c3d227374796c6573686565742220747970653d22746578742f6373732220687265663d226373732f6d696e6964617070732e637373223e0a0a093c212d2d20203c6d657461206e616d653d2276696577706f72742220636f6e74656e743d2277696474683d3730302c20696e697469616c2d7363616c653d312c206d6178696d756d2d7363616c653d31223e202d2d3e0a0a3c2f686561643e0a0a3c626f6479207374796c653d226261636b67726f756e642d696d6167653a2075726c2874696c652d677265792e6a706567293b223e0a0a3c73637269707420747970653d22746578742f6a617661736372697074223e0a0a097661722072656d6f766555524c203d2073657373696f6e53746f726167652e6765744974656d28277265696e7374616c6c27293b0a096966202872656d6f766555524c20213d3d206e756c6c29207b0a090973657373696f6e53746f726167652e72656d6f76654974656d28277265696e7374616c6c27293b0a090977696e646f772e6c6f636174696f6e2e687265663d72656d6f766555524c3b0a097d0a0a092f2f5761697420666f7220746865207061676520746f206c6f61642e2e0a0977696e646f772e6164644576656e744c697374656e657228226c6f6164222c2066756e6374696f6e28297b0a0909636f6e737420696e707574456c656d656e74203d20646f63756d656e742e676574456c656d656e7442794964282266696c652d696e70757422293b0a0909696e707574456c656d656e742e6164644576656e744c697374656e657228226368616e6765222c2068616e646c6546696c65732c2066616c7365293b0a0a090966756e6374696f6e2068616e646c6546696c65732829207b0a09092020646f63756d656e742e676574456c656d656e74427949642827696e7374616c6c666f726d27292e7375626d697428293b0a09097d0a097d293b0a0a0966756e6374696f6e20696e7374616c6c4441505028297b0a09092f2f4f70656e20612066696c65206469616c6f672e2e0a0909646f63756d656e742e676574456c656d656e7442794964282766696c652d696e70757427292e636c69636b28293b0a097d0a0a0966756e6374696f6e20756e696e7374616c6c44415050286e616d652c20726f6f74297b0a0909696628636f6e6669726d282241726520796f75207375726520796f75207769736820746f20756e696e7374616c6c20222b6e616d652b223f2229297b0a0909092f2f556e696e7374616c6c207468697320726f6f74206170702e2e0a09090977696e646f772e6c6f636174696f6e2e687265663d22696e6465782e68746d6c3f756e696e7374616c6c3d222b726f6f743b0a09097d0a097d0a0a0966756e6374696f6e207265496e7374616c6c44415050286e616d652c20726f6f7429207b0a0909696628636f6e6669726d2822506c65617365206e6f74652c20696620796f752068617665206e6f74206368616e67656420222b6e616d652b222c207468697320616374696f6e2077696c6c20756e696e7374616c6c2069742e2041726520796f75207375726520796f75207769736820746f2072652d696e7374616c6c20222b6e616d652b223f2229297b0a09090972656d6f766555524c203d2022696e6465782e68746d6c3f756e696e7374616c6c3d222b726f6f743b0a09090973657373696f6e53746f726167652e7365744974656d28277265696e7374616c6c272c2072656d6f766555524c293b0a090909646f63756d656e742e676574456c656d656e7442794964282766696c652d696e70757427292e636c69636b28293b0a09097d0a097d0a0a3c2f7363726970743e0a0a3c7461626c6520616c69676e3d2263656e74657222207374796c653d2270616464696e673a33303b77696474683a3730303b6865696768743a313030253b223e0a3c74723e0a3c74643e0a0a093c212d2d204d41494e205449544c45202d2d3e0a093c7461626c6520636c6173733d226d61696e7469746c65222077690a").getData();
//1000 - 2000
HEXDATA[1] = new MiniData("0x6474683D313030253E0A09093C74723E0A0909093C74643E3C696D67207372633D2269636F6E2E706E67222077696474683D3130303E3C2F74643E200A0909093C74642077696474683D313030253E3C623E4D494E4944415050533C2F623E3C62723E0A0909093C646976207374796C653D22666F6E742D73697A653A31323B223E0A090909266E6273703B266E6273703B266E6273703B266E6273703B266E6273703B266E6273703B0A090909266E6273703B266E6273703B266E6273703B266E6273703B266E6273703B266E6273703B0A090909266E6273703B266E6273703B266E6273703B0A090909506F7765726564206279204D696E696D613C2F6469763E0A0909093C2F74643E200A0909093C74643E3C696D67207372633D2269636F6E2E706E67222077696474683D3130303E3C2F74643E090A09093C2F74723E0A093C2F7461626C653E0A090A3C2F74643E0A3C2F74723E0A0A3C74723E0A093C74642077696474683D31303025206865696768743D3130302520636C6173733D226D696E69646170706C697374223E0A09092323232323230A093C2F74643E0A3C2F74723E0A0A3C74723E0A093C74643E0A0A093C7461626C6520636C6173733D226D61696E666F6F746572222077696474683D3130302520626F726465723D303E0A09093C74723E0A0909093C74642077696474683D313530207374796C653D22746578742D616C69676E3A6C6566743B223E200A090909093C627574746F6E20636C6173733D22696E7374616C6C6461707022206F6E636C69636B3D2277696E646F772E6C6F636174696F6E2E687265663D2768656C702E68746D6C273B223E48454C503C2F627574746F6E3E200A0909093C2F74643E0A0909090A0909093C7464207374796C653D22746578742D616C69676E3A63656E7465723B666F6E742D73697A653A323422206E6F777261703E0A0909090909546865203C7370616E207374796C653D22636F6C6F723A23303030223E453C2F7370616E3E3C7370616E207374796C653D22636F6C6F723A23323232223E763C2F7370616E3E3C7370616E207374796C653D22636F6C6F723A23343434223E6F3C2F7370616E3E3C7370616E207374796C653D22636F6C6F723A23363636223E6C3C2F7370616E3E3C7370616E207374796C653D22636F6C6F723A23383838223E753C2F7370616E3E3C7370616E207374796C653D22636F6C6F723A23616161223E743C2F7370616E3E3C7370616E207374796C653D22636F6C6F723A23363636223E693C2F7370616E3E3C7370616E207374796C653D22636F6C6F723A23646464223E6F3C2F7370616E3E3C7370616E207374796C653D22636F6C6F723A23666666223E6E3C2F7370616E3E").getData();
//2000 - 2598
Expand All @@ -27,7 +27,7 @@ public static byte[] returnData() throws IOException {
baos.write(HEXDATA[i]);
}
baos.flush();
FINAL_ARRAY = baos.toByteArray();
FINAL_ARRAY = baos.toByteArray();
}
return FINAL_ARRAY;
}
Expand Down
Loading