use of org.apache.camel.WrappedFile in project camel by apache.
the class ZipAggregationStrategy method aggregate.
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
File zipFile;
Exchange answer = oldExchange;
// Guard against empty new exchanges
if (newExchange == null) {
return oldExchange;
}
// First time for this aggregation
if (oldExchange == null) {
try {
zipFile = FileUtil.createTempFile(this.filePrefix, this.fileSuffix);
} catch (IOException e) {
throw new GenericFileOperationFailedException(e.getMessage(), e);
}
answer = newExchange;
answer.addOnCompletion(new DeleteZipFileOnCompletion(zipFile));
} else {
zipFile = oldExchange.getIn().getBody(File.class);
}
Object body = newExchange.getIn().getBody();
if (body instanceof WrappedFile) {
body = ((WrappedFile) body).getFile();
}
if (body instanceof File) {
try {
File appendFile = (File) body;
// do not try to append empty files
if (appendFile.length() > 0) {
String entryName = preserveFolderStructure ? newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class) : newExchange.getIn().getMessageId();
addFileToZip(zipFile, appendFile, this.preserveFolderStructure ? entryName : null);
GenericFile<File> genericFile = FileConsumer.asGenericFile(zipFile.getParent(), zipFile, Charset.defaultCharset().toString(), false);
genericFile.bindToExchange(answer);
}
} catch (Exception e) {
throw new GenericFileOperationFailedException(e.getMessage(), e);
}
} else {
// Handle all other messages
try {
byte[] buffer = newExchange.getIn().getMandatoryBody(byte[].class);
// do not try to append empty data
if (buffer.length > 0) {
String entryName = useFilenameHeader ? newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class) : newExchange.getIn().getMessageId();
addEntryToZip(zipFile, entryName, buffer, buffer.length);
GenericFile<File> genericFile = FileConsumer.asGenericFile(zipFile.getParent(), zipFile, Charset.defaultCharset().toString(), false);
genericFile.bindToExchange(answer);
}
} catch (Exception e) {
throw new GenericFileOperationFailedException(e.getMessage(), e);
}
}
return answer;
}
use of org.apache.camel.WrappedFile in project camel by apache.
the class FileOperations method storeFile.
public boolean storeFile(String fileName, Exchange exchange) throws GenericFileOperationFailedException {
ObjectHelper.notNull(endpoint, "endpoint");
File file = new File(fileName);
// if an existing file already exists what should we do?
if (file.exists()) {
if (endpoint.getFileExist() == GenericFileExist.Ignore) {
// ignore but indicate that the file was written
LOG.trace("An existing file already exists: {}. Ignore and do not override it.", file);
return true;
} else if (endpoint.getFileExist() == GenericFileExist.Fail) {
throw new GenericFileOperationFailedException("File already exist: " + file + ". Cannot write new file.");
} else if (endpoint.getFileExist() == GenericFileExist.Move) {
// move any existing file first
doMoveExistingFile(fileName);
}
}
// Do an explicit test for a null body and decide what to do
if (exchange.getIn().getBody() == null) {
if (endpoint.isAllowNullBody()) {
LOG.trace("Writing empty file.");
try {
writeFileEmptyBody(file);
return true;
} catch (IOException e) {
throw new GenericFileOperationFailedException("Cannot store file: " + file, e);
}
} else {
throw new GenericFileOperationFailedException("Cannot write null body to file: " + file);
}
}
// 3. write stream to file
try {
// is there an explicit charset configured we must write the file as
String charset = endpoint.getCharset();
// we can optimize and use file based if no charset must be used, and the input body is a file
File source = null;
boolean fileBased = false;
if (charset == null) {
// if no charset, then we can try using file directly (optimized)
Object body = exchange.getIn().getBody();
if (body instanceof WrappedFile) {
body = ((WrappedFile<?>) body).getFile();
}
if (body instanceof File) {
source = (File) body;
fileBased = true;
}
}
if (fileBased) {
// okay we know the body is a file based
// so try to see if we can optimize by renaming the local work path file instead of doing
// a full file to file copy, as the local work copy is to be deleted afterwards anyway
// local work path
File local = exchange.getIn().getHeader(Exchange.FILE_LOCAL_WORK_PATH, File.class);
if (local != null && local.exists()) {
boolean renamed = writeFileByLocalWorkPath(local, file);
if (renamed) {
// try to keep last modified timestamp if configured to do so
keepLastModified(exchange, file);
// set permissions if the chmod option was set
if (ObjectHelper.isNotEmpty(endpoint.getChmod())) {
Set<PosixFilePermission> permissions = endpoint.getPermissions();
if (!permissions.isEmpty()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Setting chmod: {} on file: {} ", PosixFilePermissions.toString(permissions), file);
}
Files.setPosixFilePermissions(file.toPath(), permissions);
}
}
// clear header as we have renamed the file
exchange.getIn().setHeader(Exchange.FILE_LOCAL_WORK_PATH, null);
// to the target.
return true;
}
} else if (source != null && source.exists()) {
// no there is no local work file so use file to file copy if the source exists
writeFileByFile(source, file);
// try to keep last modified timestamp if configured to do so
keepLastModified(exchange, file);
// set permissions if the chmod option was set
if (ObjectHelper.isNotEmpty(endpoint.getChmod())) {
Set<PosixFilePermission> permissions = endpoint.getPermissions();
if (!permissions.isEmpty()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Setting chmod: {} on file: {} ", PosixFilePermissions.toString(permissions), file);
}
Files.setPosixFilePermissions(file.toPath(), permissions);
}
}
return true;
}
}
if (charset != null) {
// charset configured so we must use a reader so we can write with encoding
Reader in = exchange.getContext().getTypeConverter().tryConvertTo(Reader.class, exchange, exchange.getIn().getBody());
if (in == null) {
// okay no direct reader conversion, so use an input stream (which a lot can be converted as)
InputStream is = exchange.getIn().getMandatoryBody(InputStream.class);
in = new InputStreamReader(is);
}
// buffer the reader
in = IOHelper.buffered(in);
writeFileByReaderWithCharset(in, file, charset);
} else {
// fallback and use stream based
InputStream in = exchange.getIn().getMandatoryBody(InputStream.class);
writeFileByStream(in, file);
}
// try to keep last modified timestamp if configured to do so
keepLastModified(exchange, file);
// set permissions if the chmod option was set
if (ObjectHelper.isNotEmpty(endpoint.getChmod())) {
Set<PosixFilePermission> permissions = endpoint.getPermissions();
if (!permissions.isEmpty()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Setting chmod: {} on file: {} ", PosixFilePermissions.toString(permissions), file);
}
Files.setPosixFilePermissions(file.toPath(), permissions);
}
}
return true;
} catch (IOException e) {
throw new GenericFileOperationFailedException("Cannot store file: " + file, e);
} catch (InvalidPayloadException e) {
throw new GenericFileOperationFailedException("Cannot store file: " + file, e);
}
}
use of org.apache.camel.WrappedFile in project camel by apache.
the class DefaultRestletBinding method createRepresentationFromBody.
protected Representation createRepresentationFromBody(Exchange exchange, MediaType mediaType) {
Object body = exchange.getIn().getBody();
if (body == null) {
return new EmptyRepresentation();
}
// unwrap file
if (body instanceof WrappedFile) {
body = ((WrappedFile) body).getFile();
}
if (body instanceof InputStream) {
return new InputRepresentation((InputStream) body, mediaType);
} else if (body instanceof File) {
return new FileRepresentation((File) body, mediaType);
} else if (body instanceof byte[]) {
return new ByteArrayRepresentation((byte[]) body, mediaType);
} else if (body instanceof String) {
return new StringRepresentation((CharSequence) body, mediaType);
}
// fallback as string
body = exchange.getIn().getBody(String.class);
if (body != null) {
return new StringRepresentation((CharSequence) body, mediaType);
} else {
return new EmptyRepresentation();
}
}
use of org.apache.camel.WrappedFile in project camel by apache.
the class MessageHelper method extractValueForLogging.
/**
* Extracts the value for logging purpose.
* <p/>
* Will clip the value if its too big for logging.
*
* @see org.apache.camel.Exchange#LOG_DEBUG_BODY_MAX_CHARS
* @param obj the value
* @param message the message
* @param prepend a message to prepend
* @param allowStreams whether or not streams is allowed
* @param allowFiles whether or not files is allowed (currently not in use)
* @param maxChars limit to maximum number of chars. Use 0 for not limit, and -1 for turning logging message body off.
* @return the logging message
*/
public static String extractValueForLogging(Object obj, Message message, String prepend, boolean allowStreams, boolean allowFiles, int maxChars) {
if (maxChars < 0) {
return prepend + "[Body is not logged]";
}
if (obj == null) {
return prepend + "[Body is null]";
}
if (!allowStreams) {
if (obj instanceof Source && !(obj instanceof StringSource || obj instanceof BytesSource)) {
// all other kinds we should not touch the body
return prepend + "[Body is instance of java.xml.transform.Source]";
} else if (obj instanceof StreamCache) {
return prepend + "[Body is instance of org.apache.camel.StreamCache]";
} else if (obj instanceof InputStream) {
return prepend + "[Body is instance of java.io.InputStream]";
} else if (obj instanceof OutputStream) {
return prepend + "[Body is instance of java.io.OutputStream]";
} else if (obj instanceof Reader) {
return prepend + "[Body is instance of java.io.Reader]";
} else if (obj instanceof Writer) {
return prepend + "[Body is instance of java.io.Writer]";
} else if (obj instanceof WrappedFile || obj instanceof File) {
if (!allowFiles) {
return prepend + "[Body is file based: " + obj + "]";
}
}
}
if (!allowFiles) {
if (obj instanceof WrappedFile || obj instanceof File) {
return prepend + "[Body is file based: " + obj + "]";
}
}
// is the body a stream cache or input stream
StreamCache cache = null;
InputStream is = null;
if (obj instanceof StreamCache) {
cache = (StreamCache) obj;
is = null;
} else if (obj instanceof InputStream) {
cache = null;
is = (InputStream) obj;
}
// grab the message body as a string
String body = null;
if (message.getExchange() != null) {
try {
body = message.getExchange().getContext().getTypeConverter().tryConvertTo(String.class, message.getExchange(), obj);
} catch (Throwable e) {
// ignore as the body is for logging purpose
}
}
if (body == null) {
try {
body = obj.toString();
} catch (Throwable e) {
// ignore as the body is for logging purpose
}
}
// reset stream cache after use
if (cache != null) {
cache.reset();
} else if (is != null && is.markSupported()) {
try {
is.reset();
} catch (IOException e) {
// ignore
}
}
if (body == null) {
return prepend + "[Body is null]";
}
// clip body if length enabled and the body is too big
if (maxChars > 0 && body.length() > maxChars) {
body = body.substring(0, maxChars) + "... [Body clipped after " + maxChars + " chars, total length is " + body.length() + "]";
}
return prepend + body;
}
use of org.apache.camel.WrappedFile in project camel by apache.
the class S3Producer method processMultiPart.
public void processMultiPart(final Exchange exchange) throws Exception {
File filePayload = null;
Object obj = exchange.getIn().getMandatoryBody();
// Need to check if the message body is WrappedFile
if (obj instanceof WrappedFile) {
obj = ((WrappedFile<?>) obj).getFile();
}
if (obj instanceof File) {
filePayload = (File) obj;
} else {
throw new InvalidArgumentException("aws-s3: MultiPart upload requires a File input.");
}
ObjectMetadata objectMetadata = determineMetadata(exchange);
if (objectMetadata.getContentLength() == 0) {
objectMetadata.setContentLength(filePayload.length());
}
final String keyName = determineKey(exchange);
final InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(getConfiguration().getBucketName(), keyName, objectMetadata);
String storageClass = determineStorageClass(exchange);
if (storageClass != null) {
initRequest.setStorageClass(StorageClass.fromValue(storageClass));
}
String cannedAcl = exchange.getIn().getHeader(S3Constants.CANNED_ACL, String.class);
if (cannedAcl != null) {
CannedAccessControlList objectAcl = CannedAccessControlList.valueOf(cannedAcl);
initRequest.setCannedACL(objectAcl);
}
AccessControlList acl = exchange.getIn().getHeader(S3Constants.ACL, AccessControlList.class);
if (acl != null) {
// note: if cannedacl and acl are both specified the last one will be used. refer to
// PutObjectRequest#setAccessControlList for more details
initRequest.setAccessControlList(acl);
}
LOG.trace("Initiating multipart upload [{}] from exchange [{}]...", initRequest, exchange);
final InitiateMultipartUploadResult initResponse = getEndpoint().getS3Client().initiateMultipartUpload(initRequest);
final long contentLength = objectMetadata.getContentLength();
final List<PartETag> partETags = new ArrayList<PartETag>();
long partSize = getConfiguration().getPartSize();
CompleteMultipartUploadResult uploadResult = null;
long filePosition = 0;
try {
for (int part = 1; filePosition < contentLength; part++) {
partSize = Math.min(partSize, contentLength - filePosition);
UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(getConfiguration().getBucketName()).withKey(keyName).withUploadId(initResponse.getUploadId()).withPartNumber(part).withFileOffset(filePosition).withFile(filePayload).withPartSize(partSize);
LOG.trace("Uploading part [{}] for {}", part, keyName);
partETags.add(getEndpoint().getS3Client().uploadPart(uploadRequest).getPartETag());
filePosition += partSize;
}
CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(getConfiguration().getBucketName(), keyName, initResponse.getUploadId(), partETags);
uploadResult = getEndpoint().getS3Client().completeMultipartUpload(compRequest);
} catch (Exception e) {
getEndpoint().getS3Client().abortMultipartUpload(new AbortMultipartUploadRequest(getConfiguration().getBucketName(), keyName, initResponse.getUploadId()));
throw e;
}
Message message = getMessageForResponse(exchange);
message.setHeader(S3Constants.E_TAG, uploadResult.getETag());
if (uploadResult.getVersionId() != null) {
message.setHeader(S3Constants.VERSION_ID, uploadResult.getVersionId());
}
if (getConfiguration().isDeleteAfterWrite() && filePayload != null) {
FileUtil.deleteFile(filePayload);
}
}
Aggregations