Search in sources :

Example 26 with Port

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

the class WebServiceExplorerInterceptor method getPortsByLocation.

private List<Port> getPortsByLocation(Service service, Port port) {
    String location = port.getAddress().getLocation();
    if (location == null)
        throw new IllegalArgumentException("Location not set for port in WSDL.");
    final List<Port> ports = new ArrayList<Port>();
    for (Port p : service.getPorts()) if (location.equals(p.getAddress().getLocation()))
        ports.add(p);
    return ports;
}
Also used : Port(com.predic8.wsdl.Port) ArrayList(java.util.ArrayList)

Example 27 with Port

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

the class WebServiceExplorerInterceptor method createSOAPUIResponse.

@Mapping("(?!.*operation)([^?]*)")
public Response createSOAPUIResponse(QueryParameter params, final String relativeRootPath, final Exchange exc) throws Exception {
    try {
        final String myPath = router.getUriFactory().create(exc.getRequestURI()).getPath();
        final Definitions w = getParsedWSDL();
        final Service service = getService(w);
        final Port port = SOAPProxy.selectPort(service.getPorts(), portName);
        final List<Port> ports = getPortsByLocation(service, port);
        StringWriter sw = new StringWriter();
        new StandardPage(sw, service.getName()) {

            @Override
            protected void createContent() {
                h1().text("Service Proxy: " + service.getName()).end();
                p();
                text("Target Namespace: " + w.getTargetNamespace());
                br().end();
                String wsdlLink = getClientURL(exc) + "?wsdl";
                text("WSDL: ").a().href(wsdlLink).text(wsdlLink).end();
                end();
                for (PortType pt : w.getPortTypes()) {
                    h2().text("Port Type: " + pt.getName()).end();
                    Documentation d = pt.getDocumentation();
                    if (d != null) {
                        p().text("Documentation: " + d.toString()).end();
                    }
                }
                Binding binding = port.getBinding();
                PortType portType = binding.getPortType();
                List<Operation> bindingOperations = getOperationsByBinding(w, binding);
                if (bindingOperations.isEmpty())
                    p().text("There are no operations defined.").end();
                else
                    createOperationsTable(w, bindingOperations, binding, portType);
                h2().text("Virtual Endpoint").end();
                p().a().href(getClientURL(exc)).text(getClientURL(exc)).end().end();
                h2().text("Target Endpoints").end();
                if (service.getPorts().isEmpty())
                    p().text("There are no endpoints defined.").end();
                else
                    createEndpointTable(service.getPorts(), ports);
            }

            private void createOperationsTable(Definitions w, List<Operation> bindingOperations, Binding binding, PortType portType) {
                table().cellspacing("0").cellpadding("0").border("" + 1);
                tr();
                th().text("Operation").end();
                th().text("Input").end();
                th().text("Output").end();
                end();
                for (Operation o : bindingOperations) {
                    tr();
                    td();
                    if ("HTTP".equals(getProtocolVersion(binding))) {
                        text(o.getName());
                    } else {
                        String link = myPath + "/operation/" + binding.getName() + "/" + portType.getName() + "/" + o.getName();
                        a().href(link).text(o.getName()).end();
                    }
                    end();
                    td();
                    for (Part p : o.getInput().getMessage().getParts()) text(p.getElement().getName());
                    end();
                    td();
                    for (Part p : o.getOutput().getMessage().getParts()) text(p.getElement().getName());
                    end();
                    end();
                }
                end();
            }

            private void createEndpointTable(List<Port> ports, List<Port> matchingPorts) {
                table().cellspacing("0").cellpadding("0").border("" + 1);
                tr();
                th().text("Port Name").end();
                th().text("Protocol").end();
                th().text("URL").end();
                end();
                for (Port p : ports) {
                    tr();
                    td().text(p.getName()).end();
                    td().text(getProtocolVersion(p.getBinding())).end();
                    td().text(p.getAddress().getLocation()).end();
                    td();
                    if (matchingPorts.contains(p))
                        text("*");
                    end();
                    end();
                }
                end();
                p().small().text("* available through this proxy").end().end();
            }
        };
        return Response.ok(sw.toString()).build();
    } catch (IllegalArgumentException e) {
        log.error("", e);
        return Response.internalServerError().build();
    }
}
Also used : Binding(com.predic8.wsdl.Binding) Definitions(com.predic8.wsdl.Definitions) Port(com.predic8.wsdl.Port) Documentation(com.predic8.wsdl.Documentation) Service(com.predic8.wsdl.Service) Operation(com.predic8.wsdl.Operation) StringWriter(java.io.StringWriter) Part(com.predic8.wsdl.Part) ArrayList(java.util.ArrayList) List(java.util.List) PortType(com.predic8.wsdl.PortType) Mapping(com.predic8.membrane.core.interceptor.administration.Mapping)

Example 28 with Port

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

the class XML2HTTP method unwrapMessageIfNecessary.

/**
 * Checks, if the response contains an XML doc with NS {@link Constants#HTTP_NS}.
 * If it does, the HTTP data (uri, method, status, headers, body) is extracted from the doc
 * and set as the response.
 *
 * Reverse of {@link com.predic8.membrane.core.http.xml.Request#write(XMLStreamWriter)} and
 * {@link com.predic8.membrane.core.http.xml.Response#write(XMLStreamWriter)}.
 */
public static void unwrapMessageIfNecessary(Message message) {
    if (MimeType.TEXT_XML_UTF8.equals(message.getHeader().getContentType())) {
        try {
            if (message.getBody().getLength() == 0)
                return;
            XMLEventReader parser;
            synchronized (xmlInputFactory) {
                parser = xmlInputFactory.createXMLEventReader(message.getBodyAsStreamDecoded(), message.getCharset());
            }
            /* States:
				 * 0 = before root element,
				 * 1 = root element has HTTP_NS namespace
				 */
            int state = 0;
            boolean keepSourceHeaders = false, foundHeaders = false, foundBody = false;
            while (parser.hasNext()) {
                XMLEvent event = parser.nextEvent();
                switch(state) {
                    case 0:
                        if (event.isStartElement()) {
                            QName name = event.asStartElement().getName();
                            if (Constants.HTTP_NS.equals(name.getNamespaceURI())) {
                                state = 1;
                                if ("request".equals(name.getLocalPart())) {
                                    Request req = (Request) message;
                                    req.setMethod(requireAttribute(event.asStartElement(), "method"));
                                    String httpVersion = getAttribute(event.asStartElement(), "http-version");
                                    if (httpVersion == null)
                                        httpVersion = "1.1";
                                    req.setVersion(httpVersion);
                                }
                            } else {
                                return;
                            }
                        }
                        break;
                    case 1:
                        if (event.isStartElement()) {
                            String localName = event.asStartElement().getName().getLocalPart();
                            if ("status".equals(localName)) {
                                Response res = (Response) message;
                                res.setStatusCode(Integer.parseInt(requireAttribute(event.asStartElement(), "code")));
                                res.setStatusMessage(slurpCharacterData(parser, event.asStartElement()));
                            }
                            if ("uri".equals(localName)) {
                                Request req = (Request) message;
                                req.setUri(requireAttribute(event.asStartElement(), "value"));
                                // uri/... (port,host,path,query) structure is ignored, as value already contains everything
                                slurpXMLData(parser, event.asStartElement());
                            }
                            if ("headers".equals(localName)) {
                                foundHeaders = true;
                                keepSourceHeaders = "true".equals(getAttribute(event.asStartElement(), "keepSourceHeaders"));
                            }
                            if ("header".equals(localName)) {
                                String key = requireAttribute(event.asStartElement(), "name");
                                boolean remove = getAttribute(event.asStartElement(), "remove") != null;
                                if (remove && !keepSourceHeaders)
                                    throw new XML2HTTPException("<headers keepSourceHeaders=\"false\"><header name=\"...\" remove=\"true\"> does not make sense.");
                                message.getHeader().removeFields(key);
                                if (!remove)
                                    message.getHeader().add(key, slurpCharacterData(parser, event.asStartElement()));
                            }
                            if ("body".equals(localName)) {
                                foundBody = true;
                                String type = requireAttribute(event.asStartElement(), "type");
                                if ("plain".equals(type)) {
                                    message.setBodyContent(slurpCharacterData(parser, event.asStartElement()).getBytes(Constants.UTF_8_CHARSET));
                                } else if ("xml".equals(type)) {
                                    message.setBodyContent(slurpXMLData(parser, event.asStartElement()).getBytes(Constants.UTF_8_CHARSET));
                                } else {
                                    throw new XML2HTTPException("XML-HTTP doc body type '" + type + "' is not supported (only 'plain' or 'xml').");
                                }
                            }
                        }
                        break;
                }
            }
            if (!foundHeaders && !keepSourceHeaders)
                message.getHeader().clear();
            if (!foundBody)
                message.setBodyContent(new byte[0]);
        } catch (XMLStreamException e) {
            log.error("", e);
        } catch (XML2HTTPException e) {
            log.error("", e);
        } catch (IOException e) {
            log.error("", e);
        }
    }
}
Also used : Response(com.predic8.membrane.core.http.Response) XMLStreamException(javax.xml.stream.XMLStreamException) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent) Request(com.predic8.membrane.core.http.Request) XMLEventReader(javax.xml.stream.XMLEventReader) IOException(java.io.IOException)

Example 29 with Port

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

the class IndexInterceptor method handleRequest.

@Override
public Outcome handleRequest(final Exchange exc) throws Exception {
    StringWriter sw = new StringWriter();
    new Html(sw) {

        {
            html();
            head();
            title().text(Constants.PRODUCT_NAME + ": Service Proxies").end();
            style();
            raw("<!--\r\n" + "body { font-family: sans-serif; }\r\n" + "h1 { font-size: 24pt; }\r\n" + "td, th { border: 1px solid black; padding: 0pt 10pt; }\r\n" + "table { border-collapse: collapse; }\r\n" + ".help { margin-top:20pt; color:#AAAAAA; padding:1em 0em 0em 0em; font-size:10pt; }\r\n" + ".footer { color:#AAAAAA; padding:0em 0em; font-size:10pt; }\r\n" + ".footer a { color:#AAAAAA; }\r\n" + ".footer a:hover { color:#000000; }\r\n" + "-->");
            end();
            end();
            body();
            h1().text("Service Proxies").end();
            List<ServiceInfo> services = getServices(exc);
            if (services.isEmpty())
                p().text("There are no services defined.").end();
            else
                createIndexTable(services, exc.getHandler() instanceof HttpServerHandler);
            p().classAttr("help").text("The hyperlinks might not work due to semantics which is not known to " + Constants.PRODUCT_NAME + ".").end();
            p().classAttr("footer").raw(Constants.HTML_FOOTER).end();
            end();
            end();
        }

        private void createIndexTable(List<ServiceInfo> services, boolean showSSL) {
            table().cellspacing("0").cellpadding("0").border("" + 1);
            tr();
            th().text("Name").end();
            th().text("Virtual Host").end();
            th().text("Port").end();
            th().text("Path").end();
            if (showSSL)
                th().text("SSL").end();
            end();
            for (ServiceInfo ri : services) {
                tr();
                td();
                if (ri.url != null && !"POST".equals(ri.method)) {
                    a().href(ri.url);
                    text(ri.name);
                    end();
                } else {
                    text(ri.name);
                }
                end();
                td().raw(ri.host).end();
                td().raw(ri.port).end();
                td().raw(ri.path).end();
                if (showSSL)
                    td().raw(ri.ssl ? "yes" : "").end();
                end();
            }
            end();
        }
    };
    exc.setResponse(Response.ok(sw.toString()).build());
    return Outcome.RETURN;
}
Also used : StringWriter(java.io.StringWriter) Html(com.googlecode.jatl.Html) ArrayList(java.util.ArrayList) List(java.util.List) HttpServerHandler(com.predic8.membrane.core.transport.http.HttpServerHandler)

Example 30 with Port

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

the class RuleMatchingInterceptor method getRule.

private Rule getRule(Exchange exc) {
    Request request = exc.getRequest();
    AbstractHttpHandler handler = exc.getHandler();
    // retrieve value to match
    String hostHeader = request.getHeader().getHost();
    String method = request.getMethod();
    String uri = request.getUri();
    String version = request.getVersion();
    int port = handler.isMatchLocalPort() ? handler.getLocalPort() : -1;
    String localIP = handler.getLocalAddress().getHostAddress();
    // match it
    Rule rule = router.getRuleManager().getMatchingRule(hostHeader, method, uri, version, port, localIP);
    if (rule != null) {
        if (log.isDebugEnabled())
            log.debug("Matching Rule found for RuleKey " + hostHeader + " " + method + " " + uri + " " + port + " " + localIP);
        return rule;
    }
    return findProxyRule(exc);
}
Also used : Request(com.predic8.membrane.core.http.Request) Rule(com.predic8.membrane.core.rules.Rule) NullRule(com.predic8.membrane.core.rules.NullRule) ProxyRule(com.predic8.membrane.core.rules.ProxyRule) AbstractHttpHandler(com.predic8.membrane.core.transport.http.AbstractHttpHandler)

Aggregations

ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)9 Test (org.junit.Test)7 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)6 StringWriter (java.io.StringWriter)6 Exchange (com.predic8.membrane.core.exchange.Exchange)5 Rule (com.predic8.membrane.core.rules.Rule)5 Process2 (com.predic8.membrane.examples.Process2)5 File (java.io.File)5 ProxyRule (com.predic8.membrane.core.rules.ProxyRule)4 HttpRouter (com.predic8.membrane.core.HttpRouter)3 Request (com.predic8.membrane.core.http.Request)3 Response (com.predic8.membrane.core.http.Response)3 LoadBalancingInterceptor (com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor)3 Node (com.predic8.membrane.core.interceptor.balancer.Node)3 Definitions (com.predic8.wsdl.Definitions)3 Port (com.predic8.wsdl.Port)3 ArrayList (java.util.ArrayList)3 EtcdNodeInformation (com.predic8.membrane.core.cloud.etcd.EtcdNodeInformation)2 IPortChangeListener (com.predic8.membrane.core.model.IPortChangeListener)2 AbstractServiceProxy (com.predic8.membrane.core.rules.AbstractServiceProxy)2