Java Code Examples for javax.ws.rs.core.Response#Status

The following examples show how to use javax.ws.rs.core.Response#Status . 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: AbstractApi.java    From gitlab4j-api with MIT License 6 votes vote down vote up
/**
 * Validates response the response from the server against the expected HTTP status and
 * the returned secret token, if either is not correct will throw a GitLabApiException.
 *
 * @param response response
 * @param expected expected response status
 * @return original response if the response status is expected
 * @throws GitLabApiException if HTTP status is not as expected, or the secret token doesn't match
 */
protected Response validate(Response response, Response.Status expected) throws GitLabApiException {

    int responseCode = response.getStatus();
    int expectedResponseCode = expected.getStatusCode();

    if (responseCode != expectedResponseCode) {

        // If the expected code is 200-204 and the response code is 200-204 it is OK.  We do this because
        // GitLab is constantly changing the expected code in the 200 to 204 range
        if (expectedResponseCode > 204 || responseCode > 204 || expectedResponseCode < 200 || responseCode < 200)
            throw new GitLabApiException(response);
    }

    if (!getApiClient().validateSecretToken(response)) {
        throw new GitLabApiException(new NotAuthorizedException("Invalid secret token in response."));
    }

    return (response);
}
 
Example 2
Source File: GetAgreementExceptionMapper.java    From pay-publicapi with MIT License 6 votes vote down vote up
@Override
public Response toResponse(GetMandateException exception) {
    Response.Status status;

    MandateError paymentError;

    if (exception.getErrorStatus() == NOT_FOUND.getStatusCode()) {
        paymentError = MandateError.aMandateError(GET_MANDATE_NOT_FOUND_ERROR);
        status = NOT_FOUND;
    } else {
        paymentError = MandateError.aMandateError(GET_MANDATE_CONNECTOR_ERROR);
        status = INTERNAL_SERVER_ERROR;
        LOGGER.error("Connector invalid response was {}.\n Returning http status {} with error body {}", exception.getMessage(), status, paymentError);
    }

    return Response
            .status(status)
            .entity(paymentError)
            .build();
}
 
Example 3
Source File: JaxRsUtils.java    From cassandra-mesos-deprecated with Apache License 2.0 6 votes vote down vote up
@NotNull
public static Response buildStreamingResponse(@NotNull final JsonFactory factory, @NotNull final Response.Status status, @NotNull final StreamingJsonResponse jsonResponse) {
    return Response.status(status).entity(new StreamingOutput() {
        @Override
        public void write(final OutputStream output) throws IOException, WebApplicationException {
            try (JsonGenerator json = factory.createGenerator(output)) {
                json.setPrettyPrinter(new DefaultPrettyPrinter());
                json.writeStartObject();

                jsonResponse.write(json);

                json.writeEndObject();
            }
        }
    }).type("application/json").build();
}
 
Example 4
Source File: ServerIdentityGovernanceService.java    From identity-api-server with Apache License 2.0 5 votes vote down vote up
private APIError handleNotFoundError(String resourceId,
                                     GovernanceConstants.ErrorMessage errorMessage) {

    Response.Status status = Response.Status.NOT_FOUND;
    ErrorResponse errorResponse =
            getErrorBuilder(errorMessage, resourceId).build(LOG, errorMessage.getDescription());

    return new APIError(status, errorResponse);
}
 
Example 5
Source File: NotificationException.java    From notification with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param status Status code to return
 * @param message Error message to return
 * @param cause Throwable which caused the exception
 */
public NotificationException(
    final Response.Status status, final String message, final Throwable cause) {
  super(cause, status);
  this.code = status.getStatusCode();
  this.status = status;
  this.message = message;
}
 
Example 6
Source File: DefaultExceptionMapperTest.java    From enmasse with Apache License 2.0 5 votes vote down vote up
@Test
public void testToResponseStatus500() {
    int code = 500;
    Response.Status status = Response.Status.fromStatusCode(code);
    String message = "Some error message";
    KubernetesClientException kubernetesClientException = new KubernetesClientException(message, new NullPointerException("something's gone bad!"));

    Response response = new DefaultExceptionMapper().toResponse(kubernetesClientException);
    assertEquals(code, response.getStatus());
    assertEquals(status.getReasonPhrase(), response.getStatusInfo().getReasonPhrase());
    assertTrue(response.getEntity() instanceof Status);
    Status responseEntity = (Status) response.getEntity();
    assertEquals("Internal Server Error", responseEntity.getReason());
    assertEquals(message, responseEntity.getMessage());
}
 
Example 7
Source File: ServerIdentityGovernanceService.java    From identity-api-server with Apache License 2.0 5 votes vote down vote up
/**
 * Get governance connector.
 *
 * @param categoryId  Governance connector category id.
 * @param connectorId Governance connector id.
 * @return Governance connectors for the give id.
 */
public ConnectorRes getGovernanceConnector(String categoryId, String connectorId) {

    try {
        IdentityGovernanceService identityGovernanceService = GovernanceDataHolder.getIdentityGovernanceService();
        String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
        String connectorName = new String(Base64.getUrlDecoder().decode(connectorId), StandardCharsets.UTF_8);
        ConnectorConfig connectorConfig =
                identityGovernanceService.getConnectorWithConfigs(tenantDomain, connectorName);
        if (connectorConfig == null) {
            throw handleNotFoundError(connectorId, GovernanceConstants.ErrorMessage.ERROR_CODE_CONNECTOR_NOT_FOUND);
        }
        String categoryIdFound = Base64.getUrlEncoder()
                .withoutPadding()
                .encodeToString(connectorConfig.getCategory().getBytes(StandardCharsets.UTF_8));
        if (!categoryId.equals(categoryIdFound)) {
            throw handleNotFoundError(connectorId, GovernanceConstants.ErrorMessage.ERROR_CODE_CONNECTOR_NOT_FOUND);
        }

        return buildConnectorResDTO(connectorConfig);

    } catch (IdentityGovernanceException e) {
        GovernanceConstants.ErrorMessage errorEnum =
                GovernanceConstants.ErrorMessage.ERROR_CODE_ERROR_RETRIEVING_CONNECTOR;
        Response.Status status = Response.Status.INTERNAL_SERVER_ERROR;
        throw handleException(e, errorEnum, status);
    }
}
 
Example 8
Source File: ErrorPageException.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public ErrorPageException(KeycloakSession session, AuthenticationSessionModel authSession, Response.Status status, String errorMessage, Object... parameters) {
    this.session = session;
    this.status = status;
    this.errorMessage = errorMessage;
    this.parameters = parameters;
    this.authSession = authSession;
}
 
Example 9
Source File: KubernetesMasterDoesNotExistException.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public KubernetesMasterDoesNotExistException(Response.Status httpStatusCode, String message) {
    super(message);
    this.message = message;
    this.httpStatusCode = httpStatusCode;
}
 
Example 10
Source File: KubernetesClusterDoesNotExistException.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public KubernetesClusterDoesNotExistException(Response.Status httpStatusCode, String message, Throwable cause) {
    super(message, cause);
    this.message = message;
    this.httpStatusCode = httpStatusCode;
}
 
Example 11
Source File: CayenneFillResponseStage.java    From agrest with Apache License 2.0 4 votes vote down vote up
public CayenneFillResponseStage(Response.Status status) {
    this.status = status;
}
 
Example 12
Source File: AuthDTO.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
public Response.Status getResponseStatus() {
    return responseStatus;
}
 
Example 13
Source File: GenericExceptionMapper.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private static Response newResponse(Response.Status status, Object entity) {
  return Response.status(status)
      .entity(entity)
      .type(APPLICATION_JSON_TYPE)
      .build();
}
 
Example 14
Source File: KubernetesHostDoesNotExistException.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public KubernetesHostDoesNotExistException(Response.Status httpStatusCode, String message, Throwable cause) {
    super(message, cause);
    this.message = message;
    this.httpStatusCode = httpStatusCode;
}
 
Example 15
Source File: FHIRRestOperationResponse.java    From FHIR with Apache License 2.0 4 votes vote down vote up
public void setStatus(Response.Status status) {
    this.status = status;
}
 
Example 16
Source File: ErrorResponse.java    From tomee with Apache License 2.0 4 votes vote down vote up
public Response.Status getStatus() {
    return status;
}
 
Example 17
Source File: EntityNotFoundMapper.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
protected Response.Status responseStatus() {
    return Response.Status.NOT_FOUND;
}
 
Example 18
Source File: ApiException.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
public ApiException(String errorKey, String message, Response.Status status) {
	super(message);
	this.addError(errorKey, status);
}
 
Example 19
Source File: BuildConflictExceptionMapper.java    From pnc with Apache License 2.0 4 votes vote down vote up
@Override
public Response toResponse(BuildConflictException e) {
    Response.Status status = Response.Status.CONFLICT;
    logger.debug("A BuildConflict error occurred when processing REST call", e);
    return Response.status(status).entity(new ErrorResponseRest(e)).build();
}
 
Example 20
Source File: ProjectApi.java    From gitlab4j-api with MIT License 2 votes vote down vote up
/**
 * Star a project.
 *
 * <pre><code>GitLab Endpoint: POST /projects/:id/star</code></pre>
 *
 * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
 * @return a Project instance with the new project info
 * @throws GitLabApiException if any exception occurs
 */
public Project starProject(Object projectIdOrPath) throws GitLabApiException {
    Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.CREATED);
    Response response = post(expectedStatus, (Form) null, "projects", getProjectIdOrPath(projectIdOrPath), "star");
    return (response.readEntity(Project.class));
}