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;
}
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();
}
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();
}
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;
}
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;
}
Aggregations