1 | package de.uni_leipzig.asv.clarin.webservices.pidservices2.impl; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.util.ArrayList; |
---|
5 | import java.util.HashMap; |
---|
6 | import java.util.Iterator; |
---|
7 | import java.util.List; |
---|
8 | import java.util.Map; |
---|
9 | |
---|
10 | import javax.ws.rs.core.MultivaluedMap; |
---|
11 | |
---|
12 | import net.sf.json.JSONArray; |
---|
13 | |
---|
14 | import org.slf4j.Logger; |
---|
15 | import org.slf4j.LoggerFactory; |
---|
16 | |
---|
17 | import com.sun.jersey.api.client.Client; |
---|
18 | import com.sun.jersey.api.client.ClientResponse; |
---|
19 | import com.sun.jersey.api.client.WebResource; |
---|
20 | import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; |
---|
21 | import com.sun.jersey.core.util.MultivaluedMapImpl; |
---|
22 | |
---|
23 | import de.uni_leipzig.asv.clarin.webservices.pidservices2.Configuration; |
---|
24 | import de.uni_leipzig.asv.clarin.webservices.pidservices2.HandleField; |
---|
25 | import de.uni_leipzig.asv.clarin.webservices.pidservices2.PidObject; |
---|
26 | import de.uni_leipzig.asv.clarin.webservices.pidservices2.interfaces.PidResolver; |
---|
27 | |
---|
28 | /** |
---|
29 | * Requests information about handle from handle server |
---|
30 | * |
---|
31 | * @author Thomas Eckart |
---|
32 | */ |
---|
33 | public class PidResolverImpl implements PidResolver { |
---|
34 | private final static Logger LOG = LoggerFactory.getLogger(PidResolverImpl.class); |
---|
35 | |
---|
36 | @Override |
---|
37 | public JSONArray resolvePidAsJSON(final Configuration configuration, final String pid) throws IOException { |
---|
38 | LOG.debug("Searching for \"" + pid + "\" at " + configuration.getServiceBaseURL()); |
---|
39 | |
---|
40 | final Client client = Client.create(); |
---|
41 | client.addFilter(new HTTPBasicAuthFilter(configuration.getUser(), configuration.getPassword())); |
---|
42 | final WebResource webResource = client.resource(configuration.getServiceBaseURL() + pid); |
---|
43 | |
---|
44 | // query |
---|
45 | final ClientResponse clientResponse = webResource.accept("application/json;charset=UTF-8").get( |
---|
46 | ClientResponse.class); |
---|
47 | if (clientResponse.getStatus() != 200) { |
---|
48 | throw new IOException("Received a different response than expected (200): " + clientResponse.getStatus() |
---|
49 | + " (URL: '" + webResource.toString() + "')"); |
---|
50 | } |
---|
51 | |
---|
52 | return JSONArray.fromObject(clientResponse.getEntity(String.class)); |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | public PidObject resolvePidAsPOJO(final Configuration configuration, final String pid) throws IOException { |
---|
57 | return new PidObject(pid, resolvePidAsJSON(configuration, pid)); |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | public Map<String, JSONArray> searchPidAsJSON(final Configuration configuration, Map<HandleField, String> fieldMap) |
---|
62 | throws IOException { |
---|
63 | Map<String, JSONArray> jsonArrayMap = new HashMap<String, JSONArray>(); |
---|
64 | |
---|
65 | for (String handle : searchPidAsList(configuration, fieldMap)) { |
---|
66 | jsonArrayMap.put(handle, resolvePidAsJSON(configuration, handle)); |
---|
67 | } |
---|
68 | |
---|
69 | return jsonArrayMap; |
---|
70 | } |
---|
71 | |
---|
72 | @Override |
---|
73 | public Map<String, PidObject> searchPidAsPOJO(final Configuration configuration, Map<HandleField, String> fieldMap) |
---|
74 | throws IOException { |
---|
75 | Map<String, JSONArray> jsonArrayMap = searchPidAsJSON(configuration, fieldMap); |
---|
76 | Map<String, PidObject> pidObjectsMap = new HashMap<String, PidObject>(); |
---|
77 | Iterator<String> handleIterator = jsonArrayMap.keySet().iterator(); |
---|
78 | while (handleIterator.hasNext()) { |
---|
79 | String handle = handleIterator.next(); |
---|
80 | pidObjectsMap.put(handle, new PidObject(handle, jsonArrayMap.get(handle))); |
---|
81 | } |
---|
82 | |
---|
83 | return pidObjectsMap; |
---|
84 | } |
---|
85 | |
---|
86 | @Override |
---|
87 | public List<String> searchPidAsList(final Configuration configuration, Map<HandleField, String> fieldMap) |
---|
88 | throws IOException { |
---|
89 | LOG.debug("Searching at " + configuration.getServiceBaseURL() + " with: " + fieldMap); |
---|
90 | List<String> handleList = new ArrayList<String>(); |
---|
91 | |
---|
92 | final Client client = Client.create(); |
---|
93 | client.addFilter(new HTTPBasicAuthFilter(configuration.getUser(), configuration.getPassword())); |
---|
94 | final WebResource webResource = client.resource(configuration.getServiceBaseURL() |
---|
95 | + configuration.getHandlePrefix()); |
---|
96 | |
---|
97 | // add URL parameters |
---|
98 | final MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); |
---|
99 | Iterator<HandleField> fieldTypeIterator = fieldMap.keySet().iterator(); |
---|
100 | while (fieldTypeIterator.hasNext()) { |
---|
101 | HandleField tmpFieldType = fieldTypeIterator.next(); |
---|
102 | queryParams.add(tmpFieldType.toString(), fieldMap.get(tmpFieldType)); |
---|
103 | } |
---|
104 | |
---|
105 | // query |
---|
106 | final ClientResponse response = webResource.queryParams(queryParams).accept("application/json;charset=UTF-8") |
---|
107 | .get(ClientResponse.class); |
---|
108 | if (response.getStatus() != 200) { |
---|
109 | throw new IOException("Received a different response than expected (200): " + response.getStatus() |
---|
110 | + " (URL: '" + webResource.toString() + "')"); |
---|
111 | } |
---|
112 | |
---|
113 | // parse response and get all handle fields |
---|
114 | JSONArray handleIdJSONArray = JSONArray.fromObject(response.getEntity(String.class)); |
---|
115 | for (int i = 0; i < handleIdJSONArray.size(); i++) { |
---|
116 | String handle = configuration.getHandlePrefix() + "/" + handleIdJSONArray.getString(i); |
---|
117 | handleList.add(handle); |
---|
118 | LOG.debug("Found handle " + i + "\t" + handle); |
---|
119 | } |
---|
120 | |
---|
121 | return handleList; |
---|
122 | } |
---|
123 | } |
---|