001: /*
002: * $Id: UploadFileImpl.java,v 1.3 2007/03/12 10:46:14 agoubard Exp $
003: */
004: package com.mycompany.fileupload.api;
005:
006: import java.io.File;
007: import java.io.FileOutputStream;
008: import java.io.FileWriter;
009: import java.util.Random;
010:
011: /**
012: * Implementation of the <code>UploadFile</code> function.
013: *
014: * <p>Description: Upload a file.
015: *
016: * @version $Revision: 1.3 $ $Date: 2007/03/12 10:46:14 $
017: * @author TODO
018: */
019: public final class UploadFileImpl extends UploadFile {
020:
021: /**
022: * Constructs a new <code>UploadFileImpl</code> instance.
023: *
024: * @param api
025: * the API to which this function belongs, guaranteed to be not
026: * <code>null</code>.
027: */
028: public UploadFileImpl(APIImpl api) {
029: super (api);
030: }
031:
032: /**
033: * Calls this function. If the function fails, it may throw any kind of
034: * exception. All exceptions will be handled by the caller.
035: *
036: * @param request
037: * the request, never <code>null</code>.
038: *
039: * @return
040: * the result of the function call, should never be <code>null</code>.
041: *
042: * @throws Throwable
043: * if anything went wrong.
044: */
045: public Result call(Request request) throws Throwable {
046:
047: String inputFileName = request.getDocumentName();
048: byte[] fileContent = request.getDocumentContent();
049:
050: String destinationDir = ((RuntimeProperties) getAPI()
051: .getProperties()).getFileuploadDirectory();
052: String outputFileName = getDestinationFile(request
053: .getDocumentName());
054: File outputFile = new File(destinationDir, outputFileName);
055:
056: FileOutputStream fos = new FileOutputStream(outputFile);
057: fos.write(fileContent);
058: fos.close();
059:
060: File descriptionFile = new File(destinationDir, outputFileName
061: + ".desc");
062: FileWriter descriptionWriter = new FileWriter(descriptionFile);
063: descriptionWriter.write("Owner: " + request.getOwner() + "\n");
064: descriptionWriter.write("Length: " + fileContent.length + "\n");
065: descriptionWriter.write("Description: "
066: + request.getDescription() + "\n");
067: if (request.isSetKeywords()) {
068: descriptionWriter.write("Keywords: "
069: + request.getKeywords() + "\n");
070: }
071: descriptionWriter.close();
072:
073: SuccessfulResult result = new SuccessfulResult();
074: return result;
075: }
076:
077: /**
078: * Gets the destination for the uploaded file.
079: *
080: * @param inputFileName
081: * the original location from the client.
082: *
083: * @return
084: * the name of the file to store the uploaded file.
085: */
086: protected String getDestinationFile(String inputFileName) {
087:
088: String outputFileName = null;
089: // The client may have sent a file from another OS
090: if (inputFileName.lastIndexOf('/') != -1) {
091: outputFileName = inputFileName.substring(inputFileName
092: .lastIndexOf('/') + 1);
093: } else if (inputFileName.lastIndexOf('\\') != -1) {
094: outputFileName = inputFileName.substring(inputFileName
095: .lastIndexOf('\\') + 1);
096: } else {
097: outputFileName = inputFileName;
098: }
099:
100: File outputFile = new File(outputFileName);
101: String newOutputFileName = outputFileName;
102: int counter = 0;
103: while (outputFile.exists()) {
104: counter++;
105: newOutputFileName = outputFileName + "." + counter;
106: outputFile = new File(newOutputFileName);
107: }
108: return newOutputFileName;
109: }
110: }
|