Skip to main content
Published: February 25 2010, 10:07:00 PMUpdated: November 28 2020, 8:15:15 PM

 File Transfer download XML Java Sample

 Detailed Description

   This sample application is designed to illustrate the use of Java bacic i/o and net libraries to make LMS File Transfer downloadFile XML call.  File Transafer downloadFile call can be used with Bulk Data Exchange API or with Trading GetCategorySpecifics API, and you can refer File Transfer API reference for the details about the same.

   PREREQUISITES  
      Must have obtained the task and file reference IDs from Trading  GetCategorySpecifics API or from Bulk Data Exchange API download job.

/*
© 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();
    }
     /*
     * read the response from the given input stream
     */  

    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();
    }
}

 


 

How well did this answer your question?
Answers others found helpful