DownloadDocumentClient.java
From LongJump Support Wiki
Revision as of 23:44, 20 December 2010 by imported>Aeric (Text replace - 'import static com.platform.api.Functions.*; ' to '')
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 DownloadDocumentClient { public static final String REST_SERVICE = "https://{domainName}/networking/RESTService"; public static final String USER_NAME = "jim@acme.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 is 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 pullDocumentFromRemoteServer( String userName, String password, String url, String documentId, String lastPublished, String filePath) { String sessionId = null; try { sessionId = login(); } catch(Exception e) { System.out.println(e.getMessage()); } StringBuilder downLoadDocumentXML = 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>document</type>") .append(" <id>").append(documentId).append("</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", downLoadDocumentXML.toString()); // Provide custom retry handler is 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; System.out.println(method.getResponseHeader("Content-Disposition").getValue()); if(method.getResponseHeader("Content-Disposition") .getValue().contains("filename")) { String fileName = method.getResponseHeader("Content-Disposition").getValue(); fileName = fileName.replace("filename=", ""); targetFile = new File(filePath + fileName); FileOutputStream out = new FileOutputStream(targetFile.getPath()); out.write(responseBody); out.close(); System.out.println("Document download completed." ); } 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[]) { DownloadDocumentClient dpc = new DownloadDocumentClient(); try { String documentId = "4ef61b05a5f547b185af7adbdf6dd529"; String lastPublishedVersion = "false"; String filePath = "C:\\"; dpc.pullDocumentFromRemoteServer(USER_NAME, PASSWORD, REST_SERVICE, documentId, lastPublishedVersion, filePath); } catch(Exception e) { System.out.println(e.getMessage()); } } }