use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.
the class EclipseService method getPublisherAgreement.
/**
* Get the publisher agreement of the given user with the given access token.
*/
public EclipseData.PublisherAgreement getPublisherAgreement(UserData user, String accessToken) {
var eclipseData = user.getEclipseData();
if (eclipseData == null || Strings.isNullOrEmpty(eclipseData.personId)) {
return null;
}
checkApiUrl();
var headers = new HttpHeaders();
headers.setBearerAuth(accessToken);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
var requestUrl = UrlUtil.createApiUrl(eclipseApiUrl, "openvsx", "publisher_agreement", eclipseData.personId);
var request = new RequestEntity<>(headers, HttpMethod.GET, URI.create(requestUrl));
try {
var json = restTemplate.exchange(request, String.class);
var response = parseAgreementResponse(json);
return updateEclipseData(user, ed -> {
ed.publisherAgreement = response.createEntityData(parseDate);
return ed.publisherAgreement;
}, ed -> {
ed.personId = response.personID;
});
} catch (RestClientException exc) {
if (exc instanceof HttpStatusCodeException) {
var status = ((HttpStatusCodeException) exc).getStatusCode();
// The endpoint yields 404 if the specified user has not signed a publisher agreement
if (status == HttpStatus.NOT_FOUND)
return null;
}
logger.error("Get request failed with URL: " + requestUrl, exc);
throw new ErrorResultException("Request for retrieving publisher agreement failed: " + exc.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.
the class EclipseService method parseEclipseProfile.
private EclipseProfile parseEclipseProfile(ResponseEntity<String> response) {
var json = response.getBody();
try {
if (json.startsWith("[\"")) {
var error = objectMapper.readValue(json, TYPE_LIST_STRING);
logger.error("Profile request failed:\n" + json);
throw new ErrorResultException("Request to the Eclipse Foundation server failed: " + error, HttpStatus.INTERNAL_SERVER_ERROR);
} else if (json.startsWith("[")) {
var profileList = objectMapper.readValue(json, TYPE_LIST_PROFILE);
if (profileList.isEmpty()) {
throw new ErrorResultException("No Eclipse user profile available.", HttpStatus.INTERNAL_SERVER_ERROR);
}
return profileList.get(0);
} else {
return objectMapper.readValue(json, EclipseProfile.class);
}
} catch (JsonProcessingException exc) {
logger.error("Failed to parse JSON response (" + response.getStatusCode() + "):\n" + json, exc);
throw new ErrorResultException("Parsing Eclipse user profile failed: " + exc.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Aggregations