Search in sources :

Example 6 with AbstractExchange

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

the class RuleMatchingInterceptor method insertXForwardedFor.

private void insertXForwardedFor(AbstractExchange exc) {
    Header h = exc.getRequest().getHeader();
    if (h.getNumberOf(Header.X_FORWARDED_FOR) > maxXForwardedForHeaders) {
        Request r = exc.getRequest();
        throw new RuntimeException("Request caused " + Header.X_FORWARDED_FOR + " flood: " + r.getStartLine() + r.getHeader().toString());
    }
    h.setXForwardedFor(getXForwardedForHeaderValue(exc));
    if (h.getNumberOf(Header.X_FORWARDED_PROTO) > maxXForwardedForHeaders) {
        Request r = exc.getRequest();
        throw new RuntimeException("Request caused " + Header.X_FORWARDED_PROTO + " flood: " + r.getStartLine() + r.getHeader().toString());
    }
    h.setXForwardedProto(getXForwardedProtoHeaderValue(exc));
    if (h.getNumberOf(Header.X_FORWARDED_HOST) > maxXForwardedForHeaders) {
        Request r = exc.getRequest();
        throw new RuntimeException("Request caused " + Header.X_FORWARDED_HOST + " flood: " + r.getStartLine() + r.getHeader().toString());
    }
    h.setXForwardedHost(getXForwardedHostHeaderValue(exc));
}
Also used : Header(com.predic8.membrane.core.http.Header) Request(com.predic8.membrane.core.http.Request)

Example 7 with AbstractExchange

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

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

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

the class REST2SOAPInterceptor method modifyRequest.

private void modifyRequest(AbstractExchange exc, Mapping mapping) {
    exc.getRequest().setMethod("POST");
    exc.getRequest().getHeader().setSOAPAction(mapping.soapAction);
    Header header = exc.getRequest().getHeader();
    header.removeFields(Header.CONTENT_TYPE);
    header.setContentType(isSOAP12(exc) ? MimeType.APPLICATION_SOAP : MimeType.TEXT_XML_UTF8);
    exc.setProperty("mapping", mapping);
    setServiceEndpoint(exc, mapping);
}
Also used : Header(com.predic8.membrane.core.http.Header)

Example 10 with AbstractExchange

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

the class AdminRESTInterceptor method getBeautifiedBody.

@Mapping("/admin/web/exchanges/(-?\\d+)/(response|request)/body")
public Response getBeautifiedBody(QueryParameter params, String relativeRootPath) throws Exception {
    AbstractExchange exc = router.getExchangeStore().getExchangeById(params.getGroupInt(1));
    if (exc == null) {
        return Response.notFound().build();
    }
    Message msg = params.getGroup(2).equals("response") ? exc.getResponse() : exc.getRequest();
    if (msg == null || msg.isBodyEmpty()) {
        return Response.noContent().build();
    }
    return Response.ok().contentType(MimeType.TEXT_HTML_UTF8).body(TextUtil.formatXML(new InputStreamReader(msg.getBodyAsStreamDecoded(), msg.getCharset()), true)).build();
}
Also used : Message(com.predic8.membrane.core.http.Message) InputStreamReader(java.io.InputStreamReader) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange)

Aggregations

AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)17 IOException (java.io.IOException)7 Exchange (com.predic8.membrane.core.exchange.Exchange)5 AbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot)4 DynamicAbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot)4 Message (com.predic8.membrane.core.http.Message)4 IExchangesStoreListener (com.predic8.membrane.core.model.IExchangesStoreListener)4 StatisticCollector (com.predic8.membrane.core.rules.StatisticCollector)4 UnknownHostException (java.net.UnknownHostException)4 CacheBuilder (com.google.common.cache.CacheBuilder)3 AbstractExchangeViewerListener (com.predic8.membrane.core.model.AbstractExchangeViewerListener)3 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 MCElement (com.predic8.membrane.annot.MCElement)2 AbstractBody (com.predic8.membrane.core.http.AbstractBody)2 Header (com.predic8.membrane.core.http.Header)2 JSONContent (com.predic8.membrane.core.interceptor.rest.JSONContent)2 SQLException (java.sql.SQLException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Cache (com.google.common.cache.Cache)1