Java Code Examples for org.apache.cxf.jaxrs.client.WebClient#form()

The following examples show how to use org.apache.cxf.jaxrs.client.WebClient#form() . 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: CommandProcessor.java    From peer-os with Apache License 2.0 6 votes vote down vote up
void notifyAgent( ResourceHostInfo resourceHostInfo )
{
    WebClient webClient = null;
    javax.ws.rs.core.Response response = null;

    try
    {
        webClient = getWebClient( resourceHostInfo );

        response = webClient.form( new Form() );

        if ( response.getStatus() == javax.ws.rs.core.Response.Status.OK.getStatusCode()
                || response.getStatus() == javax.ws.rs.core.Response.Status.ACCEPTED.getStatusCode() )
        {
            hostRegistry.updateResourceHostEntryTimestamp( resourceHostInfo.getId() );
        }
    }
    finally
    {
        RestUtil.close( response, webClient );
    }
}
 
Example 2
Source File: JAXRSSpringSecurityClassTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testBookFromForm() throws Exception {

    WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstorestorage/bookforms",
                                    "foo", "bar", null);
    wc.accept("application/xml");
    Response r = wc.form(new Form().param("name", "CXF Rocks").param("id", "123"));

    Book b = readBook((InputStream)r.getEntity());
    assertEquals("CXF Rocks", b.getName());
    assertEquals(123L, b.getId());
}
 
Example 3
Source File: JAXRSSpringSecurityClassTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore("Spring Security 3 does not preserve POSTed form parameters as HTTPServletRequest parameters")
public void testBookFromHttpRequestParameters() throws Exception {

    WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstorestorage/bookforms2",
                                    "foo", "bar", null);
    WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L);
    wc.accept("application/xml");
    Response r = wc.form(new Form().param("name", "CXF Rocks").param("id", "123"));

    Book b = readBook((InputStream)r.getEntity());
    assertEquals("CXF Rocks", b.getName());
    assertEquals(123L, b.getId());
}
 
Example 4
Source File: JAXRSClientServerSpringBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testTooManyFormParams() throws Exception {
    String endpointAddress =
        "http://localhost:" + PORT + "/the/thebooks9/depth-form";
    WebClient wc = WebClient.create(endpointAddress);
    Response r = wc.form(new Form().param("a", "b"));
    assertEquals(204, r.getStatus());
    r = wc.form(new Form().param("a", "b").param("c", "b"));
    assertEquals(413, r.getStatus());
}
 
Example 5
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testPostEmptyForm() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/emptyform";
    WebClient wc = WebClient.create(address);
    Response r = wc.form(new Form());
    assertEquals("empty form", r.readEntity(String.class));
}
 
Example 6
Source File: OAuthClientUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * Obtains the access token from OAuth AccessToken Service
 * using the initialized web client
 * @param accessTokenService the AccessToken client
 * @param consumer {@link Consumer} representing the registered client.
 * @param grant {@link AccessTokenGrant} grant
 * @param extraParams extra parameters
 * @param defaultTokenType default expected token type - some early
 *        well-known OAuth2 services do not return a required token_type parameter
 * @param setAuthorizationHeader if set to true then HTTP Basic scheme
 *           will be used to pass client id and secret, otherwise they will
 *           be passed in the form payload
 * @return {@link ClientAccessToken} access token
 * @throws OAuthServiceException
 */
public static ClientAccessToken getAccessToken(WebClient accessTokenService,
                                               Consumer consumer,
                                               AccessTokenGrant grant,
                                               Map<String, String> extraParams,
                                               String defaultTokenType,
                                               boolean setAuthorizationHeader)
    throws OAuthServiceException {

    if (accessTokenService == null) {
        throw new OAuthServiceException(OAuthConstants.SERVER_ERROR);
    }

    Form form = new Form(grant.toMap());
    if (extraParams != null) {
        for (Map.Entry<String, String> entry : extraParams.entrySet()) {
            form.param(entry.getKey(), entry.getValue());
        }
    }
    if (consumer != null) {
        boolean secretAvailable = !StringUtils.isEmpty(consumer.getClientSecret());
        if (setAuthorizationHeader && secretAvailable) {
            accessTokenService.replaceHeader(HttpHeaders.AUTHORIZATION,
                DefaultBasicAuthSupplier.getBasicAuthHeader(consumer.getClientId(), consumer.getClientSecret()));
        } else {
            form.param(OAuthConstants.CLIENT_ID, consumer.getClientId());
            if (secretAvailable) {
                form.param(OAuthConstants.CLIENT_SECRET, consumer.getClientSecret());
            }
        }
    } else {
        // in this case the AccessToken service is expected to find a mapping between
        // the authenticated credentials and the client registration id
    }
    Response response = accessTokenService.form(form);
    final Map<String, String> map;
    try {
        map = response.getMediaType() == null
                || response.getMediaType().isCompatible(MediaType.APPLICATION_JSON_TYPE)
                        ? new OAuthJSONProvider().readJSONResponse((InputStream) response.getEntity())
                        : Collections.emptyMap();
    } catch (Exception ex) {
        throw new ResponseProcessingException(response, ex);
    }
    if (200 == response.getStatus()) {
        ClientAccessToken token = fromMapToClientToken(map, defaultTokenType);
        if (token == null) {
            throw new OAuthServiceException(OAuthConstants.SERVER_ERROR);
        }
        return token;
    } else if (response.getStatus() >= 400 && map.containsKey(OAuthConstants.ERROR_KEY)) {
        OAuthError error = new OAuthError(map.get(OAuthConstants.ERROR_KEY),
                                          map.get(OAuthConstants.ERROR_DESCRIPTION_KEY));
        error.setErrorUri(map.get(OAuthConstants.ERROR_URI_KEY));
        throw new OAuthServiceException(error);
    }
    throw new OAuthServiceException(OAuthConstants.SERVER_ERROR);
}