Search in sources :

Example 6 with MCRIdentifierUnresolvableException

use of org.mycore.pi.exceptions.MCRIdentifierUnresolvableException 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 MCRIdentifierUnresolvableException

use of org.mycore.pi.exceptions.MCRIdentifierUnresolvableException 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)

Example 8 with MCRIdentifierUnresolvableException

use of org.mycore.pi.exceptions.MCRIdentifierUnresolvableException in project mycore by MyCoRe-Org.

the class MCRDOICommands method updateMediaListForDOI.

@MCRCommand(syntax = REPAIR_MEDIALIST_OF_0_AND_SERVICE_1, help = "Sends new media list to Datacite. {0} is the DOI. The Service ID{1} is the id from the configuration.")
public static void updateMediaListForDOI(String doiString, String serviceID) {
    MCRDOIRegistrationService registrationService = new MCRDOIRegistrationService(serviceID);
    MCRDataciteClient dataciteClient = registrationService.getDataciteClient();
    MCRDigitalObjectIdentifier doi = new MCRDOIParser().parse(doiString).orElseThrow(() -> new IllegalArgumentException("The String " + doiString + " is no valid DOI!"));
    try {
        URI uri = dataciteClient.resolveDOI(doi);
        if (uri.toString().startsWith(registrationService.getRegisterURL())) {
            String s = uri.toString();
            LOGGER.info("Checking DOI: {} / {}", doi.asString(), s);
            String idString = s.substring(s.lastIndexOf("/") + 1);
            MCRObjectID objectID = MCRObjectID.getInstance(idString);
            if (MCRMetadataManager.exists(objectID)) {
                MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
                List<Map.Entry<String, URI>> newMediaList = registrationService.getMediaList(obj);
                List<Map.Entry<String, URI>> oldMediaList;
                try {
                    oldMediaList = dataciteClient.getMediaList(doi);
                } catch (MCRIdentifierUnresolvableException e) {
                    LOGGER.warn("{} had no media list!", doi);
                    oldMediaList = new ArrayList<>();
                }
                HashMap<String, URI> newHashMap = new HashMap<>();
                newMediaList.forEach(e -> newHashMap.put(e.getKey(), e.getValue()));
                oldMediaList.forEach(e -> {
                    /*
                        Currently it is not possible to delete inserted values key values (mime types).
                        So we update old media mimetypes which are not present in new list to the same URL of the first
                        mimetype entry.
                        */
                    if (!newHashMap.containsKey(e.getKey())) {
                        newHashMap.put(e.getKey(), newMediaList.stream().findFirst().orElseThrow(() -> new MCRException("new media list is empty (this should not happen)")).getValue());
                    }
                });
                dataciteClient.setMediaList(doi, newHashMap.entrySet().stream().collect(Collectors.toList()));
                LOGGER.info("Updated media-list of {}", doiString);
            } else {
                LOGGER.info("Object {} does not exist in this application!", objectID);
            }
        } else {
            LOGGER.info("DOI is not from this application: ({}) {}", uri, registrationService.getRegisterURL());
        }
    } catch (MCRPersistentIdentifierException e) {
        LOGGER.error("Error occurred for DOI: {}", doi, e);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) URI(java.net.URI) MCRDOIParser(org.mycore.pi.doi.MCRDOIParser) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRDigitalObjectIdentifier(org.mycore.pi.doi.MCRDigitalObjectIdentifier) MCRDOIRegistrationService(org.mycore.pi.doi.MCRDOIRegistrationService) MCRDataciteClient(org.mycore.pi.doi.MCRDataciteClient) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRIdentifierUnresolvableException (org.mycore.pi.exceptions.MCRIdentifierUnresolvableException)8 IOException (java.io.IOException)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 URI (java.net.URI)5 HttpGet (org.apache.http.client.methods.HttpGet)5 HttpEntity (org.apache.http.HttpEntity)4 StatusLine (org.apache.http.StatusLine)4 MCRDatacenterAuthenticationException (org.mycore.pi.exceptions.MCRDatacenterAuthenticationException)4 MCRDatacenterException (org.mycore.pi.exceptions.MCRDatacenterException)4 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 Scanner (java.util.Scanner)2 JDOMException (org.jdom2.JDOMException)2 SAXBuilder (org.jdom2.input.SAXBuilder)2 MCRException (org.mycore.common.MCRException)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1