Changeset 5497


Ignore:
Timestamp:
07/29/14 15:25:48 (10 years ago)
Author:
Twan Goosen
Message:

Updated the PidWriter? (part of the Leipzig PID client library) to support custom PIDs. Adapted the provider that wraps this and is used by the VCR to call this new method. Fixes #596

Location:
VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/de/uni_leipzig/asv/clarin/webservices/pidservices2/impl/PidWriterImpl.java

    r5482 r5497  
    1414
    1515import com.sun.jersey.api.client.Client;
     16import com.sun.jersey.api.client.ClientHandlerException;
    1617import com.sun.jersey.api.client.ClientResponse;
     18import com.sun.jersey.api.client.UniformInterfaceException;
    1719import com.sun.jersey.api.client.WebResource;
    1820import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
     
    2426/**
    2527 * Registering new handles at handle server or modifying existing PID entries
    26  * 
     28 *
    2729 * @author Thomas Eckart
    2830 */
    2931public class PidWriterImpl implements PidWriter {
    30         private final static Logger LOG = LoggerFactory.getLogger(PidWriterImpl.class);
    31         private static final Pattern PID_OUTPUT_PATTERN = Pattern.compile(".*location</dt><dd><a href=\"([0-9A-Z-]+)\">.*");
    3232
    33         @Override
    34         public String registerNewPID(final Configuration configuration, Map<HandleField, String> fieldMap)
    35                         throws HttpException {
    36                 LOG.debug("Try to create handle at " + configuration.getServiceBaseURL() + " with values: " + fieldMap);
     33    private final static Logger LOG = LoggerFactory.getLogger(PidWriterImpl.class);
     34    private static final Pattern PID_OUTPUT_PATTERN = Pattern.compile(".*location</dt><dd><a href=\"([0-9A-z-]+)\">.*");
    3735
    38                 final Client client = Client.create();
    39                 client.addFilter(new HTTPBasicAuthFilter(configuration.getUser(), configuration.getPassword()));
    40                 final WebResource webResource = client.resource(configuration.getServiceBaseURL()
    41                                 + configuration.getHandlePrefix());
     36    @Override
     37    public String registerNewPID(final Configuration configuration, Map<HandleField, String> fieldMap, String pid)
     38            throws HttpException {
     39        LOG.debug("Try to create handle {} at {} with values: {}", pid, configuration.getServiceBaseURL(), fieldMap);
    4240
    43                 JSONArray jsonArray = createJSONArray(fieldMap);
     41        final String baseUrl = String.format("%s%s/%s",
     42                configuration.getServiceBaseURL(), configuration.getHandlePrefix(), pid);
     43        final WebResource.Builder resourceBuilder = createResourceBuilder(configuration, baseUrl);
    4444
    45                 final ClientResponse response = webResource.accept("application/json").type("application/json")
    46                                 .post(ClientResponse.class, jsonArray.toString());
    47                 if (response.getStatus() != 201) {
    48                         throw new HttpException("" + response.getStatus());
    49                 }
     45        final JSONArray jsonArray = createJSONArray(fieldMap);
     46        final ClientResponse response = resourceBuilder
     47                .header("If-None-Match", "*")
     48                .put(ClientResponse.class, jsonArray.toString());
     49        return processCreateResponse(response, configuration);
     50    }
    5051
    51                 // TODO CHANGE this ASAP, when GWDG respects accept header
    52                 String responseString = response.getEntity(String.class).trim().replaceAll("\n", "");
    53                 Matcher matcher = PID_OUTPUT_PATTERN.matcher(responseString);
    54                 if (matcher.matches())
    55                         return configuration.getHandlePrefix() + "/" + matcher.group(1);
    56                 else {
    57                         throw new RuntimeException("Unparsable response from " + configuration.getServiceBaseURL());
    58                 }
    59         }
     52    @Override
     53    public String registerNewPID(final Configuration configuration, Map<HandleField, String> fieldMap)
     54            throws HttpException {
     55        LOG.debug("Try to create handle at {} with values: {}", configuration.getServiceBaseURL(), fieldMap);
    6056
    61         @Override
    62         public void modifyPid(final Configuration configuration, final String pid, Map<HandleField, String> fieldMap) {
    63                 LOG.debug("Try to modify handle \"" + pid + "\" at " + configuration.getServiceBaseURL() + " with new values: "
    64                                 + fieldMap);
     57        final String baseUrl = configuration.getServiceBaseURL() + configuration.getHandlePrefix();
     58        final WebResource.Builder resourceBuilder = createResourceBuilder(configuration, baseUrl);
    6559
    66                 final Client client = Client.create();
    67                 client.addFilter(new HTTPBasicAuthFilter(configuration.getUser(), configuration.getPassword()));
    68                 final WebResource webResource = client.resource(configuration.getServiceBaseURL() + pid);
     60        final JSONArray jsonArray = createJSONArray(fieldMap);
     61        final ClientResponse response = resourceBuilder
     62                .post(ClientResponse.class, jsonArray.toString());
     63        return processCreateResponse(response, configuration);
     64    }
    6965
    70                 JSONArray jsonArray = createJSONArray(fieldMap);
    71                 webResource.accept("application/json").type("application/json").put(ClientResponse.class, jsonArray.toString());
    72         }
     66    private WebResource.Builder createResourceBuilder(final Configuration configuration, final String baseUrl) {
     67        final Client client = Client.create();
     68        client.addFilter(new HTTPBasicAuthFilter(configuration.getUser(), configuration.getPassword()));
     69        final WebResource.Builder resourceBuilder = client.resource(baseUrl).accept("application/json").type("application/json");
     70        return resourceBuilder;
     71    }
    7372
    74         /**
    75          * Generates JSON array that is understood by the EPIC handle service
    76          *
    77          * @param fieldMap
    78          *            mapping handle field -> value
    79          * @return JSON array
    80          */
    81         private JSONArray createJSONArray(Map<HandleField, String> fieldMap) {
    82                 JSONArray jsonArray = new JSONArray();
    83                 JSONObject jsonObject;
     73    private String processCreateResponse(final ClientResponse response, final Configuration configuration) throws HttpException, UniformInterfaceException, RuntimeException, ClientHandlerException {
     74        if (response.getStatus() != 201) {
     75            throw new HttpException("" + response.getStatus());
     76        }
    8477
    85                 Iterator<HandleField> fieldIter = fieldMap.keySet().iterator();
    86                 while (fieldIter.hasNext()) {
    87                         jsonObject = new JSONObject();
    88                         HandleField handleFieldTyp = fieldIter.next();
    89                         jsonObject.put("type", handleFieldTyp);
    90                         jsonObject.put("parsed_data", fieldMap.get(handleFieldTyp));
    91                         jsonArray.add(jsonObject);
    92                 }
     78        // TODO CHANGE this ASAP, when GWDG respects accept header
     79        String responseString = response.getEntity(String.class).trim().replaceAll("\n", "");
     80        Matcher matcher = PID_OUTPUT_PATTERN.matcher(responseString);
     81        if (matcher.matches()) {
     82            return configuration.getHandlePrefix() + "/" + matcher.group(1);
     83        } else {
     84            LOG.error("No PID found in response string: {}", responseString);
     85            throw new RuntimeException("Unparsable response from " + configuration.getServiceBaseURL());
     86        }
     87    }
    9388
    94                 return jsonArray;
    95         }
     89    @Override
     90    public void modifyPid(final Configuration configuration, final String pid, Map<HandleField, String> fieldMap) {
     91        LOG.debug("Try to modify handle \"" + pid + "\" at " + configuration.getServiceBaseURL() + " with new values: "
     92                + fieldMap);
     93
     94        final Client client = Client.create();
     95        client.addFilter(new HTTPBasicAuthFilter(configuration.getUser(), configuration.getPassword()));
     96        final WebResource webResource = client.resource(configuration.getServiceBaseURL() + pid);
     97
     98        JSONArray jsonArray = createJSONArray(fieldMap);
     99        webResource.accept("application/json").type("application/json").put(ClientResponse.class, jsonArray.toString());
     100    }
     101
     102    /**
     103     * Generates JSON array that is understood by the EPIC handle service
     104     *
     105     * @param fieldMap mapping handle field -> value
     106     * @return JSON array
     107     */
     108    private JSONArray createJSONArray(Map<HandleField, String> fieldMap) {
     109        JSONArray jsonArray = new JSONArray();
     110        JSONObject jsonObject;
     111
     112        Iterator<HandleField> fieldIter = fieldMap.keySet().iterator();
     113        while (fieldIter.hasNext()) {
     114            jsonObject = new JSONObject();
     115            HandleField handleFieldTyp = fieldIter.next();
     116            jsonObject.put("type", handleFieldTyp);
     117            jsonObject.put("parsed_data", fieldMap.get(handleFieldTyp));
     118            jsonArray.add(jsonObject);
     119        }
     120
     121        return jsonArray;
     122    }
    96123}
  • VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/de/uni_leipzig/asv/clarin/webservices/pidservices2/interfaces/PidWriter.java

    r5482 r5497  
    1515 */
    1616public interface PidWriter {
     17   
     18        /**
     19         * Try to register a new PID at handle server. Returns registered handle if successful.
     20         *
     21         * @param configuration
     22         * @param fieldMap
     23         *            handle mapping field -> value
     24         * @param pid PID to register
     25         * @return registered handle identifier
     26         * @throws HTTPException if the handle could not be created, possibly
     27         * because the ID already exists at the server
     28         */
     29        public String registerNewPID(final Configuration configuration, Map<HandleField, String> fieldMap, String pid)
     30            throws HttpException;
    1731        /**
    1832         * Try to register a new PID at handle server. Returns registered handle if successful.
  • VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/pid/EPICPersistentIdentifierProvider.java

    r5416 r5497  
    99import java.util.EnumMap;
    1010import java.util.Map;
     11import java.util.UUID;
    1112import org.apache.commons.httpclient.HttpException;
    1213import org.slf4j.Logger;
     
    5455        final Map<HandleField, String> fieldMap = createPIDFieldMap(vc);
    5556        try {
    56             final String pid = pidWriter.registerNewPID(configuration, fieldMap);
     57            final String requestedPid = "VCR-" + UUID.randomUUID().toString();
     58            final String pid = pidWriter.registerNewPID(configuration, fieldMap, requestedPid);
    5759            return new PersistentIdentifier(vc, PersistentIdentifier.Type.HANDLE, pid);
    5860        } catch (HttpException ex) {
  • VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/test/java/eu/clarin/cmdi/virtualcollectionregistry/pid/EPICPersistentIdentifierProviderTest.java

    r5416 r5497  
    5454                                hasEntry(HandleField.TITLE, "VC Name"),
    5555                                hasEntry(HandleField.CREATOR, "Joe Unit")
    56                         )
     56                        ),
     57                        startsWith("VCR-")
    5758                );
    58                 will(returnValue("9999/1234-567"));
     59                will(returnValue("9999/VCR-1234-567"));
    5960            }
    6061        });
    6162
    6263        PersistentIdentifier result = instance.createIdentifier(vc);
    63         assertEquals("9999/1234-567", result.getIdentifier());
     64        assertEquals("9999/VCR-1234-567", result.getIdentifier());
    6465        assertEquals(Type.HANDLE, result.getType());
    6566    }
Note: See TracChangeset for help on using the changeset viewer.