Search in sources :

Example 6 with Form

use of com.sun.jersey.api.representation.Form in project OpenAM by OpenRock.

the class PrivilegeRestTest method getAndDeleteAndAdd.

@Test(dependsOnMethods = "getAndPut")
public void getAndDeleteAndAdd() throws Exception {
    String result = webClient.path(PRIVILEGE_NAME).queryParam("subject", hashedTokenId).header(RestServiceManager.SUBJECT_HEADER_NAME, tokenIdHeader).cookie(cookie).get(String.class);
    JSONObject jbody = parseResult(result);
    String jsonStr = jbody.getString(PrivilegeResource.RESULT);
    result = webClient.path(PRIVILEGE_NAME).queryParam("subject", hashedTokenId).header(RestServiceManager.SUBJECT_HEADER_NAME, tokenIdHeader).cookie(cookie).delete(String.class);
    //OK
    validateResult(result, 200, "OK");
    Form form = new Form();
    form.add("privilege.json", jsonStr);
    form.add("subject", hashedTokenId);
    result = webClient.header(RestServiceManager.SUBJECT_HEADER_NAME, tokenIdHeader).cookie(cookie).post(String.class, form);
    validateResult(result, 201, "Created");
}
Also used : JSONObject(org.json.JSONObject) Form(com.sun.jersey.api.representation.Form) Test(org.testng.annotations.Test)

Example 7 with Form

use of com.sun.jersey.api.representation.Form in project OpenAM by OpenRock.

the class PrivilegeRestTest method getAndPut.

@Test(dependsOnMethods = "search")
public void getAndPut() throws Exception {
    String result = webClient.path(PRIVILEGE_NAME).queryParam("subject", hashedTokenId).header(RestServiceManager.SUBJECT_HEADER_NAME, tokenIdHeader).cookie(cookie).get(String.class);
    JSONObject jbody = parseResult(result);
    String jsonStr = jbody.getString(PrivilegeResource.RESULT);
    Privilege privilege = Privilege.getNewInstance(new JSONObject(jsonStr));
    privilege.setDescription("desciption1");
    Form form = new Form();
    form.add("privilege.json", privilege.toMinimalJSONObject());
    result = webClient.path(PRIVILEGE_NAME).queryParam("subject", hashedTokenId).header(RestServiceManager.SUBJECT_HEADER_NAME, tokenIdHeader).cookie(cookie).put(String.class, form);
    //OK
    validateResult(result, 200, "OK");
}
Also used : JSONObject(org.json.JSONObject) Form(com.sun.jersey.api.representation.Form) Privilege(com.sun.identity.entitlement.Privilege) Test(org.testng.annotations.Test)

Example 8 with Form

use of com.sun.jersey.api.representation.Form in project OpenAM by OpenRock.

the class RestPermissionTest method decisionRestCall.

private void decisionRestCall() throws Exception {
    Form params = new Form();
    params.add("subject", hashedTokenId);
    params.add("resource", "http://www.example.com/index.html");
    params.add("action", "GET");
    params.add("realm", REALM);
    decisionClient.queryParams(params).header(RestServiceManager.SUBJECT_HEADER_NAME, tokenIdHeader).cookie(cookie).accept("text/plain").get(String.class);
}
Also used : Form(com.sun.jersey.api.representation.Form)

Example 9 with Form

use of com.sun.jersey.api.representation.Form in project ORCID-Source by ORCID.

the class SalesForceDaoImpl method getFreshAccessToken.

private String getFreshAccessToken() {
    LOGGER.info("About get SalesForce access token");
    WebResource resource = client.resource(tokenEndPointUrl);
    Form form = new Form();
    form.add("grant_type", "password");
    form.add("client_id", clientId);
    form.add("client_secret", clientSecret);
    form.add("username", username);
    form.add("password", password);
    ClientResponse response = resource.accept(MediaType.APPLICATION_JSON_TYPE).type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(ClientResponse.class, form);
    if (response.getStatus() == 200) {
        try {
            return response.getEntity(JSONObject.class).getString("access_token");
        } catch (ClientHandlerException | UniformInterfaceException | JSONException e) {
            throw new RuntimeException("Unable to extract access token from response", e);
        }
    } else {
        throw new RuntimeException("Error getting access token from SalesForce, status code =  " + response.getStatus() + ", reason = " + response.getStatusInfo().getReasonPhrase() + ", body = " + response.getEntity(String.class));
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) JSONObject(org.codehaus.jettison.json.JSONObject) Form(com.sun.jersey.api.representation.Form) WebResource(com.sun.jersey.api.client.WebResource) JSONException(org.codehaus.jettison.json.JSONException)

Example 10 with Form

use of com.sun.jersey.api.representation.Form in project OpenAM by OpenRock.

the class RestTest method missingResourceTest.

@Test
public void missingResourceTest() throws Exception {
    Form params = new Form();
    params.add("subject", hashedUserTokenId);
    params.add("action", "GET");
    params.add("env", ATTR_NAME + "=" + ATTR_VAL);
    params.add("realm", REALM);
    try {
        entitlementClient.queryParams(params).header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader).cookie(cookie).accept("application/json").get(String.class);
        throw new Exception("RESTTest.missingResourceTest: no exception thrown.");
    } catch (UniformInterfaceException e) {
        int errorCode = e.getResponse().getStatus();
        if (errorCode != 400) {
            throw new Exception("RESTTest.missingResourceTest: incorrect error code");
        }
        String json = e.getResponse().getEntity(String.class);
        JSONObject jo = new JSONObject(json);
        if (jo.optInt("statusCode") != 420) {
            throw new Exception("RESTTest.missingResourceTest() failed, status code not 420");
        }
        if (jo.optJSONObject("body") != null) {
            throw new Exception("RESTTest.missingResourceTest() failed, body not empty");
        }
    }
}
Also used : UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) JSONObject(org.json.JSONObject) Form(com.sun.jersey.api.representation.Form) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) Test(org.testng.annotations.Test)

Aggregations

Form (com.sun.jersey.api.representation.Form)19 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)16 Test (org.testng.annotations.Test)13 JSONObject (org.json.JSONObject)12 JSONEntitlement (com.sun.identity.entitlement.JSONEntitlement)4 EntitlementListener (com.sun.identity.entitlement.EntitlementListener)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 MalformedURLException (java.net.MalformedURLException)3 EncodingException (org.owasp.esapi.errors.EncodingException)3 Privilege (com.sun.identity.entitlement.Privilege)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 JSONArray (org.json.JSONArray)2 DelegationPrivilege (com.sun.identity.delegation.DelegationPrivilege)1 EntitlementException (com.sun.identity.entitlement.EntitlementException)1 NumericAttributeCondition (com.sun.identity.entitlement.NumericAttributeCondition)1 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)1 ClientResponse (com.sun.jersey.api.client.ClientResponse)1 WebResource (com.sun.jersey.api.client.WebResource)1 Set (java.util.Set)1