Search in sources :

Example 6 with ObjectMapper

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

the class JsonGeneratorTest method testGeneratorWithoutAnyAttachements.

@Test
public void testGeneratorWithoutAnyAttachements() {
    try {
        JobVertex source1 = new JobVertex("source 1");
        JobVertex source2 = new JobVertex("source 2");
        source2.setInvokableClass(DummyInvokable.class);
        JobVertex source3 = new JobVertex("source 3");
        JobVertex intermediate1 = new JobVertex("intermediate 1");
        JobVertex intermediate2 = new JobVertex("intermediate 2");
        JobVertex join1 = new JobVertex("join 1");
        JobVertex join2 = new JobVertex("join 2");
        JobVertex sink1 = new JobVertex("sink 1");
        JobVertex sink2 = new JobVertex("sink 2");
        intermediate1.connectNewDataSetAsInput(source1, DistributionPattern.POINTWISE, ResultPartitionType.PIPELINED);
        intermediate2.connectNewDataSetAsInput(source2, DistributionPattern.ALL_TO_ALL, ResultPartitionType.PIPELINED);
        join1.connectNewDataSetAsInput(intermediate1, DistributionPattern.POINTWISE, ResultPartitionType.BLOCKING);
        join1.connectNewDataSetAsInput(intermediate2, DistributionPattern.ALL_TO_ALL, ResultPartitionType.BLOCKING);
        join2.connectNewDataSetAsInput(join1, DistributionPattern.POINTWISE, ResultPartitionType.PIPELINED);
        join2.connectNewDataSetAsInput(source3, DistributionPattern.POINTWISE, ResultPartitionType.BLOCKING);
        sink1.connectNewDataSetAsInput(join2, DistributionPattern.POINTWISE, ResultPartitionType.PIPELINED);
        sink2.connectNewDataSetAsInput(join1, DistributionPattern.ALL_TO_ALL, ResultPartitionType.PIPELINED);
        JobGraph jg = new JobGraph("my job", source1, source2, source3, intermediate1, intermediate2, join1, join2, sink1, sink2);
        String plan = JsonPlanGenerator.generatePlan(jg);
        assertNotNull(plan);
        // validate the produced JSON
        ObjectMapper m = new ObjectMapper();
        JsonNode rootNode = m.readTree(plan);
        // core fields
        assertEquals(new TextNode(jg.getJobID().toString()), rootNode.get("jid"));
        assertEquals(new TextNode(jg.getName()), rootNode.get("name"));
        assertTrue(rootNode.path("nodes").isArray());
        for (Iterator<JsonNode> iter = rootNode.path("nodes").elements(); iter.hasNext(); ) {
            JsonNode next = iter.next();
            JsonNode idNode = next.get("id");
            assertNotNull(idNode);
            assertTrue(idNode.isTextual());
            checkVertexExists(idNode.asText(), jg);
            String description = next.get("description").asText();
            assertTrue(description.startsWith("source") || description.startsWith("sink") || description.startsWith("intermediate") || description.startsWith("join"));
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) JobVertex(org.apache.flink.runtime.jobgraph.JobVertex) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 7 with ObjectMapper

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

the class DelegationTokenAuthenticationHandler method managementOperation.

@Override
@SuppressWarnings("unchecked")
public boolean managementOperation(AuthenticationToken token, HttpServletRequest request, HttpServletResponse response) throws IOException, AuthenticationException {
    boolean requestContinues = true;
    String op = ServletUtils.getParameter(request, KerberosDelegationTokenAuthenticator.OP_PARAM);
    op = (op != null) ? StringUtils.toUpperCase(op) : null;
    if (isManagementOperation(request)) {
        KerberosDelegationTokenAuthenticator.DelegationTokenOperation dtOp = KerberosDelegationTokenAuthenticator.DelegationTokenOperation.valueOf(op);
        if (dtOp.getHttpMethod().equals(request.getMethod())) {
            boolean doManagement;
            if (dtOp.requiresKerberosCredentials() && token == null) {
                // Don't authenticate via DT for DT ops.
                token = authHandler.authenticate(request, response);
                if (token == null) {
                    requestContinues = false;
                    doManagement = false;
                } else {
                    doManagement = true;
                }
            } else {
                doManagement = true;
            }
            if (doManagement) {
                UserGroupInformation requestUgi = (token != null) ? UserGroupInformation.createRemoteUser(token.getUserName()) : null;
                // Create the proxy user if doAsUser exists
                String doAsUser = DelegationTokenAuthenticationFilter.getDoAs(request);
                if (requestUgi != null && doAsUser != null) {
                    requestUgi = UserGroupInformation.createProxyUser(doAsUser, requestUgi);
                    try {
                        ProxyUsers.authorize(requestUgi, request.getRemoteAddr());
                    } catch (AuthorizationException ex) {
                        HttpExceptionUtils.createServletExceptionResponse(response, HttpServletResponse.SC_FORBIDDEN, ex);
                        return false;
                    }
                }
                Map map = null;
                switch(dtOp) {
                    case GETDELEGATIONTOKEN:
                        if (requestUgi == null) {
                            throw new IllegalStateException("request UGI cannot be NULL");
                        }
                        String renewer = ServletUtils.getParameter(request, KerberosDelegationTokenAuthenticator.RENEWER_PARAM);
                        try {
                            Token<?> dToken = tokenManager.createToken(requestUgi, renewer);
                            map = delegationTokenToJSON(dToken);
                        } catch (IOException ex) {
                            throw new AuthenticationException(ex.toString(), ex);
                        }
                        break;
                    case RENEWDELEGATIONTOKEN:
                        if (requestUgi == null) {
                            throw new IllegalStateException("request UGI cannot be NULL");
                        }
                        String tokenToRenew = ServletUtils.getParameter(request, KerberosDelegationTokenAuthenticator.TOKEN_PARAM);
                        if (tokenToRenew == null) {
                            response.sendError(HttpServletResponse.SC_BAD_REQUEST, MessageFormat.format("Operation [{0}] requires the parameter [{1}]", dtOp, KerberosDelegationTokenAuthenticator.TOKEN_PARAM));
                            requestContinues = false;
                        } else {
                            Token<AbstractDelegationTokenIdentifier> dt = new Token();
                            try {
                                dt.decodeFromUrlString(tokenToRenew);
                                long expirationTime = tokenManager.renewToken(dt, requestUgi.getShortUserName());
                                map = new HashMap();
                                map.put("long", expirationTime);
                            } catch (IOException ex) {
                                throw new AuthenticationException(ex.toString(), ex);
                            }
                        }
                        break;
                    case CANCELDELEGATIONTOKEN:
                        String tokenToCancel = ServletUtils.getParameter(request, KerberosDelegationTokenAuthenticator.TOKEN_PARAM);
                        if (tokenToCancel == null) {
                            response.sendError(HttpServletResponse.SC_BAD_REQUEST, MessageFormat.format("Operation [{0}] requires the parameter [{1}]", dtOp, KerberosDelegationTokenAuthenticator.TOKEN_PARAM));
                            requestContinues = false;
                        } else {
                            Token<AbstractDelegationTokenIdentifier> dt = new Token();
                            try {
                                dt.decodeFromUrlString(tokenToCancel);
                                tokenManager.cancelToken(dt, (requestUgi != null) ? requestUgi.getShortUserName() : null);
                            } catch (IOException ex) {
                                response.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid delegation token, cannot cancel");
                                requestContinues = false;
                            }
                        }
                        break;
                }
                if (requestContinues) {
                    response.setStatus(HttpServletResponse.SC_OK);
                    if (map != null) {
                        response.setContentType(MediaType.APPLICATION_JSON);
                        Writer writer = response.getWriter();
                        ObjectMapper jsonMapper = new ObjectMapper(jsonFactory);
                        jsonMapper.writeValue(writer, map);
                        writer.write(ENTER);
                        writer.flush();
                    }
                    requestContinues = false;
                }
            }
        } else {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, MessageFormat.format("Wrong HTTP method [{0}] for operation [{1}], it should be " + "[{2}]", request.getMethod(), dtOp, dtOp.getHttpMethod()));
            requestContinues = false;
        }
    }
    return requestContinues;
}
Also used : AuthorizationException(org.apache.hadoop.security.authorize.AuthorizationException) AuthenticationException(org.apache.hadoop.security.authentication.client.AuthenticationException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AuthenticationToken(org.apache.hadoop.security.authentication.server.AuthenticationToken) Token(org.apache.hadoop.security.token.Token) IOException(java.io.IOException) AbstractDelegationTokenIdentifier(org.apache.hadoop.security.token.delegation.AbstractDelegationTokenIdentifier) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Writer(java.io.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 8 with ObjectMapper

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

the class WebFrontendITCase method getTaskmanagers.

@Test
public void getTaskmanagers() {
    try {
        String json = getFromHTTP("http://localhost:" + port + "/taskmanagers/");
        ObjectMapper mapper = new ObjectMapper();
        JsonNode parsed = mapper.readTree(json);
        ArrayNode taskManagers = (ArrayNode) parsed.get("taskmanagers");
        assertNotNull(taskManagers);
        assertEquals(cluster.numTaskManagers(), taskManagers.size());
        JsonNode taskManager = taskManagers.get(0);
        assertNotNull(taskManager);
        assertEquals(NUM_SLOTS, taskManager.get("slotsNumber").asInt());
        assertTrue(taskManager.get("freeSlots").asInt() <= NUM_SLOTS);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 9 with ObjectMapper

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

the class HttpExceptionUtils method validateResponse.

/**
   * Validates the status of an <code>HttpURLConnection</code> against an
   * expected HTTP status code. If the current status code is not the expected
   * one it throws an exception with a detail message using Server side error
   * messages if available.
   * <p/>
   * <b>NOTE:</b> this method will throw the deserialized exception even if not
   * declared in the <code>throws</code> of the method signature.
   *
   * @param conn the <code>HttpURLConnection</code>.
   * @param expectedStatus the expected HTTP status code.
   * @throws IOException thrown if the current status code does not match the
   * expected one.
   */
@SuppressWarnings("unchecked")
public static void validateResponse(HttpURLConnection conn, int expectedStatus) throws IOException {
    if (conn.getResponseCode() != expectedStatus) {
        Exception toThrow;
        InputStream es = null;
        try {
            es = conn.getErrorStream();
            ObjectMapper mapper = new ObjectMapper();
            Map json = mapper.readValue(es, Map.class);
            json = (Map) json.get(ERROR_JSON);
            String exClass = (String) json.get(ERROR_CLASSNAME_JSON);
            String exMsg = (String) json.get(ERROR_MESSAGE_JSON);
            if (exClass != null) {
                try {
                    ClassLoader cl = HttpExceptionUtils.class.getClassLoader();
                    Class klass = cl.loadClass(exClass);
                    Constructor constr = klass.getConstructor(String.class);
                    toThrow = (Exception) constr.newInstance(exMsg);
                } catch (Exception ex) {
                    toThrow = new IOException(String.format("HTTP status [%d], exception [%s], message [%s] ", conn.getResponseCode(), exClass, exMsg));
                }
            } else {
                String msg = (exMsg != null) ? exMsg : conn.getResponseMessage();
                toThrow = new IOException(String.format("HTTP status [%d], message [%s]", conn.getResponseCode(), msg));
            }
        } catch (Exception ex) {
            toThrow = new IOException(String.format("HTTP status [%d], message [%s]", conn.getResponseCode(), conn.getResponseMessage()));
        } finally {
            if (es != null) {
                try {
                    es.close();
                } catch (IOException ex) {
                //ignore
                }
            }
        }
        throwEx(toThrow);
    }
}
Also used : InputStream(java.io.InputStream) Constructor(java.lang.reflect.Constructor) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 10 with ObjectMapper

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

the class TestConfiguration method testDumpConfiguration.

public void testDumpConfiguration() throws IOException {
    StringWriter outWriter = new StringWriter();
    Configuration.dumpConfiguration(conf, outWriter);
    String jsonStr = outWriter.toString();
    ObjectMapper mapper = new ObjectMapper();
    JsonConfiguration jconf = mapper.readValue(jsonStr, JsonConfiguration.class);
    int defaultLength = jconf.getProperties().length;
    // add 3 keys to the existing configuration properties
    out = new BufferedWriter(new FileWriter(CONFIG));
    startConfig();
    appendProperty("test.key1", "value1");
    appendProperty("test.key2", "value2", true);
    appendProperty("test.key3", "value3");
    endConfig();
    Path fileResource = new Path(CONFIG);
    conf.addResource(fileResource);
    out.close();
    outWriter = new StringWriter();
    Configuration.dumpConfiguration(conf, outWriter);
    jsonStr = outWriter.toString();
    mapper = new ObjectMapper();
    jconf = mapper.readValue(jsonStr, JsonConfiguration.class);
    int length = jconf.getProperties().length;
    // check for consistency in the number of properties parsed in Json format.
    assertEquals(length, defaultLength + 3);
    //change few keys in another resource file
    out = new BufferedWriter(new FileWriter(CONFIG2));
    startConfig();
    appendProperty("test.key1", "newValue1");
    appendProperty("test.key2", "newValue2");
    endConfig();
    Path fileResource1 = new Path(CONFIG2);
    conf.addResource(fileResource1);
    out.close();
    outWriter = new StringWriter();
    Configuration.dumpConfiguration(conf, outWriter);
    jsonStr = outWriter.toString();
    mapper = new ObjectMapper();
    jconf = mapper.readValue(jsonStr, JsonConfiguration.class);
    // put the keys and their corresponding attributes into a hashmap for their 
    // efficient retrieval
    HashMap<String, JsonProperty> confDump = new HashMap<String, JsonProperty>();
    for (JsonProperty prop : jconf.getProperties()) {
        confDump.put(prop.getKey(), prop);
    }
    // check if the value and resource of test.key1 is changed
    assertEquals("newValue1", confDump.get("test.key1").getValue());
    assertEquals(false, confDump.get("test.key1").getIsFinal());
    assertEquals(fileResource1.toString(), confDump.get("test.key1").getResource());
    // check if final parameter test.key2 is not changed, since it is first 
    // loaded as final parameter
    assertEquals("value2", confDump.get("test.key2").getValue());
    assertEquals(true, confDump.get("test.key2").getIsFinal());
    assertEquals(fileResource.toString(), confDump.get("test.key2").getResource());
    // check for other keys which are not modified later
    assertEquals("value3", confDump.get("test.key3").getValue());
    assertEquals(false, confDump.get("test.key3").getIsFinal());
    assertEquals(fileResource.toString(), confDump.get("test.key3").getResource());
    // check for resource to be "Unknown" for keys which are loaded using 'set' 
    // and expansion of properties
    conf.set("test.key4", "value4");
    conf.set("test.key5", "value5");
    conf.set("test.key6", "${test.key5}");
    outWriter = new StringWriter();
    Configuration.dumpConfiguration(conf, outWriter);
    jsonStr = outWriter.toString();
    mapper = new ObjectMapper();
    jconf = mapper.readValue(jsonStr, JsonConfiguration.class);
    confDump = new HashMap<String, JsonProperty>();
    for (JsonProperty prop : jconf.getProperties()) {
        confDump.put(prop.getKey(), prop);
    }
    assertEquals("value5", confDump.get("test.key6").getValue());
    assertEquals("programmatically", confDump.get("test.key4").getResource());
    outWriter.close();
}
Also used : Path(org.apache.hadoop.fs.Path) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BufferedWriter(java.io.BufferedWriter)

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