Java Code Examples for javax.ws.rs.container.ContainerResponseContext#setStatus()

The following examples show how to use javax.ws.rs.container.ContainerResponseContext#setStatus() . 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: ResponseWrapperHandler.java    From azeroth with Apache License 2.0 6 votes vote down vote up
@Override
public void processResponse(ContainerRequestContext requestContext,
                            ContainerResponseContext responseContext,
                            ResourceInfo resourceInfo) {
    MediaType mediaType = responseContext.getMediaType();
    if (mediaType != null && MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) {
        Object responseData = responseContext.getEntity();
        WrapperResponseEntity jsonResponse;

        if (responseData instanceof WrapperResponseEntity) {
            jsonResponse = (WrapperResponseEntity) responseData;
        } else {
            jsonResponse = new WrapperResponseEntity(ResponseCode.OK);
            jsonResponse.setData(responseData);
        }
        responseContext.setStatus(ResponseCode.OK.getCode());

        responseContext.setEntity(jsonResponse);

    }
}
 
Example 2
Source File: ResponseWrapperHandler.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
@Override
public void processResponse(ContainerRequestContext requestContext, ContainerResponseContext responseContext,
		ResourceInfo resourceInfo) {
	MediaType mediaType = responseContext.getMediaType();
	if (mediaType != null && MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) {
		Object responseData = responseContext.getEntity();
		WrapperResponseEntity jsonResponse;

		if (responseData instanceof WrapperResponseEntity) {
			jsonResponse = (WrapperResponseEntity) responseData;
		} else {
			jsonResponse = new WrapperResponseEntity(ResponseCode.OK);
			jsonResponse.setData(responseData);
		}
		responseContext.setStatus(ResponseCode.OK.getCode());

		responseContext.setEntity(jsonResponse);

	}
}
 
Example 3
Source File: JaxRsContext.java    From jax-rs-pac4j with Apache License 2.0 6 votes vote down vote up
public void populateResponse(ContainerResponseContext responseContext) {
    if (hasResponseContent) {
        responseContext.setEntity(responseContent);
    }
    if (hasResponseContentType) {
        responseContext.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, responseContentType);
    }
    if (hasResponseStatus) {
        responseContext.setStatus(responseStatus);
    }
    for (Entry<String, String> headers : responseHeaders.entrySet()) {
        responseContext.getHeaders().putSingle(headers.getKey(), headers.getValue());
    }
    for (NewCookie cookie : responseCookies) {
        responseContext.getHeaders().add(HttpHeaders.SET_COOKIE, cookie);
    }
}
 
Example 4
Source File: CORSFilter.java    From OpenAs2App with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
    // if there is no Origin header, then it is not a
    // cross origin request. We don't do anything.
    if (requestContext.getHeaderString("Origin") == null) {
        return;
    }
    // If it is a preflight request, then we add all
    // the CORS headers here.
    MultivaluedMap<String, Object> headers = responseContext.getHeaders();
    headers.add("Access-Control-Allow-Origin", requestContext.getHeaderString("Origin")); // for now, allows CORS requests coming from any source
    if (this.isPreflightRequest(requestContext)) {

        headers.add("Access-Control-Allow-Credentials", true);


        headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Authorization");

        headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS");

        headers.add("Access-Control-Max-Age", 86400);
        headers.add("Vary", "Accept-Encoding, Origin");
        responseContext.setStatus(200);
    }
}
 
Example 5
Source File: RespondWithStatusFilter.java    From pnc with Apache License 2.0 6 votes vote down vote up
@Override
public void filter(
        ContainerRequestContext containerRequestContext,
        ContainerResponseContext containerResponseContext) throws IOException {

    // for any 2xx status code, this gets activated
    // Skip filter for Http Method OPTIONS to not break CORS
    if (containerResponseContext.getStatus() / 100 == 2
            && containerResponseContext.getEntityAnnotations() != null) {
        for (Annotation annotation : containerResponseContext.getEntityAnnotations()) {

            if (annotation instanceof RespondWithStatus) {
                containerResponseContext.setStatus(((RespondWithStatus) annotation).value().getStatusCode());
                break;
            }
        }
    }
}
 
Example 6
Source File: StatusFilter.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ContainerRequestContext requestContext,
                   ContainerResponseContext responseContext)
                   throws IOException {
    if (responseContext.getStatus() == 200) {
        for (Annotation i : responseContext.getEntityAnnotations()) {
            if (i instanceof Status) {
                responseContext.setStatus(((Status) i).value());
                break;
            }
        }
    }
}
 
Example 7
Source File: MCRSessionFilter.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
    LOGGER.debug("ResponseFilter start");
    try {
        MCRSessionMgr.unlock();
        MCRSession currentSession = MCRSessionMgr.getCurrentSession();
        if (responseContext.getStatus() == Response.Status.FORBIDDEN.getStatusCode() && currentSession
            .getUserInformation().getUserID().equals(MCRSystemUserInformation.getGuestInstance().getUserID())) {
            LOGGER.debug("Guest detected, change response from FORBIDDEN to UNAUTHORIZED.");
            responseContext.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
            responseContext.getHeaders().putSingle(HttpHeaders.WWW_AUTHENTICATE,
                MCRRestAPIUtil.getWWWAuthenticateHeader("Basic", null, app));
        }
        if (responseContext.getStatus() == Response.Status.UNAUTHORIZED.getStatusCode()
            && doNotWWWAuthenticate(requestContext)) {
            LOGGER.debug("Remove {} header.", HttpHeaders.WWW_AUTHENTICATE);
            responseContext.getHeaders().remove(HttpHeaders.WWW_AUTHENTICATE);
        }
        addJWTToResponse(requestContext, responseContext);
        if (responseContext.hasEntity()) {
            responseContext.setEntityStream(new ProxyOutputStream(responseContext.getEntityStream()) {
                @Override
                public void close() throws IOException {
                    LOGGER.debug("Closing EntityStream");
                    try {
                        super.close();
                    } finally {
                        closeSessionIfNeeded();
                        LOGGER.debug("Closing EntityStream done");
                    }
                }
            });
        } else {
            LOGGER.debug("No Entity in response, closing MCRSession");
            closeSessionIfNeeded();
        }
    } finally {
        LOGGER.debug("ResponseFilter stop");
    }
}
 
Example 8
Source File: HealthResponseFilter.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ContainerRequestContext req, ContainerResponseContext resp) throws IOException {

    if (resp.hasEntity() && (resp.getEntity() instanceof Status)) {
        Status status = (Status) resp.getEntity();
        int code = (Status.State.UP == status.getState()) ? 200 : 503;
        resp.setStatus(code);
        resp.setEntity(status.toJson());
        resp.getHeaders().putSingle("Content-Type", MediaType.APPLICATION_JSON);
    }
}
 
Example 9
Source File: RestResponseFilter.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void filter(ContainerRequestContext request, ContainerResponseContext response) {
  Object entity = response.getEntity();
  if (entity instanceof AbstractRestResponse) {
    AbstractRestResponse restResponse = (AbstractRestResponse) entity;
    response.setStatus(restResponse.getHttpStatusCode());
  }
}
 
Example 10
Source File: ResponseStatusFilter.java    From agrest with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
		throws IOException {

	Object entity = responseContext.getEntity();
	if (entity instanceof AgResponse) {

		AgResponse response = (AgResponse) entity;
		if (response.getStatus() != null) {
			responseContext.setStatus(response.getStatus().getStatusCode());
		}
	}
}
 
Example 11
Source File: NotFoundForward.java    From ameba with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void filter(ContainerRequestContext requestContext,
                   ContainerResponseContext responseContext) throws IOException {
    int status = responseContext.getStatus();
    if (status == 404 || status == UnprocessableEntityException.STATUS) {
        String path = mappedViewPath();
        if (path != null) {
            responseContext.setEntity(Viewables.newDefaultViewable(path),
                    new Annotation[0], MediaType.TEXT_HTML_TYPE);
            responseContext.setStatus(Response.Status.OK.getStatusCode());
        }
    }
}
 
Example 12
Source File: NoContentResponseFilter.java    From attic-rave with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
    if (containerResponseContext.getStatus() == Response.Status.OK.getStatusCode()) {

        Object o = containerResponseContext.getEntity();

        if (o == null) {
            containerResponseContext.setStatus(Response.Status.NO_CONTENT.getStatusCode());
        }
    }
}
 
Example 13
Source File: CreatedResponseFilter.java    From attic-rave with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
    String method = containerRequestContext.getRequest().getMethod();

    if (method.equals("POST") && containerResponseContext.getStatus() == Response.Status.OK.getStatusCode()) {
        containerResponseContext.setStatus(Response.Status.CREATED.getStatusCode());
        RestEntity entity = (RestEntity) containerResponseContext.getEntity();
        String id = entity.getId();
        containerResponseContext.getHeaders().add("Location",
                containerRequestContext.getUriInfo().getAbsolutePathBuilder().path(id).build().toString());
    }

    //containerResponseContext.getHeaders().put("Location")
}
 
Example 14
Source File: BookStoreResponseFilter.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ContainerRequestContext requestContext,
        ContainerResponseContext responseContext) throws IOException {
    if (authenticator != null) {
        // This filter should not be created using CDI runtime (it is vetoed) as 
        // such the injection should not be performed.
        responseContext.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
}
 
Example 15
Source File: JaxRsServerWithProviderTest.java    From testfun with Apache License 2.0 4 votes vote down vote up
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
    responseContext.setStatus(Response.Status.ACCEPTED.getStatusCode());
    responseContext.setEntity(new JaxRsTestObject("kuki", 2323));
}