DownloadPackageClient.java
From AgileApps Support Wiki
Revision as of 23:47, 20 December 2010 by imported>Aeric (Text replace - 'import static com.platform.api.Functions.*; ' to '')
- <syntaxhighlight lang="java" enclose="div">
import com.platform.api.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams;
public class DownloadPackageClient {
public static final String REST_SERVICE = "https://{domainName}/networking/RESTService"; public static final String USER_NAME = "jimacme.com"; public static final String PASSWORD = "jimacme"; public static String login() throws Exception { String sessionId = null; StringBuilder loginRequestXML = new StringBuilder("<?xml version=\"1.0\" ?>") .append(" <longjump ver=\"2.0\">") .append(" <login_request>") .append(" <login>").append(USER_NAME).append("</login>") .append(" <password>").append(PASSWORD).append("</password>") .append(" </login_request>") .append(" </longjump>"); // Create an instance of HttpClient. HttpClient client = new HttpClient(); //Create GET method PostMethod method = new PostMethod(REST_SERVICE); //add to Request method.addParameter("xml_data", loginRequestXML.toString());
// Provide custom retry handler (necessary) method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); String file = new String(responseBody); int indexOfXMLStart = file.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); int indexOfXMLEnd = file.indexOf("</longjump>"); String xmlPartOfResponse = file.substring(indexOfXMLStart, indexOfXMLEnd + 11); if(xmlPartOfResponse.contains("<session_id>")) { int sessionIdBeginTagIndx = xmlPartOfResponse.indexOf("<session_id>"); int sessionIdEndTagIndx = xmlPartOfResponse.indexOf("</session_id>") + "</session_id>".length(); sessionId = (xmlPartOfResponse.substring(sessionIdBeginTagIndx, sessionIdEndTagIndx)) .replace("<session_id>", "") .replace("</session_id>", "") .trim(); } else if (xmlPartOfResponse.contains("<error>")) { int messageBeginTagIndx = xmlPartOfResponse.indexOf("<message>"); int messageEndTagIndx = xmlPartOfResponse.indexOf("</message>") + "</message>".length(); String message = (xmlPartOfResponse.substring(messageBeginTagIndx, messageEndTagIndx)) .replace("<message>", "") .replace("</message>", "") .trim(); throw new Exception(message); } } catch (HttpException e) { e.printStackTrace(); throw new InvocationTargetException(e, "Fatal protocol violation: " + e.getMessage()); } catch (IOException e) { e.printStackTrace(); throw new InvocationTargetException(e, "Fatal transport error: " + e.getMessage()); } catch (Exception e) { e.printStackTrace(); throw new InvocationTargetException(e, e.getMessage()); } finally { // Release the connection. method.releaseConnection(); return sessionId; } }
public static void pullPackageFromRemoteServer( String userName, String password, String url, String packageId, String lastPublished, String filePath) { String sessionId = null; try { sessionId = login(); } catch(Exception e) { System.out.println(e.getMessage()); } StringBuilder downLoadPackageXML = new StringBuilder("<?xml version=\"1.0\" ?>") .append(" <longjump ver=\"2.0\">") .append(" <resource_download_request>") .append(" <session_id>").append(sessionId).append("</session_id>") .append(" <type>package</type>") .append(" <package_id>").append(packageId).append("</package_id>") .append(" <last_published_version>") .append(lastPublished).append("</last_published_version>") .append(" </resource_download_request>") .append(" </longjump>");
// Create an instance of HttpClient. HttpClient client = new HttpClient(); //Create GET method PostMethod method = new PostMethod(url); //add to Request method.addParameter("xml_data", downLoadPackageXML.toString()); // Provide custom retry handler (necessary) method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new Exception("Method failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); File targetFile = null; if ( method.getResponseHeader("Content-Type").getValue() .contains("application/octet-stream")) { String fileName = method.getResponseHeader("Content-ID").getValue(); targetFile = new File(filePath + fileName); FileOutputStream out = new FileOutputStream(targetFile.getPath()); out.write(responseBody); out.close(); System.out.println("Package download completed successfully"); } else { throw new Exception(method.getResponseBodyAsString()); } } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // Release the connection. method.releaseConnection(); } } public static void main(String args[]) { DownloadPackageClient dpc = new DownloadPackageClient(); try { String packageId = "8d5dd4c1c4ac4190911d091d0e24fba6"; String lastPublishedVersion = "false"; String filePath = "C:\\";
for(int i=0; i< 1; i++) { dpc.pullPackageFromRemoteServer( USER_NAME, PASSWORD, REST_SERVICE, packageId, lastPublishedVersion, filePath); }
} catch(Exception e) { System.out.println(e.getMessage()); } }
} </syntaxhighlight>