-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ERCFFileUpload, a file upload component for RackSpace's CloudFi…
…les that let you specify the target container.
- Loading branch information
Pascal Robert
committed
Jun 2, 2012
1 parent
67f4cd7
commit 671cb34
Showing
6 changed files
with
228 additions
and
1 deletion.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
1 change: 1 addition & 0 deletions
1
Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.wo/ERCFFileUpload.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<webobject name = "FileUpload"/> |
10 changes: 10 additions & 0 deletions
10
Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.wo/ERCFFileUpload.wod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
4 changes: 4 additions & 0 deletions
4
Frameworks/BusinessLogic/ERAttachment/Components/ERCFFileUpload.wo/ERCFFileUpload.woo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"WebObjects Release" = "WebObjects 5.0"; | ||
encoding = "UTF-8"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/components/ERCFFileUpload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |