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

The following examples show how to use org.apache.cxf.jaxrs.client.WebClient#get() . 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: RestRequestTandemUseSpringRest.java    From activiti-in-action-codes with Apache License 2.0 6 votes vote down vote up
private static JsonNode queryLastVersionOfLeaveProcess() {
    WebClient client = createClient("repository/process-definitions?key=leave&latest=true");
    Response response = client.get();

    StringWriter writer = new StringWriter();
    InputStream stream = (InputStream) response.getEntity();
    try {
        IOUtils.copy(stream, writer, "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
    }
    String respText = writer.toString();
    System.out.println(respText);

    response = client.get();
    // 转换并输出响应结果
    return printResult("查询最新版本的请假流程定义", response);
}
 
Example 2
Source File: STSRESTTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@org.junit.Test
public void testIssueJWTTokenClaims() throws Exception {
    WebClient client = webClient()
        .path("jwt")
        .accept(MediaType.TEXT_PLAIN);

    // First check that the role isn't usually in the generated token

    String token = client.get(String.class);
    JwtToken jwt = validateJWTToken(token);

    assertNull(jwt.getClaim("roles"));

    // Now get another token specifying the role
    client.query("claim", "roles");

    token = client.get(String.class);
    jwt = validateJWTToken(token);

    assertEquals("ordinary-user", jwt.getClaim("roles"));
}
 
Example 3
Source File: SakaiScriptAddMemberToSiteWithRoleBatchTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testRemoveMemberFromSiteBatch() {
	WebClient client = WebClient.create(getFullEndpointAddress());

	addClientMocks(client);

	// client call
	client.accept("text/plain");
	client.path("/" + getOperation());
	client.query("sessionid", SESSION_ID);
	client.query("siteid", "site1");
	client.query("eids", "user1,user2");
	client.query("roleid", "student");

	// client result
	String result = client.get(String.class);

	// test verifications
	assertNotNull(result);
	assertEquals("success", result);
}
 
Example 4
Source File: BraveTracingTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testThatNewSpanIsCreatedOnClientTimeout() {
    final WebClient client = WebClient
        .create("http://localhost:" + PORT + "/bookstore/books/long", Collections.emptyList(),
            Arrays.asList(new BraveClientFeature(brave)), null)
        .accept(MediaType.APPLICATION_JSON);

    HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
    httpClientPolicy.setConnectionTimeout(100);
    httpClientPolicy.setReceiveTimeout(100);
    WebClient.getConfig(client).getHttpConduit().setClient(httpClientPolicy);

    expectedException.expect(ProcessingException.class);
    try {
        client.get();
    } finally {
        await().atMost(Duration.ofSeconds(1L)).until(()-> TestSpanReporter.getAllSpans().size() == 2);
        assertThat(TestSpanReporter.getAllSpans().size(), equalTo(2));
        assertThat(TestSpanReporter.getAllSpans().get(0).name(), equalTo("get " + client.getCurrentURI()));
        assertThat(TestSpanReporter.getAllSpans().get(0).tags(), hasKey("error"));
        assertThat(TestSpanReporter.getAllSpans().get(1).name(), equalTo("get /bookstore/books/long"));
    }
}
 
Example 5
Source File: PeerWebClient.java    From peer-os with Apache License 2.0 6 votes vote down vote up
public PeerResources getResourceLimits( final PeerId peerId ) throws PeerException
{
    WebClient client = null;
    Response response;
    try
    {
        remotePeer.checkRelation();
        String path = String.format( "/limits/%s", peerId.getId() );

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

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

    return WebClientBuilder.checkResponse( response, PeerResources.class );
}
 
Example 6
Source File: SakaiScriptGetParentSiteTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testGetParentSite() {
	WebClient client = WebClient.create(getFullEndpointAddress());

	addClientMocks(client);

	// client call
	client.accept("text/plain");
	client.path("/" + getOperation());
	client.query("sessionid", SESSION_ID);
	client.query("siteid", "xxx");

	// client result
	String result = client.get(String.class);

	// test verifications
	assertNotNull(result);
	assertEquals("yyy", result);
}
 
Example 7
Source File: OIDCKeysServiceTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@org.junit.Test
public void testGetJWKRSAPublicKey() throws Exception {
    URL busFile = OIDCFlowTest.class.getResource("client.xml");

    String address = "https://localhost:" + JCACHE_SERVER.getPort() + "/services2/";
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(),
                                        "alice", "security", busFile.toString());
    client.accept("application/json");

    client.path("keys/");
    Response response = client.get();
    JsonWebKeys jsonWebKeys = response.readEntity(JsonWebKeys.class);

    assertEquals(1, jsonWebKeys.getKeys().size());

    JsonWebKey jsonWebKey = jsonWebKeys.getKeys().get(0);
    assertEquals(KeyType.RSA, jsonWebKey.getKeyType());
    assertEquals("2011-04-29", jsonWebKey.getKeyId());
    assertNotNull(jsonWebKey.getProperty("n"));
    assertNotNull(jsonWebKey.getProperty("e"));
    // Check we don't send the private key back
    checkPrivateKeyParametersNotPresent(jsonWebKeys);
}
 
Example 8
Source File: JAXRSUriInfoMatchTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testMatchedUrisParam2() throws Exception {
    WebClient wc = WebClient.create("http://localhost:" + PORT
                                    + "/match/my/resource/1/matched/uris/param/2");
    WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L);
    wc.accept("text/plain");
    String data = wc.get(String.class);
    assertEquals("my/resource/1/matched/uris/param/2,my/resource/1", data);
}
 
Example 9
Source File: PeerWebClient.java    From peer-os with Apache License 2.0 5 votes vote down vote up
public HostInterfaces getInterfaces() throws PeerException
{
    WebClient client = null;
    Response response;
    try
    {
        remotePeer.checkRelation();
        String path = "/interfaces";

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

        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 interfaces: %s", e.getMessage() ) );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    return WebClientBuilder.checkResponse( response, HostInterfaces.class );
}
 
Example 10
Source File: JAXRSClientServerSpringBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetDefaultBookMatrixParam() throws Exception {
    String endpointAddress = "http://localhost:" + PORT + "/the/bookstore/2/";
    WebClient wc = WebClient.create(endpointAddress);
    wc.matrix("a", "b");
    wc.accept("application/json");
    WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
    Book book = wc.get(Book.class);
    assertEquals(123L, book.getId());
    assertEquals("Defaultb", book.getName());
}
 
Example 11
Source File: RegistrationClientImpl.java    From peer-os with Apache License 2.0 5 votes vote down vote up
@Override
public RegistrationStatus getStatus( String destinationHost, String peerId )
{
    WebClient client = null;
    Response response;

    try
    {
        client = restUtil.getTrustedWebClient( buildUrl( destinationHost, "status/" + peerId ), provider );
        client.type( MediaType.APPLICATION_JSON );
        client.accept( MediaType.APPLICATION_JSON );

        response = client.get();

        return WebClientBuilder.checkResponse( response, RegistrationStatus.class );
    }
    catch ( Exception e )
    {
        LOG.warn( e.getMessage() );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    return RegistrationStatus.OFFLINE;
}
 
Example 12
Source File: EnvironmentWebClient.java    From peer-os with Apache License 2.0 5 votes vote down vote up
public SshKeys getContainerAuthorizedKeys( final ContainerId containerId ) throws PeerException
{
    WebClient client = null;
    Response response;
    try
    {
        String path = String.format( "/%s/container/%s/sshkeys", 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 reading authorized keys of container: " + e.getMessage() );
    }
    finally
    {
        WebClientBuilder.close( client );
    }

    return WebClientBuilder.checkResponse( response, SshKeys.class );
}
 
Example 13
Source File: JAXRSClientServerNonSpringBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetPathFromUriInfo() throws Exception {
    String address = "http://localhost:" + PORT + "/application/bookstore/uifromconstructor";
    WebClient wc = WebClient.create(address);
    wc.accept("text/plain");
    String response = wc.get(String.class);
    assertEquals(address + "?prop=cxf", response);

}
 
Example 14
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetBookSameUriAutoRedirect() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/redirect?sameuri=true";
    WebClient wc = WebClient.create(address);
    WebClient.getConfig(wc).getHttpConduit().getClient().setAutoRedirect(true);
    WebClient.getConfig(wc).getRequestContext().put(
        org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    Response r = wc.get();
    Book book = r.readEntity(Book.class);
    assertEquals(123L, book.getId());
    String requestUri = r.getStringHeaders().getFirst("RequestURI");
    assertEquals("http://localhost:" + PORT + "/bookstore/redirect?redirect=true", requestUri);
    String theCookie = r.getStringHeaders().getFirst("TheCookie");
    assertEquals("b", theCookie);
}
 
Example 15
Source File: AbstractJAXRSContinuationsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testResumeFromFastAppThread() throws Exception {
    WebClient wc = WebClient.create("http://localhost:" + getPort() + getBaseAddress2()
        + "/books/resumeFromFastThread");
    wc.accept("text/plain");
    String str = wc.get(String.class);
    assertEquals("resumeFromFastThread", str);
}
 
Example 16
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlockAndThrowException() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/blockAndThrowException";
    WebClient wc = WebClient.create(address);
    Response r = wc.get();
    assertEquals(500, r.getStatus());
}
 
Example 17
Source File: JAXRSHttpsBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetBook123ProxyToWebClient() throws Exception {

    BookStore bs = JAXRSClientFactory.create("https://localhost:" + PORT, BookStore.class,
                                             CLIENT_CONFIG_FILE1);
    Book b = bs.getSecureBook("123");
    assertEquals(b.getId(), 123);
    WebClient wc = WebClient.fromClient(WebClient.client(bs));
    wc.path("/bookstore/securebooks/123").accept(MediaType.APPLICATION_XML_TYPE);
    Book b2 = wc.get(Book.class);
    assertEquals(123, b2.getId());
}
 
Example 18
Source File: JAXRSSimpleRequestDispatcherTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRedirectedBook() throws Exception {
    String address = "http://localhost:" + PORT + "/dispatch/bookstore2/books/redirectStart";
    WebClient client = WebClient.create(address);
    WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(10000000L);
    client.accept("application/json");
    Book book = client.get(Book.class);
    assertEquals("Redirect complete: /dispatch/bookstore/books/redirectComplete", book.getName());
}
 
Example 19
Source File: STSRESTTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testValidateJWTAndIssueSAML() throws Exception {
    WebClient client = webClient()
        .path("jwt")
        .accept(MediaType.TEXT_PLAIN);

    // 1. Get a token via GET
    String token = client.get(String.class);
    assertNotNull(token);

    // 2. Now validate it in the STS using POST
    client = webClient()
        .query("action", "validate")
        .type(MediaType.APPLICATION_XML)
        .accept(MediaType.APPLICATION_XML);

    // Create RequestSecurityToken
    W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
    writer.writeStartElement("wst", "RequestSecurityToken", WST_NS_05_12);

    writer.writeStartElement("wst", "RequestType", WST_NS_05_12);
    writer.writeCharacters(WST_NS_05_12 + "/Validate");
    writer.writeEndElement();

    writer.writeStartElement("wst", "TokenType", WST_NS_05_12);
    writer.writeCharacters(SAML2_TOKEN_TYPE);
    writer.writeEndElement();

    writer.writeStartElement("wst", "ValidateTarget", WST_NS_05_12);
    writer.writeStartElement("TokenWrapper");
    writer.writeCharacters(token);
    writer.writeEndElement();
    writer.writeEndElement();

    writer.writeEndElement();

    RequestSecurityTokenResponseType securityResponse = client.post(
        new DOMSource(writer.getDocument().getDocumentElement()),
        RequestSecurityTokenResponseType.class);

    assertTrue(getValidationStatus(securityResponse));

    // Check the token
    validateSAMLSecurityTokenResponse(securityResponse, true);
}
 
Example 20
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testBookWithSpace() throws Exception {
    WebClient client = WebClient.create("http://localhost:" + PORT + "/bookstore/").path("the books/123");
    Book book = client.get(Book.class);
    assertEquals(123L, book.getId());
}