Search in sources :

Example 21 with ErrorResultException

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);
    }
}
Also used : ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) HttpHeaders(org.springframework.http.HttpHeaders) RestClientException(org.springframework.web.client.RestClientException) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) RequestEntity(org.springframework.http.RequestEntity)

Example 22 with ErrorResultException

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);
    }
}
Also used : ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

ErrorResultException (org.eclipse.openvsx.util.ErrorResultException)22 Transactional (javax.transaction.Transactional)5 HttpHeaders (org.springframework.http.HttpHeaders)5 RestClientException (org.springframework.web.client.RestClientException)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 Extension (org.eclipse.openvsx.entities.Extension)3 ExtensionVersion (org.eclipse.openvsx.entities.ExtensionVersion)3 ExtensionJson (org.eclipse.openvsx.json.ExtensionJson)3 ResultJson (org.eclipse.openvsx.json.ResultJson)3 RepositoryService (org.eclipse.openvsx.repositories.RepositoryService)3 SearchUtilService (org.eclipse.openvsx.search.SearchUtilService)3 NotFoundException (org.eclipse.openvsx.util.NotFoundException)3 UrlUtil (org.eclipse.openvsx.util.UrlUtil)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 HttpStatus (org.springframework.http.HttpStatus)3 ResponseEntity (org.springframework.http.ResponseEntity)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 ResponseStatusException (org.springframework.web.server.ResponseStatusException)3