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.CamelContext;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.ExchangePattern;
026    import org.apache.camel.impl.DefaultExchange;
027    
028    /**
029     * A {@link Exchange} for File
030     *
031     * @version $Revision: 699619 $
032     */
033    public class FileExchange extends DefaultExchange {
034        private File file;
035    
036        public FileExchange(CamelContext camelContext, ExchangePattern pattern, File file) {
037            super(camelContext, pattern);
038            setIn(new FileMessage(file));
039            this.file = file;
040    
041            // set additional headers with file information
042            if (file != null) {
043                getIn().setHeader("CamelFileName", file.getName());
044                getIn().setHeader("CamelFileAbsolutePath", file.getAbsolutePath());
045                getIn().setHeader("CamelFileParent", file.getParent());
046                getIn().setHeader("CamelFilePath", file.getPath());
047                try {
048                    getIn().setHeader("CamelFileCanonicalPath", file.getCanonicalPath());
049                } catch (IOException e) {
050                    // ignore
051                }
052                if (file.length() > 0) {
053                    getIn().setHeader("CamelFileLength", new Long(file.length()));
054                }
055                if (file.lastModified() > 0) {
056                    getIn().setHeader("CamelFileLastModified", new Date(file.lastModified()));
057                }
058            }
059        }
060    
061        public FileExchange(DefaultExchange parent, File file) {
062            super(parent);
063            this.file = file;
064        }
065    
066        public File getFile() {
067            return this.file;
068        }
069    
070        public void setFile(File file) {
071            this.file = file;
072        }
073    
074        public Exchange newInstance() {
075            return new FileExchange(this, getFile());
076        }
077    
078    }