Search in sources :

Example 6 with ErrorResultException

use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.

the class EclipseService method getPublicProfile.

/**
 * Get the publicly available user profile.
 */
public EclipseProfile getPublicProfile(String personId) {
    checkApiUrl();
    var headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    var requestUrl = UrlUtil.createApiUrl(eclipseApiUrl, "account", "profile", personId);
    var request = new RequestEntity<>(headers, HttpMethod.GET, URI.create(requestUrl));
    try {
        var response = restTemplate.exchange(request, String.class);
        return parseEclipseProfile(response);
    } catch (RestClientException exc) {
        if (exc instanceof HttpStatusCodeException) {
            var status = ((HttpStatusCodeException) exc).getStatusCode();
            if (status == HttpStatus.NOT_FOUND)
                throw new ErrorResultException("No Eclipse profile data available for user: " + personId);
        }
        logger.error("Get request failed with URL: " + requestUrl, exc);
        throw new ErrorResultException("Request for retrieving user profile 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 7 with ErrorResultException

use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.

the class EclipseService method parseAgreementResponse.

private PublisherAgreementResponse parseAgreementResponse(ResponseEntity<String> response) {
    var json = response.getBody();
    try {
        if (json.startsWith("[\"")) {
            var error = objectMapper.readValue(json, TYPE_LIST_STRING);
            logger.error("Publisher agreement 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_AGREEMENT);
            if (profileList.isEmpty()) {
                throw new ErrorResultException("No publisher agreement available.", HttpStatus.INTERNAL_SERVER_ERROR);
            }
            return profileList.get(0);
        } else {
            return objectMapper.readValue(json, PublisherAgreementResponse.class);
        }
    } catch (JsonProcessingException exc) {
        logger.error("Failed to parse JSON response (" + response.getStatusCode() + "):\n" + json, exc);
        throw new ErrorResultException("Parsing publisher agreement response failed: " + exc.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
Also used : ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 8 with ErrorResultException

use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.

the class EclipseService method revokePublisherAgreement.

/**
 * Revoke the given user's publisher agreement. If an admin user is given,
 * the admin's access token is used for the Eclipse API request, otherwise
 * the access token of the target user is used.
 */
public void revokePublisherAgreement(UserData user, UserData admin) {
    checkApiUrl();
    AuthToken eclipseToken;
    if (admin == null)
        eclipseToken = checkEclipseToken(user);
    else
        eclipseToken = checkEclipseToken(admin);
    var headers = new HttpHeaders();
    headers.setBearerAuth(eclipseToken.accessToken);
    var request = new HttpEntity<Void>(headers);
    var eclipseData = checkEclipseData(user);
    var requestUrl = UrlUtil.createApiUrl(eclipseApiUrl, "openvsx", "publisher_agreement", eclipseData.personId);
    try {
        var requestCallback = restTemplate.httpEntityCallback(request);
        restTemplate.execute(requestUrl, HttpMethod.DELETE, requestCallback, null);
        if (eclipseData.publisherAgreement != null) {
            updateEclipseData(user, ed -> {
                ed.publisherAgreement.isActive = false;
                return null;
            }, NOP_INIT);
        }
    } catch (RestClientException exc) {
        logger.error("Delete request failed with URL: " + requestUrl, exc);
        throw new ErrorResultException("Request for revoking publisher agreement failed: " + exc.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
Also used : ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) RestClientException(org.springframework.web.client.RestClientException) AuthToken(org.eclipse.openvsx.entities.AuthToken)

Example 9 with ErrorResultException

use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.

the class EclipseService method signPublisherAgreement.

/**
 * Sign the publisher agreement on behalf of the given user.
 */
public EclipseData.PublisherAgreement signPublisherAgreement(UserData user) {
    checkApiUrl();
    var eclipseToken = checkEclipseToken(user);
    var headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setBearerAuth(eclipseToken.accessToken);
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    var data = new SignAgreementParam(publisherAgreementVersion, user.getLoginName());
    var request = new HttpEntity<>(data, headers);
    var requestUrl = UrlUtil.createApiUrl(eclipseApiUrl, "openvsx", "publisher_agreement");
    try {
        var json = restTemplate.postForEntity(requestUrl, request, String.class);
        // The request was successful: reactivate all previously published extensions
        extensions.reactivateExtensions(user);
        // Parse the response and store the publisher agreement metadata
        var response = parseAgreementResponse(json);
        var result = updateEclipseData(user, ed -> {
            ed.publisherAgreement = response.createEntityData(parseDate);
            return ed.publisherAgreement;
        }, ed -> {
            ed.personId = response.personID;
        });
        return result;
    } catch (RestClientException exc) {
        String message = exc.getMessage();
        var statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
        if (exc instanceof HttpStatusCodeException) {
            var excStatus = ((HttpStatusCodeException) exc).getStatusCode();
            // The endpoint yields 409 if the specified user has already signed a publisher agreement
            if (excStatus == HttpStatus.CONFLICT) {
                message = "A publisher agreement is already present for user " + user.getLoginName() + ".";
                statusCode = HttpStatus.BAD_REQUEST;
            } else if (excStatus == HttpStatus.BAD_REQUEST) {
                var matcher = STATUS_400_MESSAGE.matcher(exc.getMessage());
                if (matcher.matches()) {
                    message = matcher.group("message");
                }
            }
        }
        if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR) {
            message = "Request for signing publisher agreement failed: " + message;
        }
        String payload;
        try {
            payload = objectMapper.writeValueAsString(data);
        } catch (JsonProcessingException exc2) {
            payload = "<" + exc2.getMessage() + ">";
        }
        logger.error("Post request failed with URL: " + requestUrl + " Payload: " + payload, exc);
        throw new ErrorResultException(message, statusCode);
    }
}
Also used : ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) RestClientException(org.springframework.web.client.RestClientException) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 10 with ErrorResultException

use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.

the class AdminAPI method getLog.

@GetMapping(path = "/admin/log", produces = MediaType.TEXT_PLAIN_VALUE)
public String getLog(@RequestParam(name = "period", required = false) String periodString) {
    try {
        admins.checkAdminUser();
        Streamable<PersistedLog> logs;
        if (Strings.isNullOrEmpty(periodString)) {
            logs = repositories.findAllPersistedLogs();
        } else {
            try {
                var period = Period.parse(periodString);
                var now = TimeUtil.getCurrentUTC();
                logs = repositories.findPersistedLogsAfter(now.minus(period));
            } catch (DateTimeParseException exc) {
                throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid period");
            }
        }
        return logs.stream().map(this::toString).collect(Collectors.joining("\n")) + "\n";
    } catch (ErrorResultException exc) {
        var status = exc.getStatus() != null ? exc.getStatus() : HttpStatus.BAD_REQUEST;
        throw new ResponseStatusException(status);
    }
}
Also used : ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) DateTimeParseException(java.time.format.DateTimeParseException) PersistedLog(org.eclipse.openvsx.entities.PersistedLog) ResponseStatusException(org.springframework.web.server.ResponseStatusException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

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