Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7473 vikram.rag 1
package com.amazonservices.mws.orders.samples;
2
 
3
import java.util.GregorianCalendar;
4
 
5
import javax.xml.datatype.DatatypeConfigurationException;
6
import javax.xml.datatype.DatatypeFactory;
7
import javax.xml.datatype.Duration;
8
import javax.xml.datatype.XMLGregorianCalendar;
9
 
10
import org.apache.http.HttpStatus;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
 
14
import com.amazonservices.mws.orders.MarketplaceWebServiceOrders;
15
import com.amazonservices.mws.orders.MarketplaceWebServiceOrdersClient;
16
import com.amazonservices.mws.orders.MarketplaceWebServiceOrdersException;
17
import com.amazonservices.mws.orders.model.ListOrdersByNextTokenRequest;
18
import com.amazonservices.mws.orders.model.ListOrdersByNextTokenResponse;
19
import com.amazonservices.mws.orders.model.ListOrdersByNextTokenResult;
20
import com.amazonservices.mws.orders.model.ListOrdersRequest;
21
import com.amazonservices.mws.orders.model.ListOrdersResponse;
22
import com.amazonservices.mws.orders.model.ListOrdersResult;
23
import com.amazonservices.mws.orders.model.Order;
24
import com.amazonservices.mws.orders.model.OrderList;
25
 
26
/**
27
 * Sample file that fetches orders created during a given time period.
28
 */
29
 
30
public class OrderFetcherSample {
31
 
32
    /*
33
     * Add required parameters in OrdersConfig.java before trying out this
34
     * sample.
35
     */
36
    public static final Log log = LogFactory.getLog(OrderFetcherSample.class);
37
 
38
    /*****************************************
39
     * Throttling Limits in Milliseconds
40
     *****************************************/
41
    static final long LIST_ORDERS_THROTTLE_LIMIT = 600000L; // 1 call/10 mins
42
 
43
    protected MarketplaceWebServiceOrders service;
44
 
45
    public OrderFetcherSample() {
46
        /*********************************************************************
47
         * Instantiate Http Client Implementation of Marketplace Web Service *
48
         * Orders
49
         *********************************************************************/
50
        this.service = new MarketplaceWebServiceOrdersClient(
51
                OrdersConfig.accessKeyId,
52
                OrdersConfig.secretAccessKey,
53
                OrdersConfig.applicationName,
54
                OrdersConfig.applicationVersion,
55
                OrdersConfig.config);
56
    }
57
 
58
    /**
59
     * Fetches all orders created in the given time period and processes them
60
     * locally. If end is null, it will be ignored (the MWS service will pick an
61
     * appropriate time, which is now - 2 minutes).
62
     */
63
    public void fetchOrders(XMLGregorianCalendar start, XMLGregorianCalendar end) throws MarketplaceWebServiceOrdersException {
64
        ListOrdersRequest listOrdersRequest = new ListOrdersRequest();
65
        listOrdersRequest.setSellerId(OrdersConfig.sellerId);
66
        if (OrdersConfig.marketplaceIdList != null) {
67
            listOrdersRequest
68
                    .setMarketplaceId(OrdersConfig.marketplaceIdList);
69
        }
70
        listOrdersRequest.setCreatedAfter(start);
71
        if (start == null) {
72
            throw new IllegalArgumentException("Start date cannot be null.");
73
        }
74
        if (end != null) {
75
            listOrdersRequest.setCreatedBefore(end);
76
        }
77
 
78
        try {
79
            ListOrdersResult listOrdersResult = listOrders(listOrdersRequest);
80
 
81
            if (listOrdersResult != null && listOrdersResult.isSetNextToken()) {
82
                ListOrdersByNextTokenRequest listOrdersByNextTokenRequest = new ListOrdersByNextTokenRequest();
83
                listOrdersByNextTokenRequest
84
                        .setSellerId(OrdersConfig.sellerId);
85
                String nextToken = listOrdersResult.getNextToken();
86
                ListOrdersByNextTokenResult listOrdersByNextTokenResult = null;
87
                while (nextToken != null) {
88
                    listOrdersByNextTokenRequest.setNextToken(nextToken);
89
                    listOrdersByNextTokenResult = listOrdersByNextToken(listOrdersByNextTokenRequest);
90
                    nextToken = listOrdersByNextTokenResult.getNextToken();
91
                }
92
            }
93
        } catch (MarketplaceWebServiceOrdersException ex) {
94
            System.out.println("Caught Exception: " + ex.getMessage());
95
            System.out.println("Response Status Code: " + ex.getStatusCode());
96
            System.out.println("Error Code: " + ex.getErrorCode());
97
            System.out.println("Error Type: " + ex.getErrorType());
98
            System.out.println("Request ID: " + ex.getRequestId());
99
            System.out.print("XML: " + ex.getXML());
100
            throw ex;
101
        }
102
    }
103
 
104
    private ListOrdersResult listOrders(ListOrdersRequest request)
105
            throws MarketplaceWebServiceOrdersException {
106
        boolean retry;
107
        ListOrdersResponse listOrdersResponse = null;
108
        ListOrdersResult listOrdersResult = null;
109
        do {
110
            retry = false;
111
            try {
112
                listOrdersResponse = service.listOrders(request);
113
 
114
            } catch (MarketplaceWebServiceOrdersException ex) {
115
                if (ex.getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE
116
                        && "RequestThrottled".equals(ex.getErrorCode())) {
117
                    retry = true;
118
                    requestThrottledExceptionHandler(LIST_ORDERS_THROTTLE_LIMIT);
119
                } else {
120
                    throw ex;
121
                }
122
            }
123
            if (listOrdersResponse != null
124
                    && listOrdersResponse.isSetListOrdersResult()) {
125
                listOrdersResult = listOrdersResponse.getListOrdersResult();
126
                if (listOrdersResult.isSetOrders()) {
127
                    processOrders(listOrdersResult.getOrders());
128
                }
129
            }
130
 
131
        } while (retry);
132
        return listOrdersResult;
133
    }
134
 
135
    private ListOrdersByNextTokenResult listOrdersByNextToken(
136
            ListOrdersByNextTokenRequest listOrdersByNextTokenRequest)
137
            throws MarketplaceWebServiceOrdersException {
138
        boolean retry;
139
        ListOrdersByNextTokenResponse listOrdersByNextTokenResponse = null;
140
        ListOrdersByNextTokenResult listOrdersByNextTokenResult = null;
141
        do {
142
            retry = false;
143
            try {
144
 
145
                listOrdersByNextTokenResponse = service
146
                        .listOrdersByNextToken(listOrdersByNextTokenRequest);
147
            } catch (MarketplaceWebServiceOrdersException ex) {
148
                if (ex.getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE
149
                        && "RequestThrottled".equals(ex.getErrorCode())) {
150
                    retry = true;
151
                    requestThrottledExceptionHandler(LIST_ORDERS_THROTTLE_LIMIT);
152
                } else {
153
                    throw ex;
154
                }
155
            }
156
            if (listOrdersByNextTokenResponse != null
157
                    && listOrdersByNextTokenResponse
158
                            .isSetListOrdersByNextTokenResult()) {
159
                listOrdersByNextTokenResult = listOrdersByNextTokenResponse
160
                        .getListOrdersByNextTokenResult();
161
                if (listOrdersByNextTokenResult.isSetOrders()) {
162
                    processOrders(listOrdersByNextTokenResult.getOrders());
163
                }
164
            }
165
 
166
        } while (retry);
167
        return listOrdersByNextTokenResult;
168
    }
169
 
170
    private void requestThrottledExceptionHandler(long throttlingLimit) {
171
        try {
172
            log.info("Request throttled. Sleeping for " + throttlingLimit
173
                    + " milliseconds.");
174
            Thread.sleep(throttlingLimit);
175
        } catch (InterruptedException e) {
176
            log.error(e.getMessage(), e);
177
            return;
178
        }
179
    }
180
 
181
    /*
182
     * TODO: Insert your order processing logic here.
183
     */
184
    protected void processOrders(OrderList orders) {
185
        System.out.println(orders.toString());
186
    }
187
 
188
    public static void main(String... args) {
189
 
190
        OrderFetcherSample orderFetcher = new OrderFetcherSample();
191
        DatatypeFactory df = null;
192
        try {
193
            df = DatatypeFactory.newInstance();
194
        } catch (DatatypeConfigurationException e) {
195
            log.error(e.getMessage(), e);
196
        }
197
 
198
        /******************************************
199
         * Uncomment the desired fetchOrders call *
200
         ******************************************/
201
 
202
        /* Fetch orders for the last 24 hours GMT */
203
        XMLGregorianCalendar start1 = df
204
                .newXMLGregorianCalendar(new GregorianCalendar());
205
        Duration negativeOneDay = df.newDurationDayTime(false, 0, 24, 0, 0);
206
        start1.add(negativeOneDay);
207
//        try {
208
//             orderFetcher.fetchOrders(start1, null);
209
//        } catch (MarketplaceWebServiceOrdersException e) {
210
//            log.error(e.getMessage(), e);
211
//        }
212
 
213
        /*
214
         * Fetch orders for the last quarter (Oct 1st, 2010 to Dec 31st, 2010
215
         * PST)
216
         */
217
        XMLGregorianCalendar start2 = df.newXMLGregorianCalendarDate(2010, 10,
218
                1, -480);
219
        XMLGregorianCalendar end2 = df.newXMLGregorianCalendarDate(2011, 01,
220
                01, -480);
221
//        try {
222
//            orderFetcher.fetchOrders(start2, end2);
223
//        } catch (MarketplaceWebServiceOrdersException e) {            
224
//            e.printStackTrace();
225
//        }
226
    }
227
 
228
}