Search in sources :

Example 11 with ResponseBuilder

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();
}
Also used : IOException(java.io.IOException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 12 with ResponseBuilder

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();
}
Also used : HashMap(java.util.HashMap) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 13 with ResponseBuilder

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();
}
Also used : MediaType(javax.ws.rs.core.MediaType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 14 with ResponseBuilder

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);
}
Also used : PaymentTransaction(org.killbill.billing.payment.api.PaymentTransaction) BillingExceptionJson(org.killbill.billing.jaxrs.json.BillingExceptionJson) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 15 with ResponseBuilder

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();
}
Also used : ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) QueryResults(com.facebook.presto.client.QueryResults)

Aggregations

ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)175 GET (javax.ws.rs.GET)84 Produces (javax.ws.rs.Produces)81 Path (javax.ws.rs.Path)69 WebApplicationException (javax.ws.rs.WebApplicationException)47 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)40 IOException (java.io.IOException)30 MediaType (javax.ws.rs.core.MediaType)29 POST (javax.ws.rs.POST)23 IRI (org.semanticweb.owlapi.model.IRI)22 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)21 Response (javax.ws.rs.core.Response)20 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)19 URI (java.net.URI)18 Consumes (javax.ws.rs.Consumes)18 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)18 ByteArrayInputStream (java.io.ByteArrayInputStream)16 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)16 HashSet (java.util.HashSet)14 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)12