Search in sources :

Example 1 with ResponseBuilder

use of com.predic8.membrane.core.http.Response.ResponseBuilder in project service-proxy by membrane.

the class Exchange method echo.

public Outcome echo() throws IOException {
    ResponseBuilder builder = Response.ok();
    byte[] content = getRequest().getBody().getContent();
    builder.body(content);
    String contentType = getRequest().getHeader().getContentType();
    if (contentType != null)
        builder.header(Header.CONTENT_TYPE, contentType);
    String contentEncoding = getRequest().getHeader().getContentEncoding();
    if (contentEncoding != null)
        builder.header(Header.CONTENT_ENCODING, contentEncoding);
    setResponse(builder.build());
    return Outcome.RETURN;
}
Also used : ResponseBuilder(com.predic8.membrane.core.http.Response.ResponseBuilder)

Example 2 with ResponseBuilder

use of com.predic8.membrane.core.http.Response.ResponseBuilder in project service-proxy by membrane.

the class AdminRESTInterceptor method getRequestBody.

@Mapping("/admin/rest/exchanges/(-?\\d+)/(response|request)/body")
public Response getRequestBody(QueryParameter params, String relativeRootPath) throws Exception {
    AbstractExchange exc = router.getExchangeStore().getExchangeById(params.getGroupInt(1));
    if (exc == null) {
        return Response.notFound().build();
    }
    Message msg = params.getGroup(2).equals("response") ? exc.getResponse() : exc.getRequest();
    String ct = params.getGroup(2).equals("response") ? exc.getResponseContentType() : exc.getRequestContentType();
    if (msg == null || msg.isBodyEmpty()) {
        return Response.noContent().build();
    }
    ResponseBuilder rb = Response.ok().contentType(ct).body(msg.getBodyAsStream(), false);
    String contentEncoding = msg.getHeader().getContentEncoding();
    if (contentEncoding != null)
        rb.header(Header.CONTENT_ENCODING, contentEncoding);
    return rb.build();
}
Also used : Message(com.predic8.membrane.core.http.Message) ResponseBuilder(com.predic8.membrane.core.http.Response.ResponseBuilder) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange)

Example 3 with ResponseBuilder

use of com.predic8.membrane.core.http.Response.ResponseBuilder in project service-proxy by membrane.

the class PositiveNode method toResponse.

@Override
public Response toResponse(Request request) {
    String ifModifiedSince = request.getHeader().getFirstValue(Header.IF_MODIFIED_SINCE);
    if (ifModifiedSince != null) {
        try {
            if (lastModified <= CacheInterceptor.fromRFC(ifModifiedSince))
                return Response.notModified(CacheInterceptor.toRFC(System.currentTimeMillis())).build();
        } catch (Exception e) {
            log.warn("", e);
        }
    }
    ResponseBuilder builder = Response.ok();
    if (status >= 300 && status < 400)
        builder.status(status, "Moved.");
    if (contentType != null)
        builder.contentType(contentType);
    if (location != null)
        builder.header(Header.LOCATION, location);
    if (lastModified != 0)
        return builder.header(Header.LAST_MODIFIED, CacheInterceptor.toRFC(lastModified)).body(content).build();
    else
        return builder.body(content).build();
}
Also used : ResponseBuilder(com.predic8.membrane.core.http.Response.ResponseBuilder) IOException(java.io.IOException) ParseException(java.text.ParseException)

Example 4 with ResponseBuilder

use of com.predic8.membrane.core.http.Response.ResponseBuilder in project service-proxy by membrane.

the class HttpUtil method setHTMLErrorResponse.

public static Response setHTMLErrorResponse(ResponseBuilder responseBuilder, String message, String comment) {
    Response response = responseBuilder.build();
    response.setHeader(createHeaders(MimeType.TEXT_HTML_UTF8));
    response.setBodyContent(getHTMLErrorBody(message, comment).getBytes(Constants.UTF_8_CHARSET));
    return response;
}
Also used : Response(com.predic8.membrane.core.http.Response)

Example 5 with ResponseBuilder

use of com.predic8.membrane.core.http.Response.ResponseBuilder in project service-proxy by membrane.

the class AbstractHttpHandler method generateErrorResponse.

private Response generateErrorResponse(Exception e) {
    String msg;
    boolean printStackTrace = transport.isPrintStackTrace();
    if (printStackTrace) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        msg = sw.toString();
    } else {
        msg = e.toString();
    }
    String comment = "Stack traces can be " + (printStackTrace ? "dis" : "en") + "abled by setting the " + "@printStackTrace attribute on <a href=\"https://membrane-soa.org/service-proxy-doc/current/configuration/reference/transport.htm\">transport</a>. " + "More details might be found in the log.";
    Response error = null;
    ResponseBuilder b = null;
    if (e instanceof URISyntaxException)
        b = Response.badRequest();
    if (b == null)
        b = Response.internalServerError();
    switch(ContentTypeDetector.detect(exchange.getRequest()).getEffectiveContentType()) {
        case XML:
            error = b.header(HttpUtil.createHeaders(MimeType.TEXT_XML_UTF8)).body(("<error><message>" + StringEscapeUtils.escapeXml(msg) + "</message><comment>" + StringEscapeUtils.escapeXml(comment) + "</comment></error>").getBytes(Constants.UTF_8_CHARSET)).build();
            break;
        case JSON:
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                JsonGenerator jg = new JsonFactory().createGenerator(baos);
                jg.writeStartObject();
                jg.writeFieldName("error");
                jg.writeString(msg);
                jg.writeFieldName("comment");
                jg.writeString(comment);
                jg.close();
            } catch (Exception f) {
                log.error("Error generating JSON error response", f);
            }
            error = b.header(HttpUtil.createHeaders(MimeType.APPLICATION_JSON_UTF8)).body(baos.toByteArray()).build();
            break;
        case SOAP:
            error = b.header(HttpUtil.createHeaders(MimeType.TEXT_XML_UTF8)).body(HttpUtil.getFaultSOAPBody("Internal Server Error", msg + " " + comment).getBytes(Constants.UTF_8_CHARSET)).build();
            break;
        case UNKNOWN:
            error = HttpUtil.setHTMLErrorResponse(b, msg, comment);
            break;
    }
    return error;
}
Also used : Response(com.predic8.membrane.core.http.Response) StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) URISyntaxException(java.net.URISyntaxException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResponseBuilder(com.predic8.membrane.core.http.Response.ResponseBuilder) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) PrintWriter(java.io.PrintWriter)

Aggregations

ResponseBuilder (com.predic8.membrane.core.http.Response.ResponseBuilder)4 Response (com.predic8.membrane.core.http.Response)2 IOException (java.io.IOException)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)1 Message (com.predic8.membrane.core.http.Message)1 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 URISyntaxException (java.net.URISyntaxException)1 ParseException (java.text.ParseException)1