Search in sources :

Example 26 with Message

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

Example 27 with Message

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);
        }
    }
}
Also used : SocketException(java.net.SocketException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) BindException(java.net.BindException) PortOccupiedException(com.predic8.membrane.core.transport.PortOccupiedException) SocketException(java.net.SocketException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) InvalidParameterException(java.security.InvalidParameterException)

Example 28 with Message

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;
}
Also used : RequestTemplateCreator(com.predic8.wstool.creator.RequestTemplateCreator) StringWriter(java.io.StringWriter) Definitions(com.predic8.wsdl.Definitions) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) MarkupBuilder(groovy.xml.MarkupBuilder) WSDLParser(com.predic8.wsdl.WSDLParser) SOARequestCreator(com.predic8.wstool.creator.SOARequestCreator)

Aggregations

Message (com.predic8.membrane.core.http.Message)9 Response (com.predic8.membrane.core.http.Response)7 IOException (java.io.IOException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)5 JsonFactory (com.fasterxml.jackson.core.JsonFactory)4 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)4 Header (com.predic8.membrane.core.http.Header)4 Request (com.predic8.membrane.core.http.Request)4 Exchange (com.predic8.membrane.core.exchange.Exchange)2 Body (com.predic8.membrane.core.http.Body)2 HeaderField (com.predic8.membrane.core.http.HeaderField)2 ResponseBuilder (com.predic8.membrane.core.http.Response.ResponseBuilder)2 AbstractExchangeViewerListener (com.predic8.membrane.core.model.AbstractExchangeViewerListener)2 ResolverMap (com.predic8.membrane.core.resolver.ResolverMap)2 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)2 StringWriter (java.io.StringWriter)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1