Search in sources :

Example 26 with Response

use of com.predic8.membrane.core.http.Response in project service-proxy by membrane.

the class TestServiceInterceptor method handleOperation.

private Response handleOperation(Element operation, boolean soap11) {
    if (!operation.getNamespaceURI().equals("http://thomas-bayer.com/blz/"))
        throw new AssertionError("Unknown operation namespace.");
    if (operation.getLocalName().equals("getBank")) {
        NodeList children = operation.getChildNodes();
        Element param = null;
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i) instanceof Text) {
                String text = ((Text) children.item(i)).getNodeValue();
                for (int j = 0; j < text.length(); j++) if (!Character.isWhitespace(text.charAt(j)))
                    throw new AssertionError("Found non-whitespace text.");
                continue;
            }
            if (!(children.item(i) instanceof Element))
                throw new AssertionError("Non-element child of <Body> found: " + children.item(i).getNodeName() + ".");
            param = (Element) children.item(i);
        }
        if (param == null)
            throw new AssertionError("No parameter child of operation element found.");
        if (!param.getNamespaceURI().equals("http://thomas-bayer.com/blz/") || !param.getLocalName().equals("blz"))
            throw new AssertionError("Unknown parameter element.");
        children = param.getChildNodes();
        if (children.getLength() != 1)
            throw new AssertionError("Parameter element has children.length != 1");
        if (!(children.item(0) instanceof Text))
            throw new AssertionError("Parameter element has non-text child.");
        Text text = (Text) children.item(0);
        String blz = text.getNodeValue();
        return getBank(blz, soap11);
    } else {
        throw new AssertionError("Unknown operation.");
    }
}
Also used : NodeList(org.w3c.dom.NodeList) MCElement(com.predic8.membrane.annot.MCElement) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text)

Example 27 with Response

use of com.predic8.membrane.core.http.Response in project service-proxy by membrane.

the class TestServiceInterceptor method handleSOAP11.

private Response handleSOAP11(Element envelope) {
    Element body = null;
    NodeList children = envelope.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Text) {
            String text = ((Text) children.item(i)).getNodeValue();
            for (int j = 0; j < text.length(); j++) if (!Character.isWhitespace(text.charAt(j)))
                throw new AssertionError("Found non-whitespace text.");
            continue;
        }
        if (!(children.item(i) instanceof Element))
            throw new AssertionError("Non-element child of <Envelope> found: " + children.item(i).getNodeName() + ".");
        Element item = (Element) children.item(i);
        if (!item.getNamespaceURI().equals(Constants.SOAP11_NS))
            throw new AssertionError("Non-SOAP child element of <Envelope> found.");
        if (item.getLocalName().equals("Body"))
            body = item;
    }
    if (body == null)
        throw new AssertionError("No SOAP <Body> found.");
    children = body.getChildNodes();
    Element operation = null;
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Text) {
            String text = ((Text) children.item(i)).getNodeValue();
            for (int j = 0; j < text.length(); j++) if (!Character.isWhitespace(text.charAt(j)))
                throw new AssertionError("Found non-whitespace text.");
            continue;
        }
        if (!(children.item(i) instanceof Element))
            throw new AssertionError("Non-element child of <Body> found: " + children.item(i).getNodeName() + ".");
        operation = (Element) children.item(i);
    }
    if (operation == null)
        throw new AssertionError("No SOAP <Body> found.");
    return handleOperation(operation, true);
}
Also used : MCElement(com.predic8.membrane.annot.MCElement) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Text(org.w3c.dom.Text)

Example 28 with Response

use of com.predic8.membrane.core.http.Response in project service-proxy by membrane.

the class HTTPSchemaResolver method resolve.

public InputStream resolve(String url) throws ResourceRetrievalException {
    try {
        Exchange exc = new Request.Builder().method(Request.METHOD_GET).url(uriFactory, url).header(Header.USER_AGENT, Constants.PRODUCT_NAME + " " + Constants.VERSION).buildExchange();
        Response response = getHttpClient().call(exc).getResponse();
        response.readBody();
        if (response.getStatusCode() != 200) {
            ResourceRetrievalException rde = new ResourceRetrievalException(url, response.getStatusCode());
            throw rde;
        }
        return new ByteArrayInputStream(ByteUtil.getByteArrayData(response.getBodyAsStreamDecoded()));
    } catch (ResourceRetrievalException e) {
        throw e;
    } catch (Exception e) {
        ResourceRetrievalException rre = new ResourceRetrievalException(url, e);
        throw rre;
    }
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Response(com.predic8.membrane.core.http.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 29 with Response

use of com.predic8.membrane.core.http.Response in project service-proxy by membrane.

the class LBNotificationClient method run.

public void run(String[] args) throws Exception {
    CommandLine cl = new DefaultParser().parse(getOptions(), args, false);
    if (cl.hasOption('h') || args.length < 2) {
        printUsage();
        return;
    }
    parseArguments(cl);
    logArguments();
    Response res = notifiyClusterManager();
    if (res.getStatusCode() != 204) {
        throw new Exception("Got StatusCode: " + res.getStatusCode());
    }
    log.info("Sent " + cmd + " message to " + host + ":" + port + (skeySpec != null ? " encrypted" : ""));
}
Also used : Response(com.predic8.membrane.core.http.Response)

Example 30 with Response

use of com.predic8.membrane.core.http.Response in project service-proxy by membrane.

the class DynamicAbstractExchangeSnapshot method addObservers.

public static void addObservers(AbstractExchange exc, AbstractExchangeSnapshot excCopy, Consumer<AbstractExchangeSnapshot> callback) {
    MessageObserver obs = new MessageObserver() {

        @Override
        public void bodyRequested(AbstractBody body) {
        }

        @Override
        public void bodyComplete(AbstractBody body) {
            update(callback, excCopy, exc);
        }
    };
    exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {

        @Override
        public void addResponse(Response response) {
            response.addObserver(obs);
        }

        @Override
        public void setExchangeFinished() {
            update(callback, excCopy, exc);
        }
    });
    Stream.of(exc.getRequest(), exc.getResponse()).forEach(msg -> {
        if (msg == null)
            return;
        if (msg.containsObserver(obs))
            return;
        msg.addObserver(obs);
    });
    update(callback, excCopy, exc);
}
Also used : Response(com.predic8.membrane.core.http.Response) MessageObserver(com.predic8.membrane.core.http.MessageObserver) AbstractBody(com.predic8.membrane.core.http.AbstractBody) AbstractExchangeViewerListener(com.predic8.membrane.core.model.AbstractExchangeViewerListener)

Aggregations

Response (com.predic8.membrane.core.http.Response)29 Exchange (com.predic8.membrane.core.exchange.Exchange)14 IOException (java.io.IOException)14 StringWriter (java.io.StringWriter)9 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)7 Request (com.predic8.membrane.core.http.Request)7 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)6 Header (com.predic8.membrane.core.http.Header)6 Test (org.junit.Test)6 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)4 Message (com.predic8.membrane.core.http.Message)4 JSONContent (com.predic8.membrane.core.interceptor.rest.JSONContent)4 ProxyRule (com.predic8.membrane.core.rules.ProxyRule)4 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)4 SQLException (java.sql.SQLException)4 Element (org.w3c.dom.Element)4 NodeList (org.w3c.dom.NodeList)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 MCElement (com.predic8.membrane.annot.MCElement)3 ResponseBuilder (com.predic8.membrane.core.http.Response.ResponseBuilder)3