Search in sources :

Example 1 with RestException

use of org.commonjava.maven.ext.io.rest.exception.RestException in project pom-manipulation-ext by release-engineering.

the class DefaultTranslator method findBlacklisted.

@Override
public List<ProjectVersionRef> findBlacklisted(ProjectRef ga) {
    Unirest.setObjectMapper(lbm);
    final String blacklistEndpointUrl = endpointUrl + (endpointUrl.endsWith("/") ? "" : "/") + LISTING_BLACKLIST_GA;
    List<ProjectVersionRef> result;
    HttpResponse<List> r;
    logger.trace("Called findBlacklisted to {} with {}", blacklistEndpointUrl, ga);
    try {
        r = Unirest.get(blacklistEndpointUrl).header("accept", "application/json").header("Content-Type", "application/json").header("Log-Context", getHeaderContext()).queryString("groupid", ga.getGroupId()).queryString("artifactid", ga.getArtifactId()).asObject(List.class);
        int status = r.getStatus();
        if (status == SC_OK) {
            result = r.getBody();
        } else {
            throw new RestException(String.format("Failed to establish blacklist calling {} with error {} ", this.endpointUrl, lbm.getErrorString()));
        }
    } catch (UnirestException e) {
        throw new RestException("Unable to contact DA", e);
    }
    return result;
}
Also used : ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) RestException(org.commonjava.maven.ext.io.rest.exception.RestException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with RestException

use of org.commonjava.maven.ext.io.rest.exception.RestException in project pom-manipulation-ext by release-engineering.

the class ReportGAVMapper method writeValue.

@Override
public String writeValue(Object value) {
    @SuppressWarnings("unchecked") List<ProjectVersionRef> projects = (List<ProjectVersionRef>) value;
    Object request;
    List<Map<String, Object>> requestBody = new ArrayList<>();
    for (ProjectVersionRef project : projects) {
        Map<String, Object> gav = new HashMap<>();
        gav.put("groupId", project.getGroupId());
        gav.put("artifactId", project.getArtifactId());
        gav.put("version", project.getVersionString());
        requestBody.add(gav);
    }
    if (protocol == Translator.RestProtocol.PNC12 || protocol == Translator.RestProtocol.CURRENT) {
        request = new GAVSchema(new String[] {}, new String[] {}, repositoryGroup, requestBody);
    } else if (protocol == Translator.RestProtocol.PNC13) {
        request = new GAV13Schema(new String[] {}, new String[] {}, repositoryGroup, versionSuffix, requestBody);
    } else {
        throw new RestException("Unknown protocol value " + protocol);
    }
    try {
        return objectMapper.writeValueAsString(request);
    } catch (JsonProcessingException e) {
        throw new RestException("Failed to serialize version request: " + e.getMessage(), e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RestException(org.commonjava.maven.ext.io.rest.exception.RestException) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with RestException

use of org.commonjava.maven.ext.io.rest.exception.RestException in project pom-manipulation-ext by release-engineering.

the class ReportGAVMapper method readValue.

@Override
public Map<ProjectVersionRef, String> readValue(String s) {
    Map<ProjectVersionRef, String> result = new HashMap<>();
    if (s.length() == 0) {
        errorString = "No content to read.";
        return result;
    } else if (s.startsWith("<")) {
        // Read an HTML string.
        String stripped = s.replaceFirst(".*</h1>\n", "").replaceFirst("\n</body></html>", "");
        logger.debug("Read HTML string '{}' rather than a JSON stream; stripping message to {}", s, stripped);
        errorString = stripped;
        return result;
    } else if (s.startsWith("{\\\"message\\\":") || s.startsWith("{\"message\":") || s.startsWith("{\\\"errorType\\\":") || s.startsWith("{\"errorType\":")) {
        String endStripped = s.replace("\\\"}", "").replace("\"}", "");
        errorString = endStripped.substring(endStripped.lastIndexOf("\"") + 1);
        logger.debug("Read message string {}, processed to {} ", s, errorString);
        return result;
    }
    try {
        @SuppressWarnings("unchecked") List<Map<String, Object>> responseBody = objectMapper.readValue(s, List.class);
        for (Map<String, Object> gav : responseBody) {
            String groupId = (String) gav.get("groupId");
            String artifactId = (String) gav.get("artifactId");
            String version = (String) gav.get("version");
            String bestMatchVersion = (String) gav.get("bestMatchVersion");
            if (bestMatchVersion != null) {
                ProjectVersionRef project = new SimpleProjectVersionRef(groupId, artifactId, version);
                result.put(project, bestMatchVersion);
            }
        }
    } catch (IOException e) {
        logger.error("Failed to decode map when reading string {}", s);
        throw new RestException("Failed to read list-of-maps response from version server: " + e.getMessage(), e);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) RestException(org.commonjava.maven.ext.io.rest.exception.RestException) IOException(java.io.IOException) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with RestException

use of org.commonjava.maven.ext.io.rest.exception.RestException in project pom-manipulation-ext by release-engineering.

the class HttpMessageTranslatorTest method testTranslateVersionsWithMessage.

@Test
public void testTranslateVersionsWithMessage() {
    List<ProjectVersionRef> gavs = new ArrayList<ProjectVersionRef>() {

        {
            add(new SimpleProjectVersionRef("com.example", "example", "1.0"));
        }
    };
    try {
        versionTranslator.translateVersions(gavs);
        fail("Failed to throw RestException when server failed to respond.");
    } catch (RestException ex) {
        assertTrue(ex.getMessage().contains("502"));
    }
}
Also used : ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) ArrayList(java.util.ArrayList) RestException(org.commonjava.maven.ext.io.rest.exception.RestException) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) Test(org.junit.Test)

Example 5 with RestException

use of org.commonjava.maven.ext.io.rest.exception.RestException in project pom-manipulation-ext by release-engineering.

the class VersionTranslatorTest method testTranslateVersionsFailNoResponse.

@Test
public void testTranslateVersionsFailNoResponse() {
    // Some url that doesn't exist used here
    Translator translator = new DefaultTranslator("http://127.0.0.2", RestProtocol.PNC12, 0, Translator.CHUNK_SPLIT_COUNT, "", "");
    List<ProjectVersionRef> gavs = new ArrayList<ProjectVersionRef>() {

        {
            add(new SimpleProjectVersionRef("com.example", "example", "1.0"));
        }
    };
    try {
        translator.translateVersions(gavs);
        fail("Failed to throw RestException when server failed to respond.");
    } catch (RestException ex) {
        System.out.println("Caught ex" + ex);
    // Pass
    } catch (Exception ex) {
        fail(String.format("Expected exception is RestException, instead %s thrown.", ex.getClass().getSimpleName()));
    }
}
Also used : ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) ArrayList(java.util.ArrayList) RestException(org.commonjava.maven.ext.io.rest.exception.RestException) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) RestException(org.commonjava.maven.ext.io.rest.exception.RestException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

RestException (org.commonjava.maven.ext.io.rest.exception.RestException)12 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)11 ArrayList (java.util.ArrayList)10 SimpleProjectVersionRef (org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef)10 Test (org.junit.Test)7 IOException (java.io.IOException)3 List (java.util.List)3 Map (java.util.Map)3 HashMap (java.util.HashMap)2 LoggerContext (ch.qos.logback.classic.LoggerContext)1 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 FileAppender (ch.qos.logback.core.FileAppender)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)1 File (java.io.File)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 Set (java.util.Set)1 XPath (javax.xml.xpath.XPath)1