Search in sources :

Example 71 with InputStream

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

the class AtmosPropertyManager method loadProperties.

private static Properties loadProperties() throws Exception {
    URL url = AtmosPropertyManager.class.getResource("/atmos.properties");
    InputStream inStream;
    try {
        inStream = url.openStream();
    } catch (IOException e) {
        throw new AtmosException("atmos.properties could not be found");
    }
    properties = new Properties();
    try {
        properties.load(inStream);
    } catch (IOException e) {
        throw new AtmosException("atmos.properties can't be read");
    }
    return properties;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL)

Example 72 with InputStream

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

the class BeanIOSplitter method createStreamFactory.

protected StreamFactory createStreamFactory(CamelContext camelContext) throws Exception {
    ObjectHelper.notNull(getStreamName(), "Stream name not configured.");
    // Create the stream factory that will be used to read/write objects.
    StreamFactory answer = StreamFactory.newInstance();
    // Load the mapping file using the resource helper to ensure it can be loaded in OSGi and other environments
    InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, getMapping());
    try {
        if (getProperties() != null) {
            answer.load(is, getProperties());
        } else {
            answer.load(is);
        }
    } finally {
        IOHelper.close(is);
    }
    return answer;
}
Also used : InputStream(java.io.InputStream) StreamFactory(org.beanio.StreamFactory)

Example 73 with InputStream

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

the class BeanIODataFormat method doStart.

@Override
protected void doStart() throws Exception {
    ObjectHelper.notNull(getStreamName(), "Stream name not configured.");
    if (factory == null) {
        // Create the stream factory that will be used to read/write objects.
        factory = StreamFactory.newInstance();
        // Load the mapping file using the resource helper to ensure it can be loaded in OSGi and other environments
        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), getMapping());
        try {
            if (getProperties() != null) {
                factory.load(is, getProperties());
            } else {
                factory.load(is);
            }
        } finally {
            IOHelper.close(is);
        }
    }
}
Also used : InputStream(java.io.InputStream)

Example 74 with InputStream

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

the class DefaultCamelContext method getLanguageParameterJsonSchema.

public String getLanguageParameterJsonSchema(String languageName) throws IOException {
    // use the language factory finder to find the package name of the language class, which is the location
    // where the documentation exists as well
    FactoryFinder finder = getFactoryFinder(DefaultLanguageResolver.LANGUAGE_RESOURCE_PATH);
    try {
        Class<?> clazz = null;
        try {
            clazz = finder.findClass(languageName);
        } catch (NoFactoryAvailableException e) {
        // ignore, i.e. if a component is an auto-configured spring-boot
        // languages
        }
        if (clazz == null) {
            return null;
        }
        String packageName = clazz.getPackage().getName();
        packageName = packageName.replace('.', '/');
        String path = packageName + "/" + languageName + ".json";
        ClassResolver resolver = getClassResolver();
        InputStream inputStream = resolver.loadResourceAsStream(path);
        log.debug("Loading language JSON Schema for: {} using class resolver: {} -> {}", new Object[] { languageName, resolver, inputStream });
        if (inputStream != null) {
            try {
                return IOHelper.loadText(inputStream);
            } finally {
                IOHelper.close(inputStream);
            }
        }
        return null;
    } catch (ClassNotFoundException e) {
        return null;
    }
}
Also used : InputStream(java.io.InputStream) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) PackageScanClassResolver(org.apache.camel.spi.PackageScanClassResolver) ClassResolver(org.apache.camel.spi.ClassResolver) FactoryFinder(org.apache.camel.spi.FactoryFinder)

Example 75 with InputStream

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

the class RestProducerBindingProcessor method process.

@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    boolean isXml = false;
    boolean isJson = false;
    // skip before binding for empty/null body
    Object body = exchange.getIn().getBody();
    if (ObjectHelper.isEmpty(body)) {
        if (outType != null) {
            // wrap callback to add reverse operation if we know the output type from the REST service
            callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, false);
        }
        // okay now we can continue routing to the producer
        return getProcessor().process(exchange, callback);
    }
    // we only need to perform before binding if the message body is POJO based
    if (body instanceof String || body instanceof byte[]) {
        // the body is text based and thus not POJO so no binding needed
        if (outType != null) {
            // wrap callback to add reverse operation if we know the output type from the REST service
            callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, false);
        }
        // okay now we can continue routing to the producer
        return getProcessor().process(exchange, callback);
    } else {
        // if its convertable to stream based then its not POJO based
        InputStream is = camelContext.getTypeConverter().tryConvertTo(InputStream.class, exchange, body);
        if (is != null) {
            exchange.getIn().setBody(is);
            if (outType != null) {
                // wrap callback to add reverse operation if we know the output type from the REST service
                callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, false);
            }
            // okay now we can continue routing to the producer
            return getProcessor().process(exchange, callback);
        }
    }
    // assume body is POJO based and binding needed
    String contentType = ExchangeHelper.getContentType(exchange);
    if (contentType != null) {
        isXml = contentType.toLowerCase(Locale.ENGLISH).contains("xml");
        isJson = contentType.toLowerCase(Locale.ENGLISH).contains("json");
    }
    // only allow xml/json if the binding mode allows that
    isXml &= bindingMode.equals("auto") || bindingMode.contains("xml");
    isJson &= bindingMode.equals("auto") || bindingMode.contains("json");
    // if we do not yet know if its xml or json, then use the binding mode to know the mode
    if (!isJson && !isXml) {
        isXml = bindingMode.equals("auto") || bindingMode.contains("xml");
        isJson = bindingMode.equals("auto") || bindingMode.contains("json");
    }
    // favor json over xml
    if (isJson && jsonMarshal != null) {
        try {
            jsonMarshal.process(exchange);
        } catch (Exception e) {
            // we failed so cannot call producer
            exchange.setException(e);
            callback.done(true);
            return true;
        }
        // need to prepare exchange first
        ExchangeHelper.prepareOutToIn(exchange);
        if (outType != null) {
            // wrap callback to add reverse operation if we know the output type from the REST service
            callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, false);
        }
        // okay now we can continue routing to the producer
        return getProcessor().process(exchange, callback);
    } else if (isXml && xmlMarshal != null) {
        try {
            xmlMarshal.process(exchange);
        } catch (Exception e) {
            // we failed so cannot call producer
            exchange.setException(e);
            callback.done(true);
            return true;
        }
        // need to prepare exchange first
        ExchangeHelper.prepareOutToIn(exchange);
        if (outType != null) {
            // wrap callback to add reverse operation if we know the output type from the REST service
            callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, true);
        }
        // okay now we can continue routing to the producer
        return getProcessor().process(exchange, callback);
    }
    // we could not bind
    if ("off".equals(bindingMode) || bindingMode.equals("auto")) {
        if (outType != null) {
            // wrap callback to add reverse operation if we know the output type from the REST service
            callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, false);
        }
        // okay now we can continue routing to the producer
        return getProcessor().process(exchange, callback);
    } else {
        if (bindingMode.contains("xml")) {
            exchange.setException(new BindingException("Cannot bind to xml as message body is not xml compatible", exchange));
        } else {
            exchange.setException(new BindingException("Cannot bind to json as message body is not json compatible", exchange));
        }
        // we failed so cannot call producer
        callback.done(true);
        return true;
    }
}
Also used : InputStream(java.io.InputStream) BindingException(org.apache.camel.processor.binding.BindingException) BindingException(org.apache.camel.processor.binding.BindingException)

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