import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.cloudsearchdomain.model.SearchRequest;
import com.amazonaws.services.cloudsearchdomain.model.SearchResult;
import com.amazonaws.services.s3.transfer.Download;
import com.amazonaws.services.s3.transfer.Transfer.TransferState;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferProgress;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.amazonaws.services.cloudsearchdomain.AmazonCloudSearchDomainClient;
/**
* This class has functionality of accessing the Amazon web services CloudSearch and S3 and downloads the file
*/
public class AWSOps
{
String endPoint = getInstance().getProperty("ENDPOINT");
public String strZipLogFilePath=Constants.ZIPLOGFILEPATH;
String ID="";
private static final Logger log=LoggerFactory.getLogger(AWSOps.class);
/**
* This method has functionality of accessing the Amazon web services CloudSearch and S3 and downloads the metric log file
* @throws SQLException
* @param Ids
* @throws IOException
* @throws ParseException
*/
public void awsOperations(ArrayList<String> Ids) throws SQLException, IOException, ParseException
{
String ACCESS_KEY = getInstance().getProperty("AWS_ACCESS_KEY");
String SECRET_KEY = getInstance().getProperty("AWS_SECRET_KEY");
String strFileID = null;
AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
AmazonCloudSearchDomainClient domain = new AmazonCloudSearchDomainClient(credentials);
domain.setEndpoint(endPoint);
SearchRequest request=new SearchRequest();
SearchResult result=new SearchResult();
for(String ID:Ids)
{
if(!ID.contains("SomeText"))
{
request.setReturn(Constants.WHAT YOU WANT TO Return);
request.setQuery(ID);
domain.search(request);
result=domain.search(request);
String strCloudSearchResult=result.toString();
if(strCloudSearchResult.contains("some text"))
{
String arrCloudSearchResult[]=strCloudSearchResult.split(":");
for(int cloudSearchResultCounter=0;cloudSearchResultCounter<arrCloudSearchResult.length;cloudSearchResultCounter++)
{
if(arrCloudSearchResult[cloudSearchResultCounter].length()>40)
{
strFileID=arrCloudSearchResult[cloudSearchResultCounter].substring(0, 41);
}
}
TransferManager transferManager=null;
try
{
File fileZipLog = new File(strZipLogFilePath);
if(!fileZipLog.exists())
{
fileZipLog.createNewFile();
}
else
{
fileZipLog.delete();
fileZipLog.createNewFile();
}
transferManager=new TransferManager(credentials);
Download download= transferManager.download("Some text", "Some path on S3"+strFileID.trim()+"Some file on S3", fileZipLog);
TransferProgress progress=download.getProgress();
Double percentage=progress.getPercentTransferred();
for(int i=0;i<=100&&percentage!=100;i++)
{
TransferState state=download.getState();
percentage=progress.getPercentTransferred();
if(((!download.isDone())&& i==100))
{
i=0;
}
if(download.isDone())
{
if ("COMPLETED".equals(state.name()))
{
transferManager.shutdownNow();
break;
}
}
Thread.sleep(1000);
}
}
catch (Exception e)
{
log.error("In AWSOps | awsOperations Method:"+e);
throw new RuntimeException(e);
}
break;
}
else
{
log.info("In AWSOps | awsOperations Method: (ZipLogFile is not available)");
continue;
}
}
}
}
}