Changeset 4980
- Timestamp:
- 04/15/14 11:54:34 (10 years ago)
- Location:
- vlo/branches/vlo-3.0/vlo-web-app/src
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/HandleClientImpl.java
r4979 r4980 17 17 package eu.clarin.cmdi.vlo.service.impl; 18 18 19 import com.sun.jersey.api.client.Client; 20 import com.sun.jersey.api.client.ClientResponse; 21 import com.sun.jersey.api.client.WebResource; 19 22 import eu.clarin.cmdi.vlo.service.HandleClient; 23 import javax.ws.rs.core.MediaType; 24 import org.apache.wicket.ajax.json.JSONArray; 25 import org.apache.wicket.ajax.json.JSONException; 26 import org.apache.wicket.ajax.json.JSONObject; 27 import org.slf4j.Logger; 28 import org.slf4j.LoggerFactory; 20 29 21 30 /** 31 * Service that connects to the handle.net REST API and retrieves the URL for a 32 * given handle 22 33 * 23 34 * @author twagoo … … 25 36 public class HandleClientImpl implements HandleClient { 26 37 38 private final static Logger logger = LoggerFactory.getLogger(HandleClientImpl.class); 39 40 private final String handleApiBaseUrl; 41 42 /** 43 * constructs a client with the default handle REST API base URL 44 */ 45 public HandleClientImpl() { 46 //TODO: get from config 47 this("http://hdl.handle.net/api/handles/"); 48 } 49 50 /** 51 * 52 * @param handleApiBaseUrl base URL of the handle REST API (handle will be 53 * directly appended to this) 54 */ 55 public HandleClientImpl(String handleApiBaseUrl) { 56 this.handleApiBaseUrl = handleApiBaseUrl; 57 } 58 59 /** 60 * 61 * @param handle handle to resolve 62 * @return the ULR provided by the handle server or null if it could not be 63 * retrieved or is not available 64 */ 27 65 @Override 28 66 public String getUrl(String handle) { 29 //TODO: Implement using jersey client 30 return "http://www.resolve.com/file.txt"; 67 final String requestUrl = handleApiBaseUrl + handle; 68 logger.debug("Making request to {}", requestUrl); 69 70 final Client client = Client.create(); 71 final WebResource resource = client.resource(requestUrl); 72 final ClientResponse response = resource.accept(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class); 73 74 if (response.getClientResponseStatus() != ClientResponse.Status.OK) { 75 logger.error("Unexpected response status {} for {}", response.getClientResponseStatus(), requestUrl); 76 return null; 77 } else { 78 final String responseString = response.getEntity(String.class); 79 try { 80 return getUrlFromJson(responseString); 81 } catch (JSONException ex) { 82 logger.error("Could not parse Handle API response", ex); 83 return null; 84 } 85 } 86 31 87 } 32 88 89 public String getUrlFromJson(final String jsonString) throws JSONException { 90 // The handle API returns a JSON structure with a number of handle 91 // record fields. We are only interested in the value at 92 // values[x].data where values[x].type == 'URL' 93 94 final JSONObject jsonResponse = new JSONObject(jsonString); 95 final JSONArray valuesArray = jsonResponse.getJSONArray("values"); 96 for (int i = 0; i < valuesArray.length(); i++) { 97 final JSONObject object = valuesArray.getJSONObject(i); 98 final String type = object.getString("type"); 99 if ("URL".equals(type)) { 100 // the field we were looking for 101 return object.getString("data"); 102 } 103 } 104 // no URL field?? 105 logger.error("Handle API response did not incude a URL field"); 106 return null; 107 } 108 109 /** 110 * { 111 * "responseCode":1, "handle":"1839/00-0000-0000-0000-0000-4", "values": [{ 112 * "index":6, "type":"FILETIME", "data":"2014-03-25 08:35:11.0", 113 * "ttl":86400, "timestamp":"1970-01-01T00:00:00Z" }, { "index":5, 114 * "type":"CHECKSUM", "data":"696d818e19744f9f0290125e6385fa77", 115 * "ttl":86400, "timestamp":"1970-01-01T00:00:00Z" }, { "index":4, 116 * "type":"ONSITE", "data":"true", "ttl":86400, 117 * "timestamp":"1970-01-01T00:00:00Z" }, { "index":3, "type":"FILESIZE", 118 * "data":"6550", "ttl":86400, "timestamp":"1970-01-01T00:00:00Z"}, 119 * {"index":2,"type":"CRAWLTIME","data":"2014-03-25 120 * 08:35:11.39","ttl":86400,"timestamp":"1970-01-01T00:00:00Z"}, 121 * {"index":1,"type":"URL","data":"http://corpus1.mpi.nl/IMDI/metadata/IMDI.imdi","ttl":86400,"timestamp":"1970-01-01T00:00:00Z"}, 122 * {"index":100,"type":"HS_ADMIN","data":{"format":"admin","value":{"handle":"0.NA/1839","index":200,"permissions":"010001110000"}},"ttl":86400,"timestamp":"1970-01-01T00:00:00Z"} 123 * ] } 124 */ 33 125 } -
vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/UriResolverImpl.java
r4979 r4980 21 21 import eu.clarin.cmdi.vlo.service.HandleClient; 22 22 import eu.clarin.cmdi.vlo.service.UriResolver; 23 import org.slf4j.Logger; 24 import org.slf4j.LoggerFactory; 23 25 24 26 /** … … 31 33 */ 32 34 public class UriResolverImpl implements UriResolver { 35 36 private final static Logger logger = LoggerFactory.getLogger(HandleClientImpl.class); 33 37 34 38 private final HandleClient handleClient; … … 45 49 return uri; 46 50 } else { 47 return handleClient.getUrl(handle); 51 logger.debug("Calling handle client to resolve handle [{}]", uri); 52 final String resolved = handleClient.getUrl(handle); 53 if (resolved == null) { 54 return uri; 55 } else { 56 return resolved; 57 } 48 58 } 49 59
Note: See TracChangeset
for help on using the changeset viewer.