Skip to content

Commit

Permalink
Added ERCFFileUpload, a file upload component for RackSpace's CloudFi…
Browse files Browse the repository at this point in the history
…les that let you specify the target container.
  • Loading branch information
Pascal Robert committed Jun 2, 2012
1 parent 67f4cd7 commit 671cb34
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wodefinitions>
<wo class="ERCFFileUpload" wocomponentcontent="false"> <binding name="container"/>
<validation message="'container' is a required binding.">
<unbound name="container"/>
</validation>
<binding name="configurationName"/>
<binding name="webPath"/>
<validation message="'webPath' is a required binding.">
<unbound name="webPath"/>
</validation>
</wo>
</wodefinitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<webobject name = "FileUpload"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FileUpload : WOFileUpload {
filePath = filePath;
streamToFilePath = tempFilePath;
finalFilePath = finalFilePath;
overwrite = true;
id = ^id;
class = ^class;
style = ^style;
title = ^title;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"WebObjects Release" = "WebObjects 5.0";
encoding = "UTF-8";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 671cb34

Please sign in to comment.