Search in sources :

Example 21 with Form

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

the class RestTest method negativeTest.

@Test
public void negativeTest() throws Exception {
    Form params = new Form();
    params.add("subject", hashedUserTokenId);
    params.add("resource", RESOURCE_NAME + "/index.html");
    params.add("action", "GET");
    params.add("realm", REALM);
    String decision = decisionClient.queryParams(params).header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader).cookie(cookie).accept("text/plain").get(String.class);
    if ((decision != null) && decision.equals("allow")) {
        throw new Exception("RESTTest.negativeTest (/decision) failed");
    }
    String json = entitlementClient.queryParams(params).header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader).cookie(cookie).accept("application/json").get(String.class);
    JSONObject jo = new JSONObject(json);
    if (jo.optInt("statusCode") != 200) {
        throw new Exception("RESTTest.negativeTest() failed, status code not 200");
    }
    JSONObject jbody = jo.optJSONObject("body");
    if (jbody == null) {
        throw new Exception("RESTTest.negativeTest() failed, body element is null");
    }
    JSONEntitlement ent = new JSONEntitlement(jbody);
    boolean result = false;
    Object resultObj = ent.getActionValue("GET");
    if (resultObj != null) {
        result = ent.getActionValue("GET");
    }
    if (result) {
        throw new Exception("RESTTest.getnegativeTest() failed");
    }
    Map<String, Set<String>> advices = ent.getAdvices();
    Set<String> setNumericCondAdvice = advices.get(NumericAttributeCondition.class.getName());
    if ((setNumericCondAdvice == null) || setNumericCondAdvice.isEmpty()) {
        throw new Exception("RESTTest.negativeTest: no advice");
    }
    String advice = setNumericCondAdvice.iterator().next();
    if (!advice.equals(ATTR_NAME + "=" + ATTR_VAL)) {
        throw new Exception("RESTTest.negativeTest: incorrect advice");
    }
}
Also used : JSONEntitlement(com.sun.identity.entitlement.JSONEntitlement) Set(java.util.Set) JSONObject(org.json.JSONObject) NumericAttributeCondition(com.sun.identity.entitlement.NumericAttributeCondition) Form(com.sun.jersey.api.representation.Form) JSONObject(org.json.JSONObject) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) Test(org.testng.annotations.Test)

Example 22 with Form

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

the class RestTest method getDecisionTest.

@Test
public void getDecisionTest() throws Exception {
    Form params = new Form();
    params.add("subject", hashedUserTokenId);
    params.add("resource", RESOURCE_NAME + "/index.html");
    params.add("action", "GET");
    params.add("env", ATTR_NAME + "=" + ATTR_VAL);
    params.add("realm", REALM);
    String decision = decisionClient.queryParams(params).header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader).cookie(cookie).accept("text/plain").get(String.class);
    if ((decision == null) || !decision.equals("allow")) {
        throw new Exception("RESTTest.getDecisionTest() failed");
    }
}
Also used : Form(com.sun.jersey.api.representation.Form) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) Test(org.testng.annotations.Test)

Example 23 with Form

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

the class RestTest method missingActionTest.

@Test
public void missingActionTest() throws Exception {
    Form params = new Form();
    params.add("subject", hashedUserTokenId);
    params.add("resource", RESOURCE_NAME + "/index.html");
    params.add("env", ATTR_NAME + "=" + ATTR_VAL);
    params.add("realm", REALM);
    try {
        decisionClient.queryParams(params).header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader).cookie(cookie).accept("text/plain").get(String.class);
        throw new Exception("RESTTest.missingActionTest: no exception thrown.");
    } catch (UniformInterfaceException e) {
        int errorCode = e.getResponse().getStatus();
        if (errorCode != 400) {
            throw new Exception("RESTTest.missingActionTest: incorrect error code");
        }
    }
}
Also used : UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) Form(com.sun.jersey.api.representation.Form) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) Test(org.testng.annotations.Test)

Example 24 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 25 with Form

use of com.sun.jersey.api.representation.Form in project activityinfo by bedatadriven.

the class PoEditorClient method addTerms.

public void addTerms(int projectId, List<PoTerm> terms) {
    String data;
    try {
        data = objectMapper.writeValueAsString(terms);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    System.out.println(data);
    Form form = new Form();
    form.putSingle("id", Integer.toString(projectId));
    form.putSingle("action", "add_terms");
    form.putSingle("api_token", token);
    form.putSingle("data", data);
}
Also used : Form(com.sun.jersey.api.representation.Form) IOException(java.io.IOException)

Aggregations

Form (com.sun.jersey.api.representation.Form)29 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)16 Test (org.testng.annotations.Test)13 JSONObject (org.json.JSONObject)12 ClientResponse (com.sun.jersey.api.client.ClientResponse)8 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 WebResource (com.sun.jersey.api.client.WebResource)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 Client (com.sun.jersey.api.client.Client)1 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)1