Search in sources :

Example 31 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 toAddress = to.trim();
    String fromEmail = getFromEmail(from);
    if (shouldBeSentThroughDedicatedIP(toAddress)) {
        if (custom)
            webResource = client.resource(getAltNotifyApiUrl());
        else if (fromEmail.endsWith("@verify.orcid.org"))
            webResource = client.resource(getAltVerifyApiUrl());
        else if (fromEmail.endsWith("@notify.orcid.org"))
            webResource = client.resource(getAltNotifyApiUrl());
        else
            webResource = client.resource(getAltApiUrl());
    } else {
        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 32 with MultivaluedMapImpl

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

the class PublicClientTest method testAuthenticateOnPublicAPI.

@Test
public void testAuthenticateOnPublicAPI() throws JSONException, InterruptedException {
    String clientId = getPublicClientId();
    String clientRedirectUri = getPublicClientRedirectUri();
    String clientSecret = getPublicClientSecret();
    String userId = getUser1OrcidId();
    String password = getUser1Password();
    String authorizationCode = getAuthorizationCode(clientId, clientRedirectUri, "/authenticate", userId, password, true);
    MultivaluedMap<String, String> params = new MultivaluedMapImpl();
    params.add("client_id", clientId);
    params.add("client_secret", clientSecret);
    params.add("grant_type", "authorization_code");
    params.add("scope", "/authenticate");
    params.add("redirect_uri", clientRedirectUri);
    params.add("code", authorizationCode);
    ClientResponse response = t1OAuthClient.obtainOauth2TokenPost("client_credentials", params);
    assertEquals(200, response.getStatus());
    String body = response.getEntity(String.class);
    JSONObject jsonObject = new JSONObject(body);
    assertNotNull(jsonObject);
    assertNotNull(jsonObject.get("access_token"));
    assertEquals(userId, jsonObject.get("orcid"));
}
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 33 with MultivaluedMapImpl

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

the class PublicClientTest method testAuthenticateOnMembersAPI.

@Test
public void testAuthenticateOnMembersAPI() throws JSONException, InterruptedException {
    String clientId = getPublicClientId();
    String clientRedirectUri = getPublicClientRedirectUri();
    String clientSecret = getPublicClientSecret();
    String userId = getUser1OrcidId();
    String password = getUser1Password();
    String authorizationCode = getAuthorizationCode(clientId, clientRedirectUri, "/authenticate", userId, password, true);
    MultivaluedMap<String, String> params = new MultivaluedMapImpl();
    params.add("client_id", clientId);
    params.add("client_secret", clientSecret);
    params.add("grant_type", "authorization_code");
    params.add("scope", "/authenticate");
    params.add("redirect_uri", clientRedirectUri);
    params.add("code", authorizationCode);
    ClientResponse response = t2OAuthClient.obtainOauth2TokenPost("client_credentials", params);
    assertEquals(200, response.getStatus());
    String body = response.getEntity(String.class);
    JSONObject jsonObject = new JSONObject(body);
    assertNotNull(jsonObject);
    assertNotNull(jsonObject.get("access_token"));
    assertEquals(userId, jsonObject.get("orcid"));
}
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 34 with MultivaluedMapImpl

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

the class RefreshTokenTest method generateRefreshTokenInPublicAPITest.

@Test
public void generateRefreshTokenInPublicAPITest() 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.PERSON_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", clientId);
    params.add("client_secret", clientSecret);
    params.add("redirect_uri", redirectUri);
    params.add("refresh_token", refreshToken);
    params.add("grant_type", "refresh_token");
    tokenResponse = oauthHelper.getOauthT1Client().obtainOauth2RefreshTokenPost("refresh_token", accessToken, params);
    assertNotNull(tokenResponse);
    assertEquals(200, tokenResponse.getStatus());
    body = tokenResponse.getEntity(String.class);
    jsonObject = new JSONObject(body);
    String refreshedAccessToken = (String) jsonObject.get("access_token");
    assertNotNull(refreshedAccessToken);
    String refreshedRefreshToken = (String) jsonObject.get("refresh_token");
    assertNotNull(refreshedRefreshToken);
    assertFalse(refreshedAccessToken.equals(accessToken));
    assertFalse(refreshedRefreshToken.equals(refreshToken));
}
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)

Example 35 with MultivaluedMapImpl

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

the class InternalAPITest method testGetTokenForInternalScopes.

@Test
public void testGetTokenForInternalScopes() 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));
}
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)

Aggregations

MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)72 JSONObject (org.codehaus.jettison.json.JSONObject)33 Test (org.junit.Test)32 ClientResponse (com.sun.jersey.api.client.ClientResponse)29 WebResource (com.sun.jersey.api.client.WebResource)21 Test (org.testng.annotations.Test)19 RMApp (org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp)12 MockNM (org.apache.hadoop.yarn.server.resourcemanager.MockNM)11 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)9 JSONArray (org.codehaus.jettison.json.JSONArray)9 MockAM (org.apache.hadoop.yarn.server.resourcemanager.MockAM)7 Response (javax.ws.rs.core.Response)6 BridgePort (org.midonet.client.resource.BridgePort)5 Router (org.midonet.client.resource.Router)5 Bridge (org.midonet.client.resource.Bridge)4 Route (org.midonet.client.resource.Route)4 OAuthServiceException (com.sun.identity.oauth.service.OAuthServiceException)3 Client (com.sun.jersey.api.client.Client)3 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)3 ArrayList (java.util.ArrayList)3