@Override public NanoHTTPD.Response handleRequest(String uri, Map<String, String> headers, NanoHTTPD.IHTTPSession session, File file, String mimeType) { View view = getCurrentRootView(HttpTools.parseQueryString(session.getQueryParameterString())); String json; if (view != null) { ViewNode vn = DetailExtractor.parse(view, false); try { json = vn.toJson().toString(); } catch (Throwable e) { json = String.format("{\"exception\":\"%s\"}", e.getMessage()); e.printStackTrace(); } } else { json = "{}"; } NanoHTTPD.Response response = new OKResponse(APP_JSON, new ByteArrayInputStream(json.getBytes())); return response; }
@Override public NanoHTTPD.Response serveFile(String uri, Map<String, String> headers, NanoHTTPD.IHTTPSession session, File file, String mimeType) { String queryString = session.getQueryParameterString(); HashMap<String, String> qs = HttpTools.parseQueryString(queryString); String type = APP_JSON; ByteArrayInputStream resultInputStream = null; if (qs.containsKey(ID)) { int id = -1; try { id = Integer.parseInt(qs.get(ID)); resultInputStream = dispatchIdRequest(id); } catch (Exception e) { ResourceResponse rr = new ResourceResponse(); rr.Data = (e == null ? "WTF NullException!" : e.getMessage()); rr.Context = e != null ? e.getClass().getName() : null; rr.DataType = STRING_DATA_TYPE; rr.Type = IdsHelper.RefType.unknown; resultInputStream = new ByteArrayInputStream(JSON.toJson(rr).getBytes()); } } else { String s = IdsHelper.toJson(mRes); resultInputStream = new ByteArrayInputStream(s.getBytes()); } NanoHTTPD.Response response = new OKResponse(type, resultInputStream); return response; }
@Override public NanoHTTPD.Response serveFile(String uri, Map<String, String> headers, NanoHTTPD.IHTTPSession session, File file, String mimeType) { InputStream inputStream; String mime; String content = null; try { String path = getPath(session.getQueryParameterString()); List<FSItem> files; String json; File f = path != null ? new File(path) : null; if (f != null && f.isFile()) { inputStream = new FileInputStream(f); mime = HttpTools.getMimeType(f);//try get proper mime to show it directly in browser, otherwise download content = "inline; filename=" + f.getName();//add download filename } else { //folder mime = APP_JSON; files = path == null ? mRootItems : FileSystemTools.get(f); json = JSON.toJson(files); inputStream = new ByteArrayInputStream(json.getBytes()); } } catch (Exception e) { inputStream = new ByteArrayInputStream(e.getMessage().getBytes()); mime = APP_JSON; } NanoHTTPD.Response response = new OKResponse(mime, inputStream); if (content != null) { response.addHeader("Content-Disposition", content); } return response; }
@Override public NanoHTTPD.Response handleRequest(String uri, Map<String, String> headers, NanoHTTPD.IHTTPSession session, File file, String mimeType) { String queryString = session.getQueryParameterString(); final int len = queryString != null ? queryString.length() : 0; ByteArrayInputStream resultInputStream = null;
@Override public NanoHTTPD.Response handleRequest(String uri, Map<String, String> headers, NanoHTTPD.IHTTPSession session, File file, String mimeType) { try { String queryString = session.getQueryParameterString(); HashMap<String, String> qsValue = HttpTools.parseQueryString(queryString); if (qsValue.containsKey(POSITION)) {
ByteArrayOutputStream bos = new ByteArrayOutputStream(); final View view = getCurrentRootView(HttpTools.parseQueryString(session.getQueryParameterString())); ByteArrayInputStream resultInputStream = null;
/** * Override this to customize the server. * <p/> * <p/> * (By default, this returns a 404 "Not Found" plain text error response.) * * @param session * The HTTP session * @return HTTP response, see class Response for details */ public Response serve(IHTTPSession session) { Map<String, String> files = new HashMap<String, String>(); Method method = session.getMethod(); if (Method.PUT.equals(method) || Method.POST.equals(method)) { try { session.parseBody(files); } catch (IOException ioe) { return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return newFixedLengthResponse(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage()); } } Map<String, String> parms = session.getParms(); parms.put(NanoHTTPD.QUERY_STRING_PARAMETER, session.getQueryParameterString()); return serve(session.getUri(), method, session.getHeaders(), parms, files); }
/** * Override this to customize the server. * <p/> * <p/> * (By default, this delegates to serveFile() and allows directory listing.) * * @param session The HTTP session * @return HTTP response, see class Response for details */ public Response serve(IHTTPSession session) { Map<String, String> files = new HashMap<String, String>(); Method method = session.getMethod(); if (Method.PUT.equals(method) || Method.POST.equals(method)) { try { session.parseBody(files); } catch (IOException ioe) { return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage()); } } Map<String, String> parms = session.getParms(); parms.put(QUERY_STRING_PARAMETER, session.getQueryParameterString()); return serve(session.getUri(), method, session.getHeaders(), parms, files); }
/** * Override this to customize the server. * <p/> * <p/> * (By default, this delegates to serveFile() and allows directory listing.) * * @param session The HTTP session * @return HTTP response, see class Response for details */ public Response serve(IHTTPSession session) { Map<String, String> files = new HashMap<String, String>(); Method method = session.getMethod(); if (Method.PUT.equals(method) || Method.POST.equals(method)) { try { session.parseBody(files); } catch (IOException ioe) { return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage()); } } Map<String, String> parms = session.getParms(); parms.put(QUERY_STRING_PARAMETER, session.getQueryParameterString()); return serve(session.getUri(), method, session.getHeaders(), parms, files); }
@Override public NanoHTTPD.Response serveFile(String uri, Map<String, String> headers, NanoHTTPD.IHTTPSession session, File file, String mimeType) { String queryString = session.getQueryParameterString(); HashMap<String, String> qsValue = queryString != null ? HttpTools.parseQueryString(queryString) : null; String type = qsValue != null ? qsValue.get("type") : null; return new OKResponse(HttpTools.MimeType.PLAIN_TEXT, LogCatProvider.dumpLogcat(type)); } }