@Override public NanoHTTPD.Response serve(final String uri, final NanoHTTPD.Method method, final Map<String, String> headers, final Map<String, String> parms, final Map<String, String> files) { if (!method.equals(NanoHTTPD.Method.GET)) { return newBadRequest("Method is not GET."); } try { JSONObject jsonObject = new JSONObject(); jsonObject.put(DConnectMessage.EXTRA_RESULT, DConnectMessage.RESULT_OK); jsonObject.put(AvailabilityProfileConstants.PARAM_VERSION, version); jsonObject.put(AvailabilityProfileConstants.PARAM_PRODUCT, product); jsonObject.put(AvailabilityProfileConstants.PARAM_NAME, name); jsonObject.put(AvailabilityProfileConstants.PARAM_UUID, uuid); return newJsonResponse(jsonObject); } catch (JSONException e) { return newInternalServerError(e.getMessage()); } } });
@Override public NanoHTTPD.Response serve(final String uri, final NanoHTTPD.Method method, final Map<String, String> headers, final Map<String, String> parms, final Map<String, String> files) { if (!method.equals(NanoHTTPD.Method.GET)) { return newBadRequest("Method is not GET.");
@Override public NanoHTTPD.Response serve(final String uri, final NanoHTTPD.Method method, final Map<String, String> headers, final Map<String, String> parms, final Map<String, String> files) { if (!method.equals(NanoHTTPD.Method.GET)) { return newBadRequest("Method is not GET.");
/** * 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 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 public NanoHTTPD.Response serve(final String uri, final NanoHTTPD.Method method, final Map<String, String> headers, final Map<String, String> parms, final Map<String, String> files) { if (!method.equals(NanoHTTPD.Method.GET)) { return newBadRequest("Method is not GET."); } Uri u = Uri.parse(uri); if (!"/gotapi/serviceInformation".equalsIgnoreCase(u.getPath())) { return newBadRequest("uri is invalid. uri=" + uri); } String at = parms.get(DConnectMessage.EXTRA_ACCESS_TOKEN); if (!accessToken.equals(at)) { return newBadRequest("accessToken is invalid. accessToken=" + at); } try { JSONArray supports = new JSONArray(); JSONArray supportApis = new JSONArray(); JSONObject jsonObject = new JSONObject(); jsonObject.put(DConnectMessage.EXTRA_RESULT, DConnectMessage.RESULT_OK); jsonObject.put(DConnectProfileConstants.PARAM_VERSION, version); jsonObject.put(DConnectProfileConstants.PARAM_PRODUCT, product); jsonObject.put(ServiceInformationProfileConstants.PARAM_SUPPORTS, supports); jsonObject.put(ServiceInformationProfileConstants.PARAM_SUPPORT_APIS, supportApis); return newJsonResponse(jsonObject); } catch (JSONException e) { return newInternalServerError(e.getMessage()); } } });
@Override public NanoHTTPD.Response serve(final String uri, final NanoHTTPD.Method method, final Map<String, String> headers, final Map<String, String> parms, final Map<String, String> files) { if (!method.equals(NanoHTTPD.Method.GET)) { return newBadRequest("Method is not GET."); } Uri u = Uri.parse(uri); if (!"/gotapi/serviceInformation".equalsIgnoreCase(u.getPath())) { return newBadRequest("uri is invalid. uri=" + uri); } String at = parms.get(DConnectMessage.EXTRA_ACCESS_TOKEN); if (!accessToken.equals(at)) { return newBadRequest("accessToken is invalid. accessToken=" + at); } try { JSONArray supports = new JSONArray(); JSONArray supportApis = new JSONArray(); JSONObject jsonObject = new JSONObject(); jsonObject.put(DConnectMessage.EXTRA_RESULT, DConnectMessage.RESULT_OK); jsonObject.put(DConnectProfileConstants.PARAM_VERSION, version); jsonObject.put(DConnectProfileConstants.PARAM_PRODUCT, product); jsonObject.put(ServiceInformationProfileConstants.PARAM_SUPPORTS, supports); jsonObject.put(ServiceInformationProfileConstants.PARAM_SUPPORT_APIS, supportApis); return newJsonResponse(jsonObject); } catch (JSONException e) { return newInternalServerError(e.getMessage()); } } });
@Override public NanoHTTPD.Response serve(final String uri, final NanoHTTPD.Method method, final Map<String, String> headers, final Map<String, String> parms, final Map<String, String> files) { if (!method.equals(NanoHTTPD.Method.POST)) { return newBadRequest("Method is not POST."); } String v = parms.get(key); if (!value.equals(v)) { return newBadRequest("body is invalid."); } try { JSONObject jsonObject = new JSONObject(); jsonObject.put(DConnectMessage.EXTRA_RESULT, DConnectMessage.RESULT_OK); jsonObject.put(AvailabilityProfileConstants.PARAM_VERSION, version); jsonObject.put(AvailabilityProfileConstants.PARAM_PRODUCT, product); jsonObject.put(AvailabilityProfileConstants.PARAM_NAME, name); jsonObject.put(AvailabilityProfileConstants.PARAM_UUID, uuid); return newJsonResponse(jsonObject); } catch (Exception e) { return newInternalServerError(e.getMessage()); } } });
@Override public NanoHTTPD.Response serve(final String uri, final NanoHTTPD.Method method, final Map<String, String> headers, final Map<String, String> parms, final Map<String, String> files) { if (!method.equals(NanoHTTPD.Method.POST)) { return newBadRequest("Method is not POST."); } String v = parms.get(key); if (!value.equals(v)) { return newBadRequest("body is invalid."); } File file = new File(files.get("data")); if (!file.isFile() || !Arrays.equals(fileData, getFile(file))) { return newBadRequest("data is invalie."); } try { JSONObject jsonObject = new JSONObject(); jsonObject.put(DConnectMessage.EXTRA_RESULT, DConnectMessage.RESULT_OK); jsonObject.put(AvailabilityProfileConstants.PARAM_VERSION, version); jsonObject.put(AvailabilityProfileConstants.PARAM_PRODUCT, product); jsonObject.put(AvailabilityProfileConstants.PARAM_NAME, name); jsonObject.put(AvailabilityProfileConstants.PARAM_UUID, uuid); return newJsonResponse(jsonObject); } catch (JSONException e) { return newInternalServerError(e.getMessage()); } } });
@Override public NanoHTTPD.Response serve(final String uri, final NanoHTTPD.Method method, final Map<String, String> headers, final Map<String, String> parms, final Map<String, String> files) { if (!method.equals(NanoHTTPD.Method.DELETE)) { return newBadRequest("Method is not DELETE."); } String v = parms.get(key); if (!value.equals(v)) { return newBadRequest("body is invalid."); } try { JSONObject jsonObject = new JSONObject(); jsonObject.put(DConnectMessage.EXTRA_RESULT, DConnectMessage.RESULT_OK); jsonObject.put(AvailabilityProfileConstants.PARAM_VERSION, version); jsonObject.put(AvailabilityProfileConstants.PARAM_PRODUCT, product); jsonObject.put(AvailabilityProfileConstants.PARAM_NAME, name); jsonObject.put(AvailabilityProfileConstants.PARAM_UUID, uuid); return newJsonResponse(jsonObject); } catch (JSONException e) { return newInternalServerError(e.getMessage()); } } });
@Override public NanoHTTPD.Response serve(final String uri, final NanoHTTPD.Method method, final Map<String, String> headers, final Map<String, String> parms, final Map<String, String> files) { if (!method.equals(NanoHTTPD.Method.GET)) { return newBadRequest("Method is not GET."); } Uri u = Uri.parse(uri); if (!"/gotapi/availability".equalsIgnoreCase(u.getPath())) { return newBadRequest("uri is invalid. uri=" + uri); } try { JSONObject jsonObject = new JSONObject(); jsonObject.put(DConnectMessage.EXTRA_RESULT, DConnectMessage.RESULT_OK); jsonObject.put(DConnectProfileConstants.PARAM_VERSION, version); jsonObject.put(DConnectProfileConstants.PARAM_PRODUCT, product); jsonObject.put(AvailabilityProfileConstants.PARAM_NAME, name); jsonObject.put(AvailabilityProfileConstants.PARAM_UUID, uuid); return newJsonResponse(jsonObject); } catch (JSONException e) { return newInternalServerError(e.getMessage()); } } });
@Override public NanoHTTPD.Response serve(final String uri, final NanoHTTPD.Method method, final Map<String, String> headers, final Map<String, String> parms, final Map<String, String> files) { if (!method.equals(NanoHTTPD.Method.DELETE)) { return newBadRequest("Method is not DELETE."); } try { JSONObject jsonObject = new JSONObject(); jsonObject.put(DConnectMessage.EXTRA_RESULT, DConnectMessage.RESULT_OK); jsonObject.put(AvailabilityProfileConstants.PARAM_VERSION, version); jsonObject.put(AvailabilityProfileConstants.PARAM_PRODUCT, product); jsonObject.put(AvailabilityProfileConstants.PARAM_NAME, name); jsonObject.put(AvailabilityProfileConstants.PARAM_UUID, uuid); return newJsonResponse(jsonObject); } catch (JSONException e) { return newInternalServerError(e.getMessage()); } } });
@Override public NanoHTTPD.Response serve(final String uri, final NanoHTTPD.Method method, final Map<String, String> headers, final Map<String, String> parms, final Map<String, String> files) { if (!method.equals(NanoHTTPD.Method.GET)) { return newBadRequest("Method is not GET."); } try { JSONObject jsonObject = new JSONObject(); jsonObject.put(DConnectMessage.EXTRA_RESULT, DConnectMessage.RESULT_OK); jsonObject.put(DConnectProfileConstants.PARAM_VERSION, version); jsonObject.put(DConnectProfileConstants.PARAM_PRODUCT, product); jsonObject.put(AvailabilityProfileConstants.PARAM_NAME, name); jsonObject.put(AvailabilityProfileConstants.PARAM_UUID, uuid); return newJsonResponse(jsonObject); } catch (JSONException e) { return newInternalServerError(e.getMessage()); } } });
@Override public NanoHTTPD.Response serve(final String uri, final NanoHTTPD.Method method, final Map<String, String> headers, final Map<String, String> parms, final Map<String, String> files) { if (!method.equals(NanoHTTPD.Method.GET)) { return newBadRequest("Method is not GET."); } Uri u = Uri.parse(uri); if (!"/gotapi/availability".equalsIgnoreCase(u.getPath())) { return newBadRequest("uri is invalid. uri=" + uri); } try { JSONObject jsonObject = new JSONObject(); jsonObject.put(DConnectMessage.EXTRA_RESULT, DConnectMessage.RESULT_OK); jsonObject.put(DConnectProfileConstants.PARAM_VERSION, version); jsonObject.put(DConnectProfileConstants.PARAM_PRODUCT, product); jsonObject.put(AvailabilityProfileConstants.PARAM_NAME, name); jsonObject.put(AvailabilityProfileConstants.PARAM_UUID, uuid); return newJsonResponse(jsonObject); } catch (JSONException e) { return newInternalServerError(e.getMessage()); } } });
private Response respond(IHTTPSession session, String responseString) { // First let's handle CORS OPTION query Response response; if (Method.OPTIONS.equals(session.getMethod())) { response = new NanoHTTPD.Response(Response.Status.OK, MIME_PLAINTEXT, null, 0); } else { response = newFixedLengthResponse(Response.Status.OK, NanoHTTPD.MIME_HTML, responseString); } response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Headers", "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type"); response.addHeader("Access-Control-Allow-Credentials", "true"); response.addHeader("Access-Control-Allow-Methods", "GET,POST,PUT,OPTIONS"); response.addHeader("Access-Control-Max-Age", "" + (42 * 60 * 60)); return response; }
private Response respond(IHTTPSession session) { Method method = session.getMethod(); String uri = session.getUri(); logger.debug("Serving {} {} request.", method, uri); if (Method.GET.equals(method) && "/version".equalsIgnoreCase(uri)) { return newFixedLengthResponse(Status.OK, NanoHTTPD.MIME_PLAINTEXT, diagnosticsApi.getVersion()); } else if (Method.POST.equals(method) && "/reload".equalsIgnoreCase(uri)) { diagnosticsApi.reload(); return newFixedLengthResponse(Status.OK, NanoHTTPD.MIME_PLAINTEXT, "Configuration reloaded\r\n"); } else { return newFixedLengthResponse(Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "Requested URI " + uri + " not found\r\n"); } }
private Response respond(Map<String, String> headers, IHTTPSession session, String uri) { // First let's handle CORS OPTION query Response r; if (cors != null && Method.OPTIONS.equals(session.getMethod())) { r = new NanoHTTPD.Response(Response.Status.OK, MIME_PLAINTEXT, null, 0); } else { r = defaultRespond(headers, session, uri); } if (cors != null) { r = addCORSHeaders(headers, r, cors); } return r; }