1 package com.melloware.jukes.ws;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Collection;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import javax.xml.parsers.DocumentBuilder;
12 import javax.xml.parsers.DocumentBuilderFactory;
13
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList;
17
18 import com.melloware.jukes.util.MessageUtil;
19 import com.melloware.jukes.exception.WebServiceException;
20 import com.melloware.jukes.gui.tool.Resources;
21
22
23
24
25
26
27
28
29
30
31
32 @SuppressWarnings("unchecked")
33 public final class AmazonSearch {
34 private static final Log LOG = LogFactory.getLog(AmazonSearch.class);
35
36
37 public static final String SORT_TITLE = "titlerank";
38 public static final String SORT_ARTIST = "artistrank";
39 public static final String SORT_DATE = "orig-rel-date";
40
41
42 private static final String SEARCH_INDEX = "Music";
43
44
45 private static final String RG_IMAGES = "Images";
46 private static final String RG_ITEMS = "ItemAttributes";
47 private static final String RG_TRACKS = "Tracks";
48
49 private static final String AWS_ACCESS_KEY_ID = "AKIAJWPLM2OQCW444Q5A";
50
51
52
53
54
55 private static final String AWS_SECRET_KEY = "MrhTu2j6htEUEh+i1iLpJkuqwatnefZ+K3hI4hWf";
56
57
58
59
60 private AmazonSearch() {
61 super();
62 }
63
64
65
66
67
68
69
70
71
72 public static Collection findItemsByDisc(String aDiscTitle, String endPoint) throws WebServiceException {
73 return findItemsByArtistDiscSort("", aDiscTitle, endPoint, SORT_TITLE);
74 }
75
76
77
78
79
80
81
82
83
84 public static Collection findItemsByArtist(String aArtistName, String endPoint) throws WebServiceException {
85 return findItemsByArtistDiscSort(aArtistName, "", endPoint, SORT_TITLE);
86 }
87
88
89
90
91
92
93
94
95
96 public static Collection findItemsByArtistDisc(String aArtistName, String aDiscTitle, String endPoint) throws WebServiceException {
97 return findItemsByArtistDiscSort(aArtistName, aDiscTitle, endPoint, SORT_TITLE);
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public static Collection findItemsByArtistDiscSort(String aArtistName, String aDiscTitle, String endPoint, String aSort)
117 throws WebServiceException {
118 if (LOG.isDebugEnabled()) {
119 LOG.debug("Amazon findItemsByArtistDiscSort: " + aArtistName + " - " + aDiscTitle + " - " + aSort);
120 }
121
122 SignedRequestsHelper helper;
123 String requestUrl = null;
124 Collection collection = null;
125
126 try {
127 helper = SignedRequestsHelper.getInstance(endPoint, AWS_ACCESS_KEY_ID, AWS_SECRET_KEY);
128
129 Map<String, String> params = new HashMap<String, String>();
130 params.put("Service", "AWSECommerceService");
131 params.put("Version", "2009-03-31");
132 params.put("Operation", "ItemSearch");
133 params.put("SearchIndex", SEARCH_INDEX);
134 params.put("Artist", aArtistName);
135 params.put("Title", aDiscTitle);
136 params.put("ResponseGroup", RG_IMAGES + "," + RG_ITEMS + "," + RG_TRACKS);
137 params.put("Sort", aSort);
138
139 requestUrl = helper.sign(params);
140 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
141 DocumentBuilder db = dbf.newDocumentBuilder();
142 Document doc = db.parse(requestUrl);
143
144 NodeList errorList = doc.getElementsByTagName("Error");
145 String errorMessage = null;
146 if (errorList.getLength() != 0) {
147 Node n = errorList.item(0);
148 final NodeList nList = n.getChildNodes();
149 if (nList.getLength() != 0) {
150 for (int i=0; i<nList.getLength(); i=i+1){
151 Node node = nList.item(i);
152 if (node.getNodeName().equalsIgnoreCase("Message")) {
153 errorMessage = node.getTextContent();
154 }
155 }
156 }
157 MessageUtil.showInformation(null, Resources.getString("label.AmazonResponse") + errorMessage);
158 } else {
159
160 NodeList nodeList = doc.getElementsByTagName("Item");
161 if (nodeList.getLength()==0) {
162 MessageUtil.showInformation(null, Resources.getString("messages.NoItemsFound"));
163 }
164
165 collection = loadItems(nodeList);
166 }
167 } catch (NullPointerException e) {
168 LOG.error(e.getMessage(), e);
169 throw new WebServiceException(e);
170 } catch (Exception ex) {
171 LOG.error(ex.getMessage(), ex);
172 throw new WebServiceException(ex);
173 }
174
175 return collection;
176 }
177
178
179
180
181
182
183
184 private static Collection loadItems(NodeList aItems) {
185 ArrayList collection = new ArrayList();
186 if (aItems != null) {
187 for (int i=0; i<aItems.getLength(); i=i+1){
188 collection.add(new AmazonItem(aItems.item(i)));
189 }
190 }
191 return collection;
192 }
193
194 }