use of org.apache.camel.component.file.GenericFile in project camel by apache.
the class SftpOperations method retrieveFileToStreamInBody.
@SuppressWarnings("unchecked")
private boolean retrieveFileToStreamInBody(String name, Exchange exchange) throws GenericFileOperationFailedException {
OutputStream os = null;
String currentDir = null;
try {
GenericFile<ChannelSftp.LsEntry> target = (GenericFile<ChannelSftp.LsEntry>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
ObjectHelper.notNull(target, "Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");
String remoteName = name;
if (endpoint.getConfiguration().isStepwise()) {
// remember current directory
currentDir = getCurrentDirectory();
// change directory to path where the file is to be retrieved
// (must do this as some FTP servers cannot retrieve using absolute path)
String path = FileUtil.onlyPath(name);
if (path != null) {
changeCurrentDirectory(path);
}
// remote name is now only the file name as we just changed directory
remoteName = FileUtil.stripPath(name);
}
// use input stream which works with Apache SSHD used for testing
InputStream is = channel.get(remoteName);
if (endpoint.getConfiguration().isStreamDownload()) {
target.setBody(is);
exchange.getIn().setHeader(RemoteFileComponent.REMOTE_FILE_INPUT_STREAM, is);
} else {
os = new ByteArrayOutputStream();
target.setBody(os);
IOHelper.copyAndCloseInput(is, os);
}
return true;
} catch (IOException e) {
throw new GenericFileOperationFailedException("Cannot retrieve file: " + name, e);
} catch (SftpException e) {
throw new GenericFileOperationFailedException("Cannot retrieve file: " + name, e);
} finally {
IOHelper.close(os, "retrieve: " + name, LOG);
// change back to current directory if we changed directory
if (currentDir != null) {
changeCurrentDirectory(currentDir);
}
}
}
use of org.apache.camel.component.file.GenericFile in project camel by apache.
the class SftpSimpleConsumeStreamingPartialReadTest method testSftpSimpleConsume.
@Test
public void testSftpSimpleConsume() throws Exception {
if (!canTest()) {
return;
}
String expected = "Hello World";
// create file using regular file
template.sendBodyAndHeader("file://" + FTP_ROOT_DIR, expected, Exchange.FILE_NAME, "hello.txt");
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.expectedHeaderReceived(Exchange.FILE_NAME, "hello.txt");
context.startRoute("foo");
assertMockEndpointsSatisfied();
GenericFile<?> remoteFile1 = (GenericFile<?>) mock.getExchanges().get(0).getIn().getBody();
assertTrue(remoteFile1.getBody() instanceof InputStream);
// Wait a little bit for the move to finish.
Thread.sleep(2000);
File resultFile = new File(FTP_ROOT_DIR + File.separator + "failed", "hello.txt");
assertTrue(resultFile.exists());
assertFalse(resultFile.isDirectory());
}
use of org.apache.camel.component.file.GenericFile in project camel by apache.
the class HttpProducer method createRequestEntity.
/**
* Creates a holder object for the data to send to the remote server.
*
* @param exchange the exchange with the IN message with data to send
* @return the data holder
* @throws CamelExchangeException is thrown if error creating RequestEntity
*/
protected HttpEntity createRequestEntity(Exchange exchange) throws CamelExchangeException {
Message in = exchange.getIn();
if (in.getBody() == null) {
return null;
}
HttpEntity answer = in.getBody(HttpEntity.class);
if (answer == null) {
try {
Object data = in.getBody();
if (data != null) {
String contentTypeString = ExchangeHelper.getContentType(exchange);
ContentType contentType = null;
//it removes "boundary" from Content-Type; I have to use contentType.create method.
if (contentTypeString != null) {
// using ContentType.parser for charset
if (contentTypeString.indexOf("charset") > 0) {
contentType = ContentType.parse(contentTypeString);
} else {
contentType = ContentType.create(contentTypeString);
}
}
if (contentTypeString != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentTypeString)) {
if (!getEndpoint().getComponent().isAllowJavaSerializedObject()) {
throw new CamelExchangeException("Content-type " + org.apache.camel.http.common.HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed", exchange);
}
// serialized java object
Serializable obj = in.getMandatoryBody(Serializable.class);
// write object to output stream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
HttpHelper.writeObjectToStream(bos, obj);
ByteArrayEntity entity = new ByteArrayEntity(bos.toByteArray());
entity.setContentType(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
IOHelper.close(bos);
answer = entity;
} else if (data instanceof File || data instanceof GenericFile) {
// file based (could potentially also be a FTP file etc)
File file = in.getBody(File.class);
if (file != null) {
if (contentType != null) {
answer = new FileEntity(file, contentType);
} else {
answer = new FileEntity(file);
}
}
} else if (data instanceof String) {
// be a bit careful with String as any type can most likely be converted to String
// so we only do an instanceof check and accept String if the body is really a String
// do not fallback to use the default charset as it can influence the request
// (for example application/x-www-form-urlencoded forms being sent)
String charset = IOHelper.getCharsetName(exchange, false);
if (charset == null && contentType != null) {
// okay try to get the charset from the content-type
Charset cs = contentType.getCharset();
if (cs != null) {
charset = cs.name();
}
}
StringEntity entity = new StringEntity((String) data, charset);
if (contentType != null) {
entity.setContentType(contentType.toString());
}
answer = entity;
}
// fallback as input stream
if (answer == null) {
// force the body as an input stream since this is the fallback
InputStream is = in.getMandatoryBody(InputStream.class);
String length = in.getHeader(Exchange.CONTENT_LENGTH, String.class);
InputStreamEntity entity = null;
if (ObjectHelper.isEmpty(length)) {
entity = new InputStreamEntity(is, -1);
} else {
entity = new InputStreamEntity(is, Long.parseLong(length));
}
if (contentType != null) {
entity.setContentType(contentType.toString());
}
answer = entity;
}
}
} catch (UnsupportedEncodingException e) {
throw new CamelExchangeException("Error creating RequestEntity from message body", exchange, e);
} catch (IOException e) {
throw new CamelExchangeException("Error serializing message body", exchange, e);
}
}
return answer;
}
use of org.apache.camel.component.file.GenericFile in project camel by apache.
the class JsonPathEngine method doRead.
private Object doRead(JsonPath path, Exchange exchange) throws IOException, CamelExchangeException {
Object json = exchange.getIn().getBody();
if (json instanceof InputStream) {
return readWithInputStream(path, exchange);
} else if (json instanceof GenericFile) {
LOG.trace("JSonPath: {} is read as generic file from message body: {}", path, json);
GenericFile<?> genericFile = (GenericFile<?>) json;
if (genericFile.getCharset() != null) {
// special treatment for generic file with charset
InputStream inputStream = new FileInputStream((File) genericFile.getFile());
return path.read(inputStream, genericFile.getCharset(), configuration);
}
}
if (json instanceof String) {
LOG.trace("JSonPath: {} is read as String from message body: {}", path, json);
String str = (String) json;
return path.read(str, configuration);
} else if (json instanceof Map) {
LOG.trace("JSonPath: {} is read as Map from message body: {}", path, json);
Map map = (Map) json;
return path.read(map, configuration);
} else if (json instanceof List) {
LOG.trace("JSonPath: {} is read as List from message body: {}", path, json);
List list = (List) json;
return path.read(list, configuration);
} else {
// can we find an adapter which can read the message body
Object answer = readWithAdapter(path, exchange);
if (answer == null) {
// fallback and attempt input stream for any other types
answer = readWithInputStream(path, exchange);
}
if (answer != null) {
return answer;
}
}
// is json path configured to suppress exceptions
if (configuration.getOptions().contains(SUPPRESS_EXCEPTIONS)) {
if (configuration.getOptions().contains(ALWAYS_RETURN_LIST)) {
return Collections.emptyList();
} else {
return null;
}
}
// okay it was not then lets throw a failure
throw new CamelExchangeException("Cannot read message body as supported JSon value", exchange);
}
use of org.apache.camel.component.file.GenericFile in project camel by apache.
the class FtpSimpleConsumeStreamingPartialReadTest method testFtpSimpleConsumeAbsolute.
@Test
public void testFtpSimpleConsumeAbsolute() throws Exception {
if (!canTest()) {
return;
}
String expected = "Hello World";
// create file using regular file
// FTP Server does not support absolute path, so lets simulate it
String path = FTP_ROOT_DIR + "/tmp/mytemp";
template.sendBodyAndHeader("file:" + path, expected, Exchange.FILE_NAME, "hello.txt");
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.expectedHeaderReceived(Exchange.FILE_NAME, "hello.txt");
context.startRoute("foo");
assertMockEndpointsSatisfied();
GenericFile<?> remoteFile1 = (GenericFile<?>) mock.getExchanges().get(0).getIn().getBody();
assertTrue(remoteFile1.getBody() instanceof InputStream);
// Wait a little bit for the move to finish.
Thread.sleep(2000);
File resultFile = new File(path + File.separator + "failed", "hello.txt");
assertTrue(resultFile.exists());
assertFalse(resultFile.isDirectory());
}
Aggregations