private void writeResponse( JSONWriter write, ResponseWrapper responseWrapper, RequestInfo requestData) throws JSONException { try { String body = responseWrapper.getDataAsString(); write.object(); write.key("url"); write.value(requestData.getUrl()); write.key("success"); write.value(true); write.key("body"); write.value(body); write.key("status"); write.value(responseWrapper.getResponseStatus()); write.key("headers"); write.object(); Dictionary<String, String> headers = responseWrapper.getResponseHeaders(); Enumeration<String> keys = headers.keys(); while (keys.hasMoreElements()) { String k = keys.nextElement(); write.key(k); write.value(headers.get(k)); } write.endObject(); write.endObject(); } catch (UnsupportedEncodingException e) { writeFailedRequest(write, requestData); } }
private void writeFailedRequest(JSONWriter write, RequestInfo requestData) throws JSONException { write.object(); write.key("url"); write.value(requestData.getUrl()); write.key("success"); write.value(false); write.endObject(); }
private void doRequest( SlingHttpServletRequest request, SlingHttpServletResponse response, RequestInfo requestInfo, JSONWriter write) throws JSONException { // Look for a matching resource in the usual way. If one is found, // the resource will also be embedded with any necessary RequestPathInfo. String requestPath = requestInfo.getUrl(); ResourceResolver resourceResolver = request.getResourceResolver(); Resource resource = resourceResolver.resolve(request, requestPath); // Wrap the request and response. RequestWrapper requestWrapper = new RequestWrapper(request, requestInfo); ResponseWrapper responseWrapper = new ResponseWrapper(response); RequestDispatcher requestDispatcher; try { // Get the response try { if (resource != null) { LOGGER.info( "Dispatching to request path='{}', resource path='{}'", requestPath, resource.getPath()); requestDispatcher = request.getRequestDispatcher(resource); } else { LOGGER.info("Dispatching to request path='{}', no resource", requestPath); requestDispatcher = request.getRequestDispatcher(requestPath); } requestDispatcher.forward(requestWrapper, responseWrapper); } catch (ResourceNotFoundException e) { responseWrapper.setStatus(HttpServletResponse.SC_NOT_FOUND); } catch (SlingException e) { responseWrapper.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } // Write the response (status, headers, body) back to the client. writeResponse(write, responseWrapper, requestInfo); } catch (ServletException e) { writeFailedRequest(write, requestInfo); } catch (IOException e) { writeFailedRequest(write, requestInfo); } }