Search in sources :

Example 6 with WrappedFile

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;
}
Also used : Exchange(org.apache.camel.Exchange) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) WrappedFile(org.apache.camel.WrappedFile) IOException(java.io.IOException) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) IOException(java.io.IOException)

Example 7 with WrappedFile

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);
    }
}
Also used : Set(java.util.Set) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) InvalidPayloadException(org.apache.camel.InvalidPayloadException) WrappedFile(org.apache.camel.WrappedFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile)

Example 8 with WrappedFile

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();
    }
}
Also used : InputRepresentation(org.restlet.representation.InputRepresentation) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) WrappedFile(org.apache.camel.WrappedFile) StringRepresentation(org.restlet.representation.StringRepresentation) InputStream(java.io.InputStream) FileRepresentation(org.restlet.representation.FileRepresentation) ByteArrayRepresentation(org.restlet.representation.ByteArrayRepresentation) WrappedFile(org.apache.camel.WrappedFile) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Example 9 with WrappedFile

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;
}
Also used : BytesSource(org.apache.camel.BytesSource) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Reader(java.io.Reader) IOException(java.io.IOException) Source(javax.xml.transform.Source) BytesSource(org.apache.camel.BytesSource) StringSource(org.apache.camel.StringSource) StreamCache(org.apache.camel.StreamCache) WrappedFile(org.apache.camel.WrappedFile) StringSource(org.apache.camel.StringSource) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) Writer(java.io.Writer)

Example 10 with WrappedFile

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);
    }
}
Also used : CannedAccessControlList(com.amazonaws.services.s3.model.CannedAccessControlList) AccessControlList(com.amazonaws.services.s3.model.AccessControlList) InitiateMultipartUploadResult(com.amazonaws.services.s3.model.InitiateMultipartUploadResult) Message(org.apache.camel.Message) InitiateMultipartUploadRequest(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest) ArrayList(java.util.ArrayList) UploadPartRequest(com.amazonaws.services.s3.model.UploadPartRequest) AbortMultipartUploadRequest(com.amazonaws.services.s3.model.AbortMultipartUploadRequest) CompleteMultipartUploadResult(com.amazonaws.services.s3.model.CompleteMultipartUploadResult) CannedAccessControlList(com.amazonaws.services.s3.model.CannedAccessControlList) PartETag(com.amazonaws.services.s3.model.PartETag) Endpoint(org.apache.camel.Endpoint) InvalidArgumentException(com.amazonaws.services.cloudfront.model.InvalidArgumentException) InvalidArgumentException(com.amazonaws.services.cloudfront.model.InvalidArgumentException) WrappedFile(org.apache.camel.WrappedFile) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) CompleteMultipartUploadRequest(com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)

Aggregations

WrappedFile (org.apache.camel.WrappedFile)12 File (java.io.File)11 InputStream (java.io.InputStream)6 IOException (java.io.IOException)4 Message (org.apache.camel.Message)4 GenericFile (org.apache.camel.component.file.GenericFile)4 Reader (java.io.Reader)3 Exchange (org.apache.camel.Exchange)3 AccessControlList (com.amazonaws.services.s3.model.AccessControlList)2 CannedAccessControlList (com.amazonaws.services.s3.model.CannedAccessControlList)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)2 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2 StringSource (org.apache.camel.StringSource)2 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)2 InvalidArgumentException (com.amazonaws.services.cloudfront.model.InvalidArgumentException)1 AbortMultipartUploadRequest (com.amazonaws.services.s3.model.AbortMultipartUploadRequest)1 CompleteMultipartUploadRequest (com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)1