Java tutorial
package se252.jan15.calvinandhobbes.project0; /* * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.model.DeleteMessageRequest; import com.amazonaws.services.sqs.model.Message; import com.amazonaws.services.sqs.model.ReceiveMessageRequest; import com.amazonaws.services.sqs.model.SendMessageRequest; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; /** * This sample demonstrates how to make basic requests to Amazon SQS using the * AWS SDK for Java. * <p> * <b>Prerequisites:</b> You must have a valid Amazon Web * Services developer account, and be signed up to use Amazon SQS. For more * information on Amazon SQS, see http://aws.amazon.com/sqs. * <p> * Fill in your AWS access credentials in the provided credentials file * template, and be sure to move the file to the default location * (~/.aws/credentials) where the sample code will load the credentials from. * <p> * <b>WARNING:</b> To avoid accidental leakage of your credentials, DO NOT keep * the credentials file in your source directory. */ @Path("dataProxy") public class IIScCampusMapGETProxyService { private static LoadingCache<String, String> categoryCache = null; public static void cacheInit() { categoryCache = CacheBuilder.newBuilder().maximumSize(7) // maximum 7 records can be cached .expireAfterAccess(10, TimeUnit.MINUTES) // cache will expire after 10 minutes of access .build(new CacheLoader<String, String>() { // build the cacheloader @Override public String load(String category) throws Exception { return getCategoryData(category); } }); } private static String getCategoryData(String category) { AmazonSQS queueClient = Queues.getQueue(); String resp = null; try { // Send a message queueClient.sendMessage(new SendMessageRequest(Queues.req, category)); // Receive messages Boolean flag = true; while (flag) { ReceiveMessageRequest receiveReq = new ReceiveMessageRequest(Queues.resp); receiveReq.setWaitTimeSeconds(10); List<Message> messages = queueClient.receiveMessage(receiveReq).getMessages(); for (Message message : messages) { String[] strs = message.getBody().split("\\$"); if (strs[0].equals(category)) { flag = false; resp = strs[1]; queueClient .deleteMessage(new DeleteMessageRequest(Queues.resp, message.getReceiptHandle())); break; } } } } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SQS, 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()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with SQS, such as not " + "being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } return resp; } @GET @Produces(MediaType.APPLICATION_JSON) public Response IIScDataProxyServer(@DefaultValue("AllCategories") @QueryParam("category") String category) { String resp = null; try { resp = categoryCache.get(category); } catch (ExecutionException e) { e.printStackTrace(); } return Response.ok(resp, MediaType.APPLICATION_JSON).header("Access-Control-Allow-Origin", "*").build(); } }