Changeset 4980


Ignore:
Timestamp:
04/15/14 11:54:34 (10 years ago)
Author:
Twan Goosen
Message:

Implemented client implementation that uses the handle.net REST API to resolve a handle to a URL

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  
    1717package eu.clarin.cmdi.vlo.service.impl;
    1818
     19import com.sun.jersey.api.client.Client;
     20import com.sun.jersey.api.client.ClientResponse;
     21import com.sun.jersey.api.client.WebResource;
    1922import eu.clarin.cmdi.vlo.service.HandleClient;
     23import javax.ws.rs.core.MediaType;
     24import org.apache.wicket.ajax.json.JSONArray;
     25import org.apache.wicket.ajax.json.JSONException;
     26import org.apache.wicket.ajax.json.JSONObject;
     27import org.slf4j.Logger;
     28import org.slf4j.LoggerFactory;
    2029
    2130/**
     31 * Service that connects to the handle.net REST API and retrieves the URL for a
     32 * given handle
    2233 *
    2334 * @author twagoo
     
    2536public class HandleClientImpl implements HandleClient {
    2637
     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     */
    2765    @Override
    2866    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
    3187    }
    3288
     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     */
    33125}
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/UriResolverImpl.java

    r4979 r4980  
    2121import eu.clarin.cmdi.vlo.service.HandleClient;
    2222import eu.clarin.cmdi.vlo.service.UriResolver;
     23import org.slf4j.Logger;
     24import org.slf4j.LoggerFactory;
    2325
    2426/**
     
    3133 */
    3234public class UriResolverImpl implements UriResolver {
     35
     36    private final static Logger logger = LoggerFactory.getLogger(HandleClientImpl.class);
    3337
    3438    private final HandleClient handleClient;
     
    4549            return uri;
    4650        } 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            }
    4858        }
    4959
Note: See TracChangeset for help on using the changeset viewer.