Search in sources :

Example 16 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class KMSClientProvider method call.

private <T> T call(HttpURLConnection conn, Map jsonOutput, int expectedResponse, Class<T> klass, int authRetryCount) throws IOException {
    T ret = null;
    try {
        if (jsonOutput != null) {
            writeJson(jsonOutput, conn.getOutputStream());
        }
    } catch (IOException ex) {
        IOUtils.closeStream(conn.getInputStream());
        throw ex;
    }
    if ((conn.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN && (conn.getResponseMessage().equals(ANONYMOUS_REQUESTS_DISALLOWED) || conn.getResponseMessage().contains(INVALID_SIGNATURE))) || conn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
        // Ideally, this should happen only when there is an Authentication
        // failure. Unfortunately, the AuthenticationFilter returns 403 when it
        // cannot authenticate (Since a 401 requires Server to send
        // WWW-Authenticate header as well)..
        KMSClientProvider.this.authToken = new DelegationTokenAuthenticatedURL.Token();
        if (authRetryCount > 0) {
            String contentType = conn.getRequestProperty(CONTENT_TYPE);
            String requestMethod = conn.getRequestMethod();
            URL url = conn.getURL();
            conn = createConnection(url, requestMethod);
            conn.setRequestProperty(CONTENT_TYPE, contentType);
            return call(conn, jsonOutput, expectedResponse, klass, authRetryCount - 1);
        }
    }
    try {
        AuthenticatedURL.extractToken(conn, authToken);
    } catch (AuthenticationException e) {
    // Ignore the AuthExceptions.. since we are just using the method to
    // extract and set the authToken.. (Workaround till we actually fix
    // AuthenticatedURL properly to set authToken post initialization)
    }
    HttpExceptionUtils.validateResponse(conn, expectedResponse);
    if (conn.getContentType() != null && conn.getContentType().trim().toLowerCase().startsWith(APPLICATION_JSON_MIME) && klass != null) {
        ObjectMapper mapper = new ObjectMapper();
        InputStream is = null;
        try {
            is = conn.getInputStream();
            ret = mapper.readValue(is, klass);
        } finally {
            IOUtils.closeStream(is);
        }
    }
    return ret;
}
Also used : DelegationTokenAuthenticatedURL(org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL) AuthenticationException(org.apache.hadoop.security.authentication.client.AuthenticationException) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) AuthenticatedURL(org.apache.hadoop.security.authentication.client.AuthenticatedURL) DelegationTokenAuthenticatedURL(org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 17 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class TestWebHDFS method toBlockLocationArray.

private BlockLocation[] toBlockLocationArray(String json) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    MapType subType = mapper.getTypeFactory().constructMapType(Map.class, String.class, BlockLocation[].class);
    MapType rootType = mapper.getTypeFactory().constructMapType(Map.class, mapper.constructType(String.class), mapper.constructType(subType));
    Map<String, Map<String, BlockLocation[]>> jsonMap = mapper.readValue(json, rootType);
    Map<String, BlockLocation[]> locationMap = jsonMap.get("BlockLocations");
    BlockLocation[] locationArray = locationMap.get(BlockLocation.class.getSimpleName());
    return locationArray;
}
Also used : BlockLocation(org.apache.hadoop.fs.BlockLocation) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MapType(com.fasterxml.jackson.databind.type.MapType)

Example 18 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class TestJsonUtil method testGetXAttrFromJson.

@Test
public void testGetXAttrFromJson() throws IOException {
    String jsonString = "{\"XAttrs\":[{\"name\":\"user.a1\",\"value\":\"0x313233\"}," + "{\"name\":\"user.a2\",\"value\":\"0x313131\"}]}";
    ObjectReader reader = new ObjectMapper().readerFor(Map.class);
    Map<?, ?> json = reader.readValue(jsonString);
    // Get xattr: user.a2
    byte[] value = JsonUtilClient.getXAttr(json, "user.a2");
    Assert.assertArrayEquals(XAttrCodec.decodeValue("0x313131"), value);
}
Also used : ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 19 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class KMSJSONWriter method writeTo.

@Override
public void writeTo(Object obj, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> stringObjectMultivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException {
    Writer writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
    ObjectMapper jsonMapper = new ObjectMapper();
    jsonMapper.writerWithDefaultPrettyPrinter().writeValue(writer, obj);
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 20 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class TestHttpExceptionUtils method testValidateResponseJsonErrorKnownException.

@Test
public void testValidateResponseJsonErrorKnownException() throws IOException {
    Map<String, Object> json = new HashMap<String, Object>();
    json.put(HttpExceptionUtils.ERROR_EXCEPTION_JSON, IllegalStateException.class.getSimpleName());
    json.put(HttpExceptionUtils.ERROR_CLASSNAME_JSON, IllegalStateException.class.getName());
    json.put(HttpExceptionUtils.ERROR_MESSAGE_JSON, "EX");
    Map<String, Object> response = new HashMap<String, Object>();
    response.put(HttpExceptionUtils.ERROR_JSON, json);
    ObjectMapper jsonMapper = new ObjectMapper();
    String msg = jsonMapper.writeValueAsString(response);
    InputStream is = new ByteArrayInputStream(msg.getBytes());
    HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
    Mockito.when(conn.getErrorStream()).thenReturn(is);
    Mockito.when(conn.getResponseMessage()).thenReturn("msg");
    Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_BAD_REQUEST);
    try {
        HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED);
        Assert.fail();
    } catch (IllegalStateException ex) {
        Assert.assertEquals("EX", ex.getMessage());
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1523 Test (org.junit.Test)608 JsonNode (com.fasterxml.jackson.databind.JsonNode)217 IOException (java.io.IOException)191 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)172 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)127 Map (java.util.Map)122 HashMap (java.util.HashMap)111 ArrayList (java.util.ArrayList)71 File (java.io.File)56 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)53 JCodeModel (com.sun.codemodel.JCodeModel)52 InputStream (java.io.InputStream)50 JPackage (com.sun.codemodel.JPackage)44 List (java.util.List)42 JsonException (jmri.server.json.JsonException)41 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)39 JType (com.sun.codemodel.JType)38 Test (org.testng.annotations.Test)36 JsonFactory (com.fasterxml.jackson.core.JsonFactory)34