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.util.Date;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.Endpoint;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.ExchangePattern;
026    import org.apache.camel.impl.DefaultExchange;
027    
028    public class GenericFileExchange<T> extends DefaultExchange {
029    
030        private GenericFile<T> file;
031    
032        public GenericFileExchange(CamelContext context) {
033            super(context);
034        }
035    
036        public GenericFileExchange(CamelContext context, ExchangePattern pattern) {
037            super(context, pattern);
038        }
039    
040        public GenericFileExchange(Exchange parent) {
041            super(parent);
042        }
043    
044        public GenericFileExchange(Endpoint fromEndpoint) {
045            super(fromEndpoint);
046        }
047    
048        public GenericFileExchange(GenericFileEndpoint endpoint, ExchangePattern pattern, GenericFile<T> file) {
049            super(endpoint, pattern);
050            setGenericFile(file);
051        }
052    
053        public GenericFileExchange(DefaultExchange parent, GenericFile<T> file) {
054            super(parent);
055            setGenericFile(file);
056        }
057    
058        public GenericFileExchange(Endpoint fromEndpoint, ExchangePattern pattern) {
059            super(fromEndpoint, pattern);
060        }
061    
062        protected void populateHeaders(GenericFile<T> file) {
063            if (file != null) {
064                getIn().setHeader(Exchange.FILE_NAME_ONLY, file.getFileNameOnly());
065                getIn().setHeader(Exchange.FILE_NAME, file.getFileName());
066                getIn().setHeader("CamelFileAbsolute", file.isAbsolute());
067                getIn().setHeader("CamelFileAbsolutePath", file.getAbsoluteFilePath());
068    
069                if (file.isAbsolute()) {
070                    getIn().setHeader("CamelFilePath", file.getAbsoluteFilePath());
071                } else {
072                    // we must normalize path according to protocol if we build our own paths
073                    String path = file.normalizePathToProtocol(file.getEndpointPath() + File.separator + file.getRelativeFilePath());
074                    getIn().setHeader("CamelFilePath", path);
075                }
076    
077                getIn().setHeader("CamelFileRelativePath", file.getRelativeFilePath());
078                getIn().setHeader("CamelFileParent", file.getParent());
079    
080                if (file.getFileLength() > 0) {
081                    getIn().setHeader("CamelFileLength", file.getFileLength());
082                }
083                if (file.getLastModified() > 0) {
084                    getIn().setHeader("CamelFileLastModified", new Date(file.getLastModified()));
085                }
086            }
087        }
088    
089        public GenericFile<T> getGenericFile() {
090            return file;
091        }
092    
093        public void setGenericFile(GenericFile<T> file) {
094            this.file = file;
095            setIn(new GenericFileMessage<T>(file));
096            populateHeaders(file);
097        }
098    
099        public Exchange newInstance() {
100            return new GenericFileExchange<T>(this, file);
101        }
102    
103    }