Search in sources :

Example 31 with Body

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

the class XSLTInterceptorTest method testRequest.

@Test
public void testRequest() throws Exception {
    exc = new Exchange(null);
    exc.setResponse(Response.ok().body(getClass().getResourceAsStream("/customer.xml"), true).build());
    XSLTInterceptor i = new XSLTInterceptor();
    i.setXslt("classpath:/customer2person.xsl");
    i.init(new HttpRouter());
    i.handleResponse(exc);
    // printBodyContent();
    assertXPath("/person/name/first", "Rick");
    assertXPath("/person/name/last", "Cort\u00e9s Ribotta");
    assertXPath("/person/address/street", "Calle P\u00fablica \"B\" 5240 Casa 121");
    assertXPath("/person/address/city", "Omaha");
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) HttpRouter(com.predic8.membrane.core.HttpRouter) Test(org.junit.Test)

Example 32 with Body

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

the class SOAPFaultTest method getExchangeCP.

private Exchange getExchangeCP(String path) throws IOException {
    Exchange exc = new Exchange(null);
    exc.setResponse(Response.ok().contentType("text/xml").body(getClass().getClassLoader().getResourceAsStream(path), true).build());
    return exc;
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange)

Example 33 with Body

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

the class ResolverTestTriggerTest method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) {
    try {
        Class<?> clazz = Class.forName("com.predic8.membrane.core.resolver.ResolverTest");
        clazz.getField("deployment").set(null, "J2EE");
        Object value = router.getResolverMap().getFileSchemaResolver();
        Object resolverMap = clazz.getField("resolverMap").get(null);
        resolverMap.getClass().getMethod("addSchemaResolver", SchemaResolver.class).invoke(resolverMap, value);
        Parameterized p = new Parameterized(clazz);
        JUnitCore c = new JUnitCore();
        Result run = c.run(Request.runner(p));
        StringBuilder sb = new StringBuilder();
        sb.append(MAGIC);
        for (Failure f : run.getFailures()) {
            sb.append(f.toString());
            StringWriter stringWriter = new StringWriter();
            f.getException().printStackTrace(new PrintWriter(stringWriter));
            sb.append(stringWriter.toString());
            sb.append("\n");
            sb.append("\n");
        }
        exc.setResponse(Response.ok().header(Header.CONTENT_TYPE, MimeType.TEXT_PLAIN_UTF8).body(sb.toString()).build());
    } catch (Throwable t) {
        LOG.error(t.getMessage(), t);
    }
    return Outcome.RETURN;
}
Also used : Parameterized(org.junit.runners.Parameterized) JUnitCore(org.junit.runner.JUnitCore) StringWriter(java.io.StringWriter) SchemaResolver(com.predic8.membrane.core.resolver.SchemaResolver) Failure(org.junit.runner.notification.Failure) Result(org.junit.runner.Result) PrintWriter(java.io.PrintWriter)

Example 34 with Body

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

the class AttributeInfo method analyze.

private void analyze(Types typeUtils) {
    if (// already analyzed?
    xsdType != null)
        return;
    if (getE().getParameters().size() != 1)
        throw new ProcessingException("Setter is supposed to have 1 parameter.", getE());
    VariableElement ve = getE().getParameters().get(0);
    switch(ve.asType().getKind()) {
        case INT:
            xsdType = "spel_number";
            return;
        case LONG:
            xsdType = "spel_number";
            return;
        case BOOLEAN:
            xsdType = "spel_boolean";
            return;
        case DECLARED:
            TypeElement e = (TypeElement) typeUtils.asElement(ve.asType());
            if (e.getQualifiedName().toString().equals("java.lang.String")) {
                xsdType = "xsd:string";
                return;
            }
            if (e.getSuperclass().getKind() == TypeKind.DECLARED) {
                TypeElement superClass = ((TypeElement) typeUtils.asElement(e.getSuperclass()));
                if (superClass.getQualifiedName().toString().equals("java.lang.Enum")) {
                    isEnum = true;
                    // TODO: restriction, but be carefull about Spring EL usage, for example "#{config.XXX}"
                    xsdType = "xsd:string";
                    /*
					 *	<xsd:attribute name=\"target\" use=\"optional\" default=\"body\">\r\n" +
					 *		<xsd:simpleType>\r\n" +
					 *			<xsd:restriction base=\"xsd:string\">\r\n" +
					 *				<xsd:enumeration value=\"body\" />\r\n" +
					 *				<xsd:enumeration value=\"header\" />\r\n" +
					 *			</xsd:restriction>\r\n" +
					 *		</xsd:simpleType>\r\n" +
					 *	</xsd:attribute>\r\n"
					 */
                    return;
                }
            }
            isBeanReference = true;
            xsdType = "xsd:string";
            return;
        default:
            throw new ProcessingException("Not implemented: XSD type for " + ve.asType().getKind().toString(), this.getE());
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) ProcessingException(com.predic8.membrane.annot.ProcessingException)

Example 35 with Body

use of com.predic8.membrane.core.http.Body 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

Exchange (com.predic8.membrane.core.exchange.Exchange)30 IOException (java.io.IOException)17 Response (com.predic8.membrane.core.http.Response)15 Request (com.predic8.membrane.core.http.Request)12 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)10 Test (org.junit.Test)10 CacheBuilder (com.google.common.cache.CacheBuilder)8 MCElement (com.predic8.membrane.annot.MCElement)6 Body (com.predic8.membrane.core.http.Body)6 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)6 UnknownHostException (java.net.UnknownHostException)6 Message (com.predic8.membrane.core.http.Message)5 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)5 InputStream (java.io.InputStream)5 JsonFactory (com.fasterxml.jackson.core.JsonFactory)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)4 HttpRouter (com.predic8.membrane.core.HttpRouter)4 AbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot)4 DynamicAbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot)4 Header (com.predic8.membrane.core.http.Header)4