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

The following examples show how to use org.apache.cxf.jaxrs.client.WebClient#path() . 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: SakaiScriptGetPlacementIdTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testGetPlacementId() {
	WebClient client = WebClient.create(getFullEndpointAddress());

	addClientMocks(client);

	// client call
	client.accept("text/plain");
	client.path("/" + getOperation());
	client.query("sessionid", SESSION_ID);
	client.query("siteId", "site");
	client.query("toolId", "tool");

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

	// test verifications
	assertNotNull(result);
	assertEquals("tool_config_id", result);
}
 
Example 2
Source File: SakaiScriptCheckSessionTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testCheckSession() {
	WebClient client = WebClient.create(getFullEndpointAddress());

	addClientMocks(client);

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

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

	// test verifications
	assertNotNull(result);
	assertEquals(SESSION_ID, result);
}
 
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: SakaiScriptSetUserTimeZoneTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testSetUserTimeZone() {
	WebClient client = WebClient.create(getFullEndpointAddress());

	addClientMocks(client);

	// client call
	client.accept("text/plain");
	client.path("/" + getOperation());
	client.query("sessionid", SESSION_ID);
	client.query("eid", "admin");
	client.query("timeZoneId", "Europe/Oslo");


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

	// test verifications
	assertNotNull(result);
	assertEquals("success", result);
}
 
Example 5
Source File: JAXRSHttpsBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBook123WebClientFromSpringWildcard() throws Exception {
    ClassPathXmlApplicationContext ctx =
        new ClassPathXmlApplicationContext(new String[] {CLIENT_CONFIG_FILE5});
    Object bean = ctx.getBean("bookService.proxyFactory");
    assertNotNull(bean);
    JAXRSClientFactoryBean cfb = (JAXRSClientFactoryBean) bean;

    WebClient wc = (WebClient)cfb.create();
    assertEquals("https://localhost:" + PORT, wc.getBaseURI().toString());

    wc.accept("application/xml");
    wc.path("bookstore/securebooks/123");
    TheBook b = wc.get(TheBook.class);

    assertEquals(b.getId(), 123);
    b = wc.get(TheBook.class);
    assertEquals(b.getId(), 123);
    ctx.close();
}
 
Example 6
Source File: OIDCKeysServiceTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@org.junit.Test
public void testGetJWKHMACExplicitlyAllowed() throws Exception {
    URL busFile = OIDCFlowTest.class.getResource("client.xml");

    String address = "https://localhost:" + JCACHE_SERVER.getPort() + "/services5/";
    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);

    // Here we explicitly allow sending back secret keys
    assertEquals(1, jsonWebKeys.getKeys().size());
}
 
Example 7
Source File: OIDCKeysServiceTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@org.junit.Test
public void testGetJWKECPublicKey() throws Exception {
    URL busFile = OIDCFlowTest.class.getResource("client.xml");

    String address = "https://localhost:" + JCACHE_SERVER.getPort() + "/services3/";
    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.EC, jsonWebKey.getKeyType());
    assertEquals("ECKey", jsonWebKey.getKeyId());
    assertNotNull(jsonWebKey.getProperty("x"));
    assertNotNull(jsonWebKey.getProperty("y"));
    // Check we don't send the private key back
    checkPrivateKeyParametersNotPresent(jsonWebKeys);
}
 
Example 8
Source File: ContentHostingSiteHideResources.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testSiteHideResourcesWithPermissionError() {
	WebClient client = WebClient.create(getFullEndpointAddress());

	addClientMocks(client);

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

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

	// test verifications
	assertNotNull(result);
	assertEquals("failure", result);
}
 
Example 9
Source File: TestService.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testEntitledAttributesJSON() {
    if (!waitForWADL()) {
        return;
    }

    WebClient client = WebClient.create(ENDPOINT_ADDRESS);

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

    client.path("entitled-attribs");

    String request = readReource("json/request-entitled-attribs-1.json");
    String response = readReource("json/response-entitled-attribs-1.json");

    String webRespose = client.post(request, String.class);
    assertEquals(response, webRespose);
}
 
Example 10
Source File: JAXRSClientServerNonSpringBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBook123UserModelAuthorize() throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress("http://localhost:" + PORT + "/usermodel/bookstore/books");
    bean.setUsername("Barry");
    bean.setPassword("password");
    bean.setModelRef("classpath:org/apache/cxf/systest/jaxrs/resources/resources.xml");
    WebClient proxy = bean.createWebClient();
    proxy.path("{id}/authorize", 123);

    Book book = proxy.get(Book.class);
    assertEquals(123L, book.getId());



}
 
Example 11
Source File: SakaiLoginLoginTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testLoginFailed() {
	WebClient client = WebClient.create(getFullEndpointAddress());

	addClientMocks(client);

	// client call
	client.accept("text/plain");
	client.path("/" + getOperation());
	Form form = new Form();
	form.param("id", "admin").param("pw", "fail"); 

	// client result
	thrown.expect(RuntimeException.class);
	client.post(form, String.class);
}
 
Example 12
Source File: TestService.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testPdpJSON() {
    if (!waitForWADL()) {
        return;
    }

    WebClient client = WebClient.create(ENDPOINT_ADDRESS);

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

    client.path("pdp");

    String request = readReource("json/request-pdp-1.json");
    String response = readReource("json/response-pdp-1.json");

    String webRespose = client.post(request, String.class);
    assertEquals(response, webRespose);
}
 
Example 13
Source File: TestService.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testPdpXML() {
    if (!waitForWADL()) {
        return;
    }

    WebClient client = WebClient.create(ENDPOINT_ADDRESS);

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

    client.path("pdp");

    String request = readReource("xml/request-pdp-1.xml");
    String response = readReource("xml/response-pdp-1.xml");

    String webRespose = client.post(request, String.class);
    assertEquals(response, webRespose);
}
 
Example 14
Source File: SakaiScriptRemoveMemberFromSiteBatchTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
	public void testRemoveMemberFromSiteBatchNotExistingSite() {
		WebClient client = WebClient.create(getFullEndpointAddress());
//MODIFY
		addClientMocks(client);

		// client call
		client.accept("text/plain");
		client.path("/" + getOperation());
		client.query("sessionid", SESSION_ID);
		client.query("siteid", "nosite");
		client.query("eids", "user1,nouser");

		// client result
		thrown.expect(RuntimeException.class);
		client.get(String.class);

	}
 
Example 15
Source File: OAuth2TestUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static String getLocation(WebClient client, OAuthAuthorizationData authzData, String state) {

        // Now call "decision" to get the authorization code grant
        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());
        if (authzData.getNonce() != null) {
            form.param("nonce", authzData.getNonce());
        }
        if (authzData.getProposedScope() != null) {
            form.param("scope", authzData.getProposedScope());
        }
        if (authzData.getState() != null) {
            form.param("state", authzData.getState());
        }
        if (authzData.getClientCodeChallenge() != null) {
            form.param("code_challenge", authzData.getClientCodeChallenge());
        }
        form.param("response_type", authzData.getResponseType());
        form.param("oauthDecision", "allow");

        Response response = client.post(form);
        String location = response.getHeaderString("Location");
        if (state != null) {
            Assert.assertTrue(location.contains("state=" + state));
        }

        return location;
    }
 
Example 16
Source File: AuthorizationGrantTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testAuthorizationCodeGrantPOST() throws Exception {
    String address = "https://localhost:" + port + "/services/";
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(),
                                        "alice", "security", null);
    // 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("redirect_uri", "http://www.blah.apache.org");
    form.param("response_type", "code");

    OAuthAuthorizationData authzData = client.post(form, 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", null);

    ClientAccessToken accessToken =
        OAuth2TestUtils.getAccessTokenWithAuthorizationCode(client, code);
    assertNotNull(accessToken.getTokenKey());

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

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

    // Create the SAML Assertion
    String assertion = OAuth2TestUtils.createToken(address + "token", false, true);

    // Get Access Token
    client.type("application/x-www-form-urlencoded").accept("application/json");
    client.path("token");

    Form form = new Form();
    form.param("grant_type", "urn:ietf:params:oauth:grant-type:saml2-bearer");
    form.param("assertion", Base64UrlUtility.encode(assertion));
    form.param("client_id", "consumer-id");

    try {
        Response response = client.post(form);
        response.readEntity(ClientAccessToken.class);
        fail("Failure expected on a SAML 1.1 assertion");
    } catch (Exception ex) {
        // expected
    }
}
 
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: AuthorizationGrantNegativeTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testSAMLHolderOfKey() throws Exception {
    URL busFile = AuthorizationGrantNegativeTest.class.getResource("client.xml");

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

    // Create the SAML Assertion
    SamlCallbackHandler samlCallbackHandler = new SamlCallbackHandler(true);
    samlCallbackHandler.setConfirmationMethod(SAML2Constants.CONF_HOLDER_KEY);
    samlCallbackHandler.setAudience(address + "token");

    SAMLCallback samlCallback = new SAMLCallback();
    SAMLUtil.doSAMLCallback(samlCallbackHandler, samlCallback);

    SamlAssertionWrapper samlAssertion = new SamlAssertionWrapper(samlCallback);
    samlAssertion.signAssertion(
        samlCallback.getIssuerKeyName(),
        samlCallback.getIssuerKeyPassword(),
        samlCallback.getIssuerCrypto(),
        samlCallback.isSendKeyValue(),
        samlCallback.getCanonicalizationAlgorithm(),
        samlCallback.getSignatureAlgorithm()
    );
    String assertion = samlAssertion.assertionToString();

    // Get Access Token
    client.type("application/x-www-form-urlencoded").accept("application/json");
    client.path("token");

    Form form = new Form();
    form.param("grant_type", "urn:ietf:params:oauth:grant-type:saml2-bearer");
    form.param("assertion", Base64UrlUtility.encode(assertion));
    form.param("client_id", "consumer-id");

    try {
        Response response = client.post(form);
        response.readEntity(ClientAccessToken.class);
        fail("Failure expected on an incorrect subject confirmation method");
    } catch (Exception ex) {
        // expected
    }
}
 
Example 20
Source File: AuthorizationGrantNegativeTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testRepeatAuthorizationCode() 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 Authorization Code
    String code = OAuth2TestUtils.getAuthorizationCode(client);
    assertNotNull(code);

    // Now get the access token
    client = WebClient.create(address, OAuth2TestUtils.setupProviders(),
                              "consumer-id", "this-is-a-secret", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(
        org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);

    client.type("application/x-www-form-urlencoded").accept("application/json");
    client.path("token");

    // First invocation
    Form form = new Form();
    form.param("grant_type", "authorization_code");
    form.param("code", code);
    form.param("client_id", "consumer-id");
    Response response = client.post(form);
    ClientAccessToken token = response.readEntity(ClientAccessToken.class);
    assertNotNull(token.getTokenKey());

    // Now try to get a second token
    response = client.post(form);
    try {
        response.readEntity(ClientAccessToken.class);
        fail("Failure expected on trying to get a second access token");
    } catch (ResponseProcessingException ex) {
        //expected
    }
}