Search in sources :

Example 91 with MultivaluedMapImpl

use of com.sun.jersey.core.util.MultivaluedMapImpl in project ORCID-Source by ORCID.

the class InternalAPITest method testGetLastModifiedDate.

@Test
public void testGetLastModifiedDate() throws JSONException {
    MultivaluedMap<String, String> params = new MultivaluedMapImpl();
    params.add("client_id", client1ClientId);
    params.add("client_secret", client1ClientSecret);
    params.add("grant_type", "client_credentials");
    params.add("scope", ScopePathType.INTERNAL_PERSON_LAST_MODIFIED.value());
    ClientResponse clientResponse = oauthHelper.getResponse(params, APIRequestType.INTERNAL);
    assertNotNull(clientResponse);
    assertEquals(Response.Status.OK.getStatusCode(), clientResponse.getStatus());
    String body = clientResponse.getEntity(String.class);
    JSONObject jsonObject = new JSONObject(body);
    String accessToken = (String) jsonObject.get("access_token");
    assertNotNull(accessToken);
    assertFalse(PojoUtil.isEmpty(accessToken));
    ClientResponse lastModifiedResponse = internalApiClient.viewPersonLastModified(user1OrcidId, accessToken);
    assertNotNull(lastModifiedResponse);
    String lastModified = lastModifiedResponse.getEntity(String.class);
    jsonObject = new JSONObject(lastModified);
    assertNotNull(jsonObject);
    assertEquals(user1OrcidId, (String) jsonObject.getString("orcid"));
    assertNotNull((String) jsonObject.getString("last-modified"));
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) JSONObject(org.codehaus.jettison.json.JSONObject) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) Test(org.junit.Test)

Example 92 with MultivaluedMapImpl

use of com.sun.jersey.core.util.MultivaluedMapImpl in project ORCID-Source by ORCID.

the class MailGunManager method sendEmail.

public boolean sendEmail(String from, String to, String subject, String text, String html, boolean custom) {
    Client client = Client.create();
    client.addFilter(new HTTPBasicAuthFilter("api", getApiKey()));
    // determine correct api based off domain.
    WebResource webResource = null;
    String fromEmail = getFromEmail(from);
    if (custom)
        webResource = client.resource(getNotifyApiUrl());
    else if (fromEmail.endsWith("@verify.orcid.org"))
        webResource = client.resource(getVerifyApiUrl());
    else if (fromEmail.endsWith("@notify.orcid.org"))
        webResource = client.resource(getNotifyApiUrl());
    else
        webResource = client.resource(getApiUrl());
    MultivaluedMapImpl formData = new MultivaluedMapImpl();
    formData.add("from", from);
    formData.add("to", to);
    formData.add("subject", subject);
    formData.add("text", text);
    if (html != null) {
        formData.add("html", html);
    }
    formData.add("o:testmode", testmode);
    if (testmode.equals("yes"))
        LOGGER.error("Email form data: \n" + formData.toString());
    // sandbox
    if (to.matches(filter)) {
        ClientResponse cr = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, formData);
        if (cr.getStatus() != 200) {
            LOGGER.error("Post MailGunManager.sendEmail not accepted: " + formData.toString());
            return false;
        }
    }
    return true;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) WebResource(com.sun.jersey.api.client.WebResource) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) Client(com.sun.jersey.api.client.Client) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Example 93 with MultivaluedMapImpl

use of com.sun.jersey.core.util.MultivaluedMapImpl in project ORCID-Source by ORCID.

the class LoadFundRefData method fetchJsonFromGeoNames.

/**
 * Queries GeoNames API for a given geonameId and return the JSON string
 */
private String fetchJsonFromGeoNames(String geoNameId) {
    String result = null;
    if (cache.containsKey("geoname_json_" + geoNameId)) {
        return cache.get("geoname_json_" + geoNameId);
    } else {
        Client c = Client.create();
        WebResource r = c.resource(geonamesApiUrl);
        MultivaluedMap<String, String> params = new MultivaluedMapImpl();
        params.add("geonameId", geoNameId);
        params.add("username", apiUser);
        result = r.queryParams(params).get(String.class);
        cache.put("geoname_json_" + geoNameId, result);
    }
    return result;
}
Also used : WebResource(com.sun.jersey.api.client.WebResource) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) Client(com.sun.jersey.api.client.Client)

Example 94 with MultivaluedMapImpl

use of com.sun.jersey.core.util.MultivaluedMapImpl in project ORCID-Source by ORCID.

the class OrcidClientCredentialEndPointDelegatorTest method generateRefreshTokenThatExpireAfterParentTokenTest.

@Test
public void generateRefreshTokenThatExpireAfterParentTokenTest() {
    // Generate the access token
    SecurityContextTestUtils.setUpSecurityContextForClientOnly(CLIENT_ID_1, ScopePathType.ACTIVITIES_UPDATE, ScopePathType.READ_LIMITED);
    OrcidOauth2AuthoriziationCodeDetail authCode = createAuthorizationCode("code-1", CLIENT_ID_1, "http://www.APP-5555555555555555.com/redirect/oauth", false, "/activities/update");
    MultivaluedMap<String, String> formParams = new MultivaluedMapImpl();
    formParams.add("client_id", CLIENT_ID_1);
    formParams.add("client_secret", "DhkFj5EI0qp6GsUKi55Vja+h+bsaKpBx");
    formParams.add("grant_type", "authorization_code");
    formParams.add("redirect_uri", "http://www.APP-5555555555555555.com/redirect/oauth");
    formParams.add("code", authCode.getId());
    Response response = orcidClientCredentialEndPointDelegator.obtainOauth2Token(null, formParams);
    assertNotNull(response);
    assertNotNull(response.getEntity());
    DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) response.getEntity();
    assertNotNull(token);
    assertTrue(!PojoUtil.isEmpty(token.getValue()));
    assertNotNull(token.getRefreshToken());
    assertTrue(!PojoUtil.isEmpty(token.getRefreshToken().getValue()));
    // Generate the refresh token that expires after parent token
    MultivaluedMap<String, String> refreshTokenformParams = new MultivaluedMapImpl();
    refreshTokenformParams.add("client_id", CLIENT_ID_1);
    refreshTokenformParams.add("client_secret", "DhkFj5EI0qp6GsUKi55Vja+h+bsaKpBx");
    refreshTokenformParams.add("grant_type", "refresh_token");
    refreshTokenformParams.add("redirect_uri", "http://www.APP-5555555555555555.com/redirect/oauth");
    refreshTokenformParams.add("refresh_token", token.getRefreshToken().getValue());
    refreshTokenformParams.add("expires_in", String.valueOf(2 * 60 * 60));
    String authorization = "bearer " + token.getValue();
    try {
        orcidClientCredentialEndPointDelegator.obtainOauth2Token(authorization, refreshTokenformParams);
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().contains("Token expiration can't be after"));
    }
    // Try again with a valid expiration value
    refreshTokenformParams = new MultivaluedMapImpl();
    refreshTokenformParams.add("client_id", CLIENT_ID_1);
    refreshTokenformParams.add("client_secret", "DhkFj5EI0qp6GsUKi55Vja+h+bsaKpBx");
    refreshTokenformParams.add("grant_type", "refresh_token");
    refreshTokenformParams.add("redirect_uri", "http://www.APP-5555555555555555.com/redirect/oauth");
    refreshTokenformParams.add("refresh_token", token.getRefreshToken().getValue());
    refreshTokenformParams.add("expires_in", String.valueOf(60 * 30));
    response = orcidClientCredentialEndPointDelegator.obtainOauth2Token(authorization, refreshTokenformParams);
    assertNotNull(response);
    assertNotNull(response.getEntity());
    DefaultOAuth2AccessToken refreshToken = (DefaultOAuth2AccessToken) response.getEntity();
    assertNotNull(refreshToken);
    assertTrue(!PojoUtil.isEmpty(refreshToken.getValue()));
    assertNotNull(refreshToken.getRefreshToken());
    assertTrue(!PojoUtil.isEmpty(refreshToken.getRefreshToken().getValue()));
    assertTrue(token.getExpiration().getTime() > refreshToken.getExpiration().getTime());
}
Also used : Response(javax.ws.rs.core.Response) OrcidOauth2AuthoriziationCodeDetail(org.orcid.persistence.jpa.entities.OrcidOauth2AuthoriziationCodeDetail) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 95 with MultivaluedMapImpl

use of com.sun.jersey.core.util.MultivaluedMapImpl in project ORCID-Source by ORCID.

the class RefreshTokenTest method generateRefreshTokenWithWrongClientIdInMemberAPITest.

@Test
public void generateRefreshTokenWithWrongClientIdInMemberAPITest() throws InterruptedException, JSONException {
    String clientId = getClient1ClientId();
    String clientSecret = getClient1ClientSecret();
    String redirectUri = getClient1RedirectUri();
    String userId = getUser1OrcidId();
    String userPassword = getUser1Password();
    WebDriverHelper webDriverHelper = new WebDriverHelper(webDriver, this.getWebBaseUrl(), redirectUri);
    oauthHelper.setWebDriverHelper(webDriverHelper);
    String authorizationCode = oauthHelper.getAuthorizationCode(clientId, ScopePathType.ACTIVITIES_UPDATE.value(), userId, userPassword, true);
    assertNotNull(authorizationCode);
    assertFalse(PojoUtil.isEmpty(authorizationCode));
    ClientResponse tokenResponse = oauthHelper.getClientResponse(clientId, clientSecret, null, redirectUri, authorizationCode);
    assertEquals(200, tokenResponse.getStatus());
    String body = tokenResponse.getEntity(String.class);
    JSONObject jsonObject = new JSONObject(body);
    String accessToken = (String) jsonObject.get("access_token");
    assertNotNull(accessToken);
    String refreshToken = (String) jsonObject.get("refresh_token");
    assertNotNull(refreshToken);
    MultivaluedMap<String, String> params = new MultivaluedMapImpl();
    params.add("client_id", getClient2ClientId());
    params.add("client_secret", getClient2ClientSecret());
    params.add("refresh_token", refreshToken);
    params.add("grant_type", "refresh_token");
    tokenResponse = oauthHelper.getOauthT2Client().obtainOauth2RefreshTokenPost("refresh_token", accessToken, params);
    assertNotNull(tokenResponse);
    assertEquals(400, tokenResponse.getStatus());
    body = tokenResponse.getEntity(String.class);
    jsonObject = new JSONObject(body);
    String error = (String) jsonObject.get("error");
    assertEquals("invalid_request", error);
    String errorDescription = (String) jsonObject.get("error_description");
    assertEquals("This token does not belong to the given client", errorDescription);
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) WebDriverHelper(org.orcid.api.common.WebDriverHelper) JSONObject(org.codehaus.jettison.json.JSONObject) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) Test(org.junit.Test)

Aggregations

MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)131 Test (org.junit.Test)55 ClientResponse (com.sun.jersey.api.client.ClientResponse)48 WebResource (com.sun.jersey.api.client.WebResource)39 JSONObject (org.codehaus.jettison.json.JSONObject)39 Test (org.testng.annotations.Test)35 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)13 RMApp (org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp)12 List (java.util.List)11 MockNM (org.apache.hadoop.yarn.server.resourcemanager.MockNM)11 JSONArray (org.codehaus.jettison.json.JSONArray)9 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)8 Client (com.sun.jersey.api.client.Client)7 SearchFilter (org.apache.atlas.model.SearchFilter)7 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)7 WebDriverHelper (org.orcid.api.common.WebDriverHelper)7 Response (javax.ws.rs.core.Response)6 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)6 AtlasLineageInfo (org.apache.atlas.model.lineage.AtlasLineageInfo)6 MockAM (org.apache.hadoop.yarn.server.resourcemanager.MockAM)6