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.InputStream;
021
022 import org.apache.camel.Exchange;
023 import org.apache.camel.Expression;
024 import org.apache.camel.impl.DefaultProducer;
025 import org.apache.camel.spi.Language;
026 import org.apache.camel.util.ExchangeHelper;
027 import org.apache.camel.util.FileUtil;
028 import org.apache.camel.util.ObjectHelper;
029 import org.apache.commons.logging.Log;
030 import org.apache.commons.logging.LogFactory;
031
032 /**
033 * Generic file producer
034 */
035 public class GenericFileProducer<T> extends DefaultProducer {
036 protected final transient Log log = LogFactory.getLog(getClass());
037 protected final GenericFileEndpoint<T> endpoint;
038 protected final GenericFileOperations<T> operations;
039
040 protected GenericFileProducer(GenericFileEndpoint<T> endpoint, GenericFileOperations<T> operations) {
041 super(endpoint);
042 this.endpoint = endpoint;
043 this.operations = operations;
044 }
045
046 protected String getFileSeparator() {
047 return File.separator;
048 }
049
050 protected String normalizePath(String name) {
051 return FileUtil.normalizePath(name);
052 }
053
054 @SuppressWarnings("unchecked")
055 public void process(Exchange exchange) throws Exception {
056 Exchange fileExchange = endpoint.createExchange(exchange);
057 processExchange(fileExchange);
058 ExchangeHelper.copyResults(exchange, fileExchange);
059 }
060
061 /**
062 * Perform the work to process the fileExchange
063 *
064 * @param exchange fileExchange
065 * @throws Exception is thrown if some error
066 */
067 protected void processExchange(Exchange exchange) throws Exception {
068 if (log.isTraceEnabled()) {
069 log.trace("Processing " + exchange);
070 }
071
072 try {
073 String target = createFileName(exchange);
074
075 preWriteCheck();
076
077 // should we write to a temporary name and then afterwards rename to real target
078 boolean writeAsTempAndRename = ObjectHelper.isNotEmpty(endpoint.getTempFileName());
079 String tempTarget = null;
080 if (writeAsTempAndRename) {
081 // compute temporary name with the temp prefix
082 tempTarget = createTempFileName(exchange, target);
083
084 if (log.isTraceEnabled()) {
085 log.trace("Writing using tempNameFile: " + tempTarget);
086 }
087
088 // cater for file exists option on the real target as
089 // the file operations code will work on the temp file
090
091 // if an existing file already exists what should we do?
092 if (operations.existsFile(target)) {
093 if (endpoint.getFileExist() == GenericFileExist.Ignore) {
094 // ignore but indicate that the file was written
095 if (log.isTraceEnabled()) {
096 log.trace("An existing file already exists: " + target + ". Ignore and do not override it.");
097 }
098 return;
099 } else if (endpoint.getFileExist() == GenericFileExist.Fail) {
100 throw new GenericFileOperationFailedException("File already exist: " + target + ". Cannot write new file.");
101 } else if (endpoint.getFileExist() == GenericFileExist.Override) {
102 // we override the target so we do this by deleting it so the temp file can be renamed later
103 // with success as the existing target file have been deleted
104 if (log.isTraceEnabled()) {
105 log.trace("Deleting existing file: " + tempTarget);
106 }
107 if (!operations.deleteFile(target)) {
108 throw new GenericFileOperationFailedException("Cannot delete file: " + target);
109 }
110
111 }
112 }
113
114 // delete any pre existing temp file
115 if (operations.existsFile(tempTarget)) {
116 if (log.isTraceEnabled()) {
117 log.trace("Deleting existing temp file: " + tempTarget);
118 }
119 if (!operations.deleteFile(tempTarget)) {
120 throw new GenericFileOperationFailedException("Cannot delete file: " + tempTarget);
121 }
122 }
123 }
124
125 // write/upload the file
126 writeFile(exchange, tempTarget != null ? tempTarget : target);
127
128 // if we did write to a temporary name then rename it to the real
129 // name after we have written the file
130 if (tempTarget != null) {
131 if (log.isTraceEnabled()) {
132 log.trace("Renaming file: [" + tempTarget + "] to: [" + target + "]");
133 }
134 boolean renamed = operations.renameFile(tempTarget, target);
135 if (!renamed) {
136 throw new GenericFileOperationFailedException("Cannot rename file from: " + tempTarget + " to: " + target);
137 }
138 }
139
140 // lets store the name we really used in the header, so end-users
141 // can retrieve it
142 exchange.getIn().setHeader(Exchange.FILE_NAME_PRODUCED, target);
143 } catch (Exception e) {
144 handleFailedWrite(exchange, e);
145 }
146
147 postWriteCheck();
148 }
149
150 /**
151 * If we fail writing out a file, we will call this method. This hook is
152 * provided to disconnect from servers or clean up files we created (if needed).
153 */
154 protected void handleFailedWrite(Exchange exchange, Exception exception) throws Exception {
155 throw exception;
156 }
157
158 /**
159 * Perform any actions that need to occur before we write such as connecting to an FTP server etc.
160 */
161 protected void preWriteCheck() throws Exception {
162 // nothing needed to check
163 }
164
165 /**
166 * Perform any actions that need to occur after we are done such as disconnecting.
167 */
168 protected void postWriteCheck() {
169 // nothing needed to check
170 }
171
172 protected void writeFile(Exchange exchange, String fileName) throws GenericFileOperationFailedException {
173 InputStream payload = exchange.getIn().getBody(InputStream.class);
174 try {
175 // build directory if auto create is enabled
176 if (endpoint.isAutoCreate()) {
177 // use java.io.File to compute the file path
178 File file = new File(fileName);
179 String directory = file.getParent();
180 boolean absolute = FileUtil.isAbsolute(file);
181 if (directory != null) {
182 if (!operations.buildDirectory(directory, absolute)) {
183 if (log.isDebugEnabled()) {
184 log.debug("Cannot build directory [" + directory + "] (could be because of denied permissions)");
185 }
186 }
187 }
188 }
189
190 // upload
191 if (log.isTraceEnabled()) {
192 log.trace("About to write [" + fileName + "] to [" + getEndpoint() + "] from exchange [" + exchange + "]");
193 }
194
195 boolean success = operations.storeFile(fileName, exchange);
196 if (!success) {
197 throw new GenericFileOperationFailedException("Error writing file [" + fileName + "]");
198 }
199 if (log.isDebugEnabled()) {
200 log.debug("Wrote [" + fileName + "] to [" + getEndpoint() + "]");
201 }
202
203 } finally {
204 ObjectHelper.close(payload, "Closing payload", log);
205 }
206
207 }
208
209 protected String createFileName(Exchange exchange) {
210 String answer;
211
212 String name = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
213
214 // expression support
215 Expression expression = endpoint.getFileName();
216 if (name != null) {
217 // the header name can be an expression too, that should override
218 // whatever configured on the endpoint
219 if (name.indexOf("${") > -1) {
220 if (log.isDebugEnabled()) {
221 log.debug(Exchange.FILE_NAME + " contains a FileLanguage expression: " + name);
222 }
223 Language language = getEndpoint().getCamelContext().resolveLanguage("file");
224 expression = language.createExpression(name);
225 }
226 }
227 if (expression != null) {
228 if (log.isDebugEnabled()) {
229 log.debug("Filename evaluated as expression: " + expression);
230 }
231 name = expression.evaluate(exchange, String.class);
232 }
233
234 // flatten name
235 if (name != null && endpoint.isFlatten()) {
236 int pos = name.lastIndexOf(getFileSeparator());
237 if (pos == -1) {
238 pos = name.lastIndexOf('/');
239 }
240 if (pos != -1) {
241 name = name.substring(pos + 1);
242 }
243 }
244
245 // compute path by adding endpoint starting directory
246 String endpointPath = endpoint.getConfiguration().getDirectory();
247 // Its a directory so we should use it as a base path for the filename
248 // If the path isn't empty, we need to add a trailing / if it isn't already there
249 String baseDir = "";
250 if (endpointPath.length() > 0) {
251 baseDir = endpointPath + (endpointPath.endsWith(getFileSeparator()) ? "" : getFileSeparator());
252 }
253 if (name != null) {
254 answer = baseDir + name;
255 } else {
256 // use a generated filename if no name provided
257 answer = baseDir + endpoint.getGeneratedFileName(exchange.getIn());
258 }
259
260 // must normalize path to cater for Windows and other OS
261 answer = normalizePath(answer);
262
263 return answer;
264 }
265
266 protected String createTempFileName(Exchange exchange, String fileName) {
267 // must normalize path to cater for Windows and other OS
268 fileName = normalizePath(fileName);
269
270 String tempName;
271 if (exchange.getIn().getHeader(Exchange.FILE_NAME) == null) {
272 // its a generated filename then add it to header so we can evaluate the expression
273 exchange.getIn().setHeader(Exchange.FILE_NAME, FileUtil.stripPath(fileName));
274 tempName = endpoint.getTempFileName().evaluate(exchange, String.class);
275 // and remove it again after evaluation
276 exchange.getIn().removeHeader(Exchange.FILE_NAME);
277 } else {
278 tempName = endpoint.getTempFileName().evaluate(exchange, String.class);
279 }
280
281 int path = fileName.lastIndexOf(getFileSeparator());
282 if (path == -1) {
283 // no path
284 return tempName;
285 } else {
286 StringBuilder sb = new StringBuilder(fileName.substring(0, path + 1));
287 sb.append(tempName);
288 return sb.toString();
289 }
290 }
291
292 }