use of org.mycore.pi.exceptions.MCRDatacenterException in project mycore by MyCoRe-Org.
the class MCRDataciteClient method getMediaList.
public List<Map.Entry<String, URI>> getMediaList(final MCRDigitalObjectIdentifier doiParam) throws MCRPersistentIdentifierException {
ArrayList<Map.Entry<String, URI>> entries = new ArrayList<>();
MCRDigitalObjectIdentifier doi = isTestPrefix() ? doiParam.toTestPrefix() : doiParam;
URI requestURI = getRequestURI("/media/" + doi.asString());
HttpGet httpGet = new HttpGet(requestURI);
try (CloseableHttpClient httpClient = getHttpClient()) {
CloseableHttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
switch(statusLine.getStatusCode()) {
case HttpStatus.SC_OK:
Scanner scanner = new Scanner(response.getEntity().getContent(), "UTF-8");
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split("=", 2);
String mediaType = parts[0];
URI mediaURI = new URI(parts[1]);
entries.add(new AbstractMap.SimpleEntry<>(mediaType, mediaURI));
}
return entries;
case HttpStatus.SC_UNAUTHORIZED:
throw new MCRDatacenterAuthenticationException();
case HttpStatus.SC_NOT_FOUND:
throw new MCRIdentifierUnresolvableException(doi.asString(), doi.asString() + " is not resolvable! " + getStatusString(response));
// return entries; // datacite says no media attached or doi does not exist (not sure what to do)
default:
throw new MCRDatacenterException(String.format(Locale.ENGLISH, "Datacenter-Error while set media-list for doi: \"%s\" : %s", doi.asString(), getStatusString(response)));
}
} catch (IOException e) {
throw new MCRDatacenterException("Unknown error while set media list", e);
} catch (URISyntaxException e) {
throw new MCRDatacenterException("Could not parse media url!", e);
}
}
use of org.mycore.pi.exceptions.MCRDatacenterException in project mycore by MyCoRe-Org.
the class MCRDataciteClient method resolveDOI.
public URI resolveDOI(final MCRDigitalObjectIdentifier doiParam) throws MCRPersistentIdentifierException {
URI requestURI = getRequestURI("/doi/" + doiParam.asString());
HttpGet get = new HttpGet(requestURI);
try (CloseableHttpClient httpClient = getHttpClient()) {
CloseableHttpResponse response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
switch(statusLine.getStatusCode()) {
case HttpStatus.SC_OK:
Scanner scanner = new Scanner(entity.getContent(), "UTF-8");
String uriString = scanner.nextLine();
return new URI(uriString);
case HttpStatus.SC_NO_CONTENT:
throw new MCRIdentifierUnresolvableException(doiParam.asString(), "The identifier " + doiParam.asString() + " is currently not resolvable");
case HttpStatus.SC_NOT_FOUND:
throw new MCRIdentifierUnresolvableException(doiParam.asString(), "The identifier " + doiParam.asString() + " was not found in the Datacenter!");
case HttpStatus.SC_UNAUTHORIZED:
throw new MCRDatacenterAuthenticationException();
case HttpStatus.SC_INTERNAL_SERVER_ERROR:
throw new MCRDatacenterException(String.format(Locale.ENGLISH, "Datacenter error while resolving doi: \"%s\" : %s", doiParam.asString(), getStatusString(response)));
default:
throw new MCRDatacenterException(String.format(Locale.ENGLISH, "Unknown error while resolving doi: \"%s\" : %s", doiParam.asString(), getStatusString(response)));
}
} catch (IOException | URISyntaxException ex) {
throw new MCRDatacenterException(String.format(Locale.ENGLISH, "Unknown error while resolving doi: \"%s\"", doiParam.asString()), ex);
}
}
use of org.mycore.pi.exceptions.MCRDatacenterException in project mycore by MyCoRe-Org.
the class MCRDataciteClient method resolveMetadata.
public Document resolveMetadata(final MCRDigitalObjectIdentifier doiParam) throws MCRPersistentIdentifierException {
MCRDigitalObjectIdentifier doi = isTestPrefix() ? doiParam.toTestPrefix() : doiParam;
URI requestURI = getRequestURI("/metadata/" + doi.asString());
HttpGet get = new HttpGet(requestURI);
try (CloseableHttpClient httpClient = getHttpClient()) {
CloseableHttpResponse response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
switch(statusLine.getStatusCode()) {
case HttpStatus.SC_OK:
SAXBuilder builder = new SAXBuilder();
return builder.build(entity.getContent());
case HttpStatus.SC_UNAUTHORIZED:
throw new MCRDatacenterAuthenticationException();
case HttpStatus.SC_NO_CONTENT:
throw new MCRIdentifierUnresolvableException(doi.asString(), "The identifier " + doi.asString() + " is currently not resolvable");
case HttpStatus.SC_NOT_FOUND:
throw new MCRIdentifierUnresolvableException(doi.asString(), "The identifier " + doi.asString() + " was not found!");
case HttpStatus.SC_GONE:
throw new MCRIdentifierUnresolvableException(doi.asString(), "The identifier " + doi.asString() + " was deleted!");
default:
throw new MCRDatacenterException("Unknown return status: " + getStatusString(response));
}
} catch (IOException | JDOMException e) {
throw new MCRDatacenterException("Error while resolving metadata!", e);
}
}
Aggregations