Search in sources :

Example 1 with MCRDatacenterAuthenticationException

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

the class MCRDataciteClient method storeMetadata.

public URI storeMetadata(Document metadata) throws MCRPersistentIdentifierException {
    URI requestURI = getRequestURI("/metadata");
    if (isTestPrefix()) {
        changeToTestDOI(metadata);
    }
    HttpPost post = new HttpPost(requestURI);
    try (CloseableHttpClient httpClient = getHttpClient()) {
        byte[] documentBytes = documentToByteArray(metadata);
        ByteArrayEntity inputStreamEntity = new ByteArrayEntity(documentBytes, ContentType.create("application/xml", "UTF-8"));
        post.setEntity(inputStreamEntity);
        CloseableHttpResponse response = httpClient.execute(post);
        StatusLine statusLine = response.getStatusLine();
        StringBuilder sb = new StringBuilder();
        try (InputStream is = response.getEntity().getContent()) {
            Scanner scanner = new Scanner(is, "UTF-8");
            while (scanner.hasNextLine()) {
                sb.append(scanner.nextLine()).append(System.lineSeparator());
            }
        } catch (IOException | UnsupportedOperationException e) {
            LOGGER.warn("Could not read content!", e);
        }
        String responseString = sb.toString();
        switch(statusLine.getStatusCode()) {
            case HttpStatus.SC_CREATED:
                Header[] responseHeaders = response.getAllHeaders();
                for (Header responseHeader : responseHeaders) {
                    if (responseHeader.getName().equals("Location")) {
                        return new URI(responseHeader.getValue());
                    }
                }
                // should not happen
                throw new MCRDatacenterException("Location header not found in response! - " + responseString);
            case // invalid xml or wrong PREFIX
            HttpStatus.SC_BAD_REQUEST:
                throw new MCRDatacenterException("Invalid xml or wrong PREFIX: " + statusLine.getStatusCode() + " - " + statusLine.getReasonPhrase() + " - " + responseString);
            case // no login
            HttpStatus.SC_UNAUTHORIZED:
                throw new MCRDatacenterAuthenticationException();
            default:
                throw new MCRDatacenterException("Unknown return status: " + statusLine.getStatusCode() + " - " + statusLine.getReasonPhrase() + " - " + responseString);
        }
    } catch (IOException | URISyntaxException e) {
        throw new MCRDatacenterException("Error while storing metadata!", e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Scanner(java.util.Scanner) MCRDatacenterException(org.mycore.pi.exceptions.MCRDatacenterException) InputStream(java.io.InputStream) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) Header(org.apache.http.Header) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MCRDatacenterAuthenticationException(org.mycore.pi.exceptions.MCRDatacenterAuthenticationException)

Example 2 with MCRDatacenterAuthenticationException

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

the class MCRDataciteClient method mintDOI.

public void mintDOI(final MCRDigitalObjectIdentifier doiParam, URI url) throws MCRPersistentIdentifierException {
    MCRDigitalObjectIdentifier doi = isTestPrefix() ? doiParam.toTestPrefix() : doiParam;
    URI requestURI = getRequestURI("/doi");
    HttpPost post = new HttpPost(requestURI);
    try (CloseableHttpClient httpClient = getHttpClient()) {
        post.setEntity(new StringEntity(String.format(Locale.ENGLISH, DOI_REGISTER_REQUEST_TEMPLATE, doi.asString(), url.toString())));
        CloseableHttpResponse response = httpClient.execute(post);
        StatusLine statusLine = response.getStatusLine();
        switch(statusLine.getStatusCode()) {
            case HttpStatus.SC_CREATED:
                return;
            case HttpStatus.SC_BAD_REQUEST:
                // invalid PREFIX or wrong format, but format is hard defined!
                throw new MCRDatacenterException(getStatusString(response));
            case HttpStatus.SC_UNAUTHORIZED:
                throw new MCRDatacenterAuthenticationException();
            case HttpStatus.SC_PRECONDITION_FAILED:
                throw new MCRDatacenterException(String.format(Locale.ENGLISH, "Metadata must be uploaded first! (%s)", getStatusString(response)));
            default:
                throw new MCRDatacenterException(String.format(Locale.ENGLISH, "Datacenter-Error while minting doi: \"%s\" : %s", doi.asString(), getStatusString(response)));
        }
    } catch (IOException e) {
        throw new MCRDatacenterException("Unknown error while mint new doi", e);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) StringEntity(org.apache.http.entity.StringEntity) MCRDatacenterException(org.mycore.pi.exceptions.MCRDatacenterException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MCRDatacenterAuthenticationException(org.mycore.pi.exceptions.MCRDatacenterAuthenticationException) IOException(java.io.IOException) URI(java.net.URI)

Example 3 with MCRDatacenterAuthenticationException

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

the class MCRDataciteClient method setMediaList.

public void setMediaList(final MCRDigitalObjectIdentifier doiParam, List<Map.Entry<String, URI>> mediaList) throws MCRPersistentIdentifierException {
    MCRDigitalObjectIdentifier doi = isTestPrefix() ? doiParam.toTestPrefix() : doiParam;
    URI requestURI = getRequestURI("/media/" + doi.asString());
    HttpPost post = new HttpPost(requestURI);
    String requestBodyString = mediaList.stream().map(buildPair()).collect(Collectors.joining("\r\n"));
    LOGGER.info(requestBodyString);
    StringEntity requestEntity = new StringEntity(requestBodyString, ContentType.create("text/plain", "UTF-8"));
    post.setEntity(requestEntity);
    try (CloseableHttpClient httpClient = getHttpClient()) {
        CloseableHttpResponse response = httpClient.execute(post);
        StatusLine statusLine = response.getStatusLine();
        switch(statusLine.getStatusCode()) {
            case HttpStatus.SC_OK:
                return;
            case HttpStatus.SC_BAD_REQUEST:
                // non-supported mime-type, not allowed URL domain
                throw new MCRDatacenterException(getStatusString(response));
            case HttpStatus.SC_UNAUTHORIZED:
                throw new MCRDatacenterAuthenticationException();
            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);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) MCRDatacenterException(org.mycore.pi.exceptions.MCRDatacenterException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MCRDatacenterAuthenticationException(org.mycore.pi.exceptions.MCRDatacenterAuthenticationException) IOException(java.io.IOException) URI(java.net.URI)

Example 4 with MCRDatacenterAuthenticationException

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

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

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