public CorePackageScanClassResolver() { converters.add(ObjectConverter.class); converters.add(CollectionConverter.class); converters.add(DateTimeConverter.class); converters.add(SQLConverter.class); converters.add(IOConverter.class); converters.add(NIOConverter.class); converters.add(StaxConverter.class); converters.add(DomConverter.class); converters.add(StreamSourceConverter.class); converters.add(XmlConverter.class); converters.add(CamelConverter.class); converters.add(StreamCacheConverter.class); converters.add(TimePatternConverter.class); converters.add(FutureTypeConverter.class); converters.add(BeanConverter.class); converters.add(GenericFileConverter.class); converters.add(DurationConverter.class); }
/** * Gets the resource as an input stream considering the cache flag as well. * <p/> * If cache is enabled then the resource content is cached in an internal buffer and this content is * returned to avoid loading the resource over and over again. * * @return the input stream * @throws IOException is thrown if error loading the content of the resource to the local cache buffer */ public InputStream getResourceAsInputStream() throws IOException { // try to get the resource input stream InputStream is; if (isContentCache()) { synchronized (this) { if (buffer == null) { log.debug("Reading resource: {} into the content cache", resourceUri); is = getResourceAsInputStreamWithoutCache(); buffer = IOConverter.toBytes(is); IOHelper.close(is, resourceUri, log); } } log.debug("Using resource: {} from the content cache", resourceUri); return new ByteArrayInputStream(buffer); } return getResourceAsInputStreamWithoutCache(); }
@Converter public static InputStream genericFileToInputStream(GenericFile<?> file, Exchange exchange) throws IOException, NoTypeConversionAvailableException { if (file.getFile() instanceof File) { // prefer to use a file input stream if its a java.io.File File f = (File) file.getFile(); // the file must exists if (f.exists()) { // read the file using the specified charset String charset = file.getCharset(); if (charset != null) { LOG.debug("Read file {} with charset {}", f, file.getCharset()); } else { LOG.debug("Read file {} (no charset)", f); } return IOConverter.toInputStream(f, charset); } } if (exchange != null) { // otherwise ensure the body is loaded as we want the input stream of the body file.getBinding().loadContent(exchange, file); return exchange.getContext().getTypeConverter().convertTo(InputStream.class, exchange, file.getBody()); } else { // should revert to fallback converter if we don't have an exchange return null; } }
@Converter public static String genericFileToString(GenericFile<?> file, Exchange exchange) throws IOException, NoTypeConversionAvailableException { // use reader first as it supports the file charset BufferedReader reader = genericFileToReader(file, exchange); if (reader != null) { return IOConverter.toString(reader); } if (exchange != null) { // otherwise ensure the body is loaded as we want the content of the body file.getBinding().loadContent(exchange, file); return exchange.getContext().getTypeConverter().convertTo(String.class, exchange, file.getBody()); } else { // should revert to fallback converter if we don't have an exchange return null; } }
private static BufferedReader genericFileToReader(GenericFile<?> file, Exchange exchange) throws IOException, NoTypeConversionAvailableException { if (file.getFile() instanceof File) { // prefer to use a file input stream if its a java.io.File File f = (File) file.getFile(); // the file must exists if (!f.exists()) { return null; } // and use the charset if the file was explicit configured with a charset String charset = file.getCharset(); if (charset != null) { LOG.debug("Read file {} with charset {}", f, file.getCharset()); return IOConverter.toReader(f, charset); } else { LOG.debug("Read file {} (no charset)", f); return IOConverter.toReader(f, exchange); } } return null; }
public void testConvertToStreamCacheInputStreamWithFileCache() throws Exception { exchange.getContext().getStreamCachingStrategy().setSpoolThreshold(1); context.start(); InputStream is = getTestFileStream(); InputStream cache = (InputStream)StreamCacheConverter.convertToStreamCache(is, exchange); assertNotNull(IOConverter.toString(cache, null)); try { // since the stream is closed you delete the temp file // reset will not work any more cache.reset(); exchange.getUnitOfWork().done(exchange); fail("except the exception here"); } catch (Exception exception) { // do nothing } }
public void testCacheStreamToMemory() throws Exception { context.getStreamCachingStrategy().setSpoolThreshold(1024); context.start(); CachedOutputStream cos = new CachedOutputStream(exchange); cos.write(TEST_STRING.getBytes("UTF-8")); File file = new File("target/cachedir"); String[] files = file.list(); assertEquals("we should have no temp file", 0, files.length); StreamCache cache = cos.newStreamCache(); assertTrue("Should get the InputStreamCache", cache instanceof InputStreamCache); String temp = IOConverter.toString((InputStream)cache, null); assertEquals("Cached a wrong file", temp, TEST_STRING); IOHelper.close(cos); }
public void testCacheStreamToMemoryAsDiskIsDisabled() throws Exception { // -1 disables disk based cache context.getStreamCachingStrategy().setSpoolThreshold(-1); context.start(); CachedOutputStream cos = new CachedOutputStream(exchange); cos.write(TEST_STRING.getBytes("UTF-8")); File file = new File("target/cachedir"); String[] files = file.list(); assertEquals("we should have no temp file", 0, files.length); StreamCache cache = cos.newStreamCache(); assertTrue("Should get the InputStreamCache", cache instanceof InputStreamCache); String temp = IOConverter.toString((InputStream)cache, null); assertEquals("Cached a wrong file", temp, TEST_STRING); exchange.getUnitOfWork().done(exchange); IOHelper.close(cos); }
public void testRenameFileExists() throws Exception { // create a file in done to let there be a duplicate file File file = new File("target/done"); file.mkdirs(); FileWriter fw = new FileWriter("target/done/london.txt"); try { fw.write("I was there once in London"); fw.flush(); } finally { fw.close(); } MockEndpoint mock = getMockEndpoint("mock:report"); mock.expectedBodiesReceived("Hello London"); template.sendBodyAndHeader("file:target/reports", "Hello London", Exchange.FILE_NAME, "london.txt"); mock.assertIsSatisfied(); oneExchangeDone.matchesMockWaitTime(); // content of file should be Hello London String content = IOConverter.toString(new File("target/done/london.txt"), null); assertEquals("The file should have been renamed replacing any existing files", "Hello London", content); }
@Test public void testProducerFileWithPathNoStepwise() throws Exception { Exchange out = template.send(getFtpUrl(), new Processor() { public void process(Exchange exchange) throws Exception { exchange.getIn().setBody("Hello World"); exchange.getIn().setHeader(Exchange.FILE_NAME, "hello\\claus.txt"); } }); assertNotNull(out); File file = new File(FTP_ROOT_DIR + "/upload/hello/claus.txt"); assertTrue("The uploaded file should exists", file.exists()); assertEquals("Hello World", IOConverter.toString(file, null)); assertEquals("upload/hello\\claus.txt", out.getIn().getHeader(Exchange.FILE_NAME_PRODUCED)); }
@Test public void testProducerFileWithPathNoStepwise() throws Exception { Exchange out = template.send(getFtpUrl(), new Processor() { public void process(Exchange exchange) throws Exception { exchange.getIn().setBody("Hello World"); exchange.getIn().setHeader(Exchange.FILE_NAME, "hello/claus.txt"); } }); assertNotNull(out); File file = new File(FTP_ROOT_DIR + "/upload/hello/claus.txt"); assertTrue("The uploaded file should exists", file.exists()); assertEquals("Hello World", IOConverter.toString(file, null)); assertEquals("upload/hello/claus.txt", out.getIn().getHeader(Exchange.FILE_NAME_PRODUCED)); }
@Test public void testLocalWorkDirectory() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedBodiesReceived("Hello World"); mock.expectedMessageCount(1); assertMockEndpointsSatisfied(); // give test some time to close file resources Thread.sleep(6000); // and the out file should exists File out = new File("target/out/hello.txt"); assertTrue("file should exists", out.exists()); assertEquals("Hello World", IOConverter.toString(out, null)); // now the lwd file should be deleted File local = new File("target/lwd/hello.txt"); assertFalse("Local work file should have been deleted", local.exists()); }
@Test public void testLocalWorkDirectory() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedBodiesReceived("Hello World"); mock.expectedMessageCount(1); assertMockEndpointsSatisfied(); // give test some time to close file resources Thread.sleep(6000); // now the lwd file should be deleted File local = new File("target/lwd/hello.txt"); assertFalse("Local work file should have been deleted", local.exists()); // and the out file should exists File out = new File("target/out/hello.txt"); assertTrue("file should exists", out.exists()); assertEquals("Hello World", IOConverter.toString(out, null)); }
@Test public void testRouteToFile() throws Exception { MockEndpoint result = getMockEndpoint("mock:result"); result.expectedMessageCount(1); deleteDirectory("target/routetofile"); template.sendBody("activemq:queue:hello", "Hello World"); // pause to let file producer save the file result.assertIsSatisfied(); // do file assertions File dir = new File("target/routetofile"); assertTrue("Should be directory", dir.isDirectory()); File file = dir.listFiles()[0]; assertTrue("File should exists", file.exists()); String body = IOConverter.toString(file, null); assertEquals("Hello World", body); }
private void sendFile(String uri) throws Exception { template.send(uri, new Processor() { public void process(Exchange exchange) throws Exception { // Read from an input stream InputStream is = IOHelper.buffered(new FileInputStream("src/test/resources/test.txt")); byte buffer[] = IOConverter.toBytes(is); is.close(); // Set the property of the charset encoding exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8"); Message in = exchange.getIn(); in.setBody(buffer); } }); }
@BeforeClass public static void initPort() throws Exception { File file = new File("target/nettyport.txt"); if (!file.exists()) { // start from somewhere in the 25xxx range port = AvailablePortFinder.getNextAvailable(25000); } else { // read port number from file String s = IOConverter.toString(file, null); port = Integer.parseInt(s); // use next free port port = AvailablePortFinder.getNextAvailable(port + 1); } }
@BeforeClass public static void initPort() throws Exception { File file = new File("target/nettyport.txt"); if (!file.exists()) { // start from somewhere in the 26xxx range port = AvailablePortFinder.getNextAvailable(26000); } else { // read port number from file String s = IOConverter.toString(file, null); port = Integer.parseInt(s); // use next free port port = AvailablePortFinder.getNextAvailable(port + 1); } }
@Converter public static BasicDBObject fromInputStreamToDBObject(InputStream is, Exchange exchange) { BasicDBObject answer = null; try { byte[] input = IOConverter.toBytes(is); if (isBson(input)) { BSONCallback callback = new JSONCallback(); new BasicBSONDecoder().decode(input, callback); answer = (BasicDBObject) callback.get(); } else { answer = (BasicDBObject) JSON.parse(IOConverter.toString(input, exchange)); } } catch (Exception e) { LOG.warn("String -> DBObject conversion selected, but the following exception occurred. Returning null.", e); } finally { // we need to make sure to close the input stream IOHelper.close(is, "InputStream", LOG); } return answer; }
private void add(File file) throws IOException { if (file.canRead()) { if (file.isDirectory()) { String[] files = file.list(); if (files != null) { for (String child : files) { add(new File(file.getAbsolutePath() + "/" + child)); } } } else { LOG.trace("Adding {}", file); openIndexWriter(); add("path", file.getPath(), false); add("contents", new String(IOConverter.toByteArray(file)), true); closeIndexWriter(); LOG.trace("Added {} successfully", file); } } else { LOG.warn("Directory/File " + file.getAbsolutePath() + " could not be read." + " This directory will not be indexed. Please check permissions and rebuild indexes."); } }
@Test public void testToJettyAndSaveToFile() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedBodiesReceived("Hello World"); Object out = template.requestBody("http://localhost:{{port}}/myworld", "Hello World"); String response = context.getTypeConverter().convertTo(String.class, out); assertEquals("Response from Jetty", "We got the file", response); assertMockEndpointsSatisfied(); // give file some time to save Thread.sleep(500); File file = new File("target/myworld/hello.txt"); assertTrue("File should exists", file.exists()); String content = IOConverter.toString(file, null); assertEquals("File content", "Hello World", content); }
@Test public void testUnmarshal() throws Exception { MockEndpoint mock = getMockEndpoint("mock:unmarshal"); // by default we get on big message mock.expectedMessageCount(1); mock.message(0).body().isInstanceOf(DataSetList.class); String data = IOConverter.toString(new File("src/test/data/fixed/PEOPLE-FixedLength.txt"), null); template.sendBody("direct:unmarshal", data); assertMockEndpointsSatisfied(); DataSetList list = mock.getExchanges().get(0).getIn().getBody(DataSetList.class); assertEquals(4, list.size()); Map<?, ?> row = list.get(0); assertEquals("JOHN", row.get("FIRSTNAME")); }
@Test public void testUnmarshal() throws Exception { MockEndpoint mock = getMockEndpoint("mock:unmarshal"); // by default we get on big message mock.expectedMessageCount(1); mock.message(0).body().isInstanceOf(DataSetList.class); String data = IOConverter.toString(new File("src/test/data/delim/INVENTORY-CommaDelimitedWithQualifier.txt"), null); template.sendBody("direct:unmarshal", data); assertMockEndpointsSatisfied(); DataSetList list = mock.getExchanges().get(0).getIn().getBody(DataSetList.class); assertEquals(4, list.size()); Map<?, ?> row = list.get(0); assertEquals("SOME VALVE", row.get("ITEM_DESC")); }
private static InputStream getBodyAsInputStream(Exchange exchange) throws IOException, InvalidPayloadException { Object rdf = exchange.getIn().getBody(); if(rdf instanceof WrappedFile) { rdf = ((WrappedFile<?>) rdf).getFile(); } if (rdf instanceof String) { return IOConverter.toInputStream((String) rdf, exchange); } else if (rdf instanceof InputStream) { return (InputStream) rdf; } else if (rdf instanceof File) { return IOConverter.toInputStream((File) rdf); } else if (rdf instanceof URL) { return IOConverter.toInputStream((URL) rdf); } // default to an InputStream return exchange.getIn().getMandatoryBody(InputStream.class); }
@Test public void testMarshal() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:textToLzf") .marshal().lzf(); } }); camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); byte[] output = (byte[]) producer.requestBody("direct:textToLzf", TEXT.getBytes("UTF-8")); InputStream stream = new LZFInputStream(new ByteArrayInputStream(output)); Assert.assertEquals(TEXT, IOConverter.toString(stream, null)); } finally { camelctx.stop(); } }
protected byte[] readSchemaResource() throws IOException { LOG.debug("reading schema resource: {}", schemaResourceUri); InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, schemaResourceUri); byte[] bytes = null; try { bytes = IOConverter.toBytes(is); } finally { // and make sure to close the input stream after the schema has been // loaded IOHelper.close(is); } return bytes; }
private void writeFileByReaderWithCharset(Reader in, File target, String charset) throws IOException { boolean append = endpoint.getFileExist() == GenericFileExist.Append; FileOutputStream os = new FileOutputStream(target, append); Writer out = IOConverter.toWriter(os, charset); try { LOG.debug("Using Reader to write file: {} with charset: {}", target, charset); int size = endpoint.getBufferSize(); IOHelper.copy(in, out, size); } finally { IOHelper.close(in, target.getName(), LOG); IOHelper.close(out, os, target.getName(), LOG, endpoint.isForceWrites()); } }
public void testConvertToStreamCacheInputStream() throws Exception { context.start(); InputStream is = getTestFileStream(); InputStream cache = (InputStream)StreamCacheConverter.convertToStreamCache(is, exchange); //assert re-readability of the cached InputStream String data = IOConverter.toString(cache, null); cache.reset(); String data2 = IOConverter.toString(cache, null); assertEquals(data, data2); }
private void doTestInputStreamPayload(String message) throws InterruptedException, IOException { successEndpoint.expectedMessageCount(0); exceptionEndpoint.expectedMessageCount(1); template.sendBody("direct:start", new ByteArrayInputStream(message.getBytes())); successEndpoint.assertIsSatisfied(); exceptionEndpoint.assertIsSatisfied(); InputStream body = (InputStream) exceptionEndpoint.getExchanges().get(0).getIn().getBody(); assertEquals("Ensure message re-readability in the exception handler", message, new String(IOConverter.toBytes(body))); }
@Test public void toGZIPInputStreamShouldReturnAByteArrayInputStream() throws IOException { InputStream inputStream = GZIPHelper.compressGzip("text", sampleBytes); byte[] bytes = IOConverter.toBytes(inputStream); assertArrayEquals(sampleBytes, bytes); }
@Test public void testCompressAndUnCompressData() throws IOException { InputStream inputStream = GZIPHelper.compressGzip("gzip", new ByteArrayInputStream(sampleString.getBytes())); assertNotNull("The inputStream should not be null.", inputStream); inputStream = GZIPHelper.uncompressGzip("gzip", inputStream); String result = IOConverter.toString(inputStream, null); assertEquals("The result is wrong.", sampleString, result); }
public void testMarshalTextToGZip() throws Exception { context.addRoutes(new RouteBuilder() { public void configure() { from("direct:start").marshal().gzip(); } }); context.start(); byte[] output = sendText(); GZIPInputStream stream = new GZIPInputStream(new ByteArrayInputStream(output)); String result = IOConverter.toString(stream, null); assertEquals("Uncompressed something different than compressed", TEXT, result); }
protected Schema getSchema(Message message) throws SAXException, XmlSignatureException, IOException { String schemaResourceUri = getSchemaResourceUri(message); if (schemaResourceUri == null || schemaResourceUri.isEmpty()) { return null; } InputStream is = ResourceHelper.resolveResourceAsInputStream(getConfiguration().getCamelContext().getClassResolver(), schemaResourceUri); if (is == null) { throw new XmlSignatureException( "XML Signature component is wrongly configured: No XML schema found for specified schema resource URI " + schemaResourceUri); } byte[] bytes = null; try { bytes = IOConverter.toBytes(is); } finally { // and make sure to close the input stream after the schema has been loaded IOHelper.close(is); } SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schemaFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); schemaFactory.setResourceResolver(new DefaultLSResourceResolver(getConfiguration().getCamelContext(), getConfiguration() .getSchemaResourceUri())); LOG.debug("Instantiating schema for validation"); return schemaFactory.newSchema(new BytesSource(bytes)); }
@SuppressWarnings("deprecation") public static void setCharsetFromContentType(String contentType, Exchange exchange) { String charset = getCharsetFromContentType(contentType); if (charset != null) { exchange.setProperty(Exchange.CHARSET_NAME, IOConverter.normalizeCharset(charset)); } }
private Document getDocument(String soapMessage) throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(IOConverter.toInputStream(soapMessage, null)); document.getDocumentElement().normalize(); return document; }