Search in sources :

Example 6 with MCRDatacenterAuthenticationException

use of org.mycore.pi.exceptions.MCRDatacenterAuthenticationException 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);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Scanner(java.util.Scanner) MCRDatacenterException(org.mycore.pi.exceptions.MCRDatacenterException) HttpEntity(org.apache.http.HttpEntity) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MCRDatacenterAuthenticationException(org.mycore.pi.exceptions.MCRDatacenterAuthenticationException)

Example 7 with MCRDatacenterAuthenticationException

use of org.mycore.pi.exceptions.MCRDatacenterAuthenticationException 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);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) MCRDatacenterException(org.mycore.pi.exceptions.MCRDatacenterException) SAXBuilder(org.jdom2.input.SAXBuilder) HttpEntity(org.apache.http.HttpEntity) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MCRDatacenterAuthenticationException(org.mycore.pi.exceptions.MCRDatacenterAuthenticationException)

Aggregations

IOException (java.io.IOException)7 URI (java.net.URI)7 StatusLine (org.apache.http.StatusLine)7 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)7 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)7 MCRDatacenterAuthenticationException (org.mycore.pi.exceptions.MCRDatacenterAuthenticationException)7 MCRDatacenterException (org.mycore.pi.exceptions.MCRDatacenterException)7 MCRIdentifierUnresolvableException (org.mycore.pi.exceptions.MCRIdentifierUnresolvableException)4 URISyntaxException (java.net.URISyntaxException)3 Scanner (java.util.Scanner)3 HttpGet (org.apache.http.client.methods.HttpGet)3 HttpPost (org.apache.http.client.methods.HttpPost)3 HttpEntity (org.apache.http.HttpEntity)2 StringEntity (org.apache.http.entity.StringEntity)2 InputStream (java.io.InputStream)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 Header (org.apache.http.Header)1 HttpDelete (org.apache.http.client.methods.HttpDelete)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1