Search in sources :

Example 11 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class WSDLInterceptor method createExchange.

private Exchange createExchange(String uri) throws MalformedURLException {
    URL url = new URL(uri);
    Request req = MessageUtil.getGetRequest(getCompletePath(url));
    req.getHeader().setHost(url.getHost());
    Exchange exc = new Exchange(null);
    exc.setRequest(req);
    exc.getDestinations().add(uri);
    return exc;
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Request(com.predic8.membrane.core.http.Request) URL(java.net.URL)

Example 12 with Exchange

use of com.predic8.membrane.core.exchange.Exchange 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 13 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class LimitedMemoryExchangeStore method oldSnap.

private void oldSnap(AbstractExchange exc, Flow flow) {
    // TODO: [fix me] support multi-snap
    // TODO: [fix me] snap message headers and request *here*, not in observer/response
    exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {

        @Override
        public void setExchangeFinished() {
            inflight.remove(exc);
        }
    });
    if (flow == Flow.REQUEST) {
        exc.getRequest().addObserver(new MessageObserver() {

            @Override
            public void bodyRequested(AbstractBody body) {
            }

            @Override
            public void bodyComplete(AbstractBody body) {
                Response r = exc.getResponse();
                if (r != null) {
                    AbstractBody b = r.getBody();
                    if (b != null && b.isRead())
                        // request-bodyComplete might occur after response-bodyComplete
                        return;
                }
                // System.out.println("Exchange put inflight " + exc.hashCode() + " " + exc.getRequest().getStartLine());
                inflight.put(exc, exc.getRequest());
                modify();
            }
        });
        return;
    }
    try {
        Message m = exc.getResponse();
        if (m != null)
            m.addObserver(new MessageObserver() {

                public void bodyRequested(AbstractBody body) {
                }

                public void bodyComplete(AbstractBody body) {
                    snapInternal(exc, flow);
                    inflight.remove(exc);
                    modify();
                // System.out.println("Exchange remove inflight " + exc.hashCode());
                }
            });
        else {
            inflight.remove(exc);
            modify();
        // System.out.println("Exchange remove inflight " + exc.hashCode() + " (2)");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : AbstractExchangeViewerListener(com.predic8.membrane.core.model.AbstractExchangeViewerListener)

Example 14 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class LimitedMemoryExchangeStore method getAllExchangesAsList.

public synchronized List<AbstractExchange> getAllExchangesAsList() {
    List<AbstractExchange> ret = new LinkedList<AbstractExchange>();
    for (Map.Entry<AbstractExchange, Request> entry : inflight.entrySet()) {
        AbstractExchange ex = entry.getKey();
        Request req = entry.getValue();
        Exchange newEx = new Exchange(null);
        newEx.setId(ex.getId());
        newEx.setRequest(req);
        newEx.setRule(ex.getRule());
        newEx.setRemoteAddr(ex.getRemoteAddr());
        newEx.setTime(ex.getTime());
        newEx.setTimeReqSent(ex.getTimeReqSent() != 0 ? ex.getTimeReqSent() : ex.getTimeReqReceived());
        newEx.setTimeResReceived(System.currentTimeMillis());
        ret.add(newEx);
    }
    ret.addAll(exchanges);
    return ret;
}
Also used : AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) Exchange(com.predic8.membrane.core.exchange.Exchange) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 15 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class IndexInterceptor method getServiceInfo.

private ServiceInfo getServiceInfo(Exchange exc, AbstractServiceProxy sp) {
    if (sp.getInterceptors().size() == 1 && sp.getInterceptors().get(0) instanceof IndexInterceptor)
        return null;
    ServiceProxyKey k = (ServiceProxyKey) sp.getKey();
    ServiceInfo ri = new ServiceInfo();
    ri.method = k.getMethod();
    // NOTE: when running as servlet, we have no idea what the protocol was
    ri.ssl = sp.getSslInboundContext() != null;
    String protocol = ri.ssl ? "https" : "http";
    String host = k.isHostWildcard() ? new HostColonPort(ri.ssl, exc.getRequest().getHeader().getHost()).host : fullfillRegexp(ServiceProxyKey.createHostPattern(k.getHost()));
    if (host == null || host.length() == 0)
        host = exc.getHandler().getLocalAddress().getHostAddress().toString();
    int port = k.getPort();
    if (port == -1 || !exc.getHandler().isMatchLocalPort())
        port = exc.getHandler().getLocalPort();
    String path;
    if (!k.isUsePathPattern()) {
        path = "/";
    } else if (k.isPathRegExp()) {
        path = fullfillRegexp(k.getPath());
    } else {
        path = "/" + StringUtils.removeStart(k.getPath(), "/");
    }
    if (!"".equals(exc.getHandler().getContextPath(exc))) {
        path = StringUtils.removeEnd(exc.getHandler().getContextPath(exc), "/") + "/" + StringUtils.removeStart(path, "/");
    }
    ri.name = sp.getName();
    if (path != null)
        ri.url = protocol + "://" + host + ":" + port + path;
    ri.host = k.isHostWildcard() ? "" : StringEscapeUtils.escapeHtml(k.getHost());
    ri.port = k.getPort() == -1 ? "" : "" + k.getPort();
    ri.path = k.isUsePathPattern() ? "<tt>" + StringEscapeUtils.escapeHtml(k.getPath()) + "</tt>" + (k.isPathRegExp() ? " (regex)" : "") : "";
    return ri;
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) HostColonPort(com.predic8.membrane.core.transport.http.HostColonPort)

Aggregations

Exchange (com.predic8.membrane.core.exchange.Exchange)107 Test (org.junit.Test)39 IOException (java.io.IOException)32 Request (com.predic8.membrane.core.http.Request)25 Outcome (com.predic8.membrane.core.interceptor.Outcome)24 Response (com.predic8.membrane.core.http.Response)16 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)16 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)16 HttpRouter (com.predic8.membrane.core.HttpRouter)14 Before (org.junit.Before)13 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)12 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)11 Header (com.predic8.membrane.core.http.Header)10 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)10 CacheBuilder (com.google.common.cache.CacheBuilder)9 Rule (com.predic8.membrane.core.rules.Rule)6 URISyntaxException (java.net.URISyntaxException)6 UnknownHostException (java.net.UnknownHostException)6 ArrayList (java.util.ArrayList)6 Session (com.predic8.membrane.core.interceptor.authentication.session.SessionManager.Session)5