Launch your IDE of choice.
Create a new sample application for Cloud Foundry
Note: You can also use an existing SAP CF java project, the outlined steps will be very similar.
$ mvn archetype:generate -DarchetypeGroupId=com.sap.cloud.s4hana.archetypes -DarchetypeArtifactId=scp-cf-tomee -DarchetypeVersion=RELEASE
Once the generation process has started, Maven will ask you for the usual module parameters:
The result should look like this:
Note: ‘artifactId’ can also be adjusted later via the manifest.yml!
Try to make yourself familiar with the code. For more info please see this tutorial.
Modify file application/pom.xml
...
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.11.755</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.755</version>
</dependency>
...
Replace file application/src/main/java/…/HelloWorldServlet.java
package com.mycompany.cloud;
import org.slf4j.Logger;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.json.JSONObject;
import org.json.JSONArray;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.AWSCredentials;
import com.sap.cloud.sdk.cloudplatform.logging.CloudLoggerFactory;
@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
private static final Logger logger = CloudLoggerFactory.getLogger(HelloWorldServlet.class);
@Override
protected void doGet( final HttpServletRequest request, final HttpServletResponse response )
throws IOException
{
logger.info("I am running!");
response.getWriter().write("Hello World!");
//NEW MARCEL
logger.info("Reading Config...");
String vcap_services = System.getenv("VCAP_SERVICES");
String vcap_application = System.getenv("VCAP_APPLICATION");
JSONObject jsonObj = new JSONObject(vcap_services);
if(jsonObj != null)
{
if(jsonObj.getJSONArray("s3").length() > 0)
{
JSONObject first = jsonObj.getJSONArray("s3").getJSONObject(0);
String S3_BUCKET_ARN = first.getJSONObject("credentials").getString("BUCKET_ARN");
response.getWriter().write("\n");
response.getWriter().write("\n");
response.getWriter().write("S3 BUCKET ARN: " + S3_BUCKET_ARN);
AWSCredentials credentials = new BasicAWSCredentials(
first.getJSONObject("credentials").getString("S3_AWS_ACCESS_KEY_ID"),
first.getJSONObject("credentials").getString("S3_AWS_SECRET_ACCESS_KEY")
);
String region = first.getJSONObject("credentials").getString("S3_REGION");
AmazonS3 s3client = AmazonS3ClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(region)
.build();
response.getWriter().write("\n");
response.getWriter().write("\n");
response.getWriter().write("Files: ");
String bucketName = first.getJSONObject("credentials").getString("BUCKET_NAME");
ObjectListing objectListing = s3client.listObjects(bucketName);
for(S3ObjectSummary os : objectListing.getObjectSummaries()) {
response.getWriter().write("\n");
response.getWriter().write(os.getKey());
}
}
}
}
}
Run the following command to build the application. It will also run the preshipped unit and intgration tests:
$ mvn package