org.eclipse.jetty.client.util.FormContentProvider Java Examples

The following examples show how to use org.eclipse.jetty.client.util.FormContentProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RestClient.java    From app-runner with MIT License 5 votes vote down vote up
public ContentResponse createApp(String gitUrl, String appName) throws Exception {
    Fields fields = new Fields();
    fields.add("gitUrl", gitUrl);
    if (appName != null) {
        fields.add("appName", appName);
    }
    return client.POST(appRunnerUrl + "/api/v1/apps")
        .content(new FormContentProvider(fields)).send();
}
 
Example #2
Source File: RestClient.java    From app-runner with MIT License 5 votes vote down vote up
public ContentResponse updateApp(String gitUrl, String appName) throws Exception {
    Fields fields = new Fields();
    fields.add("gitUrl", gitUrl);
    return client.newRequest(appRunnerUrl + "/api/v1/apps/" + appName)
        .method("PUT")
        .content(new FormContentProvider(fields)).send();
}
 
Example #3
Source File: OAuthConnector.java    From openhab-core with Eclipse Public License 2.0 4 votes vote down vote up
private AccessTokenResponse doRequest(final String grantType, HttpClient httpClient, final Request request,
        Fields fields) throws OAuthResponseException, OAuthException, IOException {
    int statusCode = 0;
    String content = "";
    try {
        final FormContentProvider entity = new FormContentProvider(fields);
        final ContentResponse response = AccessController
                .doPrivileged((PrivilegedExceptionAction<ContentResponse>) () -> {
                    Request requestWithContent = request.content(entity);
                    return requestWithContent.send();
                });

        statusCode = response.getStatus();
        content = response.getContentAsString();

        if (statusCode == HttpStatus.OK_200) {
            AccessTokenResponse jsonResponse = gson.fromJson(content, AccessTokenResponse.class);
            jsonResponse.setCreatedOn(LocalDateTime.now()); // this is not supplied by the response
            logger.debug("grant type {} to URL {} success", grantType, request.getURI());
            return jsonResponse;
        } else if (statusCode == HttpStatus.BAD_REQUEST_400) {
            OAuthResponseException errorResponse = gson.fromJson(content, OAuthResponseException.class);
            logger.error("grant type {} to URL {} failed with error code {}, description {}", grantType,
                    request.getURI(), errorResponse.getError(), errorResponse.getErrorDescription());

            throw errorResponse;
        } else {
            logger.error("grant type {} to URL {} failed with HTTP response code {}", grantType, request.getURI(),
                    statusCode);
            throw new OAuthException("Bad http response, http code " + statusCode);
        }
    } catch (PrivilegedActionException pae) {
        Exception underlyingException = pae.getException();
        if (underlyingException instanceof InterruptedException || underlyingException instanceof TimeoutException
                || underlyingException instanceof ExecutionException) {
            throw new IOException("Exception in oauth communication, grant type " + grantType, underlyingException);
        }
        // Dont know what exception it is, wrap it up and throw it out
        throw new OAuthException("Exception in oauth communication, grant type " + grantType, underlyingException);
    } catch (JsonSyntaxException e) {
        throw new OAuthException(String.format(
                "Unable to deserialize json into AccessTokenResponse/ OAuthResponseException. httpCode: %i json: %s",
                statusCode, content), e);
    }
}
 
Example #4
Source File: OAuthConnector.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
private AccessTokenResponse doRequest(final String grantType, HttpClient httpClient, final Request request,
        Fields fields) throws OAuthResponseException, OAuthException, IOException {

    int statusCode = 0;
    String content = "";
    try {
        final FormContentProvider entity = new FormContentProvider(fields);
        final ContentResponse response = AccessController
                .doPrivileged((PrivilegedExceptionAction<ContentResponse>) () -> {
                    Request requestWithContent = request.content(entity);
                    return requestWithContent.send();
                });

        statusCode = response.getStatus();
        content = response.getContentAsString();

        if (statusCode == HttpStatus.OK_200) {
            AccessTokenResponse jsonResponse = gson.fromJson(content, AccessTokenResponse.class);
            jsonResponse.setCreatedOn(LocalDateTime.now()); // this is not supplied by the response
            logger.info("grant type {} to URL {} success", grantType, request.getURI());
            return jsonResponse;
        } else if (statusCode == HttpStatus.BAD_REQUEST_400) {
            OAuthResponseException errorResponse = gson.fromJson(content, OAuthResponseException.class);
            logger.error("grant type {} to URL {} failed with error code {}, description {}", grantType,
                    request.getURI(), errorResponse.getError(), errorResponse.getErrorDescription());

            throw errorResponse;
        } else {
            logger.error("grant type {} to URL {} failed with HTTP response code {}", grantType, request.getURI(),
                    statusCode);
            throw new OAuthException("Bad http response, http code " + statusCode);
        }
    } catch (PrivilegedActionException pae) {
        Exception underlyingException = pae.getException();
        if (underlyingException instanceof InterruptedException || underlyingException instanceof TimeoutException
                || underlyingException instanceof ExecutionException) {
            throw new IOException("Exception in oauth communication, grant type " + grantType, underlyingException);
        }
        // Dont know what exception it is, wrap it up and throw it out
        throw new OAuthException("Exception in oauth communication, grant type " + grantType, underlyingException);
    } catch (JsonSyntaxException e) {
        throw new OAuthException(String.format(
                "Unable to deserialize json into AccessTokenResponse/ OAuthResponseException. httpCode: %i json: %s",
                statusCode, content), e);
    }
}