/*
© 2010-2013 eBay Inc., All Rights Reserved
Licensed under CDDL 1.0 - http://opensource.org/licenses/cddl1.php
*/
import java.io.*;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
/**
*/
public class LMS_DownloadFile {
private String USERTOKEN = "YOUR TOKEN";
private String SERVERURL = "https://storage.sandbox.ebay.com/FileTransferService";
private String ATTACHMENT_CONTENTID = "Content-ID: <urn:uuid:.*>";
private String BOUNDARY = "--MIMEBoundaryurn_uuid_";
public static void main(String[] args) {
//INSERT YOUR jobId and refId here
String jobId = "50000060955";
String refId = "50000068125";
new LMS_DownloadFile(). download(jobId, refId, "download3.gzip");
}
public LMS_DownloadFile(String serverURL, String userToken) {
this.USERTOKEN = userToken;
this.SERVERURL = serverURL;
}
public LMS_DownloadFile() {
}
/*
* 1. send download api request for the given jobID and refId
* 2. write the downloaded to the given file downloadFileName
*/
private void download(String jobId, String refId, String downloadFileName) {
String reqXml = setRequestXml(jobId, refId);
String response = "";
try {
InputStream is = openConnectionFileRequest(reqXml);
response = getResponseAsString(is);
FileOutputStream fos = new FileOutputStream(downloadFileName);
fos.write(getAttachment(response).getBytes("ISO-8859-1"));
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* open SSL https connection and send the POST request
*/
private InputStream openConnectionFileRequest(String Xml) throws Exception {
URL url = new URL(SERVERURL);
// open SSL https connection
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
// conn.setRequestProperty("Content-Type", "text/xml");
// set the required http headers conn.setRequestProperty("X-EBAY-SOA-OPERATION-NAME", "downloadFile");
conn.setRequestProperty("X-EBAY-SOA-SECURITY-TOKEN", USERTOKEN);
PrintWriter output = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
System.out.println(conn.getURL());
System.out.println("REQUEST : " + Xml);
output.println(Xml);
output.close();
conn.connect();
return conn.getInputStream();
}
private String getResponseAsString(InputStream is) {
StringBuffer buffer = new StringBuffer();
char[] buf = new char[4096];
int len;
try {
InputStreamReader isr = new InputStreamReader(is, "ISO-8859-1");
while ((len = isr.read(buf)) >= 0) {
buffer.append(buf, 0, len);
}
} catch (Exception e) {
}
System.out.println("RESPONSE_BODY: " + buffer.toString());
return buffer.toString();
}
public String[] getMultiParts(String str) {
String[] temp = null;
temp = str.split(BOUNDARY, -1);
return temp;
}
/*
* get attachment part for the given content */
private String getAttachment(String responseBody) {
String[] parts = getMultiParts(responseBody);
String attachment = parts[2];
String[] temp = null;
temp = attachment.split(ATTACHMENT_CONTENTID, -1);
return temp[1].substring(4, temp[1].length());
}
/*
* build request xml payload for given jobId and refId */
private String setRequestXml(String jobId, String refId) {
StringBuffer requestXML = new StringBuffer("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
requestXML.append("<downloadFileRequest xmlns=\"http://www.ebay.com/marketplace/services\">");
requestXML.append("<RequesterCredentials><eBayAuthToken>"+USERTOKEN+"</eBayAuthToken></RequesterCredentials>");
requestXML.append("<fileReferenceId>" + refId + "</fileReferenceId>");
requestXML.append("<taskReferenceId>" + jobId + "</taskReferenceId>");
requestXML.append("</downloadFileRequest>");
return requestXML.toString();
}
}
|