예제 #1
0
 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);
   }
 }
예제 #2
0
 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();
 }
예제 #3
0
  /**
   * Takes the original request and starts the batching.
   *
   * @param request
   * @param response
   * @throws IOException
   */
  protected void batchRequest(
      SlingHttpServletRequest request, SlingHttpServletResponse response, boolean allowModify)
      throws IOException {
    // Grab the JSON block out of it and convert it to RequestData objects we can use.
    String json = request.getParameter(REQUESTS_PARAMETER);
    List<RequestInfo> batchedRequests = new ArrayList<RequestInfo>();
    try {
      JSONArray arr = new JSONArray(json);
      for (int i = 0; i < arr.length(); i++) {
        JSONObject obj = arr.getJSONObject(i);
        RequestInfo r = new RequestInfo(obj);
        if (allowModify || r.isSafe()) {
          batchedRequests.add(r);
        }
      }
    } catch (JSONException e) {
      response.sendError(
          HttpServletResponse.SC_BAD_REQUEST,
          "Failed to parse the " + REQUESTS_PARAMETER + " parameter");
      LOGGER.warn("Failed to parse the " + REQUESTS_PARAMETER + " parameter");
      return;
    }

    // Loop over the requests and handle each one.
    try {
      StringWriter sw = new StringWriter();
      JSONWriter write = new JSONWriter(sw);
      write.object();
      write.key("results");
      write.array();

      for (RequestInfo r : batchedRequests) {
        doRequest(request, response, r, write);
      }
      write.endArray();
      write.endObject();
      response.setContentType("application/json");
      response.setCharacterEncoding("UTF-8");
      response.getWriter().write(sw.getBuffer().toString());
    } catch (JSONException e) {
      LOGGER.warn("Failed to create a JSON response");
      response.sendError(
          HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to write JSON response");
    }
  }
예제 #4
0
  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);
    }
  }