List of usage examples for com.amazonaws.auth PropertiesCredentials PropertiesCredentials
public PropertiesCredentials(InputStream inputStream) throws IOException
From source file:getting_started.Requests.java
License:Open Source License
/** * The only information needed to create a client are security credentials * consisting of the AWS Access Key ID and Secret Access Key. All other * configuration, such as the service endpoints, are performed * automatically. Client parameters, such as proxies, can be specified in an * optional ClientConfiguration object when constructing a client. * * @see com.amazonaws.auth.BasicAWSCredentials * @see com.amazonaws.auth.PropertiesCredentials * @see com.amazonaws.ClientConfiguration *//*from ww w . j av a 2 s .c o m*/ private void init() throws Exception { AWSCredentials credentials = new PropertiesCredentials( InlineGettingStartedCodeSampleApp.class.getResourceAsStream("AwsCredentials.properties")); ec2 = new AmazonEC2Client(credentials); }
From source file:getting_started.SimpleQueueServiceSample.java
License:Open Source License
public static void main(String[] args) throws Exception { /*/*from w ww .j a v a 2 s. com*/ * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this * sample. * http://aws.amazon.com/security-credentials */ AmazonSQS sqs = new AmazonSQSClient(new PropertiesCredentials( SimpleQueueServiceSample.class.getResourceAsStream("AwsCredentials.properties"))); System.out.println("==========================================="); System.out.println("Getting Started with Amazon SQS"); System.out.println("===========================================\n"); try { // Create a queue System.out.println("Creating a new SQS queue called MyQueue.\n"); CreateQueueRequest createQueueRequest = new CreateQueueRequest("MyQueue"); String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl(); // List queues System.out.println("Listing all queues in your account.\n"); for (String queueUrl : sqs.listQueues().getQueueUrls()) { System.out.println(" QueueUrl: " + queueUrl); } System.out.println(); // Send a message System.out.println("Sending a message to MyQueue.\n"); sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my message text.")); // Receive messages System.out.println("Receiving messages from MyQueue.\n"); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl); List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); for (Message message : messages) { System.out.println(" Message"); System.out.println(" MessageId: " + message.getMessageId()); System.out.println(" ReceiptHandle: " + message.getReceiptHandle()); System.out.println(" MD5OfBody: " + message.getMD5OfBody()); System.out.println(" Body: " + message.getBody()); for (Entry<String, String> entry : message.getAttributes().entrySet()) { System.out.println(" Attribute"); System.out.println(" Name: " + entry.getKey()); System.out.println(" Value: " + entry.getValue()); } } System.out.println(); // Delete a message System.out.println("Deleting a message.\n"); String messageRecieptHandle = messages.get(0).getReceiptHandle(); sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageRecieptHandle)); // Delete a queue System.out.println("Deleting the test queue.\n"); sqs.deleteQueue(new DeleteQueueRequest(myQueueUrl)); } 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()); } }
From source file:hydrograph.engine.spark.datasource.utils.AWSS3Util.java
License:Apache License
public void upload(RunFileTransferEntity runFileTransferEntity) { log.debug("Start AWSS3Util upload"); int retryAttempt = 0; int i;//from w w w. j av a2 s. com java.nio.file.Path inputfile = new File(runFileTransferEntity.getLocalPath()).toPath(); String keyName = inputfile.getFileName().toString(); log.info("keyName is: " + keyName); log.info("bucket name is:" + runFileTransferEntity.getBucketName()); log.info("Folder Name is" + runFileTransferEntity.getFolder_name_in_bucket()); String amazonFileUploadLocationOriginal = null; FileInputStream stream = null; File filecheck = new File(runFileTransferEntity.getLocalPath()); if (runFileTransferEntity.getFailOnError()) if (!(filecheck.isFile() || filecheck.isDirectory()) && !(runFileTransferEntity.getLocalPath().contains("hdfs://"))) { Log.error("Invalid local path.Please provide valid path"); throw new AWSUtilException("Invalid local path"); } if (runFileTransferEntity.getRetryAttempt() == 0) retryAttempt = 1; else retryAttempt = runFileTransferEntity.getRetryAttempt(); for (i = 0; i < retryAttempt; i++) { log.info("connection attempt: " + (i + 1)); try { AmazonS3 s3Client = null; ClientConfiguration clientConf = new ClientConfiguration(); clientConf.setProtocol(Protocol.HTTPS); if (runFileTransferEntity.getCrediationalPropertiesFile() == null) { BasicAWSCredentials creds = new BasicAWSCredentials(runFileTransferEntity.getAccessKeyID(), runFileTransferEntity.getSecretAccessKey()); s3Client = AmazonS3ClientBuilder.standard().withClientConfiguration(clientConf) .withRegion(runFileTransferEntity.getRegion()) .withCredentials(new AWSStaticCredentialsProvider(creds)).build(); } else { File securityFile = new File(runFileTransferEntity.getCrediationalPropertiesFile()); PropertiesCredentials creds = new PropertiesCredentials(securityFile); s3Client = AmazonS3ClientBuilder.standard().withClientConfiguration(clientConf) .withRegion(runFileTransferEntity.getRegion()) .withCredentials(new AWSStaticCredentialsProvider(creds)).build(); } String s3folderName = null; String filepath = runFileTransferEntity.getFolder_name_in_bucket(); log.debug("file path name" + filepath); s3folderName = filepath; if (s3folderName != null && !s3folderName.trim().equals("")) { amazonFileUploadLocationOriginal = runFileTransferEntity.getBucketName() + "/" + s3folderName; } else { amazonFileUploadLocationOriginal = runFileTransferEntity.getBucketName(); } File f = new File(runFileTransferEntity.getLocalPath()); if (runFileTransferEntity.getLocalPath().contains("hdfs://")) { log.debug("Provided HDFS local path "); String inputPath = runFileTransferEntity.getLocalPath(); String s1 = inputPath.substring(7, inputPath.length()); String s2 = s1.substring(0, s1.indexOf("/")); File file = new File("/tmp"); if (!file.exists()) file.mkdir(); Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://" + s2); FileSystem hdfsFileSystem = FileSystem.get(conf); Path local = new Path("/tmp"); String s = inputPath.substring(7, inputPath.length()); String hdfspath = s.substring(s.indexOf("/"), s.length()); Path hdfs = new Path(hdfspath); ObjectMetadata objectMetadata = new ObjectMetadata(); if (runFileTransferEntity.getEncoding() != null) objectMetadata.setContentEncoding(runFileTransferEntity.getEncoding()); File dir = new File(hdfspath); if (hdfsFileSystem.isDirectory(new Path(hdfspath))) { InputStream is = null; OutputStream os = null; String localDirectory = hdfspath.substring(hdfspath.lastIndexOf("/") + 1); FileStatus[] fileStatus = hdfsFileSystem .listStatus(new Path(runFileTransferEntity.getLocalPath())); Path[] paths = FileUtil.stat2Paths(fileStatus); File dirs = null; try { String folderName = hdfspath.substring(hdfspath.lastIndexOf("/") + 1); DateFormat df = new SimpleDateFormat("dd-MM-yyyy"); String dateWithoutTime = df.format(new Date()).toString(); Random ran = new Random(); String tempFolder = "ftp_sftp_" + System.nanoTime() + "_" + ran.nextInt(1000); dirs = new File("/tmp/" + tempFolder); boolean success = dirs.mkdirs(); for (Path files : paths) { is = hdfsFileSystem.open(files); os = new BufferedOutputStream(new FileOutputStream(dirs + "/" + files.getName())); org.apache.hadoop.io.IOUtils.copyBytes(is, os, conf); } for (File files : dirs.listFiles()) { if (files.isFile()) { s3Client.putObject(new PutObjectRequest( amazonFileUploadLocationOriginal + "/" + folderName, files.getName(), files)); } } } catch (IOException e) { Log.error("IOException occured while transfering the file", e); } finally { org.apache.hadoop.io.IOUtils.closeStream(is); org.apache.hadoop.io.IOUtils.closeStream(os); if (dirs != null) { FileUtils.deleteDirectory(dirs); } } } else { hdfsFileSystem.copyToLocalFile(false, hdfs, local); stream = new FileInputStream("/tmp/" + f.getName()); File S3file = new File("/tmp/" + f.getName()); PutObjectRequest putObjectRequest = new PutObjectRequest(amazonFileUploadLocationOriginal, keyName, file); PutObjectResult result = s3Client.putObject(putObjectRequest); } } else { ObjectMetadata objectMetadata = new ObjectMetadata(); if (runFileTransferEntity.getEncoding() != null) objectMetadata.setContentEncoding(runFileTransferEntity.getEncoding()); if (Files.isDirectory(inputfile)) { File fileloc = new File(inputfile.toAbsolutePath().toString()); String folderName = new File(runFileTransferEntity.getLocalPath()).getName(); for (File files : fileloc.listFiles()) { if (files.isFile()) { PutObjectRequest putObjectRequest = new PutObjectRequest( amazonFileUploadLocationOriginal + "/" + folderName, files.getName(), files); PutObjectResult result = s3Client.putObject(putObjectRequest); } } } else { PutObjectRequest putObjectRequest = null; File file = new File(runFileTransferEntity.getLocalPath()); stream = new FileInputStream(runFileTransferEntity.getLocalPath()); putObjectRequest = new PutObjectRequest(amazonFileUploadLocationOriginal, keyName, file); PutObjectResult result = s3Client.putObject(putObjectRequest); } } } catch (AmazonServiceException e) { if (e.getStatusCode() == 403 || e.getStatusCode() == 404) { if (runFileTransferEntity.getFailOnError()) Log.error("Incorrect details provided.Please provide valid details", e); throw new AWSUtilException("Incorrect details provided"); } { try { Thread.sleep(runFileTransferEntity.getRetryAfterDuration()); } catch (Exception e1) { Log.error("Exception occured while sleeping the thread"); } continue; } } catch (Exception e) { log.error("error while transferring file", e); try { Thread.sleep(runFileTransferEntity.getRetryAfterDuration()); } catch (Exception e1) { Log.error("Exception occured while sleeping the thread"); } continue; } catch (Error err) { Log.error("Error occured while uploading the file", err); throw new AWSUtilException(err); } done = true; break; } if (runFileTransferEntity.getFailOnError() && !done) { log.error("File transfer failed"); throw new AWSUtilException("File transfer failed"); } else if (!done) { log.error("File transfer failed but mentioned fail on error as false"); } if (i == runFileTransferEntity.getRetryAttempt()) { if (runFileTransferEntity.getFailOnError()) throw new AWSUtilException("File transfer failed"); } log.debug("Finished AWSS3Util upload"); }
From source file:hydrograph.engine.spark.datasource.utils.AWSS3Util.java
License:Apache License
public void download(RunFileTransferEntity runFileTransferEntity) { log.debug("Start AWSS3Util download"); File filecheck = new File(runFileTransferEntity.getLocalPath()); if (runFileTransferEntity.getFailOnError()) if (!(filecheck.exists() && filecheck.isDirectory()) && !(runFileTransferEntity.getLocalPath().contains("hdfs://"))) { throw new AWSUtilException("Invalid local path"); }//from ww w . j ava2s. c o m boolean fail_if_exist = false; int retryAttempt = 0; int i; String amazonFileUploadLocationOriginal = null; String keyName = null; if (runFileTransferEntity.getRetryAttempt() == 0) retryAttempt = 1; else retryAttempt = runFileTransferEntity.getRetryAttempt(); for (i = 0; i < retryAttempt; i++) { log.info("connection attempt: " + (i + 1)); try { AmazonS3 s3Client = null; ClientConfiguration clientConf = new ClientConfiguration(); clientConf.setProtocol(Protocol.HTTPS); if (runFileTransferEntity.getCrediationalPropertiesFile() == null) { BasicAWSCredentials creds = new BasicAWSCredentials(runFileTransferEntity.getAccessKeyID(), runFileTransferEntity.getSecretAccessKey()); s3Client = AmazonS3ClientBuilder.standard().withClientConfiguration(clientConf) .withRegion(runFileTransferEntity.getRegion()) .withCredentials(new AWSStaticCredentialsProvider(creds)).build(); } else { File securityFile = new File(runFileTransferEntity.getCrediationalPropertiesFile()); PropertiesCredentials creds = new PropertiesCredentials(securityFile); s3Client = AmazonS3ClientBuilder.standard().withClientConfiguration(clientConf) .withRegion(runFileTransferEntity.getRegion()) .withCredentials(new AWSStaticCredentialsProvider(creds)).build(); } String s3folderName = null; String filepath = runFileTransferEntity.getFolder_name_in_bucket(); if (filepath.lastIndexOf("/") != -1) { s3folderName = filepath.substring(0, filepath.lastIndexOf("/")); keyName = filepath.substring(filepath.lastIndexOf("/") + 1); } else { keyName = filepath; } log.debug("keyName is: " + keyName); log.debug("bucket name is:" + runFileTransferEntity.getBucketName()); log.debug("Folder Name is" + runFileTransferEntity.getFolder_name_in_bucket()); if (s3folderName != null) { amazonFileUploadLocationOriginal = runFileTransferEntity.getBucketName() + "/" + s3folderName; } else { amazonFileUploadLocationOriginal = runFileTransferEntity.getBucketName(); } if (runFileTransferEntity.getLocalPath().contains("hdfs://")) { String outputPath = runFileTransferEntity.getLocalPath(); String s1 = outputPath.substring(7, outputPath.length()); String s2 = s1.substring(0, s1.indexOf("/")); File f = new File("/tmp"); if (!f.exists()) f.mkdir(); GetObjectRequest request = new GetObjectRequest(amazonFileUploadLocationOriginal, keyName); S3Object object = s3Client.getObject(request); if (runFileTransferEntity.getEncoding() != null) object.getObjectMetadata().setContentEncoding(runFileTransferEntity.getEncoding()); File fexist = new File(runFileTransferEntity.getLocalPath() + File.separatorChar + keyName); if (runFileTransferEntity.getOverwrite().trim().equalsIgnoreCase("Overwrite If Exists")) { S3ObjectInputStream objectContent = object.getObjectContent(); IOUtils.copyLarge(objectContent, new FileOutputStream("/tmp/" + keyName)); } else { if (!(fexist.exists() && !fexist.isDirectory())) { S3ObjectInputStream objectContent = object.getObjectContent(); IOUtils.copyLarge(objectContent, new FileOutputStream( runFileTransferEntity.getLocalPath() + File.separatorChar + keyName)); } else { fail_if_exist = true; Log.error("File already exists"); throw new AWSUtilException("File already exists"); } } Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://" + s2); FileSystem hdfsFileSystem = FileSystem.get(conf); String s = outputPath.substring(7, outputPath.length()); String hdfspath = s.substring(s.indexOf("/"), s.length()); Path local = new Path("/tmp/" + keyName); Path hdfs = new Path(hdfspath); hdfsFileSystem.copyFromLocalFile(local, hdfs); } else { GetObjectRequest request = new GetObjectRequest(amazonFileUploadLocationOriginal, keyName); S3Object object = s3Client.getObject(request); if (runFileTransferEntity.getEncoding() != null) object.getObjectMetadata().setContentEncoding(runFileTransferEntity.getEncoding()); File fexist = new File(runFileTransferEntity.getLocalPath() + File.separatorChar + keyName); if (runFileTransferEntity.getOverwrite().trim().equalsIgnoreCase("Overwrite If Exists")) { S3ObjectInputStream objectContent = object.getObjectContent(); IOUtils.copyLarge(objectContent, new FileOutputStream( runFileTransferEntity.getLocalPath() + File.separatorChar + keyName)); } else { if (!(fexist.exists() && !fexist.isDirectory())) { S3ObjectInputStream objectContent = object.getObjectContent(); IOUtils.copyLarge(objectContent, new FileOutputStream( runFileTransferEntity.getLocalPath() + File.separatorChar + keyName)); } else { fail_if_exist = true; Log.error("File already exists"); throw new AWSUtilException("File already exists"); } } } } catch (AmazonServiceException e) { log.error("Amazon Service Exception", e); if (e.getStatusCode() == 403 || e.getStatusCode() == 404) { if (runFileTransferEntity.getFailOnError()) { Log.error("Incorrect details provided.Please provide correct details", e); throw new AWSUtilException("Incorrect details provided"); } else { Log.error("Unknown amezon exception occured", e); } } { try { Thread.sleep(runFileTransferEntity.getRetryAfterDuration()); } catch (Exception e1) { Log.error("Exception occured while sleeping the thread"); } continue; } } catch (Error e) { Log.error("Error occured while sleeping the thread"); throw new AWSUtilException(e); } catch (Exception e) { log.error("error while transfering file", e); try { Thread.sleep(runFileTransferEntity.getRetryAfterDuration()); } catch (Exception e1) { } catch (Error err) { Log.error("Error occured while downloading"); throw new AWSUtilException(err); } continue; } done = true; break; } if (runFileTransferEntity.getFailOnError() && !done) { log.error("File transfer failed"); throw new AWSUtilException("File transfer failed"); } else if (!done) { log.error("File transfer failed but mentioned fail on error as false"); } if (i == runFileTransferEntity.getRetryAttempt()) { if (runFileTransferEntity.getFailOnError()) { throw new AWSUtilException("File transfer failed"); } } log.debug("Finished AWSS3Util download"); }
From source file:io.starter.messaging.SNSMobilePush.java
License:Open Source License
public static void main(String[] args) throws IOException { /*/* ww w. jav a2 s.c o m*/ * TODO: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this sample. * http://aws.amazon.com/security-credentials */ String awsAccessKey = System.getProperty("AWS_ACCESS_KEY_ID"); // "YOUR_AWS_ACCESS_KEY"; String awsSecretKey = System.getProperty("AWS_SECRET_KEY"); // "YOUR_AWS_SECRET_KEY"; if (awsAccessKey == null) awsAccessKey = AWS_ACCESS_KEY; if (awsSecretKey == null) awsSecretKey = AWS_SECRET_KEY; AWSCredentials credentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey); AmazonSNS sns = new AmazonSNSClient( new PropertiesCredentials(SNSMobilePush.class.getResourceAsStream("AwsCredentials.properties"))); // AmazonSNS sns = new AmazonSNSClient(credentials); sns.setEndpoint("https://sns.us-west-2.amazonaws.com"); System.out.println("===========================================\n"); System.out.println("Initializing Communication with Amazon SNS"); System.out.println("===========================================\n"); try { SNSMobilePush sample = new SNSMobilePush(sns); /* TODO: Uncomment the services you wish to use. */ // sample.starterAndroidAppNotification(); // sample.starterKindleAppNotification(); sample.starterAppleAppNotification(); // sample.starterAppleSandboxAppNotification(); // sample.starterBaiduAppNotification(); // sample.starterWNSAppNotification(); // sample.starterMPNSAppNotification(); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SNS, 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 SNS, such as not " + "being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:it.polimi.modaclouds.cpimlibrary.blobmng.AmazonBlobManagerFactory.java
License:Apache License
@Override public CloudBlobManager createCloudBlobManager() { AWSCredentials credentials = null;//from w w w . j ava 2 s. c o m try { credentials = new PropertiesCredentials( getClass().getClassLoader().getResourceAsStream("AwsCredentials.properties")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return new AmazonBlobManager(new AmazonS3Client(credentials)); }
From source file:it.polimi.modaclouds.cpimlibrary.mailservice.AmazonMailManager.java
License:Apache License
public AmazonMailManager(CloudMetadata metadata) { this.username = metadata.getUsernameMail(); this.credentials = null; try {//from ww w . j av a2s. c o m credentials = new PropertiesCredentials( getClass().getClassLoader().getResourceAsStream("AwsCredentials.properties")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.ses = new AmazonSimpleEmailServiceClient(this.credentials); verifyEmailAddress(this.username); }
From source file:it.polimi.modaclouds.cpimlibrary.memcache.AmazonMemcache.java
License:Apache License
private void start(String memcacheAddr) { AWSCredentials credentials = null;/*from w ww.java2 s .c o m*/ try { credentials = new PropertiesCredentials( getClass().getClassLoader().getResourceAsStream("AwsCredentials.properties")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ec = new AmazonElastiCacheClient(credentials); DescribeCacheClustersRequest describe_req = new DescribeCacheClustersRequest(); describe_req.setShowCacheNodeInfo(true); DescribeCacheClustersResult describe_res = ec.describeCacheClusters(describe_req); List<CacheCluster> clusters = describe_res.getCacheClusters(); for (CacheCluster c : clusters) { Endpoint conf_end = c.getConfigurationEndpoint(); String addr = conf_end.getAddress() + ":" + conf_end.getPort(); if (addr.equals(memcacheAddr)) { cluster = c; break; } } //setting security groups // List<String> sec_groups_names = new ArrayList<String>(); // List<CacheSecurityGroup> res = ec.describeCacheSecurityGroups().getCacheSecurityGroups(); // for(int i = 0; i < res.size(); i++) { // if(res.get(i).getDescription().contains("Cloud Platform Independent Model")) // sec_groups_names.add(res.get(i).getCacheSecurityGroupName()); // } // ModifyCacheClusterRequest req = new ModifyCacheClusterRequest(cluster.getCacheClusterId()); // req.setCacheSecurityGroupNames(sec_groups_names); // req.setApplyImmediately(true); // ec.modifyCacheCluster(req); //creation of memcached client List<InetSocketAddress> addrs = new ArrayList<InetSocketAddress>(); if (cluster != null && cluster.getCacheNodes() != null) { for (CacheNode node : cluster.getCacheNodes()) { if (node != null) { Endpoint endpoint = node.getEndpoint(); if (endpoint != null && endpoint.getAddress() != null) { addrs.add(new InetSocketAddress(endpoint.getAddress(), endpoint.getPort())); } } } } if (addrs.size() > 0) { try { System.out.println("Creation of memCacheClient."); memCache = new MemcachedClient(addrs); System.out.println("memCacheClient created."); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else System.out.println("No available nodes."); }
From source file:it.polimi.modaclouds.cpimlibrary.msgqueuemng.AmazonMessageQueueFactory.java
License:Apache License
public AmazonMessageQueueFactory(CloudMetadata metadata) { AWSCredentials credentials = null;/* w ww.ja v a2 s . c o m*/ try { credentials = new PropertiesCredentials( getClass().getClassLoader().getResourceAsStream("AwsCredentials.properties")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.sqs = new AmazonSQSClient(credentials); this.info = metadata.getQueueMedatada(); }
From source file:it.polimi.modaclouds.cpimlibrary.taskqueuemng.AmazonInternalWorker.java
License:Apache License
public void run() { //queue = (AmazonTaskQueue) MF.getFactory().getTaskQueueFactory().getQueue(queueName); AWSCredentials credentials = null;// w w w. j a va 2s . c o m try { credentials = new PropertiesCredentials( getClass().getClassLoader().getResourceAsStream("AwsCredentials.properties")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.sqs = new AmazonSQSClient(credentials); queue = new AmazonTaskQueue(queueName, sqs); AmazonTaskQueueMessage msg = null; while (true) { System.out.println("Iteration.."); msg = queue.getMessage(); if (msg != null) { System.out.println("Message found"); final AmazonTaskQueueMessage aqmsg = msg; new Thread() { ///////////////////////////////////////THREAD //////////////////////////// public void run() { System.out.println("Run"); handleTask(aqmsg); System.out.println("Finish"); //this.interrupt(); } private void handleTask(AmazonTaskQueueMessage msg) { info = parserMessage(msg.getMessageText()); String parameters = ""; boolean firstIt = true; for (String key : info.getParameters().keySet()) { if (firstIt) { parameters = parameters + "?"; firstIt = false; } else { parameters = parameters + "&"; } parameters = parameters + key + "=" + info.getParameters().get(key); } System.out.println("Starting servlet."); //creo l'url URI host = URI.create("http://localhost:8080/"); String url = "http://" + host.getHost() + ":" + host.getPort() + host.getPath() + info.getServletUri().getPath() + parameters; System.out.println(url); URL iurl = null; HttpURLConnection uc = null; //DataOutputStream out = null; BufferedReader rd = null; try { iurl = new URL(url); uc = (HttpURLConnection) iurl.openConnection(); uc.setDoOutput(true); uc.setRequestMethod(info.getMethod().toUpperCase()); //uc.addRequestProperty("Content-Length", file_length.toString()); //out = new DataOutputStream(uc.getOutputStream()); //out.write(file_content, 0, file_length); //out.flush(); //out.close(); //uc.setReadTimeout(file_length/50); //System.out.println("Set timeout: " + uc.getReadTimeout() + "ms."); InputStream is = uc.getInputStream(); rd = new BufferedReader(new InputStreamReader(is)); String line; while ((line = rd.readLine()) != null) { System.out.println("Response: " + line); } rd.close(); System.out.println("Response code: " + uc.getResponseCode()); if (uc.getResponseCode() == 500) { ; ; } } catch (RuntimeException e) { // TODO Auto-generated catch block System.out.println("RuntimeException error!!"); System.out.println("------"); e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block System.out.println("MalformedURLException error!!"); System.out.println("------"); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("IOException error!!"); System.out.println("------"); e.printStackTrace(); } System.out.println("End of servlet."); /* try { StringBuffer sb = new StringBuffer(); sb.append(info.getMethod() + " " + info.getHostUri() + info.getServletUri() + parameters + " HTTP/1.1\r\n"); sb.append(HttpHeader.HOST + ": " + info.getHostUri().getHost() + ":" + info.getHostUri().getPort() + "\r\n"); sb.append(HttpHeader.CONNECTION + ": " + HttpHeader.CONNECTION_CLOSE + "\r\n"); sb.append("\r\n"); String request = sb.toString(); System.out .println("********************REQUEST !!!!!************************"); System.out.println(request); System.out .println("********************END REQUEST !!!!!!!!************************"); SocketFactory socketFactory = SocketFactory.getDefault(); InetAddress ip = InetAddress.getByName(info.getHostUri().getHost()); Socket socket = socketFactory.createSocket(ip, info.getHostUri() .getPort()); OutputStreamWriter osw = new OutputStreamWriter( socket.getOutputStream()); int requestLength = request.length(); BufferedWriter bw = new BufferedWriter(osw, requestLength); bw.write(request); bw.flush(); InputStreamReader isr = new InputStreamReader( socket.getInputStream()); BufferedReader br = new BufferedReader(isr, 8192); @SuppressWarnings("unused") HttpResult result = buildResult(HttpMethod.POST, br); br.close(); isr.close(); socket.close(); */ int i = 0; Boolean deleted = queue.deleteMessage(msg); while (!deleted && i < NUM_ATTEMPT_DELETE) { i++; deleted = queue.deleteMessage(msg); } if (!deleted) try { throw new CloudTaskQueueException( "Deletion of Message " + msg.getMessageId() + " failed!!"); } catch (CloudTaskQueueException e) { // TODO Auto-generated catch block e.printStackTrace(); } else System.out.println("Message " + msg.getMessageId() + "Deleted at attempt number " + i); /* } catch (Exception e) { new Diagnostics("Exception during queue management:" + e.getMessage()).save(); } */ } private CloudTask parserMessage(String messageText) { CloudTask t = new CloudTask(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db; try { db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(messageText)); Document doc = db.parse(is); Element root = doc.getDocumentElement(); //String uri = getTextValue(root, "URL"); //t.setHostUri(uri); String uriS = getTextValue(root, "SERVLET"); t.setServletUri(uriS); String method = getTextValue(root, "METHOD"); t.setMethod(method); NodeList par = root.getElementsByTagName("PARAMETER"); for (int i = 0; i < par.getLength(); i++) { Element elPar = (Element) par.item(i); String key = getTextValue(elPar, "KEY"); String value = getTextValue(elPar, "VALUE"); t.setParameters(key, value); } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return t; } private String getTextValue(Element ele, String tagName) { String textVal = null; NodeList nl = ele.getElementsByTagName(tagName); if (nl != null && nl.getLength() > 0) { Element el = (Element) nl.item(0); textVal = el.getFirstChild().getNodeValue(); } return textVal; } ///////////////////////////// THREAD /////////////////////////////////// }.start(); } System.out.println("Sleeping.."); try { Thread.sleep(queueInfo.getRate()); } catch (InterruptedException e) { e.printStackTrace(); } } }