Search in sources :

Example 1 with PathRewriter

use of com.predic8.membrane.core.ws.relocator.Relocator.PathRewriter in project service-proxy by membrane.

the class WADLInterceptor method rewrite.

@Override
protected void rewrite(Exchange exc) throws Exception, IOException {
    log.debug("Changing endpoint address in WADL");
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Relocator relocator = new Relocator(new OutputStreamWriter(stream, exc.getResponse().getCharset()), getLocationProtocol(), getLocationHost(exc), getLocationPort(exc), pathRewriter);
    relocator.getRelocatingAttributes().put(new QName(WADL_NS, "resources"), "base");
    relocator.getRelocatingAttributes().put(new QName(WADL_NS, "include"), "href");
    relocator.relocate(new InputStreamReader(exc.getResponse().getBodyAsStreamDecoded(), exc.getResponse().getCharset()));
    exc.getResponse().setBodyContent(stream.toByteArray());
}
Also used : InputStreamReader(java.io.InputStreamReader) QName(javax.xml.namespace.QName) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Relocator(com.predic8.membrane.core.ws.relocator.Relocator)

Example 2 with PathRewriter

use of com.predic8.membrane.core.ws.relocator.Relocator.PathRewriter in project service-proxy by membrane.

the class WSDLInterceptor method rewrite.

@Override
protected void rewrite(Exchange exc) throws Exception, IOException {
    log.debug("Changing endpoint address in WSDL");
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Relocator relocator = new Relocator(new OutputStreamWriter(stream, exc.getResponse().getCharset()), getLocationProtocol(), getLocationHost(exc), getLocationPort(exc), pathRewriter);
    if (rewriteEndpoint) {
        relocator.getRelocatingAttributes().put(new QName(WSDL_SOAP11_NS, "address"), "location");
        relocator.getRelocatingAttributes().put(new QName(WSDL_SOAP12_NS, "address"), "location");
        relocator.getRelocatingAttributes().put(new QName(WSDL_HTTP_NS, "address"), "location");
    }
    relocator.getRelocatingAttributes().put(new QName(XSD_NS, "import"), "schemaLocation");
    relocator.getRelocatingAttributes().put(new QName(XSD_NS, "include"), "schemaLocation");
    relocator.relocate(new InputStreamReader(exc.getResponse().getBodyAsStreamDecoded(), exc.getResponse().getCharset()));
    if (relocator.isWsdlFound()) {
        registerWSDL(exc);
    }
    exc.getResponse().setBodyContent(stream.toByteArray());
}
Also used : InputStreamReader(java.io.InputStreamReader) QName(javax.xml.namespace.QName) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Relocator(com.predic8.membrane.core.ws.relocator.Relocator)

Example 3 with PathRewriter

use of com.predic8.membrane.core.ws.relocator.Relocator.PathRewriter in project service-proxy by membrane.

the class SOAPProxy method configure.

public void configure() throws Exception {
    parseWSDL();
    // remove previously added interceptors
    for (; automaticallyAddedInterceptorCount > 0; automaticallyAddedInterceptorCount--) interceptors.remove(0);
    // add interceptors (in reverse order) to position 0.
    WebServiceExplorerInterceptor sui = new WebServiceExplorerInterceptor();
    sui.setWsdl(wsdl);
    sui.setPortName(portName);
    interceptors.add(0, sui);
    automaticallyAddedInterceptorCount++;
    boolean hasPublisher = getInterceptorOfType(WSDLPublisherInterceptor.class) != null;
    if (!hasPublisher) {
        WSDLPublisherInterceptor wp = new WSDLPublisherInterceptor();
        wp.setWsdl(wsdl);
        interceptors.add(0, wp);
        automaticallyAddedInterceptorCount++;
    }
    WSDLInterceptor wsdlInterceptor = getInterceptorOfType(WSDLInterceptor.class);
    boolean hasRewriter = wsdlInterceptor != null;
    if (!hasRewriter) {
        wsdlInterceptor = new WSDLInterceptor();
        interceptors.add(0, wsdlInterceptor);
        automaticallyAddedInterceptorCount++;
    }
    if (key.getPath() != null) {
        final String keyPath = key.getPath();
        final String name = URLUtil.getName(router.getUriFactory(), keyPath);
        wsdlInterceptor.setPathRewriter(new PathRewriter() {

            @Override
            public String rewrite(String path2) {
                try {
                    if (path2.contains("://")) {
                        path2 = new URL(new URL(path2), keyPath).toString();
                    } else {
                        Matcher m = relativePathPattern.matcher(path2);
                        path2 = m.replaceAll("./" + name + "?");
                    }
                } catch (MalformedURLException e) {
                }
                return path2;
            }
        });
    }
    if (hasRewriter && !hasPublisher)
        log.warn("A <soapProxy> contains a <wsdlRewriter>, but no <wsdlPublisher>. Probably you want to insert a <wsdlPublisher> just after the <wsdlRewriter>. (Or, if this is a valid use case, please notify us at " + Constants.PRODUCT_CONTACT_EMAIL + ".)");
    if (targetPath != null) {
        RewriteInterceptor ri = new RewriteInterceptor();
        ri.setMappings(Lists.newArrayList(new RewriteInterceptor.Mapping("^" + Pattern.quote(key.getPath()), Matcher.quoteReplacement(targetPath), "rewrite")));
        interceptors.add(0, ri);
        automaticallyAddedInterceptorCount++;
    }
}
Also used : RewriteInterceptor(com.predic8.membrane.core.interceptor.rewrite.RewriteInterceptor) MalformedURLException(java.net.MalformedURLException) PathRewriter(com.predic8.membrane.core.ws.relocator.Relocator.PathRewriter) Matcher(java.util.regex.Matcher) WebServiceExplorerInterceptor(com.predic8.membrane.core.interceptor.soap.WebServiceExplorerInterceptor) WSDLInterceptor(com.predic8.membrane.core.interceptor.WSDLInterceptor) WSDLPublisherInterceptor(com.predic8.membrane.core.interceptor.server.WSDLPublisherInterceptor) URL(java.net.URL)

Example 4 with PathRewriter

use of com.predic8.membrane.core.ws.relocator.Relocator.PathRewriter in project service-proxy by membrane.

the class TestServiceInterceptor method init.

@Override
public void init(final Router router) throws Exception {
    super.init(router);
    wi.init(router);
    Rule r = router.getParentProxy(this);
    if (r instanceof AbstractServiceProxy) {
        final Path path = ((AbstractServiceProxy) r).getPath();
        if (path != null) {
            if (path.isRegExp())
                throw new Exception("<testService> may not be used together with <path isRegExp=\"true\">.");
            final String keyPath = path.getValue();
            final String name = URLUtil.getName(router.getUriFactory(), keyPath);
            wi.setPathRewriter(new PathRewriter() {

                @Override
                public String rewrite(String path2) {
                    try {
                        if (path2.contains("://")) {
                            path2 = new URL(new URL(path2), keyPath).toString();
                        } else {
                            Matcher m = RELATIVE_PATH_PATTERN.matcher(path2);
                            path2 = m.replaceAll("./" + name + "?");
                        }
                    } catch (MalformedURLException e) {
                    }
                    return path2;
                }
            });
        }
    }
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) Path(com.predic8.membrane.core.config.Path) MalformedURLException(java.net.MalformedURLException) PathRewriter(com.predic8.membrane.core.ws.relocator.Relocator.PathRewriter) Matcher(java.util.regex.Matcher) Rule(com.predic8.membrane.core.rules.Rule) MalformedURLException(java.net.MalformedURLException) SAXParseException(org.xml.sax.SAXParseException) URL(java.net.URL)

Aggregations

Relocator (com.predic8.membrane.core.ws.relocator.Relocator)2 PathRewriter (com.predic8.membrane.core.ws.relocator.Relocator.PathRewriter)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStreamReader (java.io.InputStreamReader)2 OutputStreamWriter (java.io.OutputStreamWriter)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 Matcher (java.util.regex.Matcher)2 QName (javax.xml.namespace.QName)2 Path (com.predic8.membrane.core.config.Path)1 WSDLInterceptor (com.predic8.membrane.core.interceptor.WSDLInterceptor)1 RewriteInterceptor (com.predic8.membrane.core.interceptor.rewrite.RewriteInterceptor)1 WSDLPublisherInterceptor (com.predic8.membrane.core.interceptor.server.WSDLPublisherInterceptor)1 WebServiceExplorerInterceptor (com.predic8.membrane.core.interceptor.soap.WebServiceExplorerInterceptor)1 AbstractServiceProxy (com.predic8.membrane.core.rules.AbstractServiceProxy)1 Rule (com.predic8.membrane.core.rules.Rule)1 SAXParseException (org.xml.sax.SAXParseException)1