Search in sources :

Example 41 with UniformInterfaceException

use of com.sun.jersey.api.client.UniformInterfaceException in project OpenAM by OpenRock.

the class PrivilegeRestTest method validateUniformInterfaceException.

private void validateUniformInterfaceException(UniformInterfaceException e, int expectedStatusCode, String methodName) throws Exception {
    int errorCode = e.getResponse().getStatus();
    if (errorCode != 400) {
        throw new Exception("PrivilegeRestTest." + methodName + ": incorrect error code");
    }
    String json = e.getResponse().getEntity(String.class);
    JSONObject jo = new JSONObject(json);
    if (jo.optInt("statusCode") != expectedStatusCode) {
        throw new Exception("PrivilegeRestTest." + methodName + ", status code not " + expectedStatusCode);
    }
}
Also used : JSONObject(org.json.JSONObject) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException)

Example 42 with UniformInterfaceException

use of com.sun.jersey.api.client.UniformInterfaceException in project apex-core by apache.

the class StramWebServicesTest method testInvalidAccept.

@Test
@SuppressWarnings("UnusedAssignment")
public void testInvalidAccept() throws JSONException, Exception {
    // suppress logging in jersey to get rid of expected stack traces from test log
    java.util.logging.Logger.getLogger("org.glassfish.grizzly.servlet.ServletHandler").setLevel(Level.OFF);
    java.util.logging.Logger.getLogger("com.sun.jersey.spi.container.ContainerResponse").setLevel(Level.OFF);
    WebResource r = resource();
    String responseStr = "";
    try {
        responseStr = r.path(StramWebServices.PATH).accept(MediaType.TEXT_PLAIN).get(String.class);
        fail("should have thrown exception on invalid accept");
    } catch (UniformInterfaceException ue) {
        ClientResponse response = ue.getResponse();
        assertEquals(Status.INTERNAL_SERVER_ERROR, response.getClientResponseStatus());
        StramTestSupport.checkStringMatch("error string exists and shouldn't", "", responseStr);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) WebResource(com.sun.jersey.api.client.WebResource) JerseyTest(com.sun.jersey.test.framework.JerseyTest) Test(org.junit.Test)

Example 43 with UniformInterfaceException

use of com.sun.jersey.api.client.UniformInterfaceException 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 44 with UniformInterfaceException

use of com.sun.jersey.api.client.UniformInterfaceException in project hadoop by apache.

the class TestRMWebServicesNodes method testNodesQueryStateInvalid.

@Test
public void testNodesQueryStateInvalid() throws JSONException, Exception {
    WebResource r = resource();
    getNewRMNode("h1", 1234, 5120);
    getNewRMNode("h2", 1235, 5121);
    try {
        r.path("ws").path("v1").path("cluster").path("nodes").queryParam("states", "BOGUSSTATE").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        fail("should have thrown exception querying invalid state");
    } catch (UniformInterfaceException ue) {
        ClientResponse response = ue.getResponse();
        assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo());
        assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, response.getType().toString());
        JSONObject msg = response.getEntity(JSONObject.class);
        JSONObject exception = msg.getJSONObject("RemoteException");
        assertEquals("incorrect number of elements", 3, exception.length());
        String message = exception.getString("message");
        String type = exception.getString("exception");
        String classname = exception.getString("javaClassName");
        WebServicesTestUtils.checkStringContains("exception message", "org.apache.hadoop.yarn.api.records.NodeState.BOGUSSTATE", message);
        WebServicesTestUtils.checkStringMatch("exception type", "IllegalArgumentException", type);
        WebServicesTestUtils.checkStringMatch("exception classname", "java.lang.IllegalArgumentException", classname);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) JSONObject(org.codehaus.jettison.json.JSONObject) WebResource(com.sun.jersey.api.client.WebResource) Test(org.junit.Test)

Example 45 with UniformInterfaceException

use of com.sun.jersey.api.client.UniformInterfaceException in project hadoop by apache.

the class TestRMWebServicesNodes method testNonexistNodeDefault.

// test that the exception output defaults to JSON
@Test
public void testNonexistNodeDefault() throws JSONException, Exception {
    getNewRMNode("h1", 1234, 5120);
    getNewRMNode("h2", 1235, 5121);
    WebResource r = resource();
    try {
        r.path("ws").path("v1").path("cluster").path("nodes").path("node_invalid:99").get(JSONObject.class);
        fail("should have thrown exception on non-existent nodeid");
    } catch (UniformInterfaceException ue) {
        ClientResponse response = ue.getResponse();
        assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo());
        assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, response.getType().toString());
        JSONObject msg = response.getEntity(JSONObject.class);
        JSONObject exception = msg.getJSONObject("RemoteException");
        assertEquals("incorrect number of elements", 3, exception.length());
        String message = exception.getString("message");
        String type = exception.getString("exception");
        String classname = exception.getString("javaClassName");
        verifyNonexistNodeException(message, type, classname);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) JSONObject(org.codehaus.jettison.json.JSONObject) WebResource(com.sun.jersey.api.client.WebResource) Test(org.junit.Test)

Aggregations

UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)79 ClientResponse (com.sun.jersey.api.client.ClientResponse)67 WebResource (com.sun.jersey.api.client.WebResource)66 Test (org.junit.Test)62 JSONObject (org.codehaus.jettison.json.JSONObject)44 JobId (org.apache.hadoop.mapreduce.v2.api.records.JobId)14 Job (org.apache.hadoop.mapreduce.v2.app.job.Job)14 Application (org.apache.hadoop.yarn.server.nodemanager.containermanager.application.Application)9 MockNM (org.apache.hadoop.yarn.server.resourcemanager.MockNM)8 Form (com.sun.jersey.api.representation.Form)7 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)4 StringReader (java.io.StringReader)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)4 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)4 JSONObject (org.json.JSONObject)4 Document (org.w3c.dom.Document)4 Element (org.w3c.dom.Element)4 NodeList (org.w3c.dom.NodeList)4 InputSource (org.xml.sax.InputSource)4 OAuthServiceException (com.sun.identity.oauth.service.OAuthServiceException)3