Java Code Examples for org.jboss.resteasy.client.jaxrs.ResteasyWebTarget#register()

The following examples show how to use org.jboss.resteasy.client.jaxrs.ResteasyWebTarget#register() . 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: CompactDiscsDatabaseClient.java    From maven-framework-project with MIT License 6 votes vote down vote up
public static List<String> getCompactDiscs() {

		ResteasyClient rsClient = new ResteasyClientBuilder().disableTrustManager().build();

		Form form = new Form().param("grant_type", "client_credentials");
		ResteasyWebTarget resourceTarget = rsClient.target(urlAuth);
		resourceTarget.register(new BasicAuthentication("andres", "andres"));
		AccessTokenResponse accessToken = resourceTarget.request().post(Entity.form(form),
				AccessTokenResponse.class);
		try {
			String bearerToken = "Bearer " + accessToken.getToken();
			Response response = rsClient.target(urlDiscs).request()
					.header(HttpHeaders.AUTHORIZATION, bearerToken).get();
			return response.readEntity(new GenericType<List<String>>() {
			});
		} finally {
			rsClient.close();
		}

	}
 
Example 2
Source File: AbstractScimClient.java    From SCIM-Client with Apache License 2.0 5 votes vote down vote up
AbstractScimClient(String domain, Class<T> serviceClass) {
    /*
     Configures a proxy to interact with the service using the new JAX-RS 2.0 Client API, see section
     "Resteasy Proxy Framework" of RESTEasy JAX-RS user guide
     */
    if (System.getProperty("httpclient.multithreaded") == null) {
        client = new ResteasyClientBuilder().build();
    } else {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        //Change defaults if supplied
        getIntegerProperty("httpclient.multithreaded.maxtotal").ifPresent(cm::setMaxTotal);
        getIntegerProperty("httpclient.multithreaded.maxperroute").ifPresent(cm::setDefaultMaxPerRoute);
        getIntegerProperty("httpclient.multithreaded.validateafterinactivity").ifPresent(cm::setValidateAfterInactivity);

        logger.debug("Using multithreaded support with maxTotalConnections={} and maxPerRoutConnections={}", cm.getMaxTotal(), cm.getDefaultMaxPerRoute());
        logger.warn("Ensure your oxTrust 'rptConnectionPoolUseConnectionPooling' property is set to true");

        CloseableHttpClient httpClient = HttpClients.custom()
	.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
        		.setConnectionManager(cm).build();
        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);

        client = new ResteasyClientBuilder().httpEngine(engine).build();
    }
    ResteasyWebTarget target = client.target(domain);

    scimService = target.proxy(serviceClass);
    target.register(ListResponseProvider.class);
    target.register(AuthorizationInjectionFilter.class);
    target.register(ScimResourceProvider.class);

    ClientMap.update(client, null);
}
 
Example 3
Source File: OauthClientTest.java    From maven-framework-project with MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {

		String truststorePath = "C:/Users/Andres/jboss/2do_jboss/jboss-as-7.1.1.Final/standalone/configuration/client-truststore.ts";
		String truststorePassword = "changeit";
		truststorePath = EnvUtil.replace(truststorePath);

		KeyStore truststore = loadKeyStore(truststorePath, truststorePassword);

		ResteasyClient client = new ResteasyClientBuilder()
				.disableTrustManager().trustStore(truststore).build();

		Form form = new Form().param("grant_type", "client_credentials");
		ResteasyWebTarget target = client
				.target("https://localhost:8443/oauth-server/j_oauth_token_grant");
		target.register(new BasicAuthentication("andres", "andres"));

		AccessTokenResponse tokenResponse = target.request().post(
				Entity.form(form), AccessTokenResponse.class);
		Response response = client
				.target("https://localhost:8443/discstore/discs")
				.request()
				.header(HttpHeaders.AUTHORIZATION,
						"Bearer " + tokenResponse.getToken()).get();
		try {
			String xml = response.readEntity(String.class);
			System.out.println(xml);
		} finally {
			client.close();
		}

	}
 
Example 4
Source File: Keycloak.java    From keycloak with Apache License 2.0 5 votes vote down vote up
Keycloak(String serverUrl, String realm, String username, String password, String clientId, String clientSecret, String grantType, Client resteasyClient, String authtoken) {
    config = new Config(serverUrl, realm, username, password, clientId, clientSecret, grantType);
    client = resteasyClient != null ? resteasyClient : newRestEasyClient(null, null, false);
    authToken = authtoken;
    tokenManager = authtoken == null ? new TokenManager(config, client) : null;

    target = (ResteasyWebTarget) client.target(config.getServerUrl());
    target.register(newAuthFilter());
}
 
Example 5
Source File: TokenManager.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public TokenManager(Config config, Client client) {
    this.config = config;
    ResteasyWebTarget target = (ResteasyWebTarget) client.target(config.getServerUrl());
    if (!config.isPublicClient()) {
        target.register(new BasicAuthFilter(config.getClientId(), config.getClientSecret()));
    }
    this.tokenService = target.proxy(TokenService.class);
    this.accessTokenGrantType = config.getGrantType();

    if (CLIENT_CREDENTIALS.equals(accessTokenGrantType) && config.isPublicClient()) {
        throw new IllegalArgumentException("Can't use " + GRANT_TYPE + "=" + CLIENT_CREDENTIALS + " with public client");
    }
}
 
Example 6
Source File: AbstractStoresTest.java    From alfresco-simple-content-stores with Apache License 2.0 3 votes vote down vote up
/**
 * Initialised a simple Java facade for calls to a particular Alfresco Public ReST API interface.
 *
 * @param client
 *            the client to use for making ReST API calls
 * @param baseUrl
 *            the base URL of the Alfresco instance
 * @param api
 *            the API interface to facade
 * @param ticket
 *            the authentication ticket to use for calls to the API
 * @return the Java facade of the API
 */
protected static <T> T createAPI(final ResteasyClient client, final String baseUrl, final Class<T> api, final String ticket)
{
    final ResteasyWebTarget targetServer = client.target(UriBuilder.fromPath(baseUrl));

    final ClientRequestFilter rqAuthFilter = (requestContext) -> {
        final String base64Token = Base64.encodeBase64String(ticket.getBytes(StandardCharsets.UTF_8));
        requestContext.getHeaders().add("Authorization", "Basic " + base64Token);
    };
    targetServer.register(rqAuthFilter);

    return targetServer.proxy(api);
}