Search in sources :

Example 1 with Reader

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

the class CxfPayloadConverter method convertTo.

@SuppressWarnings("unchecked")
@FallbackConverter
public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
    // CxfPayloads from other types
    if (type.isAssignableFrom(CxfPayload.class)) {
        try {
            if (!value.getClass().isArray()) {
                Source src = null;
                // directly
                if (value instanceof InputStream) {
                    src = new StreamSource((InputStream) value);
                } else if (value instanceof Reader) {
                    src = new StreamSource((Reader) value);
                } else if (value instanceof String) {
                    src = new StreamSource(new StringReader((String) value));
                } else if (value instanceof Node) {
                    src = new DOMSource((Node) value);
                } else if (value instanceof Source) {
                    src = (Source) value;
                }
                if (src == null) {
                    // assuming staxsource is preferred, otherwise use the
                    // one preferred
                    TypeConverter tc = registry.lookup(javax.xml.transform.stax.StAXSource.class, value.getClass());
                    if (tc == null) {
                        tc = registry.lookup(Source.class, value.getClass());
                    }
                    if (tc != null) {
                        src = tc.convertTo(Source.class, exchange, value);
                    }
                }
                if (src != null) {
                    return (T) sourceToCxfPayload(src, exchange);
                }
            }
            TypeConverter tc = registry.lookup(NodeList.class, value.getClass());
            if (tc != null) {
                NodeList nodeList = tc.convertTo(NodeList.class, exchange, value);
                return (T) nodeListToCxfPayload(nodeList, exchange);
            }
            tc = registry.lookup(Document.class, value.getClass());
            if (tc != null) {
                Document document = tc.convertTo(Document.class, exchange, value);
                return (T) documentToCxfPayload(document, exchange);
            }
            // maybe we can convert via an InputStream
            CxfPayload<?> p;
            p = convertVia(InputStream.class, exchange, value, registry);
            if (p != null) {
                return (T) p;
            }
            // String is the converter of last resort
            p = convertVia(String.class, exchange, value, registry);
            if (p != null) {
                return (T) p;
            }
        } catch (RuntimeCamelException e) {
        // the internal conversion to XML can throw an exception if the content is not XML
        // ignore this and return Void.TYPE to indicate that we cannot convert this
        }
        // no we could not do it currently
        return (T) Void.TYPE;
    }
    // Convert a CxfPayload into something else
    if (CxfPayload.class.isAssignableFrom(value.getClass())) {
        CxfPayload<?> payload = (CxfPayload<?>) value;
        int size = payload.getBodySources().size();
        if (size == 1) {
            if (type.isAssignableFrom(Document.class)) {
                Source s = payload.getBodySources().get(0);
                Document d;
                try {
                    d = StaxUtils.read(s);
                } catch (XMLStreamException e) {
                    throw new RuntimeException(e);
                }
                return type.cast(d);
            }
            // CAMEL-8410 Just make sure we get the Source object directly from the payload body source
            Source s = payload.getBodySources().get(0);
            if (type.isInstance(s)) {
                return type.cast(s);
            }
            TypeConverter tc = registry.lookup(type, XMLStreamReader.class);
            if (tc != null && (s instanceof StaxSource || s instanceof StAXSource)) {
                XMLStreamReader r = (s instanceof StAXSource) ? ((StAXSource) s).getXMLStreamReader() : ((StaxSource) s).getXMLStreamReader();
                if (payload.getNsMap() != null) {
                    r = new DelegatingXMLStreamReader(r, payload.getNsMap());
                }
                return tc.convertTo(type, exchange, r);
            }
            tc = registry.lookup(type, Source.class);
            if (tc != null) {
                XMLStreamReader r = null;
                if (payload.getNsMap() != null) {
                    if (s instanceof StaxSource) {
                        r = ((StaxSource) s).getXMLStreamReader();
                    } else if (s instanceof StAXSource) {
                        r = ((StAXSource) s).getXMLStreamReader();
                    }
                    if (r != null) {
                        s = new StAXSource(new DelegatingXMLStreamReader(r, payload.getNsMap()));
                    }
                }
                return tc.convertTo(type, exchange, s);
            }
        }
        TypeConverter tc = registry.lookup(type, NodeList.class);
        if (tc != null) {
            Object result = tc.convertTo(type, exchange, cxfPayloadToNodeList((CxfPayload<?>) value, exchange));
            if (result == null) {
                // no we could not do it currently, and we just abort the convert here
                return (T) Void.TYPE;
            } else {
                return (T) result;
            }
        }
        // we cannot convert a node list, so we try the first item from the
        // node list
        tc = registry.lookup(type, Node.class);
        if (tc != null) {
            NodeList nodeList = cxfPayloadToNodeList((CxfPayload<?>) value, exchange);
            if (nodeList.getLength() > 0) {
                return tc.convertTo(type, exchange, nodeList.item(0));
            } else {
                // no we could not do it currently
                return (T) Void.TYPE;
            }
        } else {
            if (size == 0) {
                // empty size so we cannot convert
                return (T) Void.TYPE;
            }
        }
    }
    return null;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) CxfPayload(org.apache.camel.component.cxf.CxfPayload) Node(org.w3c.dom.Node) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) StaxSource(org.apache.cxf.staxutils.StaxSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) StringReader(java.io.StringReader) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) StAXSource(javax.xml.transform.stax.StAXSource) TypeConverter(org.apache.camel.TypeConverter) XMLStreamException(javax.xml.stream.XMLStreamException) StaxSource(org.apache.cxf.staxutils.StaxSource) RuntimeCamelException(org.apache.camel.RuntimeCamelException) FallbackConverter(org.apache.camel.FallbackConverter)

Example 2 with Reader

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

the class DefaultCxfBinding method getContentFromCxf.

protected static Object getContentFromCxf(Message message, DataFormat dataFormat, String encoding) {
    Set<Class<?>> contentFormats = message.getContentFormats();
    Object answer = null;
    if (contentFormats != null) {
        if (LOG.isTraceEnabled()) {
            for (Class<?> contentFormat : contentFormats) {
                LOG.trace("Content format={} value={}", contentFormat, message.getContent(contentFormat));
            }
        }
        if (dataFormat == DataFormat.POJO) {
            answer = message.getContent(List.class);
            if (answer == null) {
                answer = message.getContent(Object.class);
                if (answer != null) {
                    answer = new MessageContentsList(answer);
                }
            }
        } else if (dataFormat == DataFormat.PAYLOAD) {
            List<SoapHeader> headers = CastUtils.cast((List<?>) message.get(Header.HEADER_LIST));
            Map<String, String> nsMap = new HashMap<String, String>();
            answer = new CxfPayload<SoapHeader>(headers, getPayloadBodyElements(message, nsMap), nsMap);
        } else if (dataFormat.dealias() == DataFormat.RAW) {
            answer = message.getContent(InputStream.class);
            if (answer == null) {
                answer = message.getContent(Reader.class);
                if (answer != null) {
                    if (encoding == null) {
                        encoding = "UTF-8";
                    }
                    LOG.trace("file encoding is = {}", encoding);
                    answer = new ReaderInputStream((Reader) answer, Charset.forName(encoding));
                }
            }
        } else if (dataFormat.dealias() == DataFormat.CXF_MESSAGE && message.getContent(List.class) != null) {
            // CAMEL-6404 added check point of message content
            // The message content of list could be null if there is a fault message is received
            answer = message.getContent(List.class).get(0);
        }
        LOG.trace("Extracted body from CXF message = {}", answer);
    }
    return answer;
}
Also used : MessageContentsList(org.apache.cxf.message.MessageContentsList) ReaderInputStream(org.apache.camel.component.cxf.util.ReaderInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) ReaderInputStream(org.apache.camel.component.cxf.util.ReaderInputStream) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) TreeMap(java.util.TreeMap)

Example 3 with Reader

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

the class FlatpackEndpoint method createDelimitedParser.

public Parser createDelimitedParser(Exchange exchange) throws InvalidPayloadException, IOException {
    Reader bodyReader = exchange.getIn().getMandatoryBody(Reader.class);
    Parser parser;
    if (ObjectHelper.isEmpty(getResourceUri())) {
        parser = getParserFactory().newDelimitedParser(bodyReader, delimiter, textQualifier);
    } else {
        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), resourceUri);
        InputStreamReader reader = new InputStreamReader(is, IOHelper.getCharsetName(exchange));
        parser = getParserFactory().newDelimitedParser(reader, bodyReader, delimiter, textQualifier, ignoreFirstRecord);
    }
    if (isAllowShortLines()) {
        parser.setHandlingShortLines(true);
        parser.setIgnoreParseWarnings(true);
    }
    if (isIgnoreExtraColumns()) {
        parser.setIgnoreExtraColumns(true);
        parser.setIgnoreParseWarnings(true);
    }
    return parser;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Parser(net.sf.flatpack.Parser)

Example 4 with Reader

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

the class DefaultPropertiesResolver method loadPropertiesFromClasspath.

protected Properties loadPropertiesFromClasspath(CamelContext context, boolean ignoreMissingLocation, PropertiesLocation location) throws IOException {
    Properties answer = new Properties();
    String path = location.getPath();
    InputStream is = context.getClassResolver().loadResourceAsStream(path);
    Reader reader = null;
    if (is == null) {
        if (!ignoreMissingLocation && !location.isOptional()) {
            throw new FileNotFoundException("Properties file " + path + " not found in classpath");
        }
    } else {
        try {
            if (propertiesComponent.getEncoding() != null) {
                reader = new BufferedReader(new InputStreamReader(is, propertiesComponent.getEncoding()));
                answer.load(reader);
            } else {
                answer.load(is);
            }
        } finally {
            IOHelper.close(reader, is);
        }
    }
    return answer;
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) BufferedReader(java.io.BufferedReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Properties(java.util.Properties)

Example 5 with Reader

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

the class BeanIOSplitter method evaluate.

public Object evaluate(Exchange exchange) throws Exception {
    Message msg = exchange.getIn();
    Object body = msg.getBody();
    if (factory == null) {
        factory = createStreamFactory(exchange.getContext());
    }
    BeanReader beanReader = null;
    if (body instanceof WrappedFile) {
        body = ((WrappedFile) body).getFile();
    }
    if (body instanceof File) {
        File file = (File) body;
        beanReader = factory.createReader(getStreamName(), file);
    }
    if (beanReader == null) {
        Reader reader = msg.getMandatoryBody(Reader.class);
        beanReader = factory.createReader(getStreamName(), reader);
    }
    BeanIOIterator iterator = new BeanIOIterator(beanReader);
    BeanReaderErrorHandler errorHandler = getOrCreateBeanReaderErrorHandler(configuration, exchange, null, iterator);
    beanReader.setErrorHandler(errorHandler);
    return iterator;
}
Also used : Message(org.apache.camel.Message) BeanIOHelper.getOrCreateBeanReaderErrorHandler(org.apache.camel.dataformat.beanio.BeanIOHelper.getOrCreateBeanReaderErrorHandler) BeanReaderErrorHandler(org.beanio.BeanReaderErrorHandler) WrappedFile(org.apache.camel.WrappedFile) BeanReader(org.beanio.BeanReader) Reader(java.io.Reader) BeanReader(org.beanio.BeanReader) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile)

Aggregations

Reader (java.io.Reader)1498 InputStreamReader (java.io.InputStreamReader)526 StringReader (java.io.StringReader)498 IOException (java.io.IOException)348 BufferedReader (java.io.BufferedReader)242 InputStream (java.io.InputStream)219 TokenStream (org.apache.lucene.analysis.TokenStream)171 Test (org.junit.Test)170 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)159 Connection (java.sql.Connection)137 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)126 FileReader (java.io.FileReader)108 FileInputStream (java.io.FileInputStream)107 File (java.io.File)105 BeforeClass (org.junit.BeforeClass)99 Tokenizer (org.apache.lucene.analysis.Tokenizer)91 SqlSession (org.apache.ibatis.session.SqlSession)83 StringWriter (java.io.StringWriter)81 ArrayList (java.util.ArrayList)77 Writer (java.io.Writer)63