| 12355 |
vikram.rag |
1 |
package com.amazonaws.mws.samples;
|
|
|
2 |
|
|
|
3 |
import java.io.BufferedReader;
|
|
|
4 |
import java.io.BufferedWriter;
|
|
|
5 |
import java.io.FileInputStream;
|
|
|
6 |
import java.io.FileNotFoundException;
|
|
|
7 |
import java.io.FileReader;
|
|
|
8 |
import java.io.FileWriter;
|
|
|
9 |
import java.io.IOException;
|
|
|
10 |
import java.security.DigestInputStream;
|
|
|
11 |
import java.security.MessageDigest;
|
|
|
12 |
import java.security.NoSuchAlgorithmException;
|
|
|
13 |
import java.util.Arrays;
|
|
|
14 |
|
|
|
15 |
import org.apache.commons.codec.binary.Base64;
|
|
|
16 |
|
|
|
17 |
import com.amazonaws.mws.MarketplaceWebService;
|
|
|
18 |
import com.amazonaws.mws.MarketplaceWebServiceClient;
|
|
|
19 |
import com.amazonaws.mws.MarketplaceWebServiceConfig;
|
|
|
20 |
import com.amazonaws.mws.MarketplaceWebServiceException;
|
|
|
21 |
import com.amazonaws.mws.model.IdList;
|
|
|
22 |
import com.amazonaws.mws.model.SubmitFeedRequest;
|
|
|
23 |
import com.amazonaws.mws.model.SubmitFeedResponse;
|
|
|
24 |
|
|
|
25 |
public class PromotionFeed {
|
|
|
26 |
public String CreatePromotionFeed() throws IOException{
|
|
|
27 |
BufferedReader reader = null;
|
|
|
28 |
BufferedWriter writer = null;
|
|
|
29 |
String promotionFilename = null;
|
|
|
30 |
try {
|
|
|
31 |
promotionFilename = "/home/vikram/Desktop/Promotion-"+System.currentTimeMillis()+".txt";
|
|
|
32 |
reader = new BufferedReader(new FileReader("/home/vikram/Desktop/Promotion-Template.txt"));
|
|
|
33 |
writer = new BufferedWriter(new FileWriter(promotionFilename));
|
|
|
34 |
String Line;
|
|
|
35 |
while ((Line = reader.readLine()) != null) {
|
|
|
36 |
writer.write(Line+"\n");
|
|
|
37 |
}
|
|
|
38 |
for(int i=1;i<=2;i++){
|
|
|
39 |
writer.write("sku"+"\t"+
|
|
|
40 |
""+"\t"
|
|
|
41 |
+""+"\t"
|
|
|
42 |
+""+"\t"
|
|
|
43 |
+""+"\t"
|
|
|
44 |
+""+"\t"
|
|
|
45 |
+"\t"
|
|
|
46 |
+"\t"
|
|
|
47 |
+"\t"
|
|
|
48 |
+"\t"
|
|
|
49 |
+"PartialUpdate"+"\t"
|
|
|
50 |
+"Standard-Price"+"\t"
|
|
|
51 |
+""+"\t"
|
|
|
52 |
+""+"\t"
|
|
|
53 |
+""+"\t"
|
|
|
54 |
+""+"\t"
|
|
|
55 |
+"Max-Order-Qty"+"\t"
|
|
|
56 |
+"Sale-Price"+"\t"
|
|
|
57 |
+"St-Date"+"\t"
|
|
|
58 |
+"End-Date"+"\t"
|
|
|
59 |
+""+"\t"+"\n");
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
} catch (FileNotFoundException e1) {
|
|
|
63 |
e1.printStackTrace();
|
|
|
64 |
}
|
|
|
65 |
finally{
|
|
|
66 |
writer.close();
|
|
|
67 |
reader.close();
|
|
|
68 |
System.out.println("Done");
|
|
|
69 |
}
|
|
|
70 |
return promotionFilename;
|
|
|
71 |
}
|
|
|
72 |
public String SubmitPromotionFeed(String file) throws FileNotFoundException, InterruptedException, MarketplaceWebServiceException{
|
|
|
73 |
MarketplaceWebService service = getMarketplaceServiceInstance();
|
|
|
74 |
SubmitFeedRequest requestPromotion = new SubmitFeedRequest();
|
|
|
75 |
requestPromotion.setMerchant("AF6E3O0VE0X4D");
|
|
|
76 |
requestPromotion.setMarketplaceIdList(new IdList(Arrays.asList("A21TJRUUN4KGV")));
|
|
|
77 |
requestPromotion.setFeedType("_POST_FLAT_FILE_LISTINGS_DATA_");
|
|
|
78 |
FileInputStream promotionfis = new FileInputStream(file);
|
|
|
79 |
requestPromotion.setContentMD5(computeContentMD5HeaderValue(promotionfis));
|
|
|
80 |
requestPromotion.setFeedContent(promotionfis);
|
|
|
81 |
return invokeSubmitFeed(service,requestPromotion);
|
|
|
82 |
}
|
|
|
83 |
public MarketplaceWebService getMarketplaceServiceInstance(){
|
|
|
84 |
final String accessKeyId = "AKIAII3SGRXBJDPCHSGQ";
|
|
|
85 |
final String secretAccessKey = "B92xTbNBTYygbGs98w01nFQUhbec1pNCkCsKVfpg";
|
|
|
86 |
final String appName = "Test";
|
|
|
87 |
final String appVersion = "1.0";
|
|
|
88 |
MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
|
|
|
89 |
config.setServiceURL("https://mws.amazonservices.in");
|
|
|
90 |
return new MarketplaceWebServiceClient(
|
|
|
91 |
accessKeyId, secretAccessKey, appName, appVersion, config);
|
|
|
92 |
}
|
|
|
93 |
public static String computeContentMD5HeaderValue(FileInputStream fis) {
|
|
|
94 |
try {
|
|
|
95 |
DigestInputStream dis = new DigestInputStream(fis,
|
|
|
96 |
MessageDigest.getInstance("MD5"));
|
|
|
97 |
byte[] buffer = new byte[8192];
|
|
|
98 |
while (dis.read(buffer) > 0)
|
|
|
99 |
;
|
|
|
100 |
String md5Content = new String(Base64.encodeBase64(dis
|
|
|
101 |
.getMessageDigest().digest()));
|
|
|
102 |
// Effectively resets the stream to be beginning of the file via a
|
|
|
103 |
fis.getChannel().position(0);
|
|
|
104 |
return md5Content;
|
|
|
105 |
} catch (NoSuchAlgorithmException e) {
|
|
|
106 |
e.printStackTrace();
|
|
|
107 |
} catch (IOException e) {
|
|
|
108 |
e.printStackTrace();
|
|
|
109 |
}
|
|
|
110 |
return null;
|
|
|
111 |
}
|
|
|
112 |
public String invokeSubmitFeed(MarketplaceWebService service,SubmitFeedRequest request) throws InterruptedException, MarketplaceWebServiceException {
|
|
|
113 |
SubmitFeedResponse response = service.submitFeed(request);
|
|
|
114 |
return response.getSubmitFeedResult().getFeedSubmissionInfo().getFeedSubmissionId();
|
|
|
115 |
}
|
|
|
116 |
}
|