下面列出了javax.ws.rs.core.Response#Status() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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();
}
@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();
}
/**
* 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);
}
/**
* 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);
}
}
/**
* 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;
}
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;
}
@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());
}
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);
}
@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();
}
public ApiException(String errorKey, String message, Response.Status status) {
super(message);
this.addError(errorKey, status);
}
@Override
protected Response.Status responseStatus() {
return Response.Status.NOT_FOUND;
}
public Response.Status getStatus() {
return status;
}
public void setStatus(Response.Status status) {
this.status = status;
}
public KubernetesHostDoesNotExistException(Response.Status httpStatusCode, String message, Throwable cause) {
super(message, cause);
this.message = message;
this.httpStatusCode = httpStatusCode;
}
public KubernetesMasterDoesNotExistException(Response.Status httpStatusCode, String message) {
super(message);
this.message = message;
this.httpStatusCode = httpStatusCode;
}
private static Response newResponse(Response.Status status, Object entity) {
return Response.status(status)
.entity(entity)
.type(APPLICATION_JSON_TYPE)
.build();
}
public Response.Status getResponseStatus() {
return responseStatus;
}
public CayenneFillResponseStage(Response.Status status) {
this.status = status;
}
public KubernetesClusterDoesNotExistException(Response.Status httpStatusCode, String message, Throwable cause) {
super(message, cause);
this.message = message;
this.httpStatusCode = httpStatusCode;
}
/**
* 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));
}