Example #1
0
  private void parseRequest(
      HttpServletRequest request, Map<String, String> parameters, List<file> files)
      throws IOException {
    if (ServletFileUpload.isMultipartContent(request)) {
      List<FileItem> fileItems = parseMultipartRequest(request);

      long fileSizeMaxMB = ServerConfig.webServerUploadMax();
      long fileSizeMax = fileSizeMaxMB > 0 ? fileSizeMaxMB * NumericUtils.Megabyte : Long.MAX_VALUE;

      for (FileItem fileItem : fileItems) {
        if (!fileItem.isFormField()) {
          if (fileItem.getSize() > fileSizeMax)
            throw new RuntimeException(
                Resources.format(
                    "Exception.fileSizeLimitExceeded", fileItem.getName(), fileSizeMaxMB));
          files.add(new file(fileItem));
        } else
          parameters.put(fileItem.getFieldName(), fileItem.getString(encoding.Default.toString()));
      }
    } else {
      @SuppressWarnings("unchecked")
      Map<String, String[]> requestParameters = request.getParameterMap();

      for (String name : requestParameters.keySet()) {
        String[] values = requestParameters.get(name);
        parameters.put(name, values.length != 0 ? values[0] : null);
      }
    }

    parameters.put(Json.ip.get(), request.getRemoteAddr());
  }
Example #2
0
  protected void writeResponse(HttpServletResponse response, byte[] content) throws IOException {
    response.setContentType("text/html;charset=" + encoding.Default.toString());

    OutputStream out = response.getOutputStream();
    out.write(content);
    out.flush();
    out.close();
  }
Example #3
0
 protected void processError(HttpServletResponse response, Throwable e)
     throws IOException, ServletException {
   response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
   writeResponse(
       response,
       (e.getMessage() != null ? e.getMessage() : "Internal server error")
           .getBytes(encoding.Default.toString()));
 }
Example #4
0
  private void writeError(HttpServletResponse response, String errorText, int status)
      throws IOException {
    JsonWriter writer = new JsonWriter();

    if (errorText == null || errorText.isEmpty()) errorText = "Internal server error.";

    writer.startResponse(null, false, status);
    writer.writeInfo(Arrays.asList(Message.error(errorText)), null, null);
    writer.startArray(Json.data);
    writer.finishArray();
    writer.finishResponse();

    writeResponse(response, writer.toString().getBytes(encoding.Default.toString()));
  }