use of javax.ws.rs.core.Response.ResponseBuilder in project hbase by apache.
the class ExistsResource method get.
@GET
@Produces({ MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF, MIMETYPE_BINARY })
public Response get(@Context final UriInfo uriInfo) {
try {
if (!tableResource.exists()) {
return Response.status(Response.Status.NOT_FOUND).type(MIMETYPE_TEXT).entity("Not found" + CRLF).build();
}
} catch (IOException e) {
return Response.status(Response.Status.SERVICE_UNAVAILABLE).type(MIMETYPE_TEXT).entity("Unavailable" + CRLF).build();
}
ResponseBuilder response = Response.ok();
response.cacheControl(cacheControl);
return response.build();
}
use of javax.ws.rs.core.Response.ResponseBuilder in project storm by apache.
the class DRPCExceptionMapper method toResponse.
@Override
public Response toResponse(DRPCExecutionException ex) {
ResponseBuilder builder = Response.status(500);
switch(ex.get_type()) {
case FAILED_REQUEST:
builder.status(400);
break;
case SERVER_SHUTDOWN:
//Not available
builder.status(503);
break;
case SERVER_TIMEOUT:
//proxy timeout
builder.status(504);
break;
case INTERNAL_ERROR:
//fall throw on purpose
default:
//Empty (Still 500)
break;
}
Map<String, String> body = new HashMap<>();
//TODO I would love to standardize this...
body.put("error", ex.is_set_type() ? ex.get_type().toString() : "Internal Error");
body.put("errorMessage", ex.get_msg());
return builder.entity(JSONValue.toJSONString(body)).type("application/json").build();
}
use of javax.ws.rs.core.Response.ResponseBuilder in project Openfire by igniterealtime.
the class RESTExceptionMapper method toResponse.
/*
* (non-Javadoc)
*
* @see javax.ws.rs.ext.ExceptionMapper#toResponse(java.lang.Throwable)
*/
public Response toResponse(ServiceException exception) {
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setRessource(exception.getRessource());
errorResponse.setMessage(exception.getMessage());
errorResponse.setException(exception.getException());
LOG.error(exception.getException() + ": " + exception.getMessage() + " with ressource " + exception.getRessource(), exception.getException());
ResponseBuilder responseBuilder = Response.status(exception.getStatus()).entity(errorResponse);
List<MediaType> accepts = headers.getAcceptableMediaTypes();
if (accepts != null && accepts.size() > 0) {
MediaType mediaType = accepts.get(0);
responseBuilder = responseBuilder.type(mediaType);
} else {
responseBuilder = responseBuilder.type(headers.getMediaType());
}
return responseBuilder.build();
}
use of javax.ws.rs.core.Response.ResponseBuilder in project killbill by killbill.
the class JaxRsResourceBase method createPaymentResponse.
protected Response createPaymentResponse(final UriInfo uriInfo, final Payment payment, final TransactionType transactionType, @Nullable final String transactionExternalKey, final HttpServletRequest request) {
final PaymentTransaction createdTransaction = findCreatedTransaction(payment, transactionType, transactionExternalKey);
Preconditions.checkNotNull(createdTransaction, "No transaction of type '%s' found", transactionType);
final ResponseBuilder responseBuilder;
final BillingExceptionJson exception;
switch(createdTransaction.getTransactionStatus()) {
case PENDING:
case SUCCESS:
return uriBuilder.buildResponse(uriInfo, PaymentResource.class, "getPayment", payment.getId(), request);
case PAYMENT_FAILURE:
// 402 - Payment Required
responseBuilder = Response.status(402);
exception = createBillingException(String.format("Payment decline by gateway. Error message: %s", createdTransaction.getGatewayErrorMsg()));
break;
case PAYMENT_SYSTEM_OFF:
// 503 - Service Unavailable
responseBuilder = Response.status(Status.SERVICE_UNAVAILABLE);
exception = createBillingException("Payment system is off.");
break;
case UNKNOWN:
// 503 - Service Unavailable
responseBuilder = Response.status(Status.SERVICE_UNAVAILABLE);
exception = createBillingException("Payment in unknown status, failed to receive gateway response.");
break;
case PLUGIN_FAILURE:
// 502 - Bad Gateway
responseBuilder = Response.status(502);
exception = createBillingException("Failed to submit payment transaction");
break;
default:
// Should never happen
responseBuilder = Response.serverError();
exception = createBillingException("This should never have happened!!!");
}
addExceptionToResponse(responseBuilder, exception);
return uriBuilder.buildResponse(responseBuilder, uriInfo, PaymentResource.class, "getPayment", payment.getId(), request);
}
use of javax.ws.rs.core.Response.ResponseBuilder in project presto by prestodb.
the class StatementResource method getQueryResults.
private static Response getQueryResults(Query query, Optional<Long> token, UriInfo uriInfo, Duration wait) throws InterruptedException {
QueryResults queryResults;
if (token.isPresent()) {
queryResults = query.getResults(token.get(), uriInfo, wait);
} else {
queryResults = query.getNextResults(uriInfo, wait);
}
ResponseBuilder response = Response.ok(queryResults);
// add set session properties
query.getSetSessionProperties().entrySet().forEach(entry -> response.header(PRESTO_SET_SESSION, entry.getKey() + '=' + entry.getValue()));
// add clear session properties
query.getResetSessionProperties().forEach(name -> response.header(PRESTO_CLEAR_SESSION, name));
// add added prepare statements
for (Entry<String, String> entry : query.getAddedPreparedStatements().entrySet()) {
String encodedKey = urlEncode(entry.getKey());
String encodedValue = urlEncode(entry.getValue());
response.header(PRESTO_ADDED_PREPARE, encodedKey + '=' + encodedValue);
}
// add deallocated prepare statements
for (String name : query.getDeallocatedPreparedStatements()) {
response.header(PRESTO_DEALLOCATED_PREPARE, urlEncode(name));
}
// add new transaction ID
query.getStartedTransactionId().ifPresent(transactionId -> response.header(PRESTO_STARTED_TRANSACTION_ID, transactionId));
// add clear transaction ID directive
if (query.isClearTransactionId()) {
response.header(PRESTO_CLEAR_TRANSACTION_ID, true);
}
return response.build();
}
Aggregations