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;
}
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);
}
}
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;
}
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"));
}
}
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()));
}
}
Aggregations