use of com.predic8.membrane.core.http.Message 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;
}
use of com.predic8.membrane.core.http.Message in project service-proxy by membrane.
the class HttpEndpointListener method run.
@Override
public void run() {
while (!closed) {
try {
Socket socket = serverSocket.accept();
openSockets.put(socket, Boolean.TRUE);
try {
if (log.isDebugEnabled())
log.debug("Accepted connection from " + socket.getRemoteSocketAddress());
transport.getExecutorService().execute(new HttpServerHandler(socket, this));
} catch (RejectedExecutionException e) {
openSockets.remove(socket);
log.error("HttpServerHandler execution rejected. Might be due to a proxies.xml hot deployment in progress or a low" + " value for <transport maxThreadPoolSize=\"...\">.");
socket.close();
}
} catch (SocketException e) {
String message = e.getMessage();
if (message != null && (message.endsWith("socket closed") || message.endsWith("Socket closed"))) {
log.debug("socket closed.");
break;
} else
log.error("", e);
} catch (NullPointerException e) {
// Ignore this. serverSocket variable is set null during a loop in the process of closing server socket.
} catch (Exception e) {
log.error("", e);
}
}
}
use of com.predic8.membrane.core.http.Message in project carbon-business-process by wso2.
the class HTRenderingApiImpl method createSoapTemplate.
/**
* Function to create response message template
*
* @param SrcWsdl source wsld : wsdl file path or url
* @param portType callback port type
* @param operation callback operation name
* @param binding callback binding
* @return DOM element of response message template
* @throws IOException If error occurred while parsing string xml to Dom element
* @throws SAXException If error occurred while parsing string xml to Dom element
*/
private static Element createSoapTemplate(String SrcWsdl, String portType, String operation, String binding) throws IOException, SAXException {
WSDLParser parser = new WSDLParser();
// BPS-677
int fileLocationPrefixIndex = SrcWsdl.indexOf(HumanTaskConstants.FILE_LOCATION_FILE_PREFIX);
if (SrcWsdl.indexOf(HumanTaskConstants.FILE_LOCATION_FILE_PREFIX) != -1) {
SrcWsdl = SrcWsdl.substring(fileLocationPrefixIndex + HumanTaskConstants.FILE_LOCATION_FILE_PREFIX.length());
}
Definitions wsdl = parser.parse(SrcWsdl);
StringWriter writer = new StringWriter();
// SOAPRequestCreator constructor: SOARequestCreator(Definitions, Creator, MarkupBuilder)
SOARequestCreator creator = new SOARequestCreator(wsdl, new RequestTemplateCreator(), new MarkupBuilder(writer));
// creator.createRequest(PortType name, Operation name, Binding name);
creator.createRequest(portType, operation, binding);
Element outGenMessageDom = DOMUtils.stringToDOM(writer.toString());
Element outMsgElement = null;
NodeList nodes = outGenMessageDom.getElementsByTagNameNS(outGenMessageDom.getNamespaceURI(), "Body").item(0).getChildNodes();
if (nodes != null) {
for (int i = 0; i < nodes.getLength(); i++) {
if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
outMsgElement = (Element) nodes.item(i);
break;
}
}
}
if (outMsgElement != null) {
// convert element to string and back to element to remove Owner Document
return DOMUtils.stringToDOM(DOMUtils.domToString(outMsgElement));
}
return null;
}
Aggregations