Search in sources :

Example 81 with InputStream

use of java.io.InputStream 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 82 with InputStream

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

the class ObjectHelper method loadResourceAsStream.

/**
     * Attempts to load the given resource as a stream using the thread context
     * class loader or the class loader used to load this class
     *
     * @param name the name of the resource to load
     * @param loader optional classloader to attempt first
     * @return the stream or null if it could not be loaded
     */
public static InputStream loadResourceAsStream(String name, ClassLoader loader) {
    InputStream in = null;
    String resolvedName = resolveUriPath(name);
    if (loader != null) {
        in = loader.getResourceAsStream(resolvedName);
    }
    if (in == null) {
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        if (contextClassLoader != null) {
            in = contextClassLoader.getResourceAsStream(resolvedName);
        }
    }
    if (in == null) {
        in = ObjectHelper.class.getClassLoader().getResourceAsStream(resolvedName);
    }
    if (in == null) {
        in = ObjectHelper.class.getResourceAsStream(resolvedName);
    }
    return in;
}
Also used : InputStream(java.io.InputStream)

Example 83 with InputStream

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

the class ObjectHelper method getScanner.

/**
     * Creates a {@link Scanner} for scanning the given value.
     *
     * @param exchange  the current exchange
     * @param value     the value, typically the message IN body
     * @return the scanner, is newer <tt>null</tt>
     */
public static Scanner getScanner(Exchange exchange, Object value) {
    if (value instanceof WrappedFile) {
        WrappedFile<?> gf = (WrappedFile<?>) value;
        Object body = gf.getBody();
        if (body != null) {
            // we have loaded the file content into the body so use that
            value = body;
        } else {
            // generic file is just a wrapper for the real file so call again with the real file
            return getScanner(exchange, gf.getFile());
        }
    }
    String charset = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
    Scanner scanner = null;
    if (value instanceof Readable) {
        scanner = new Scanner((Readable) value);
    } else if (value instanceof InputStream) {
        scanner = charset == null ? new Scanner((InputStream) value) : new Scanner((InputStream) value, charset);
    } else if (value instanceof File) {
        try {
            scanner = charset == null ? new Scanner((File) value) : new Scanner((File) value, charset);
        } catch (FileNotFoundException e) {
            throw new RuntimeCamelException(e);
        }
    } else if (value instanceof String) {
        scanner = new Scanner((String) value);
    } else if (value instanceof ReadableByteChannel) {
        scanner = charset == null ? new Scanner((ReadableByteChannel) value) : new Scanner((ReadableByteChannel) value, charset);
    }
    if (scanner == null) {
        // value is not a suitable type, try to convert value to a string
        String text = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, value);
        if (text != null) {
            scanner = new Scanner(text);
        }
    }
    if (scanner == null) {
        scanner = new Scanner("");
    }
    return scanner;
}
Also used : Scanner(java.util.Scanner) ReadableByteChannel(java.nio.channels.ReadableByteChannel) WrappedFile(org.apache.camel.WrappedFile) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile)

Example 84 with InputStream

use of java.io.InputStream 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)

Example 85 with InputStream

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

the class BeanWithInputStreamBodyTest method testBeanWithInputStreamBodyMethod.

public void testBeanWithInputStreamBodyMethod() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").bean(MyCoolBean.class, "doSomething").to("mock:result");
        }
    });
    context.start();
    getMockEndpoint("mock:result").expectedBodiesReceived("There is 11 bytes");
    InputStream bais = new ByteArrayInputStream("Hello World".getBytes());
    template.sendBody("direct:start", bais);
    assertMockEndpointsSatisfied();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

InputStream (java.io.InputStream)12635 IOException (java.io.IOException)4626 ByteArrayInputStream (java.io.ByteArrayInputStream)2818 Test (org.junit.Test)2722 FileInputStream (java.io.FileInputStream)2443 File (java.io.File)1750 OutputStream (java.io.OutputStream)1173 InputStreamReader (java.io.InputStreamReader)1091 BufferedInputStream (java.io.BufferedInputStream)1075 URL (java.net.URL)1032 FileOutputStream (java.io.FileOutputStream)821 ByteArrayOutputStream (java.io.ByteArrayOutputStream)782 BufferedReader (java.io.BufferedReader)742 FileNotFoundException (java.io.FileNotFoundException)587 Properties (java.util.Properties)580 ArrayList (java.util.ArrayList)562 HttpURLConnection (java.net.HttpURLConnection)424 HashMap (java.util.HashMap)397 GZIPInputStream (java.util.zip.GZIPInputStream)342 Metadata (org.apache.tika.metadata.Metadata)319