Search in sources :

Example 1 with MCRIdentifierUnresolvableException

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

the class MCRDOIRest method get.

public static MCRDOIRestResponse get(MCRDigitalObjectIdentifier doi) throws MCRIdentifierUnresolvableException {
    HttpGet get = new HttpGet(URL_TEMPLATE.replaceAll("\\{doi\\}", doi.asString()));
    try (CloseableHttpClient httpClient = getHttpClient()) {
        CloseableHttpResponse response = httpClient.execute(get);
        HttpEntity entity = response.getEntity();
        try (BufferedReader buffer = new BufferedReader(new InputStreamReader(entity.getContent(), Charset.forName("UTF-8")))) {
            String json = buffer.lines().collect(Collectors.joining("\n"));
            Gson gson = new GsonBuilder().registerTypeAdapter(MCRDOIRestResponseEntryData.class, new MCRDOIRestResponseEntryDataValueDeserializer()).create();
            return gson.fromJson(json, MCRDOIRestResponse.class);
        }
    } catch (IOException e) {
        throw new MCRIdentifierUnresolvableException(doi.asString(), "The identifier " + doi.asString() + " is not resolvable!", e);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) HttpGet(org.apache.http.client.methods.HttpGet) Gson(com.google.gson.Gson) IOException(java.io.IOException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader)

Example 2 with MCRIdentifierUnresolvableException

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

the class MCRDNBPIDefProvider method get.

public static Document get(String identifier) throws MCRIdentifierUnresolvableException {
    HttpGet get = new HttpGet(RESOLVING_URL_TEMPLATE.replaceAll("\\{urn\\}", identifier));
    try (CloseableHttpClient httpClient = MCRHttpUtils.getHttpClient()) {
        CloseableHttpResponse response = httpClient.execute(get);
        HttpEntity entity = response.getEntity();
        return new SAXBuilder().build(entity.getContent());
    } catch (IOException | JDOMException e) {
        String message = "The identifier " + identifier + " is not resolvable!";
        throw new MCRIdentifierUnresolvableException(identifier, message, e);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) SAXBuilder(org.jdom2.input.SAXBuilder) HttpEntity(org.apache.http.HttpEntity) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException)

Example 3 with MCRIdentifierUnresolvableException

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

the class MCRURNOAIRegistrationService method isRegistered.

@Override
public boolean isRegistered(MCRObjectID id, String additional) {
    boolean registered = super.isRegistered(id, additional);
    if (registered)
        return true;
    if (!isCreated(id, additional)) {
        return false;
    }
    // URN is created. Now we need to check if it is resolvable
    MCRPI mcrpi = getTableEntry(id, additional);
    MCRDNBURN dnburn = new MCRDNBURNParser().parse(mcrpi.getIdentifier()).orElseThrow(() -> new MCRException("Cannot parse Identifier from table: " + mcrpi.getIdentifier()));
    try {
        // Find register date in dnb rest
        Date dnbRegisteredDate = MCRURNUtils.getDNBRegisterDate(dnburn);
        if (dnbRegisteredDate == null) {
            return false;
        }
        mcrpi.setRegistered(dnbRegisteredDate);
        updateFlag(id, additional, mcrpi);
        return true;
    } catch (ParseException e) {
        LOGGER.error("Could not parse Date from PIDEF ! URN wont be marked as registered because of this! ", e);
        return false;
    } catch (MCRIdentifierUnresolvableException e) {
        return false;
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) MCRPI(org.mycore.pi.backend.MCRPI) ParseException(java.text.ParseException) Date(java.util.Date)

Example 4 with MCRIdentifierUnresolvableException

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

the class MCRDataciteClient method deleteMetadata.

public void deleteMetadata(final MCRDigitalObjectIdentifier doiParam) throws MCRPersistentIdentifierException {
    MCRDigitalObjectIdentifier doi = isTestPrefix() ? doiParam.toTestPrefix() : doiParam;
    URI requestURI = getRequestURI("/metadata/" + doi.asString());
    HttpDelete delete = new HttpDelete(requestURI);
    try (CloseableHttpClient httpClient = getHttpClient()) {
        CloseableHttpResponse response = httpClient.execute(delete);
        StatusLine statusLine = response.getStatusLine();
        switch(statusLine.getStatusCode()) {
            case HttpStatus.SC_OK:
                return;
            case HttpStatus.SC_UNAUTHORIZED:
                throw new MCRDatacenterAuthenticationException();
            case HttpStatus.SC_NOT_FOUND:
                throw new MCRIdentifierUnresolvableException(doi.asString(), doi.asString() + " was not found!");
            default:
                throw new MCRDatacenterException("Unknown return status: " + statusLine.getStatusCode() + " - " + statusLine.getReasonPhrase());
        }
    } catch (IOException e) {
        throw new MCRDatacenterException("Error while deleting metadata!", e);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) MCRDatacenterException(org.mycore.pi.exceptions.MCRDatacenterException) HttpDelete(org.apache.http.client.methods.HttpDelete) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MCRDatacenterAuthenticationException(org.mycore.pi.exceptions.MCRDatacenterAuthenticationException) IOException(java.io.IOException) URI(java.net.URI)

Example 5 with MCRIdentifierUnresolvableException

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

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