001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.file;
018
019 import java.io.File;
020 import java.io.IOException;
021 import java.util.Date;
022
023 import org.apache.camel.Endpoint;
024 import org.apache.camel.Exchange;
025 import org.apache.camel.PollingConsumerAware;
026 import org.apache.camel.RuntimeExchangeException;
027 import org.apache.camel.impl.DefaultExchange;
028
029 public class GenericFileExchange<T> extends DefaultExchange implements PollingConsumerAware {
030
031 private GenericFile<T> file;
032
033 public GenericFileExchange(Endpoint fromEndpoint) {
034 super(fromEndpoint);
035 }
036
037 public GenericFileExchange(DefaultExchange parent, GenericFile<T> file) {
038 super(parent);
039 setGenericFile(file);
040 }
041
042 protected void populateHeaders(GenericFile<T> file) {
043 if (file != null) {
044 getIn().setHeader(Exchange.FILE_NAME_ONLY, file.getFileNameOnly());
045 getIn().setHeader(Exchange.FILE_NAME, file.getFileName());
046 getIn().setHeader("CamelFileAbsolute", file.isAbsolute());
047 getIn().setHeader("CamelFileAbsolutePath", file.getAbsoluteFilePath());
048
049 if (file.isAbsolute()) {
050 getIn().setHeader(Exchange.FILE_PATH, file.getAbsoluteFilePath());
051 } else {
052 // we must normalize path according to protocol if we build our own paths
053 String path = file.normalizePathToProtocol(file.getEndpointPath() + File.separator + file.getRelativeFilePath());
054 getIn().setHeader(Exchange.FILE_PATH, path);
055 }
056
057 getIn().setHeader("CamelFileRelativePath", file.getRelativeFilePath());
058 getIn().setHeader(Exchange.FILE_PARENT, file.getParent());
059
060 if (file.getFileLength() > 0) {
061 getIn().setHeader("CamelFileLength", file.getFileLength());
062 }
063 if (file.getLastModified() > 0) {
064 getIn().setHeader("CamelFileLastModified", new Date(file.getLastModified()));
065 }
066 }
067 }
068
069 public GenericFile<T> getGenericFile() {
070 return file;
071 }
072
073 public void setGenericFile(GenericFile<T> file) {
074 this.file = file;
075 setIn(new GenericFileMessage<T>(file));
076 populateHeaders(file);
077 }
078
079 public Exchange newInstance() {
080 return new GenericFileExchange<T>(this, file);
081 }
082
083 public void exchangePolled(Exchange exchange) {
084 try {
085 // load content into memory
086 file.getBinding().loadContent(exchange, file);
087 } catch (IOException e) {
088 throw new RuntimeExchangeException("Cannot load content of file: " + file.getAbsoluteFilePath(), exchange, e);
089 }
090 }
091 }