Esempio n. 1
0
 public boolean checkCorsPreflight(Request request, Response response) {
   log.finer("checkCorsPreflight " + request.getRequestURI());
   if (!request.getMethod().equalsIgnoreCase("OPTIONS")) {
     log.finer("checkCorsPreflight: not options ");
     return false;
   }
   if (request.getHeader("Origin") == null) {
     log.finer("checkCorsPreflight: no origin header");
     return false;
   }
   log.finer("Preflight request returning");
   response.setStatus(HttpServletResponse.SC_OK);
   String origin = request.getHeader("Origin");
   response.setHeader("Access-Control-Allow-Origin", origin);
   response.setHeader("Access-Control-Allow-Credentials", "true");
   String requestMethods = request.getHeader("Access-Control-Request-Method");
   if (requestMethods != null) {
     if (deployment.getCorsAllowedMethods() != null) {
       requestMethods = deployment.getCorsAllowedMethods();
     }
     response.setHeader("Access-Control-Allow-Methods", requestMethods);
   }
   String allowHeaders = request.getHeader("Access-Control-Request-Headers");
   if (allowHeaders != null) {
     if (deployment.getCorsAllowedHeaders() != null) {
       allowHeaders = deployment.getCorsAllowedHeaders();
     }
     response.setHeader("Access-Control-Allow-Headers", allowHeaders);
   }
   if (deployment.getCorsMaxAge() > -1) {
     response.setHeader("Access-Control-Max-Age", Integer.toString(deployment.getCorsMaxAge()));
   }
   return true;
 }
Esempio n. 2
0
  /**
   * Internal method that allows a redirect to be sent with a status other than {@link
   * HttpServletResponse#SC_FOUND} (302). No attempt is made to validate the status code.
   */
  public void sendRedirect(String location, int status) throws IOException {
    if (isCommitted()) {
      throw new IllegalStateException(sm.getString("coyoteResponse.sendRedirect.ise"));
    }

    // Ignore any call from an included servlet
    if (included) {
      return;
    }

    // Clear any data content that has been buffered
    resetBuffer(true);

    // Generate a temporary redirect to the specified location
    try {
      String absolute = toAbsolute(location);
      setStatus(status);
      setHeader("Location", absolute);
      if (getContext().getSendRedirectBody()) {
        PrintWriter writer = getWriter();
        writer.print(
            sm.getString("coyoteResponse.sendRedirect.note", RequestUtil.filter(absolute)));
        flushBuffer();
      }
    } catch (IllegalArgumentException e) {
      setStatus(SC_NOT_FOUND);
    }

    // Cause the response to be finished (from the application perspective)
    setSuspended(true);
  }
  @Override
  public void setHeader(String name, String value) {

    if (isCommitted()) return;

    response.setHeader(name, value);
  }
Esempio n. 4
0
  /**
   * Set the specified integer header to the specified value.
   *
   * @param name Name of the header to set
   * @param value Integer value to be set
   */
  @Override
  public void setIntHeader(String name, int value) {

    if (name == null || name.length() == 0) {
      return;
    }

    if (isCommitted()) {
      return;
    }

    // Ignore any call from an included servlet
    if (included) {
      return;
    }

    setHeader(name, "" + value);
  }
Esempio n. 5
0
  /**
   * Set the specified date header to the specified value.
   *
   * @param name Name of the header to set
   * @param value Date value to be set
   */
  @Override
  public void setDateHeader(String name, long value) {

    if (name == null || name.length() == 0) {
      return;
    }

    if (isCommitted()) {
      return;
    }

    // Ignore any call from an included servlet
    if (included) {
      return;
    }

    if (format == null) {
      format = new SimpleDateFormat(FastHttpDateFormat.RFC1123_DATE, Locale.US);
      format.setTimeZone(TimeZone.getTimeZone("GMT"));
    }

    setHeader(name, FastHttpDateFormat.formatDate(value, format));
  }