Java Code Examples for javax.servlet.http.HttpServletResponse#SC_CREATED

The following examples show how to use javax.servlet.http.HttpServletResponse#SC_CREATED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FoodManagementApi.java    From springboot-rest-h2-swagger with GNU General Public License v3.0 5 votes vote down vote up
@PostMapping(value = "/admin/addFood", 
		produces = {MediaType.APPLICATION_JSON_VALUE}, 
		consumes = {MediaType.APPLICATION_JSON_VALUE})
@ApiOperation(value="Add a new Food with details", notes="This is a public API with admin right", response=Response.class)
@ApiResponses(value = { 
		@ApiResponse(code = HttpServletResponse.SC_CREATED, message = "A new food has been added successfully"),
		@ApiResponse(code = HttpServletResponse.SC_UNAUTHORIZED, message = "Invalid Consumer Key")
})
ResponseEntity addFood(@RequestHeader(name="Consumer-Key", required=false) String key, @RequestBody Food food);
 
Example 2
Source File: KMSAuthenticationFilter.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain filterChain) throws IOException, ServletException {
  KMSResponse kmsResponse = new KMSResponse(response);
  super.doFilter(request, kmsResponse, filterChain);

  if (kmsResponse.statusCode != HttpServletResponse.SC_OK &&
      kmsResponse.statusCode != HttpServletResponse.SC_CREATED &&
      kmsResponse.statusCode != HttpServletResponse.SC_UNAUTHORIZED) {
    KMSWebApp.getInvalidCallsMeter().mark();
  }

  // HttpServletResponse.SC_UNAUTHORIZED is because the request does not
  // belong to an authenticated user.
  if (kmsResponse.statusCode == HttpServletResponse.SC_UNAUTHORIZED) {
    KMSWebApp.getUnauthenticatedCallsMeter().mark();
    String method = ((HttpServletRequest) request).getMethod();
    StringBuffer requestURL = ((HttpServletRequest) request).getRequestURL();
    String queryString = ((HttpServletRequest) request).getQueryString();
    if (queryString != null) {
      requestURL.append("?").append(queryString);
    }

    KMSWebApp.getKMSAudit().unauthenticated(
        request.getRemoteHost(), method, requestURL.toString(),
        kmsResponse.msg);
  }
}
 
Example 3
Source File: KMSAuthenticationFilter.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain filterChain) throws IOException, ServletException {
  KMSResponse kmsResponse = new KMSResponse(response);
  super.doFilter(request, kmsResponse, filterChain);

  if (kmsResponse.statusCode != HttpServletResponse.SC_OK &&
      kmsResponse.statusCode != HttpServletResponse.SC_CREATED &&
      kmsResponse.statusCode != HttpServletResponse.SC_UNAUTHORIZED) {
    KMSWebApp.getInvalidCallsMeter().mark();
  }

  // HttpServletResponse.SC_UNAUTHORIZED is because the request does not
  // belong to an authenticated user.
  if (kmsResponse.statusCode == HttpServletResponse.SC_UNAUTHORIZED) {
    KMSWebApp.getUnauthenticatedCallsMeter().mark();
    String method = ((HttpServletRequest) request).getMethod();
    StringBuffer requestURL = ((HttpServletRequest) request).getRequestURL();
    String queryString = ((HttpServletRequest) request).getQueryString();
    if (queryString != null) {
      requestURL.append("?").append(queryString);
    }

    KMSWebApp.getKMSAudit().unauthenticated(
        request.getRemoteHost(), method, requestURL.toString(),
        kmsResponse.msg);
  }
}
 
Example 4
Source File: KMSAuthenticationFilter.java    From ranger with Apache License 2.0 5 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain filterChain) throws IOException, ServletException {
  KMSResponse kmsResponse = new KMSResponse(response);
  super.doFilter(request, kmsResponse, filterChain);

  if (kmsResponse.statusCode != HttpServletResponse.SC_OK &&
      kmsResponse.statusCode != HttpServletResponse.SC_CREATED &&
      kmsResponse.statusCode != HttpServletResponse.SC_UNAUTHORIZED) {
    KMSWebApp.getInvalidCallsMeter().mark();
  }

  // HttpServletResponse.SC_UNAUTHORIZED is because the request does not
  // belong to an authenticated user.
  if (kmsResponse.statusCode == HttpServletResponse.SC_UNAUTHORIZED) {
    KMSWebApp.getUnauthenticatedCallsMeter().mark();
    String method = ((HttpServletRequest) request).getMethod();
    StringBuffer requestURL = ((HttpServletRequest) request).getRequestURL();
    String queryString = ((HttpServletRequest) request).getQueryString();
    if (queryString != null) {
      requestURL.append("?").append(queryString);
    }

    if (!method.equals("OPTIONS")) {
      // an HTTP OPTIONS request is made as part of the SPNEGO authentication
      // sequence. We do not need to audit log it, since it doesn't belong
      // to KMS context. KMS server doesn't handle OPTIONS either.
      KMSWebApp.getKMSAudit().unauthenticated(request.getRemoteHost(), method,
        requestURL.toString(), kmsResponse.msg);
    }
  }
}
 
Example 5
Source File: DefaultHTTPResponseValidator.java    From cloud-sfsf-benefits-ext with Apache License 2.0 5 votes vote down vote up
private void validateStatusCode(SimpleHttpResponse httpResponse) throws InvalidResponseException {
    final int statusCode = httpResponse.getResponseCode();
    final String statusLine = httpResponse.getResponseMessage();
    if (statusCode == HttpServletResponse.SC_OK) {
        return;
    }
    if (statusCode == HttpServletResponse.SC_CREATED) {
        return;
    }

    String errMessage;
    switch (statusCode) {
    case HttpServletResponse.SC_NOT_FOUND:
        errMessage = String.format("Requesting path [%s] was not found.", httpResponse.getRequestPath());
        break;
    case HttpServletResponse.SC_UNAUTHORIZED:
        errMessage = String.format("Missing or incorrect credentials for path [%s].", httpResponse.getRequestPath());
        break;
    case HttpServletResponse.SC_FORBIDDEN:
        errMessage = String.format("Unauthorized request to path [%s].", httpResponse.getRequestPath());
        break;
    default:
        errMessage = String.format("Requesting path [%s] returns unexpected response.", httpResponse.getRequestPath());
    }
    errMessage += String.format(" Service returned [%d] [%s].", statusCode, statusLine);

    throw new InvalidResponseException(errMessage);
}
 
Example 6
Source File: AbstractManagementController.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
private ManagementResponse handlePostOrPut(final ManagementRequest request) throws ManagementException
{
    final RequestType type = getRequestType(request);
    final Collection<String> hierarchy = getCategoryHierarchy(request.getRoot(), request.getCategory());
    switch (type)
    {
        case OPERATION:
        {
            final List<String> operationPath = request.getPath().subList(0, hierarchy.size());
            final String operationName = request.getPath().get(hierarchy.size());
            @SuppressWarnings("unchecked")
            final Map<String, Object> arguments = request.getBody(LinkedHashMap.class);
            return invoke(request.getRoot(),
                              request.getCategory(),
                              operationPath,
                              operationName,
                              arguments,
                              true,
                              request.isSecure()
                              || request.isConfidentialOperationAllowedOnInsecureChannel());
        }
        case MODEL_OBJECT:
        {
            @SuppressWarnings("unchecked")
            final Map<String, Object> attributes = request.getBody(LinkedHashMap.class);
            final Object response = createOrUpdate(request.getRoot(),
                                      request.getCategory(),
                                      request.getPath(),
                                      attributes,
                                      "POST".equalsIgnoreCase(request.getMethod()));
            int responseCode = HttpServletResponse.SC_OK;
            ResponseType responseType = ResponseType.EMPTY;
            Map<String, String> headers = Collections.emptyMap();
            if (response != null)
            {
                responseCode = HttpServletResponse.SC_CREATED;
                final StringBuilder requestURL = new StringBuilder(request.getRequestURL());
                if (hierarchy.size() != request.getPath().size())
                {
                    Object name = attributes.get("name");
                    requestURL.append("/").append(encode(String.valueOf(name)));
                }
                headers = Collections.singletonMap("Location", requestURL.toString());
                responseType = ResponseType.MODEL_OBJECT;
            }
            return new ControllerManagementResponse(responseType, response, responseCode, headers);
        }
        case VISIBLE_PREFERENCES:
        case USER_PREFERENCES:
        {
            setPreferences(request.getRoot(),
                           request.getCategory(),
                           request.getPath(),
                           request.getBody(Object.class),
                           request.getParameters(),
                           "POST".equalsIgnoreCase(request.getMethod()));
            return new ControllerManagementResponse(ResponseType.EMPTY, null);
        }
        default:
        {
            throw createBadRequestManagementException(String.format("Unexpected request type '%s' for path '%s'",
                                                                    type,
                                                                    getCategoryMapping(request.getCategory())));
        }
    }

}