Changeset 5498
- Timestamp:
- 07/30/14 09:32:22 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/de/uni_leipzig/asv/clarin/webservices/pidservices2/impl/PidWriterImpl.java
r5497 r5498 28 28 * 29 29 * @author Thomas Eckart 30 * @author Twan Goosen 30 31 */ 31 32 public class PidWriterImpl implements PidWriter { 32 33 33 34 private final static Logger LOG = LoggerFactory.getLogger(PidWriterImpl.class); 35 public static final Pattern PID_INPUT_PATTERN = Pattern.compile("^[0-9A-z-]+$"); 34 36 private static final Pattern PID_OUTPUT_PATTERN = Pattern.compile(".*location</dt><dd><a href=\"([0-9A-z-]+)\">.*"); 35 37 38 /** 39 * 40 * @param configuration 41 * @param fieldMap 42 * @param pid PID to be created, must match {@link #PID_INPUT_PATTERN} 43 * @return 44 * @throws HttpException if the PID could not be created, for instance 45 * because the requested PID already exists 46 * @throws IllegalArgumentException if the provided PID does not match 47 * {@link #PID_INPUT_PATTERN} 48 */ 36 49 @Override 37 50 public String registerNewPID(final Configuration configuration, Map<HandleField, String> fieldMap, String pid) 38 throws HttpException {51 throws HttpException, IllegalArgumentException { 39 52 LOG.debug("Try to create handle {} at {} with values: {}", pid, configuration.getServiceBaseURL(), fieldMap); 40 53 54 // validate the requested PID 55 if (!PID_INPUT_PATTERN.matcher(pid).matches()) { 56 throw new IllegalArgumentException(pid); 57 } 58 59 // adding the PID to the request URL 41 60 final String baseUrl = String.format("%s%s/%s", 42 61 configuration.getServiceBaseURL(), configuration.getHandlePrefix(), pid); 43 final WebResource.Builder resourceBuilder = createResourceBuilder (configuration, baseUrl);62 final WebResource.Builder resourceBuilder = createResourceBuilderForNewPID(configuration, baseUrl); 44 63 45 64 final JSONArray jsonArray = createJSONArray(fieldMap); 46 65 final ClientResponse response = resourceBuilder 66 // this header will tell the server to fail if the requested PID already exists 47 67 .header("If-None-Match", "*") 68 // PUT the handle at the specified location 48 69 .put(ClientResponse.class, jsonArray.toString()); 49 70 return processCreateResponse(response, configuration); … … 56 77 57 78 final String baseUrl = configuration.getServiceBaseURL() + configuration.getHandlePrefix(); 58 final WebResource.Builder resourceBuilder = createResourceBuilder (configuration, baseUrl);79 final WebResource.Builder resourceBuilder = createResourceBuilderForNewPID(configuration, baseUrl); 59 80 60 81 final JSONArray jsonArray = createJSONArray(fieldMap); 61 82 final ClientResponse response = resourceBuilder 83 // POST the new handle definition to the handles resource 62 84 .post(ClientResponse.class, jsonArray.toString()); 63 85 return processCreateResponse(response, configuration); 64 86 } 65 87 66 private WebResource.Builder createResourceBuilder (final Configuration configuration, final String baseUrl) {88 private WebResource.Builder createResourceBuilderForNewPID(final Configuration configuration, final String baseUrl) { 67 89 final Client client = Client.create(); 68 90 client.addFilter(new HTTPBasicAuthFilter(configuration.getUser(), configuration.getPassword())); 69 final WebResource.Builder resourceBuilder = client.resource(baseUrl).accept("application/json").type("application/json"); 91 // TODO: request JSON ("application/json") as soon as GWDG respects that accept header 92 final WebResource.Builder resourceBuilder = client.resource(baseUrl).accept("application/xhtml+xml").type("application/json"); 70 93 return resourceBuilder; 71 94 } … … 76 99 } 77 100 78 // TODO CHANGE this ASAP, when GWDG respects accept header101 // TODO CHANGE this to JSON processing ASAP, when GWDG respects accept header 79 102 String responseString = response.getEntity(String.class).trim().replaceAll("\n", ""); 80 103 Matcher matcher = PID_OUTPUT_PATTERN.matcher(responseString);
Note: See TracChangeset
for help on using the changeset viewer.