Changeset 5497
- Timestamp:
- 07/29/14 15:25:48 (10 years ago)
- 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 14 14 15 15 import com.sun.jersey.api.client.Client; 16 import com.sun.jersey.api.client.ClientHandlerException; 16 17 import com.sun.jersey.api.client.ClientResponse; 18 import com.sun.jersey.api.client.UniformInterfaceException; 17 19 import com.sun.jersey.api.client.WebResource; 18 20 import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; … … 24 26 /** 25 27 * Registering new handles at handle server or modifying existing PID entries 26 * 28 * 27 29 * @author Thomas Eckart 28 30 */ 29 31 public 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-]+)\">.*");32 32 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-]+)\">.*"); 37 35 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); 42 40 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); 44 44 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 } 50 51 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); 60 56 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); 65 59 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 } 69 65 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 } 73 72 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 } 84 77 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 } 93 88 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 } 96 123 } -
VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/de/uni_leipzig/asv/clarin/webservices/pidservices2/interfaces/PidWriter.java
r5482 r5497 15 15 */ 16 16 public 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; 17 31 /** 18 32 * 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 9 9 import java.util.EnumMap; 10 10 import java.util.Map; 11 import java.util.UUID; 11 12 import org.apache.commons.httpclient.HttpException; 12 13 import org.slf4j.Logger; … … 54 55 final Map<HandleField, String> fieldMap = createPIDFieldMap(vc); 55 56 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); 57 59 return new PersistentIdentifier(vc, PersistentIdentifier.Type.HANDLE, pid); 58 60 } catch (HttpException ex) { -
VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/test/java/eu/clarin/cmdi/virtualcollectionregistry/pid/EPICPersistentIdentifierProviderTest.java
r5416 r5497 54 54 hasEntry(HandleField.TITLE, "VC Name"), 55 55 hasEntry(HandleField.CREATOR, "Joe Unit") 56 ) 56 ), 57 startsWith("VCR-") 57 58 ); 58 will(returnValue("9999/ 1234-567"));59 will(returnValue("9999/VCR-1234-567")); 59 60 } 60 61 }); 61 62 62 63 PersistentIdentifier result = instance.createIdentifier(vc); 63 assertEquals("9999/ 1234-567", result.getIdentifier());64 assertEquals("9999/VCR-1234-567", result.getIdentifier()); 64 65 assertEquals(Type.HANDLE, result.getType()); 65 66 }
Note: See TracChangeset
for help on using the changeset viewer.