diff --git a/Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.api b/Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.api new file mode 100644 index 00000000000..4651e3d1da1 --- /dev/null +++ b/Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.api @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.wo/ERCFFileUpload.html b/Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.wo/ERCFFileUpload.html new file mode 100644 index 00000000000..f937407e2e5 --- /dev/null +++ b/Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.wo/ERCFFileUpload.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.wo/ERCFFileUpload.wod b/Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.wo/ERCFFileUpload.wod new file mode 100644 index 00000000000..a1169b11653 --- /dev/null +++ b/Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.wo/ERCFFileUpload.wod @@ -0,0 +1,10 @@ +FileUpload : WOFileUpload { + filePath = filePath; + streamToFilePath = tempFilePath; + finalFilePath = finalFilePath; + overwrite = true; + id = ^id; + class = ^class; + style = ^style; + title = ^title; +} \ No newline at end of file diff --git a/Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.wo/ERCFFileUpload.woo b/Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.wo/ERCFFileUpload.woo new file mode 100644 index 00000000000..a076a051a7e --- /dev/null +++ b/Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.wo/ERCFFileUpload.woo @@ -0,0 +1,4 @@ +{ + "WebObjects Release" = "WebObjects 5.0"; + encoding = "UTF-8"; +} \ No newline at end of file diff --git a/Frameworks/BusinessLogic/ERAttachment/Sources/com/rackspacecloud/client/cloudfiles/FilesUtil.java b/Frameworks/BusinessLogic/ERAttachment/Sources/com/rackspacecloud/client/cloudfiles/FilesUtil.java index 20f7bfddf24..dedfb9e0bf2 100644 --- a/Frameworks/BusinessLogic/ERAttachment/Sources/com/rackspacecloud/client/cloudfiles/FilesUtil.java +++ b/Frameworks/BusinessLogic/ERAttachment/Sources/com/rackspacecloud/client/cloudfiles/FilesUtil.java @@ -40,7 +40,7 @@ private static synchronized void loadPropertiesFromClasspath() throws IOExceptio InputStream io = FilesUtil.class.getClassLoader().getResourceAsStream(file); if (io == null) { - //throw new FileNotFoundException("Property file '" + file + "' not found in the classpath."); + throw new FileNotFoundException("Property file '" + file + "' not found in the classpath."); } loadProperties(io); } diff --git a/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/components/ERCFFileUpload.java b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/components/ERCFFileUpload.java new file mode 100644 index 00000000000..034e1eb4850 --- /dev/null +++ b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/components/ERCFFileUpload.java @@ -0,0 +1,199 @@ +package er.attachment.components; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +import org.apache.http.HttpException; + +import com.rackspacecloud.client.cloudfiles.FilesClient; +import com.rackspacecloud.client.cloudfiles.FilesException; +import com.rackspacecloud.client.cloudfiles.FilesNotFoundException; +import com.webobjects.appserver.WOActionResults; +import com.webobjects.appserver.WOContext; +import com.webobjects.appserver.WORequest; +import com.webobjects.foundation.NSForwardException; +import com.webobjects.foundation.NSPathUtilities; + +import er.attachment.utils.ERMimeType; +import er.attachment.utils.ERMimeTypeManager; +import er.extensions.components.ERXComponent; +import er.extensions.foundation.ERXFileUtilities; +import er.extensions.foundation.ERXProperties; + +public class ERCFFileUpload extends ERXComponent { + + private String _filePath; + private String _finalFilePath; + private String _configurationName; + + public ERCFFileUpload(WOContext context) { + super(context); + } + + public void setFilePath(String filePath) { + _filePath = filePath; + } + + public String filePath() { + return _filePath; + } + + public void setFinalFilePath(String finalFilePath) { + _finalFilePath = finalFilePath; + } + + public String finalFilePath() { + return _finalFilePath; + } + + public String configurationName() { + return _configurationName; + } + + public void setConfigurationName(String configurationName) { + this._configurationName = configurationName; + } + + public String container() { + return (String)valueForBinding("container"); + } + + public String tempFilePath() throws IOException { + String configurationName = (String) valueForBinding("configurationName"); + String tempFolderPath = ERXProperties.stringForKey("er.attachment." + configurationName + ".tempFolder"); + if (tempFolderPath == null) { + tempFolderPath = ERXProperties.stringForKey("er.attachment.tempFolder"); + } + + String fileExtension = ERXFileUtilities.fileExtension(_filePath); + if (fileExtension == null) { + fileExtension = "tmp"; + } + fileExtension = "." + fileExtension; + + File tempFile; + if (tempFolderPath != null) { + File tempFolder = new File(tempFolderPath); + tempFile = File.createTempFile("ERAttachmentUpload-", fileExtension, tempFolder); + } + else { + tempFile = File.createTempFile("ERAttachmentUpload-", fileExtension); + } + return tempFile.getAbsolutePath(); + } + + public FilesClient cloudFilesConnection() { + FilesClient conn = new FilesClient(username(), accessKeyID(), authUrl(), null, connectionTimeOut()); + try { + conn.login(); + } + catch (IOException e) { + e.printStackTrace(); + } + catch (HttpException e) { + e.printStackTrace(); + } + return conn; + } + + public String accessKeyID() { + String accessKeyID = ERXProperties.decryptedStringForKey("er.attachment." + configurationName() + ".cf.apiAccessKey"); + if (accessKeyID == null) { + accessKeyID = ERXProperties.decryptedStringForKey("er.attachment.cf.apiAccessKey"); + } + if (accessKeyID == null) { + throw new IllegalArgumentException("There is no 'er.attachment." + configurationName() + ".cf.apiAccessKey' or 'er.attachment.cf.apiAccessKey' property set."); + } + return accessKeyID; + } + + public String username() { + String username = ERXProperties.decryptedStringForKey("er.attachment." + configurationName() + ".cf.username"); + if (username == null) { + username = ERXProperties.decryptedStringForKey("er.attachment.cf.username"); + } + if (username == null) { + throw new IllegalArgumentException("There is no 'er.attachment." + configurationName() + ".cf.username' or 'er.attachment.cf.username' property set."); + } + return username; + } + + public String authUrl() { + String authUrl = ERXProperties.decryptedStringForKey("er.attachment." + configurationName() + ".cf.authUrl"); + if (authUrl == null) { + authUrl = ERXProperties.decryptedStringForKeyWithDefault("er.attachment.cf.authUrl", "https://auth.api.rackspacecloud.com/v1.0"); + } + return authUrl; + } + + public int connectionTimeOut() { + String connectionTimeOut = ERXProperties.decryptedStringForKey("er.attachment." + configurationName() + ".cf.connectionTimeOut"); + if (connectionTimeOut == null) { + connectionTimeOut = ERXProperties.decryptedStringForKeyWithDefault("er.attachment.cf.connectionTimeOut", "5000"); + } + return new Integer(connectionTimeOut); + } + + public void _uploadSucceeded() throws IOException, FilesException, HttpException { + if (_finalFilePath == null) { + return; + } + + File uploadedFile = new File(_finalFilePath); + + try { + cloudFilesConnection().getContainerInfo(container()); + } + catch (FilesNotFoundException e) { + cloudFilesConnection().createContainer(container()); + } finally { + cloudFilesConnection().storeObjectAs(container(), uploadedFile, mimeType(uploadedFile.getName()), NSPathUtilities.lastPathComponent(_filePath)); + URL pathToFile = new URL(cloudFilesConnection().getStorageURL() + "/" + container() + "/" + NSPathUtilities.lastPathComponent(_filePath)); + setValueForBinding(pathToFile.toExternalForm(), "webPath"); + } + } + + private String mimeType(String recommendedFileName) { + String suggestedMimeType = null; + String extension = ERXFileUtilities.fileExtension(recommendedFileName); + + ERMimeType erMimeType = ERMimeTypeManager.mimeTypeManager().mimeTypeForExtension(extension, false); + if (erMimeType != null) { + suggestedMimeType = erMimeType.mimeType(); + } + + if (suggestedMimeType == null) { + suggestedMimeType = "application/x-octet-stream"; + } + + return suggestedMimeType; + } + + + @Override + public WOActionResults invokeAction(WORequest request, WOContext context) { + WOActionResults results = super.invokeAction(request, context); + if (context.wasFormSubmitted()) { + try { + _uploadSucceeded(); + } + catch (IOException e) { + throw new NSForwardException(e, "Failed to process uploaded attachment."); + } + catch (FilesException e) { + throw new NSForwardException(e, "Failed to process uploaded attachment."); + } + catch (HttpException e) { + throw new NSForwardException(e, "Failed to process uploaded attachment."); + } + } + return results; + } + + @Override + public boolean synchronizesVariablesWithBindings() { + return false; + } + +} \ No newline at end of file