Search in sources :

Example 56 with Service

use of com.predic8.wsdl.Service 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 57 with Service

use of com.predic8.wsdl.Service in project service-proxy by membrane.

the class SOAPProxy method parseWSDL.

/**
 * @return error or null for success
 */
private void parseWSDL() throws Exception {
    WSDLParserContext ctx = new WSDLParserContext();
    ctx.setInput(ResolverMap.combine(router.getBaseLocation(), wsdl));
    try {
        WSDLParser wsdlParser = new WSDLParser();
        wsdlParser.setResourceResolver(resolverMap.toExternalResolver().toExternalResolver());
        Definitions definitions = wsdlParser.parse(ctx);
        List<Service> services = definitions.getServices();
        if (services.size() != 1)
            throw new IllegalArgumentException("There are " + services.size() + " services defined in the WSDL, but exactly 1 is required for soapProxy.");
        Service service = services.get(0);
        if (StringUtils.isEmpty(name))
            name = StringUtils.isEmpty(service.getName()) ? definitions.getName() : service.getName();
        List<Port> ports = service.getPorts();
        Port port = selectPort(ports, portName);
        String location = port.getAddress().getLocation();
        if (location == null)
            throw new IllegalArgumentException("In the WSDL, there is no @location defined on the port.");
        try {
            URL url = new URL(location);
            target.setHost(url.getHost());
            if (url.getPort() != -1)
                target.setPort(url.getPort());
            else
                target.setPort(url.getDefaultPort());
            if (key.getPath() == null) {
                key.setUsePathPattern(true);
                key.setPathRegExp(false);
                key.setPath(url.getPath());
            } else {
                String query = "";
                if (url.getQuery() != null) {
                    query = "?" + url.getQuery();
                }
                targetPath = url.getPath() + query;
            }
            if (location.startsWith("https")) {
                SSLParser sslOutboundParser = new SSLParser();
                target.setSslParser(sslOutboundParser);
            }
            ((ServiceProxyKey) key).setMethod("*");
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("WSDL endpoint location '" + location + "' is not an URL.", e);
        }
        return;
    } catch (Exception e) {
        Throwable f = e;
        while (f.getCause() != null && !(f instanceof ResourceRetrievalException)) f = f.getCause();
        if (f instanceof ResourceRetrievalException) {
            ResourceRetrievalException rre = (ResourceRetrievalException) f;
            if (rre.getStatus() >= 400)
                throw rre;
            Throwable cause = rre.getCause();
            if (cause != null) {
                if (cause instanceof UnknownHostException)
                    throw (UnknownHostException) cause;
                else if (cause instanceof ConnectException)
                    throw (ConnectException) cause;
            }
        }
        throw new IllegalArgumentException("Could not download the WSDL '" + wsdl + "'.", e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) Definitions(com.predic8.wsdl.Definitions) Port(com.predic8.wsdl.Port) Service(com.predic8.wsdl.Service) URL(java.net.URL) ResourceRetrievalException(com.predic8.membrane.core.resolver.ResourceRetrievalException) ConnectException(java.net.ConnectException) MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) ResourceRetrievalException(com.predic8.membrane.core.resolver.ResourceRetrievalException) SSLParser(com.predic8.membrane.core.config.security.SSLParser) WSDLParser(com.predic8.wsdl.WSDLParser) WSDLParserContext(com.predic8.wsdl.WSDLParserContext) ConnectException(java.net.ConnectException)

Aggregations

Test (org.junit.Test)39 Process2 (com.predic8.membrane.examples.Process2)35 File (java.io.File)29 StringWriter (java.io.StringWriter)5 AbstractServiceProxy (com.predic8.membrane.core.rules.AbstractServiceProxy)4 Rule (com.predic8.membrane.core.rules.Rule)4 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 HttpRouter (com.predic8.membrane.core.HttpRouter)3 ProxiesXmlUtil (com.predic8.membrane.examples.ProxiesXmlUtil)3 BufferLogger (com.predic8.membrane.examples.util.BufferLogger)3 SubstringWaitableConsoleEvent (com.predic8.membrane.examples.util.SubstringWaitableConsoleEvent)3 Definitions (com.predic8.wsdl.Definitions)3 Port (com.predic8.wsdl.Port)3 Service (com.predic8.wsdl.Service)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Exchange (com.predic8.membrane.core.exchange.Exchange)2 Request (com.predic8.membrane.core.http.Request)2 Response (com.predic8.membrane.core.http.Response)2