Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ox s3 integratiton #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Addons/properties-templates/naviox.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ storePasswordAsHex=true
initialPasswordForAdmin=admin
# There are many more password options in Configuration module of XavaPro (http://www.openxava.org/xavapro)

#Amazon s3 details
bucketName=bucketName
accessKey=accessKey
secretKey=secretKey
region=region
bucketPrefixFolderName=bucketPrefixFolderName

# Navigation options
startInLastVisitedModule=true
showModulesMenuWhenNotLogged=true
Expand Down
32 changes: 31 additions & 1 deletion Addons/src/com/openxava/naviox/util/NaviOXPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,35 @@ public boolean isShowModulesMenuWhenNotLogged() {
public String getEmailValidatorForSignUpClass() {
return getProperties().getProperty("emailValidatorForSignUpClass", EmailValidator.class.getName()).trim();
}


/**
* Amazon s3 credentials
*/

/** Amazon s3 bucketName */
public String getAmazonBucketName() {
return getProperties().getProperty("bucketName", "").trim();
}

/** Amazon s3 AccessKey */
public String getAmazonAccessKey() {
return getProperties().getProperty("accessKey", "").trim();
}

/** Amazon s3 SecretKey */
public String getAmazonSecretKey() {
return getProperties().getProperty("secretKey", "").trim();
}

/** Amazon s3 Region */
public String getAmazonRegion() {
return getProperties().getProperty("region", "").trim();
}

/** Amazon s3 Region */
public String getAmazonBucketPrefixFolderName() {
String prefixName = "";
prefixName = getProperties().getProperty("bucketPrefixFolderName", "").trim();
return prefixName.isEmpty() ? prefixName : prefixName + "/";
}
}
3 changes: 3 additions & 0 deletions OpenXava/project-templates/en/properties/xava.properties
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,6 @@ javaLoggingLevel=FINE
#
# For email notifications and log access tracking
#accessTrackerProvidersClasses=org.openxava.util.EmailNotificationsAccessTrackerProvider,org.openxava.util.LogAccessTrackerProvider

#Store the file in Amazon S3
amazonS3Persistor=true
5 changes: 5 additions & 0 deletions OpenXava/src/org/openxava/util/XavaPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,9 @@ public String getFilesPath() {
return path != null ? path : System.getProperty("user.home") + System.getProperty("file.separator") + "oxfiles";
}

/** S3 Enabled */
public boolean isSaveToS3Enabled() {
return "true".equalsIgnoreCase(getProperties().getProperty("amazonS3Persistor", "true").trim());
}

}
151 changes: 151 additions & 0 deletions OpenXava/src/org/openxava/web/editors/AmazonS3Persistor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package org.openxava.web.editors;

import java.io.*;

import org.apache.commons.io.*;
import org.openxava.util.*;

import com.amazonaws.*;
import com.amazonaws.auth.*;
import com.amazonaws.services.s3.*;
import com.amazonaws.services.s3.model.*;
import com.openxava.naviox.util.*;

public class AmazonS3Persistor {

private static final java.io.File PARENT = new java.io.File(XavaPreferences.getInstance().getFilesPath());
private static final String PrefixFolderName = NaviOXPreferences.getInstance().getAmazonBucketPrefixFolderName();

public static AmazonS3Client initialize() {

/**
* Amazon s3 credentials
*/
String accessKey = NaviOXPreferences.getInstance().getAmazonAccessKey();

String secretKey = NaviOXPreferences.getInstance().getAmazonSecretKey();

String region = NaviOXPreferences.getInstance().getAmazonRegion();

/**
* Building a s3 client using credentials and region
*/
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

AmazonS3Client s3 = (AmazonS3Client) AmazonS3ClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(region)
.build();

return s3;
}

public static String amazonBucketName() {
/** Amazon s3 Bucket Name */
return NaviOXPreferences.getInstance().getAmazonBucketName();

}

public static void uploadToAmazonS3(String filename) {
try {

/**
* Initializing the Amazon S3 Client
* */
AmazonS3Client s3 = initialize();

/** Amazon S3 Bucket name */
String bucketName = amazonBucketName();

/**
* Uploading a new object to S3 from a file..
* 1) Creating a file object using the uploaded filename
* 2) Uploading the file to s3 with bucketname, filename as key and file object.
*/

File file = new File( PARENT +"/"+ filename.toString());

System.out.println("Uploading a new object to S3 from a file" + file.getName());

s3.putObject(new PutObjectRequest(bucketName, PrefixFolderName+file.getName(), file));

/** Deleting the new file stored in local directory */
FileUtils.deleteQuietly(file);

} catch (AmazonServiceException ase) {

amazonException(ase);

}
catch (AmazonClientException ace) {

amazonClientException(ace);

}
}


public static void downloadFromAmazonS3(String uuid) throws IOException {
try
{
/** Initializing the Amazon S3 Client Connection with access key and secret key */
AmazonS3Client s3 = initialize();

/** Amazon S3 Bucket name */
String bucketName = amazonBucketName();

/**
* Fetching all to objects with prefix with unique uuid
* Note: Always only one object will be present in
*/

ObjectListing s3Objects = s3.listObjects(bucketName, PrefixFolderName+uuid);

/**
* Downloading the file from s3..
* 1) Loop through the Objectlisting and get key to get the object.
* 2) Copy the content to local directory.
*/

for (S3ObjectSummary objectSummary: s3Objects.getObjectSummaries()) {

String key = objectSummary.getKey();

S3Object s3object = s3.getObject(new GetObjectRequest(bucketName, key));

S3ObjectInputStream inputStream = ((S3Object) s3object).getObjectContent();

String keyWithoutPrefix = (PrefixFolderName.isEmpty()) ? key : key.split(PrefixFolderName)[1];

FileUtils.copyInputStreamToFile(inputStream, new File(PARENT+"/"+keyWithoutPrefix));

}
} catch (AmazonServiceException ase) {

amazonException(ase);

} catch (AmazonClientException ace) {

amazonClientException(ace);
}
}

public static void amazonException(AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which means your request made it "
+ "to Amazon S3, but was rejected with an error response for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
}

public static void amazonClientException(AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with S3, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}

}
59 changes: 52 additions & 7 deletions OpenXava/src/org/openxava/web/editors/FileSystemPersistor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import org.openxava.calculators.*;
import org.openxava.util.*;

import com.amazonaws.*;
import com.amazonaws.services.s3.*;
import com.amazonaws.services.s3.model.*;

/**
* A implementation of {@link IFilePersistor} <p>
*
Expand Down Expand Up @@ -45,6 +49,17 @@ public void save(AttachedFile file) {
filename.append(Is.emptyString(file.getLibraryId()) ? "NOLIBRARY" : file.getLibraryId());
FileUtils.writeByteArrayToFile(new java.io.File(PARENT, filename.toString()), file.getData());
file.setId(uuid);

/**
* Save to Amazon S3 if amazonS3Persistor property is enabled in xava.properties
*/
if (XavaPreferences.getInstance().isSaveToS3Enabled() == true){
/**
* Upload to Amazon S3
*/
AmazonS3Persistor.uploadToAmazonS3(filename.toString());
}

} catch(Exception ex) {
log.error(ex.getMessage(), ex);
throw new RuntimeException("save_file_error");
Expand All @@ -56,9 +71,15 @@ public void save(AttachedFile file) {
*/
@Override
public void remove(String id) {
java.io.File f = findIOFile(id);
if(f == null) return;
FileUtils.deleteQuietly(f);
java.io.File f;
try {
f = findIOFile(id);
if(f == null) return;
FileUtils.deleteQuietly(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
Expand All @@ -78,9 +99,16 @@ public void removeLibrary(String libraryId) {
*/
@Override
public AttachedFile find(String id) {
java.io.File f = findIOFile(id);
if(f == null) return null;
return convertIOFileToOXFile(f);
java.io.File f;
try {
f = findIOFile(id);
if(f == null) return null;
return convertIOFileToOXFile(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

/**
Expand All @@ -97,11 +125,28 @@ public Collection<AttachedFile> findLibrary(String libraryId) {
return oxFiles;
}

private java.io.File findIOFile(String uuid) {
private java.io.File findIOFile(String uuid) throws IOException{
Collection<java.io.File> files = FileUtils.listFiles(PARENT,
FileFilterUtils.prefixFileFilter(uuid), null);
if(files.size() == 1) return files.iterator().next();
if(files.size() > 1) log.warn(XavaResources.getString("multiple_file_matches", uuid));

/**
* Save to Amazon S3 if amazonS3Persistor property is enabled in xava.properties
*/
if (XavaPreferences.getInstance().isSaveToS3Enabled() == true){
if(files.size() == 0) {
/**
* Download from Amazon S3 if the file doesn't exist in the local
*/
AmazonS3Persistor.downloadFromAmazonS3(uuid);

Collection<java.io.File> files1 = FileUtils.listFiles(PARENT,
FileFilterUtils.prefixFileFilter(uuid), null);
if(files1.size() == 1) return files1.iterator().next();
if(files1.size() > 1) log.warn(XavaResources.getString("multiple_file_matches", uuid));
}
}
return null;
}

Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
# Mahaswami forked OpenXava
# Steps to Integrate amazon S3


OpenXava version: 6.2.2

Library required

1) aws-java-sdk-1.11.699-javadoc.jar
2) aws-java-sdk-1.11.699-sources.jar
3) aws-java-sdk-1.11.699.jar
4) jackson-annotations-2.10.0.jar
5) jackson-core-2.10.0.jar
6) jackson-databind-2.10.0.jar
7) jackson-dataformat-xml-2.10.0.jar
8) joda-time-2.9.4.jar