Search in sources :

Example 31 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project camel by apache.

the class DefaultAhcBinding method onComplete.

@Override
public void onComplete(AhcEndpoint endpoint, Exchange exchange, String url, ByteArrayOutputStream os, int contentLength, int statusCode, String statusText) throws Exception {
    // copy from output stream to input stream
    os.flush();
    os.close();
    InputStream is = new ByteArrayInputStream(os.toByteArray());
    String contentEncoding = exchange.getOut().getHeader(Exchange.CONTENT_ENCODING, String.class);
    if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
        is = GZIPHelper.uncompressGzip(contentEncoding, is);
    }
    // Honor the character encoding
    String contentType = exchange.getOut().getHeader(Exchange.CONTENT_TYPE, String.class);
    if (contentType != null) {
        // find the charset and set it to the Exchange
        AhcHelper.setCharsetFromContentType(contentType, exchange);
    }
    Object body = is;
    // an exception can also be transffered as java object
    if (contentType != null && contentType.equals(AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT)) {
        if (endpoint.getComponent().isAllowJavaSerializedObject() || endpoint.isTransferException()) {
            body = AhcHelper.deserializeJavaObjectFromStream(is);
        }
    }
    if (!endpoint.isThrowExceptionOnFailure()) {
        // if we do not use failed exception then populate response for all response codes
        populateResponse(exchange, body, contentLength, statusCode);
    } else {
        if (statusCode >= 100 && statusCode < 300) {
            // only populate response for OK response
            populateResponse(exchange, body, contentLength, statusCode);
        } else {
            // operation failed so populate exception to throw
            throw populateHttpOperationFailedException(endpoint, exchange, url, body, contentLength, statusCode, statusText);
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 32 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project camel by apache.

the class AhcBridgeEndpointTest method testBridgeEndpoint.

@Test
public void testBridgeEndpoint() throws Exception {
    String response = template.requestBodyAndHeader("http://localhost:" + port1 + "/test/hello", new ByteArrayInputStream("This is a test".getBytes()), "Content-Type", "application/xml", String.class);
    assertEquals("Get a wrong response", "/", response);
    response = template.requestBody("http://localhost:" + port2 + "/hello/world", "hello", String.class);
    assertEquals("Get a wrong response", "/hello/world", response);
    try {
        template.requestBody("http://localhost:" + port1 + "/hello/world", "hello", String.class);
        fail("Expect exception here!");
    } catch (Exception ex) {
        assertTrue("We should get a RuntimeCamelException", ex instanceof RuntimeCamelException);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) RuntimeCamelException(org.apache.camel.RuntimeCamelException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Test(org.junit.Test)

Example 33 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project camel by apache.

the class ManagedCamelContext method dumpRoutesAsXml.

@Override
public String dumpRoutesAsXml(boolean resolvePlaceholders) throws Exception {
    List<RouteDefinition> routes = context.getRouteDefinitions();
    if (routes.isEmpty()) {
        return null;
    }
    // use a routes definition to dump the routes
    RoutesDefinition def = new RoutesDefinition();
    def.setRoutes(routes);
    String xml = ModelHelper.dumpModelAsXml(context, def);
    // if resolving placeholders we parse the xml, and resolve the property placeholders during parsing
    if (resolvePlaceholders) {
        final AtomicBoolean changed = new AtomicBoolean();
        InputStream is = new ByteArrayInputStream(xml.getBytes());
        Document dom = XmlLineNumberParser.parseXml(is, new XmlLineNumberParser.XmlTextTransformer() {

            @Override
            public String transform(String text) {
                try {
                    String after = getContext().resolvePropertyPlaceholders(text);
                    if (!changed.get()) {
                        changed.set(!text.equals(after));
                    }
                    return after;
                } catch (Exception e) {
                    // ignore
                    return text;
                }
            }
        });
        // okay there were some property placeholder replaced so re-create the model
        if (changed.get()) {
            xml = context.getTypeConverter().mandatoryConvertTo(String.class, dom);
            RoutesDefinition copy = ModelHelper.createModelFromXml(context, xml, RoutesDefinition.class);
            xml = ModelHelper.dumpModelAsXml(context, copy);
        }
    }
    return xml;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RouteDefinition(org.apache.camel.model.RouteDefinition) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RoutesDefinition(org.apache.camel.model.RoutesDefinition) Document(org.w3c.dom.Document) XmlLineNumberParser(org.apache.camel.util.XmlLineNumberParser) IOException(java.io.IOException)

Example 34 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project camel by apache.

the class ManagedCamelContext method dumpRestsAsXml.

@Override
public String dumpRestsAsXml(boolean resolvePlaceholders) throws Exception {
    List<RestDefinition> rests = context.getRestDefinitions();
    if (rests.isEmpty()) {
        return null;
    }
    // use a routes definition to dump the rests
    RestsDefinition def = new RestsDefinition();
    def.setRests(rests);
    String xml = ModelHelper.dumpModelAsXml(context, def);
    // if resolving placeholders we parse the xml, and resolve the property placeholders during parsing
    if (resolvePlaceholders) {
        final AtomicBoolean changed = new AtomicBoolean();
        InputStream is = new ByteArrayInputStream(xml.getBytes());
        Document dom = XmlLineNumberParser.parseXml(is, new XmlLineNumberParser.XmlTextTransformer() {

            @Override
            public String transform(String text) {
                try {
                    String after = getContext().resolvePropertyPlaceholders(text);
                    if (!changed.get()) {
                        changed.set(!text.equals(after));
                    }
                    return after;
                } catch (Exception e) {
                    // ignore
                    return text;
                }
            }
        });
        // okay there were some property placeholder replaced so re-create the model
        if (changed.get()) {
            xml = context.getTypeConverter().mandatoryConvertTo(String.class, dom);
            RestsDefinition copy = ModelHelper.createModelFromXml(context, xml, RestsDefinition.class);
            xml = ModelHelper.dumpModelAsXml(context, copy);
        }
    }
    return xml;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RestDefinition(org.apache.camel.model.rest.RestDefinition) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RestsDefinition(org.apache.camel.model.rest.RestsDefinition) Document(org.w3c.dom.Document) XmlLineNumberParser(org.apache.camel.util.XmlLineNumberParser) IOException(java.io.IOException)

Example 35 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project camel by apache.

the class ResourceHelper method resolveMandatoryResourceAsInputStream.

/**
     * Resolves the mandatory resource.
     * <p/>
     * The resource uri can refer to the following systems to be loaded from
     * <ul>
     *     <il>file:nameOfFile - to refer to the file system</il>
     *     <il>classpath:nameOfFile - to refer to the classpath (default)</il>
     *     <il>http:uri - to load the resource using HTTP</il>
     *     <il>ref:nameOfBean - to lookup the resource in the {@link org.apache.camel.spi.Registry}</il>
     *     <il>bean:nameOfBean.methodName - to lookup a bean in the {@link org.apache.camel.spi.Registry} and call the method</il>
     * </ul>
     * If no prefix has been given, then the resource is loaded from the classpath
     * <p/>
     * If possible recommended to use {@link #resolveMandatoryResourceAsUrl(org.apache.camel.spi.ClassResolver, String)}
     *
     * @param camelContext the Camel Context
     * @param uri URI of the resource
     * @return the resource as an {@link InputStream}.  Remember to close this stream after usage.
     * @throws java.io.IOException is thrown if the resource file could not be found or loaded as {@link InputStream}
     */
public static InputStream resolveMandatoryResourceAsInputStream(CamelContext camelContext, String uri) throws IOException {
    if (uri.startsWith("ref:")) {
        String ref = uri.substring(4);
        String value = CamelContextHelper.mandatoryLookup(camelContext, ref, String.class);
        return new ByteArrayInputStream(value.getBytes());
    } else if (uri.startsWith("bean:")) {
        String bean = uri.substring(5);
        if (bean.contains(".")) {
            String method = StringHelper.after(bean, ".");
            bean = StringHelper.before(bean, ".") + "?method=" + method;
        }
        Exchange dummy = new DefaultExchange(camelContext);
        Object out = camelContext.resolveLanguage("bean").createExpression(bean).evaluate(dummy, Object.class);
        if (dummy.getException() != null) {
            IOException io = new IOException("Cannot find resource: " + uri + " from calling the bean");
            io.initCause(dummy.getException());
            throw io;
        }
        if (out != null) {
            InputStream is = camelContext.getTypeConverter().tryConvertTo(InputStream.class, dummy, out);
            if (is == null) {
                String text = camelContext.getTypeConverter().tryConvertTo(String.class, dummy, out);
                if (text != null) {
                    return new ByteArrayInputStream(text.getBytes());
                }
            } else {
                return is;
            }
        } else {
            throw new IOException("Cannot find resource: " + uri + " from calling the bean");
        }
    }
    InputStream is = resolveResourceAsInputStream(camelContext.getClassResolver(), uri);
    if (is == null) {
        String resolvedName = resolveUriPath(uri);
        throw new FileNotFoundException("Cannot find resource: " + resolvedName + " in classpath for URI: " + uri);
    } else {
        return is;
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)6879 Test (org.junit.Test)2274 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1791 InputStream (java.io.InputStream)1531 IOException (java.io.IOException)1400 DataInputStream (java.io.DataInputStream)600 ObjectInputStream (java.io.ObjectInputStream)597 X509Certificate (java.security.cert.X509Certificate)397 CertificateFactory (java.security.cert.CertificateFactory)355 ObjectOutputStream (java.io.ObjectOutputStream)333 File (java.io.File)279 ArrayList (java.util.ArrayList)270 Certificate (java.security.cert.Certificate)234 HashMap (java.util.HashMap)212 DataOutputStream (java.io.DataOutputStream)200 FileInputStream (java.io.FileInputStream)182 InputStreamReader (java.io.InputStreamReader)180 Test (org.testng.annotations.Test)171 Document (org.w3c.dom.Document)143 Map (java.util.Map)138