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

The following examples show how to use org.apache.cxf.jaxrs.client.WebClient#type() . 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: TestService.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testHomeJSON() {
    if (!waitForWADL()) {
        return;
    }

    WebClient client = WebClient.create(ENDPOINT_ADDRESS);

    client.header("Authorization", "Basic YWRtaW46YWRtaW4=");
    client.type("application/json");
    client.accept("application/json");

    client.path("home");

    String response = readReource("json/response-home.json");

    String webRespose = client.get(String.class);
    assertEquals(response, webRespose);
}
 
Example 2
Source File: RegistrationClientImpl.java    From peer-os with Apache License 2.0 6 votes vote down vote up
@Override
public PeerInfo getPeerInfo( final String destinationHost ) throws PeerException
{

    WebClient client = null;
    Response response;

    try
    {
        client = restUtil.getTrustedWebClient( buildUrl( destinationHost, "info" ), provider );
        client.type( MediaType.APPLICATION_JSON );
        client.accept( MediaType.APPLICATION_JSON );

        response = client.get();
    }
    catch ( Exception e )
    {
        throw new PeerException( String.format( "Can not connect to '%s'.", destinationHost ) );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    return WebClientBuilder.checkResponse( response, PeerInfo.class );
}
 
Example 3
Source File: EnvironmentWebClient.java    From peer-os with Apache License 2.0 6 votes vote down vote up
public void excludePeerFromEnvironment( final String environmentId, final String peerId ) throws PeerException
{
    WebClient client = null;
    Response response;
    try
    {
        remotePeer.checkRelation();
        String path = String.format( "/%s/peers/%s/exclude", environmentId, peerId );
        client = WebClientBuilder.buildEnvironmentWebClient( peerInfo, path, provider );

        client.type( MediaType.APPLICATION_JSON );
        client.accept( MediaType.APPLICATION_JSON );
        response = client.post( null );
    }
    catch ( Exception e )
    {
        LOG.error( e.getMessage(), e );
        throw new PeerException( "Error excluding peer from environment: " + e.getMessage() );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    WebClientBuilder.checkResponse( response );
}
 
Example 4
Source File: PeerWebClient.java    From peer-os with Apache License 2.0 6 votes vote down vote up
public void joinOrUpdateP2PSwarm( final P2PConfig config ) throws PeerException
{
    WebClient client = null;
    Response response;
    try
    {
        remotePeer.checkRelation();
        String path = "/p2ptunnel";

        client = WebClientBuilder.buildPeerWebClient( peerInfo, path, provider );

        client.type( MediaType.APPLICATION_JSON );
        response = client.put( config );
    }
    catch ( Exception e )
    {
        LOG.error( e.getMessage(), e );
        throw new PeerException( String.format( "Error joining/updating P2P swarm: %s", e.getMessage() ) );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    WebClientBuilder.checkResponse( response );
}
 
Example 5
Source File: RestRequestTandemUseSpringRest.java    From activiti-in-action-codes with Apache License 2.0 5 votes vote down vote up
private static void completeTaskUseForm(String taskId, String taskName, List<Map<String, String>> properties) throws IOException {
    WebClient client = createClient("form/form-data");
    // 非常重要
    client.type("application/json;charset=UTF-8");
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("taskId", taskId);
    if (properties != null) {
        parameters.put("properties", properties);
    }

    String body = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(parameters);
    printJsonString("完成任务-Form[" + taskId + "-" + taskName + "]", body);
    Response response = client.post(body);
    printResult("完成任务-Form[" + taskId + "-" + taskName + "]", response);
}
 
Example 6
Source File: RestRequestTandem.java    From activiti-in-action-codes with Apache License 2.0 5 votes vote down vote up
private static void completeTask(String taskId, String taskName, List<Map<String, String>> variables) throws IOException {
    WebClient client = createClient("runtime/tasks/" + taskId);
    // 非常重要
    client.type("application/json;charset=UTF-8");
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("action", "complete");
    if (variables != null) {
        parameters.put("variables", variables);
    }

    String body = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(parameters);
    printJsonString("完成任务[" + taskId + "-" + taskName + "]", body);
    Response response = client.post(body);
    printResult("完成任务[" + taskId + "-" + taskName + "]", response);
}
 
Example 7
Source File: EnvironmentWebClient.java    From peer-os with Apache License 2.0 5 votes vote down vote up
public void destroyContainer( ContainerId containerId ) throws PeerException
{
    WebClient client = null;
    Response response;
    try
    {
        remotePeer.checkRelation();
        String path = String.format( "/%s/container/%s/destroy", containerId.getEnvironmentId().getId(),
                containerId.getId() );
        client = WebClientBuilder.buildEnvironmentWebClient( peerInfo, path, provider );

        client.type( MediaType.APPLICATION_JSON );
        client.accept( MediaType.APPLICATION_JSON );
        response = client.post( null );
    }
    catch ( Exception e )
    {
        LOG.error( e.getMessage(), e );
        throw new PeerException( "Error destroying container: " + e.getMessage() );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    WebClientBuilder.checkResponse( response );
}
 
Example 8
Source File: EnvironmentWebClient.java    From peer-os with Apache License 2.0 5 votes vote down vote up
public Quota getRawQuota( final ContainerId containerId ) throws PeerException
{
    WebClient client = null;
    Response response;
    try
    {
        remotePeer.checkRelation();
        String path = String.format( "/%s/container/%s/quota/raw", containerId.getEnvironmentId().getId(),
                containerId.getId() );
        client = WebClientBuilder.buildEnvironmentWebClient( peerInfo, path, provider, 5000L, 15000L, 1 );

        client.type( MediaType.APPLICATION_JSON );
        client.accept( MediaType.APPLICATION_JSON );
        response = client.get();
    }
    catch ( Exception e )
    {
        LOG.warn( e.getMessage() );
        throw new PeerException( "Error on reading container quota: " + e.getMessage() );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    return WebClientBuilder.checkResponse( response, Quota.class );
}
 
Example 9
Source File: PeerWebClient.java    From peer-os with Apache License 2.0 5 votes vote down vote up
public void cleanupEnvironment( final EnvironmentId environmentId ) throws PeerException
{
    WebClient client = null;
    Response response;
    try
    {
        remotePeer.checkRelation();
        String path = String.format( "/cleanup/%s", environmentId.getId() );

        client = WebClientBuilder.buildPeerWebClient( peerInfo, path, provider );

        client.type( MediaType.APPLICATION_JSON );
        client.accept( MediaType.APPLICATION_JSON );
        response = client.delete();
    }
    catch ( Exception e )
    {
        LOG.error( e.getMessage(), e );
        throw new PeerException( String.format( "Error cleaning up environment: %s", e.getMessage() ) );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    WebClientBuilder.checkResponse( response );
}
 
Example 10
Source File: EnvironmentWebClient.java    From peer-os with Apache License 2.0 5 votes vote down vote up
public Containers configureSshInEnvironment( final EnvironmentId environmentId, final SshKeys sshKeys )
        throws PeerException
{
    WebClient client = null;
    Response response;
    try
    {
        String path = String.format( "/%s/containers/sshkeys", environmentId.getId() );

        client = WebClientBuilder.buildEnvironmentWebClient( peerInfo, path, provider );

        client.type( MediaType.APPLICATION_JSON );
        client.accept( MediaType.APPLICATION_JSON );

        response = client.post( sshKeys );
    }
    catch ( Exception e )
    {
        LOG.error( e.getMessage(), e );
        throw new PeerException( "Error configuring ssh in environment: " + e.getMessage() );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    return WebClientBuilder.checkResponse( response, Containers.class );
}
 
Example 11
Source File: EnvironmentWebClient.java    From peer-os with Apache License 2.0 5 votes vote down vote up
public ContainerQuota getQuota( final ContainerId containerId ) throws PeerException
{
    WebClient client = null;
    Response response;
    try
    {
        remotePeer.checkRelation();
        String path = String.format( "/%s/container/%s/quota", containerId.getEnvironmentId().getId(),
                containerId.getId() );

        client = WebClientBuilder.buildEnvironmentWebClient( peerInfo, path, provider, 5000L, 15000L, 1 );

        client.type( MediaType.APPLICATION_JSON );
        client.accept( MediaType.APPLICATION_JSON );
        response = client.get();
    }
    catch ( Exception e )
    {
        LOG.error( e.getMessage(), e );
        throw new PeerException( "Error on obtaining available quota: " + e.getMessage() );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    return WebClientBuilder.checkResponse( response, ContainerQuota.class );
}
 
Example 12
Source File: RegistrationClientImpl.java    From peer-os with Apache License 2.0 5 votes vote down vote up
@Override
public void sendApproveRequest( String destinationHost, final RegistrationData registrationData )
        throws PeerException
{
    WebClient client = null;
    Response response;

    try
    {
        client = restUtil.getTrustedWebClient( buildUrl( destinationHost, "approve" ), provider );
        client.type( MediaType.APPLICATION_JSON );
        client.accept( MediaType.APPLICATION_JSON );

        response = client.post( registrationData );
    }
    catch ( Exception e )
    {
        throw new PeerException(
                String.format( "Error requesting remote peer '%s': %s.", destinationHost, e.getMessage() ) );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    WebClientBuilder.checkResponse( response, Response.Status.OK );
}
 
Example 13
Source File: PeerWebClient.java    From peer-os with Apache License 2.0 5 votes vote down vote up
public PeerInfo getInfo() throws PeerException
{
    WebClient client = null;
    Response response;
    try
    {
        remotePeer.checkRelation();

        String path = "/info";

        client = WebClientBuilder.buildPeerWebClient( peerInfo, path, provider, 3000, 15000, 1 );

        client.type( MediaType.APPLICATION_JSON );
        client.accept( MediaType.APPLICATION_JSON );

        response = client.get();
    }
    catch ( Exception e )
    {
        LOG.error( e.getMessage(), e );
        throw new PeerException( String.format( "Error getting peer info: %s", e.getMessage() ) );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    return WebClientBuilder.checkResponse( response, PeerInfo.class );
}
 
Example 14
Source File: PeerWebClient.java    From peer-os with Apache License 2.0 5 votes vote down vote up
public PublicKeyContainer createEnvironmentKeyPair( final RelationLinkDto envLink ) throws PeerException
{
    WebClient client = null;
    Response response;
    try
    {
        remotePeer.checkRelation();
        String path = "/pek";

        client = WebClientBuilder.buildPeerWebClient( peerInfo, path, provider );

        client.type( MediaType.APPLICATION_JSON );
        client.accept( MediaType.APPLICATION_JSON );

        response = client.post( envLink );
    }
    catch ( Exception e )
    {
        LOG.error( e.getMessage(), e );
        throw new PeerException( String.format( "Error creating peer environment key: %s", e.getMessage() ) );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    return WebClientBuilder.checkResponse( response, PublicKeyContainer.class );
}
 
Example 15
Source File: AuthorizationGrantNegativeTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testRefreshImplicitGrant() throws Exception {
    URL busFile = AuthorizationGrantTest.class.getResource("client.xml");

    String address = "https://localhost:" + port + "/services/";
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(),
                                        "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(
        org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);

    // Get Access Token
    client.type("application/json").accept("application/json");
    client.query("client_id", "consumer-id");
    client.query("redirect_uri", "http://www.blah.apache.org");
    client.query("response_type", "token");
    client.path("authorize-implicit/");
    Response response = client.get();

    OAuthAuthorizationData authzData = response.readEntity(OAuthAuthorizationData.class);

    // Now call "decision" to get the access token
    client.path("decision");
    client.type("application/x-www-form-urlencoded");

    Form form = new Form();
    form.param("session_authenticity_token", authzData.getAuthenticityToken());
    form.param("client_id", authzData.getClientId());
    form.param("redirect_uri", authzData.getRedirectUri());
    form.param("oauthDecision", "allow");

    response = client.post(form);

    String location = response.getHeaderString("Location");
    String accessToken = OAuth2TestUtils.getSubstring(location, "access_token");
    assertNotNull(accessToken);
    assertFalse(location.contains("refresh"));
}
 
Example 16
Source File: JAXRSMultipartTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testBookAsRootAttachmentInputStreamReadItself() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/books/istream2";
    WebClient wc = WebClient.create(address);
    wc.type("multipart/mixed;type=text/xml");
    wc.accept("text/xml");
    WebClient.getConfig(wc).getRequestContext().put("support.type.as.multipart",
        "true");
    Book book = wc.post(new Book("CXF in Action - 2", 12345L), Book.class);
    assertEquals(432L, book.getId());
}
 
Example 17
Source File: OIDCNegativeTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testImplicitFlowNoATHash() throws Exception {
    URL busFile = OIDCFlowTest.class.getResource("client.xml");

    String address = "https://localhost:" + port + "/services/";
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(),
                                        "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(
        org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);

    // Get Access Token
    client.type("application/json").accept("application/json");
    client.query("client_id", "consumer-id");
    client.query("redirect_uri", "http://www.blah.apache.org");
    client.query("scope", "openid");
    client.query("response_type", "id_token");
    client.query("nonce", "1234565635");
    client.query("max_age", "300");
    client.path("authorize-implicit/");
    Response response = client.get();

    OAuthAuthorizationData authzData = response.readEntity(OAuthAuthorizationData.class);

    // Now call "decision" to get the access token
    client.path("decision");
    client.type("application/x-www-form-urlencoded");

    Form form = new Form();
    form.param("session_authenticity_token", authzData.getAuthenticityToken());
    form.param("client_id", authzData.getClientId());
    form.param("redirect_uri", authzData.getRedirectUri());
    form.param("scope", authzData.getProposedScope());
    if (authzData.getResponseType() != null) {
        form.param("response_type", authzData.getResponseType());
    }
    if (authzData.getNonce() != null) {
        form.param("nonce", authzData.getNonce());
    }
    form.param("oauthDecision", "allow");

    response = client.post(form);

    String location = response.getHeaderString("Location");

    // Check IdToken
    String idToken = OAuth2TestUtils.getSubstring(location, "id_token");
    assertNotNull(idToken);

    JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(idToken);
    JwtToken jwt = jwtConsumer.getJwtToken();
    Assert.assertNull(jwt.getClaims().getClaim(IdToken.ACCESS_TOKEN_HASH_CLAIM));
}
 
Example 18
Source File: OIDCNegativeTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testImplicitFlowNoNonce() throws Exception {
    URL busFile = OIDCFlowTest.class.getResource("client.xml");

    String address = "https://localhost:" + port + "/services/";
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(),
                                        "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(
        org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);

    // Get Access Token
    client.type("application/json").accept("application/json");
    client.query("client_id", "consumer-id");
    client.query("redirect_uri", "http://www.blah.apache.org");
    client.query("scope", "openid");
    client.query("response_type", "id_token");
    client.path("authorize-implicit/");
    Response response = client.get();

    try {
        response.readEntity(OAuthAuthorizationData.class);
        fail("Failure expected on no nonce");
    } catch (Exception ex) {
        // expected
    }

    // Add a nonce and it should succeed
    String nonce = "1234565635";
    client.query("nonce", nonce);
    response = client.get();

    OAuthAuthorizationData authzData = response.readEntity(OAuthAuthorizationData.class);

    // Now call "decision" to get the access token
    client.path("decision");
    client.type("application/x-www-form-urlencoded");

    Form form = new Form();
    form.param("session_authenticity_token", authzData.getAuthenticityToken());
    form.param("client_id", authzData.getClientId());
    form.param("redirect_uri", authzData.getRedirectUri());
    form.param("scope", authzData.getProposedScope());
    if (authzData.getResponseType() != null) {
        form.param("response_type", authzData.getResponseType());
    }
    if (authzData.getNonce() != null) {
        form.param("nonce", authzData.getNonce());
    }
    form.param("oauthDecision", "allow");

    response = client.post(form);

    String location = response.getHeaderString("Location");

    // Check IdToken
    String idToken = OAuth2TestUtils.getSubstring(location, "id_token");
    assertNotNull(idToken);

    JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(idToken);
    JwtToken jwt = jwtConsumer.getJwtToken();
    // Check the nonce is in the idToken
    assertEquals(jwt.getClaim("nonce"), nonce);
}
 
Example 19
Source File: OIDCFlowTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testAuthorizationCodeFlowPOST() throws Exception {
    URL busFile = OIDCFlowTest.class.getResource("client.xml");

    String address = "https://localhost:" + port + "/services/";
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(),
                                        "alice", "security", busFile.toString());

    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(
        org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);

    // Make initial authorization request
    client.type("application/x-www-form-urlencoded");

    client.path("authorize/");

    Form form = new Form();
    form.param("client_id", "consumer-id");
    form.param("scope", "openid");
    form.param("redirect_uri", "http://www.blah.apache.org");
    form.param("response_type", "code");
    Response response = client.post(form);

    OAuthAuthorizationData authzData = response.readEntity(OAuthAuthorizationData.class);
    String location = OAuth2TestUtils.getLocation(client, authzData, null);
    String code =  OAuth2TestUtils.getSubstring(location, "code");
    assertNotNull(code);

    // Now get the access token
    client = WebClient.create(address, "consumer-id", "this-is-a-secret", busFile.toString());

    ClientAccessToken accessToken =
        OAuth2TestUtils.getAccessTokenWithAuthorizationCode(client, code);
    assertNotNull(accessToken.getTokenKey());
    assertTrue(accessToken.getApprovedScope().contains("openid"));

    String idToken = accessToken.getParameters().get("id_token");
    assertNotNull(idToken);
    validateIdToken(idToken, null);

    if (isAccessTokenInJWTFormat()) {
        validateAccessToken(accessToken.getTokenKey());
    }
}
 
Example 20
Source File: OIDCFlowTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testImplicitFlowNoAccessToken() throws Exception {
    URL busFile = OIDCFlowTest.class.getResource("client.xml");

    String address = "https://localhost:" + port + "/services/";
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(),
                                        "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(
        org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);

    // Get Access Token
    client.type("application/json").accept("application/json");
    client.query("client_id", "consumer-id");
    client.query("redirect_uri", "http://www.blah.apache.org");
    client.query("scope", "openid");
    client.query("response_type", "id_token");
    client.query("nonce", "123456789");
    client.path("authorize-implicit/");
    Response response = client.get();

    OAuthAuthorizationData authzData = response.readEntity(OAuthAuthorizationData.class);

    // Now call "decision" to get the access token
    client.path("decision");
    client.type("application/x-www-form-urlencoded");

    Form form = new Form();
    form.param("session_authenticity_token", authzData.getAuthenticityToken());
    form.param("client_id", authzData.getClientId());
    form.param("redirect_uri", authzData.getRedirectUri());
    form.param("scope", authzData.getProposedScope());
    if (authzData.getResponseType() != null) {
        form.param("response_type", authzData.getResponseType());
    }
    if (authzData.getNonce() != null) {
        form.param("nonce", authzData.getNonce());
    }
    form.param("oauthDecision", "allow");

    response = client.post(form);

    String location = response.getHeaderString("Location");

    // Check Access Token - it should not be present
    String accessToken = OAuth2TestUtils.getSubstring(location, "access_token");
    assertNull(accessToken);

    // Check IdToken
    String idToken = OAuth2TestUtils.getSubstring(location, "id_token");
    assertNotNull(idToken);
    validateIdToken(idToken, null);

    JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(idToken);
    JwtToken jwt = jwtConsumer.getJwtToken();
    assertNull(jwt.getClaims().getClaim(IdToken.ACCESS_TOKEN_HASH_CLAIM));
    assertNotNull(jwt.getClaims().getClaim(IdToken.NONCE_CLAIM));
}