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.FileOutputStream;
021    import java.io.InputStream;
022    import java.io.RandomAccessFile;
023    import java.nio.ByteBuffer;
024    import java.nio.channels.FileChannel;
025    
026    import org.apache.camel.Exchange;
027    import org.apache.camel.Message;
028    import org.apache.camel.impl.DefaultProducer;
029    import org.apache.camel.util.ExchangeHelper;
030    import org.apache.camel.util.ObjectHelper;
031    import org.apache.camel.util.UuidGenerator;
032    import org.apache.commons.logging.Log;
033    import org.apache.commons.logging.LogFactory;
034    
035    /**
036     * For producing files.
037     *
038     * @version $Revision: 676309 $
039     */
040    public class FileProducer extends DefaultProducer {
041        private static final transient Log LOG = LogFactory.getLog(FileProducer.class);
042        private FileEndpoint endpoint;
043    
044        public FileProducer(FileEndpoint endpoint) {
045            super(endpoint);
046            this.endpoint = endpoint;
047        }
048    
049        public FileEndpoint getEndpoint() {
050            return (FileEndpoint) super.getEndpoint();
051        }
052    
053        public void process(Exchange exchange) throws Exception {
054            FileExchange fileExchange = endpoint.createExchange(exchange);
055            process(fileExchange);
056            ExchangeHelper.copyResults(exchange, fileExchange);
057        }
058    
059        public void process(FileExchange exchange) throws Exception {
060            InputStream in = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class);
061            File file = createFileName(exchange.getIn());
062            buildDirectory(file);
063    
064            if (LOG.isDebugEnabled()) {
065                LOG.debug("About to write to: " + file + " from exchange: " + exchange);
066            }
067            FileChannel fc = null;
068            try {
069                if (getEndpoint().isAppend()) {
070                    fc = new RandomAccessFile(file, "rw").getChannel();
071                    fc.position(fc.size());
072                } else {
073                    fc = new FileOutputStream(file).getChannel();
074                }
075    
076                int size = getEndpoint().getBufferSize();
077                byte[] buffer = new byte[size];
078                ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
079                while (true) {
080                    int count = in.read(buffer);
081                    if (count <= 0) {
082                        break;
083                    } else if (count < size) {
084                        byteBuffer = ByteBuffer.wrap(buffer, 0, count);
085                        fc.write(byteBuffer);
086                        break;
087                    } else {
088                        fc.write(byteBuffer);
089                        byteBuffer.clear();
090                    }
091                }
092            } finally {
093                ObjectHelper.close(in, file.getName(), LOG);
094                ObjectHelper.close(fc, file.getName(), LOG);
095            }
096        }
097    
098        protected File createFileName(Message message) {
099            File answer;
100    
101            String name = null;
102            if (!endpoint.isIgnoreFileNameHeader()) {
103                name = message.getHeader(FileComponent.HEADER_FILE_NAME, String.class);
104            }
105    
106            File endpointFile = endpoint.getFile();
107            if (endpointFile.isDirectory()) {
108                if (name != null) {
109                    answer = new File(endpointFile, name);
110                    if (answer.isDirectory()) {
111                        answer = new File(answer, endpoint.getGeneratedFileName(message));
112                    }
113                } else {
114                    answer = new File(endpointFile, endpoint.getGeneratedFileName(message));
115                }
116            } else {
117                if (name == null) {
118                    answer = endpointFile;
119                } else {
120                    answer = new File(endpointFile, name);
121                }
122            }
123    
124            // lets store the name we really used in the header, so end-users can retrieve it
125            message.setHeader(FileComponent.HEADER_FILE_NAME_PRODUCED, answer.getAbsolutePath());
126    
127            return answer;
128        }
129    
130        private void buildDirectory(File file) {
131            String dirName = file.getAbsolutePath();
132            int index = dirName.lastIndexOf(File.separatorChar);
133            if (index > 0) {
134                dirName = dirName.substring(0, index);
135                File dir = new File(dirName);
136                dir.mkdirs();
137            }
138        }
139    
140    }