Search in sources :

Example 41 with Client

use of com.sun.jersey.api.client.Client in project ORCID-Source by ORCID.

the class CrossRefManagerImpl method createResource.

private WebResource createResource(String searchTerms) {
    Client client = createClient();
    String encoded = null;
    try {
        encoded = URLEncoder.encode(searchTerms, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        LOGGER.error("search terms: " + searchTerms + "cannot be encoded", e);
    }
    WebResource resource = client.resource(crossRefUrl + "?" + queryString + encoded);
    return resource;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) WebResource(com.sun.jersey.api.client.WebResource) Client(com.sun.jersey.api.client.Client)

Example 42 with Client

use of com.sun.jersey.api.client.Client in project ORCID-Source by ORCID.

the class CrossRefManagerImpl method createClient.

private Client createClient() {
    Set<Class<?>> providers = new HashSet<Class<?>>();
    providers.add(JacksonJsonProvider.class);
    ClientConfig config = new DefaultClientConfig(providers);
    config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    Client client = ApacheHttpClient4.create(config);
    client.setFollowRedirects(true);
    client.addFilter(new ContentTypeFromTextToJsonClientFilter());
    return client;
}
Also used : ContentTypeFromTextToJsonClientFilter(org.orcid.core.utils.ContentTypeFromTextToJsonClientFilter) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) Client(com.sun.jersey.api.client.Client) HashSet(java.util.HashSet)

Example 43 with Client

use of com.sun.jersey.api.client.Client in project ORCID-Source by ORCID.

the class OpenIDConnectTest method checkIDTokenAndUserInfo.

//client must have openid scope.
@Test
public void checkIDTokenAndUserInfo() throws InterruptedException, JSONException, ParseException, URISyntaxException, JOSEException {
    //Get id token
    String clientId = getClient1ClientId();
    String clientRedirectUri = getClient1RedirectUri();
    String clientSecret = getClient1ClientSecret();
    String userId = getUser1OrcidId();
    String password = getUser1Password();
    String scope = "openid";
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("nonce", "yesMate");
    String authorizationCode = getAuthorizationCode(clientId, clientRedirectUri, scope, userId, password, true, params);
    assertNotNull(authorizationCode);
    ClientResponse tokenResponse = getAccessTokenResponse(clientId, clientSecret, clientRedirectUri, authorizationCode);
    assertEquals(200, tokenResponse.getStatus());
    String body = tokenResponse.getEntity(String.class);
    JSONObject tokenJSON = new JSONObject(body);
    String id = tokenJSON.getString("id_token");
    assertNotNull(id);
    SignedJWT signedJWT = SignedJWT.parse(id);
    Assert.assertEquals("https://orcid.org", signedJWT.getJWTClaimsSet().getIssuer());
    Assert.assertEquals("9999-0000-0000-0004", signedJWT.getJWTClaimsSet().getSubject());
    Assert.assertEquals("APP-9999999999999901", signedJWT.getJWTClaimsSet().getAudience().get(0));
    Assert.assertEquals("yesMate", signedJWT.getJWTClaimsSet().getClaim("nonce"));
    //get JWKS         
    Client client = Client.create();
    WebResource webResource = client.resource(baseUri + "/oauth/jwks");
    ClientResponse jwksResponse = webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
    String jwkString = jwksResponse.getEntity(String.class);
    RSAKey jwk = (RSAKey) JWK.parse(jwkString);
    //check sig
    JWSVerifier verifier = new RSASSAVerifier(jwk);
    Assert.assertTrue(signedJWT.verify(verifier));
    //get userinfo
    webResource = client.resource(baseUri + "/oauth/userinfo");
    ClientResponse userInfo = webResource.header("Authorization", "Bearer " + tokenJSON.getString("access_token")).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
    String userInfoString = userInfo.getEntity(String.class);
    JSONObject user = new JSONObject(userInfoString);
    Assert.assertEquals("9999-0000-0000-0004", user.get("sub"));
    Assert.assertEquals("User One Credit name", user.get("name"));
    Assert.assertEquals("One", user.get("family_name"));
    Assert.assertEquals("User", user.get("given_name"));
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) RSAKey(com.nimbusds.jose.jwk.RSAKey) JSONObject(org.codehaus.jettison.json.JSONObject) HashMap(java.util.HashMap) RSASSAVerifier(com.nimbusds.jose.crypto.RSASSAVerifier) JWSVerifier(com.nimbusds.jose.JWSVerifier) WebResource(com.sun.jersey.api.client.WebResource) SignedJWT(com.nimbusds.jwt.SignedJWT) Client(com.sun.jersey.api.client.Client) Test(org.junit.Test)

Example 44 with Client

use of com.sun.jersey.api.client.Client in project incubator-atlas by apache.

the class AtlasBaseClient method initializeState.

void initializeState(Configuration configuration, String[] baseUrls, UserGroupInformation ugi, String doAsUser) {
    this.configuration = configuration;
    Client client = getClient(configuration, ugi, doAsUser);
    if ((!AuthenticationUtil.isKerberosAuthenticationEnabled()) && basicAuthUser != null && basicAuthPassword != null) {
        final HTTPBasicAuthFilter authFilter = new HTTPBasicAuthFilter(basicAuthUser, basicAuthPassword);
        client.addFilter(authFilter);
    }
    String activeServiceUrl = determineActiveServiceURL(baseUrls, client);
    atlasClientContext = new AtlasClientContext(baseUrls, client, ugi, doAsUser);
    service = client.resource(UriBuilder.fromUri(activeServiceUrl).build());
}
Also used : Client(com.sun.jersey.api.client.Client) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Example 45 with Client

use of com.sun.jersey.api.client.Client in project incubator-atlas by apache.

the class AtlasBaseClient method getClient.

@VisibleForTesting
protected Client getClient(Configuration configuration, UserGroupInformation ugi, String doAsUser) {
    DefaultClientConfig config = new DefaultClientConfig();
    // Enable POJO mapping feature
    config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    int readTimeout = configuration.getInt("atlas.client.readTimeoutMSecs", 60000);
    int connectTimeout = configuration.getInt("atlas.client.connectTimeoutMSecs", 60000);
    if (configuration.getBoolean(TLS_ENABLED, false)) {
        // configuration object, persist it, then subsequently pass in an empty configuration to SSLFactory
        try {
            SecureClientUtils.persistSSLClientConfiguration(configuration);
        } catch (Exception e) {
            LOG.info("Error processing client configuration.", e);
        }
    }
    final URLConnectionClientHandler handler;
    if ((!AuthenticationUtil.isKerberosAuthenticationEnabled()) && basicAuthUser != null && basicAuthPassword != null) {
        if (configuration.getBoolean(TLS_ENABLED, false)) {
            handler = SecureClientUtils.getUrlConnectionClientHandler();
        } else {
            handler = new URLConnectionClientHandler();
        }
    } else {
        handler = SecureClientUtils.getClientConnectionHandler(config, configuration, doAsUser, ugi);
    }
    Client client = new Client(handler, config);
    client.setReadTimeout(readTimeout);
    client.setConnectTimeout(connectTimeout);
    return client;
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) URLConnectionClientHandler(com.sun.jersey.client.urlconnection.URLConnectionClientHandler) Client(com.sun.jersey.api.client.Client) ConnectException(java.net.ConnectException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

Client (com.sun.jersey.api.client.Client)87 ClientResponse (com.sun.jersey.api.client.ClientResponse)61 Test (org.junit.Test)59 URI (java.net.URI)51 TimelineEntity (org.apache.hadoop.yarn.api.records.timelineservice.TimelineEntity)36 Set (java.util.Set)30 WebResource (com.sun.jersey.api.client.WebResource)19 HashSet (java.util.HashSet)19 GenericType (com.sun.jersey.api.client.GenericType)17 DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)9 TimelineMetric (org.apache.hadoop.yarn.api.records.timelineservice.TimelineMetric)9 JSONObject (org.codehaus.jettison.json.JSONObject)7 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)6 URLConnectionClientHandler (com.sun.jersey.client.urlconnection.URLConnectionClientHandler)4 ArrayList (java.util.ArrayList)4 FlowRunEntity (org.apache.hadoop.yarn.api.records.timelineservice.FlowRunEntity)4 YarnClient (org.apache.hadoop.yarn.client.api.YarnClient)4 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)3 HTTPBasicAuthFilter (com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)3 JSONException (org.codehaus.jettison.json.JSONException)3