Search in sources :

Example 21 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project MSEC by Tencent.

the class Worker method returnErrorMessage.

private void returnErrorMessage(String msg) {
    JsonRPCResponseBase r = new JsonRPCResponseBase();
    ObjectMapper objectMapper = new ObjectMapper();
    r.setMessage(msg);
    r.setStatus(100);
    try {
        String s = objectMapper.writeValueAsString(r);
        socket.getOutputStream().write(s.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
}
Also used : ObjectMapper(org.codehaus.jackson.map.ObjectMapper) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 22 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project MSEC by Tencent.

the class Worker method handleRequest.

//���÷�����ƣ�
// 1.��������json�ַ����е�handleClass�ֶΣ�ʵ����ʵ�ʵĴ�����
// 2. �Զ�������json�ַ���ʵ����Ϊjava�࣬�൱�ڲ������
// 3. �Զ�������󷵻ص�java��ʵ�������л�Ϊjson �ַ�������ΪӦ���͸�ǰ��
private String handleRequest(String jsonStr, InputStream in, OutputStream out) {
    JSONObject jsonObject = new JSONObject(jsonStr);
    String handleClassStr = jsonObject.getString("handleClass");
    JSONObject requestBodyObj = jsonObject.getJSONObject("requestBody");
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        Class<?> clazz = Class.forName(handleClassStr);
        //ʵ����handle classָ������
        JsonRPCHandler handler = (JsonRPCHandler) clazz.newInstance();
        //��ø�������з������ҵ�exec����������
        Method[] methods = clazz.getMethods();
        boolean execFound = false;
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals("exec")) {
                execFound = true;
                //exec�����IJ��������ͣ���json�����л�Ϊ�ò�����ʵ��
                Class<?>[] paramTypes = methods[i].getParameterTypes();
                if (paramTypes.length != 1) {
                    returnErrorMessage("handle class's exec() method's param number is not 1!");
                    return "failed";
                }
                Object exec_request = objectMapper.readValue(requestBodyObj.toString(), paramTypes[0]);
                //���exec�����ķ�������,Ҫ��̳���JsonRPCResponseBase
                Class<?> returnType = methods[i].getReturnType();
                String superClass = returnType.getSuperclass().getName();
                if (superClass == null || !superClass.equals("ngse.remote_shell.JsonRPCResponseBase")) {
                    returnErrorMessage("method exec() should return a class extending JsonRPCResponseBase.");
                    return "failed";
                }
                //����exec����,�������ص�java��ʵ�����л�Ϊjson�ַ�����Ӧ��ǰ��
                try {
                    Object exec_result = methods[i].invoke(handler, exec_request);
                    if (exec_request != null) {
                        //???????bean???��??json?????
                        String s = objectMapper.writeValueAsString(exec_result);
                        out.write(s.getBytes());
                    }
                    return "success";
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                    return "failed";
                }
            }
        }
        if (!execFound) {
            return "exec method not found";
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return "ClassNotFoundException:" + e.toString();
    } catch (InstantiationException e) {
        e.printStackTrace();
        return "InstantiationException:" + e.toString();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return "IllegalAccessException:" + e.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return "IllegalAccessException:" + e.toString();
    }
    return "failed";
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 23 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project SimianArmy by Netflix.

the class JanitorMonkeyResource method addEvent.

/**
     * POST /api/v1/janitor will try a add a new event with the information in the url context.
     *
     * @param content
     *            the Json content passed to the http POST request
     * @return the response
     * @throws IOException
     */
@POST
public Response addEvent(String content) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    LOGGER.info(String.format("JSON content: '%s'", content));
    JsonNode input = mapper.readTree(content);
    String eventType = getStringField(input, "eventType");
    String resourceId = getStringField(input, "resourceId");
    String region = getStringField(input, "region");
    Response.Status responseStatus;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonGenerator gen = JSON_FACTORY.createJsonGenerator(baos, JsonEncoding.UTF8);
    gen.writeStartObject();
    gen.writeStringField("eventType", eventType);
    gen.writeStringField("resourceId", resourceId);
    if (StringUtils.isEmpty(eventType) || StringUtils.isEmpty(resourceId)) {
        responseStatus = Response.Status.BAD_REQUEST;
        gen.writeStringField("message", "eventType and resourceId parameters are all required");
    } else {
        if (eventType.equals("OPTIN")) {
            responseStatus = optInResource(resourceId, true, region, gen);
        } else if (eventType.equals("OPTOUT")) {
            responseStatus = optInResource(resourceId, false, region, gen);
        } else {
            responseStatus = Response.Status.BAD_REQUEST;
            gen.writeStringField("message", String.format("Unrecognized event type: %s", eventType));
        }
    }
    gen.writeEndObject();
    gen.close();
    LOGGER.info("entity content is '{}'", baos.toString("UTF-8"));
    return Response.status(responseStatus).entity(baos.toString("UTF-8")).build();
}
Also used : Response(javax.ws.rs.core.Response) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonNode(org.codehaus.jackson.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) POST(javax.ws.rs.POST)

Example 24 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project OpenAttestation by OpenAttestation.

the class ReadXmlTest method mapXmlToJson.

private void mapXmlToJson(String xmlfile) throws Exception {
    JAXB jaxb = new JAXB();
    InputStream in = getClass().getResourceAsStream(xmlfile);
    try {
        String xml = IOUtils.toString(in);
        SelectionsType selections = jaxb.read(xml, SelectionsType.class);
        ObjectMapper mapper = new ObjectMapper();
        //mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); // omit empty attributes, for example {"selection":[{"subject":[],"attribute":[],"id":"8ed9140b-e6a1-41b2-a8d4-258948633153","name":null,"notBefore":null,"notAfter":null}]}  becomes   {"selection":[{"id":"8ed9140b-e6a1-41b2-a8d4-258948633153"}]}
        // omit empty attributes, for example {"selection":[{"subject":[],"attribute":[],"id":"8ed9140b-e6a1-41b2-a8d4-258948633153","name":null,"notBefore":null,"notAfter":null}]}  becomes   {"selection":[{"id":"8ed9140b-e6a1-41b2-a8d4-258948633153"}]}
        mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_EMPTY);
        log.debug("{}: {}", xmlfile, mapper.writeValueAsString(selections));
    } catch (Exception e) {
        throw e;
    }
}
Also used : JAXB(com.intel.dcsg.cpg.xml.JAXB) InputStream(java.io.InputStream) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 25 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project OpenAttestation by OpenAttestation.

the class ReadXmlTest method testParseJsonSelection.

@Test
public void testParseJsonSelection() throws Exception {
    String selection1 = "{\"selection\":[{\"attribute\":[{\"text\":{\"value\":\"Country=US\"},\"oid\":\"2.5.4.789.1\"},{\"text\":{\"value\":\"State=CA\"},\"oid\":\"2.5.4.789.1\"},{\"text\":{\"value\":\"State=TX\"},\"oid\":\"2.5.4.789.1\"},{\"text\":{\"value\":\"City=Folsom\"},\"oid\":\"2.5.4.789.1\"},{\"text\":{\"value\":\"City=El Paso\"},\"oid\":\"2.5.4.789.1\"}]}]}";
    ObjectMapper mapper = new ObjectMapper();
    SelectionsType selections = mapper.readValue(selection1, SelectionsType.class);
    printSelections(selections);
/*
15:56:22.291 [main] DEBUG test.xml.ReadXmlTest - /selection-xml-examples/selection1.xml: {"selection":[{"attribute":[{"text":{"value":"Country=US"},"oid":"2.5.4.789.1"},{"text":{"value":"State=CA"},"oid":"2.5.4.789.1"},{"text":{"value":"State=TX"},"oid":"2.5.4.789.1"},{"text":{"value":"City=Folsom"},"oid":"2.5.4.789.1"},{"text":{"value":"City=El Paso"},"oid":"2.5.4.789.1"}]}]}
15:56:22.331 [main] DEBUG test.xml.ReadXmlTest - /selection-xml-examples/selection2.xml: {"selection":[{"id":"0b52784b-4588-4c73-900d-a1bac622dde1"}]}
15:56:22.370 [main] DEBUG test.xml.ReadXmlTest - /selection-xml-examples/selection3.xml: {"selection":[{"attribute":[{"text":{"value":"US"},"oid":"2.5.4.6"},{"text":{"value":"CA"},"oid":"2.5.4.8"},{"text":{"value":"TX"},"oid":"2.5.4.8"},{"text":{"value":"Folsom"},"oid":"2.5.4.7"},{"text":{"value":"El Paso"},"oid":"2.5.4.7"}]}]}
15:56:22.435 [main] DEBUG test.xml.ReadXmlTest - /selection-xml-examples/selection4.xml: {"selection":[{"attribute":[{"text":{"value":"Country=US"},"oid":"2.5.4.789.1"},{"text":{"value":"State=CA"},"oid":"2.5.4.789.1"},{"text":{"value":"State=TX"},"oid":"2.5.4.789.1"},{"text":{"value":"City=Folsom"},"oid":"2.5.4.789.1"},{"text":{"value":"City=El Paso"},"oid":"2.5.4.789.1"}],"id":"8ed9140b-e6a1-41b2-a8d4-258948633153","name":"California","notBefore":1385555160000,"notAfter":1388143559000}]}
15:56:22.476 [main] DEBUG test.xml.ReadXmlTest - /selection-xml-examples/selection5.xml: {"selection":[{"attribute":[{"text":{"value":"Country=US"},"oid":"2.5.4.789.1"},{"text":{"value":"State=CA"},"oid":"2.5.4.789.1"},{"text":{"value":"City=Folsom"},"oid":"2.5.4.789.1"}],"id":"0b52784b-4588-4c73-900d-a1bac622dde1"},{"attribute":[{"text":{"value":"Country=US"},"oid":"2.5.4.789.1"},{"text":{"value":"State=TX"},"oid":"2.5.4.789.1"},{"text":{"value":"City=El Paso"},"oid":"2.5.4.789.1"}],"id":"1c22784b-4588-4c73-900d-b1bef279abf7"}]}
15:56:22.513 [main] DEBUG test.xml.ReadXmlTest - /selection-xml-examples/selection6.xml: {"selection":[{"id":"0b52784b-4588-4c73-900d-a1bac622dde1"},{"id":"bbbe1c68-4792-454c-9295-1a1234e1aa9f"}]}
15:56:22.544 [main] DEBUG test.xml.ReadXmlTest - /selection-xml-examples/selection7.xml: {"selection":[{"attribute":[{"text":{"value":"Country=US"},"oid":"2.5.4.789.1"},{"text":{"value":"State=CA"},"oid":"2.5.4.789.1"},{"text":{"value":"City=Folsom"},"oid":"2.5.4.789.1"}],"id":"8ed9140b-e6a1-41b2-a8d4-258948633153","name":"California"},{"attribute":[{"text":{"value":"Country=US"},"oid":"2.5.4.789.1"},{"text":{"value":"State=TX"},"oid":"2.5.4.789.1"},{"text":{"value":"City=El Paso"},"oid":"2.5.4.789.1"}],"id":"24e7d7be-f337-47f7-a1af-9a4dbdaeb69d"},{"attribute":[{"text":{"value":"Country=CA"},"oid":"2.5.4.789.1"},{"text":{"value":"Province=Quebec"},"oid":"2.5.4.789.1"},{"text":{"value":"City=Quebec City"},"oid":"2.5.4.789.1"}],"name":"Canada"}]}
15:56:22.590 [main] DEBUG test.xml.ReadXmlTest - /selection-xml-examples/selection8.xml: {"selection":[{"subject":[{"ip":{"value":"192.168.1.101"}}],"attribute":[{"text":{"value":"Country=US"},"oid":"2.5.4.789.1"},{"text":{"value":"State=CA"},"oid":"2.5.4.789.1"},{"text":{"value":"City=Folsom"},"oid":"2.5.4.789.1"}],"id":"8ed9140b-e6a1-41b2-a8d4-258948633153","name":"California"},{"subject":[{"ip":{"value":"192.168.1.102"}}],"attribute":[{"text":{"value":"Country=US"},"oid":"2.5.4.789.1"},{"text":{"value":"State=TX"},"oid":"2.5.4.789.1"},{"text":{"value":"City=El Paso"},"oid":"2.5.4.789.1"}],"id":"24e7d7be-f337-47f7-a1af-9a4dbdaeb69d"},{"subject":[{"ip":{"value":"192.168.1.103"}},{"ip":{"value":"192.168.1.104"}},{"ip":{"value":"192.168.1.105"}}],"attribute":[{"text":{"value":"Country=CA"},"oid":"2.5.4.789.1"},{"text":{"value":"Province=Quebec"},"oid":"2.5.4.789.1"},{"text":{"value":"City=Quebec City"},"oid":"2.5.4.789.1"}],"name":"Canada"}]}
15:56:22.627 [main] DEBUG test.xml.ReadXmlTest - /selection-xml-examples/selection9.xml: {"selection":[{"subject":[{"uuid":{"value":"f98cabf1-6ab1-47c2-8b5c-e3be3d6865ad"}}],"attribute":[{"text":{"value":"Country=US"},"oid":"2.5.4.789.1"},{"text":{"value":"State=CA"},"oid":"2.5.4.789.1"},{"text":{"value":"City=Folsom"},"oid":"2.5.4.789.1"}],"id":"8ed9140b-e6a1-41b2-a8d4-258948633153","name":"California"},{"subject":[{"uuid":{"value":"21c26a6e-5401-41de-a168-d637e1e1b154"}}],"attribute":[{"text":{"value":"Country=US"},"oid":"2.5.4.789.1"},{"text":{"value":"State=TX"},"oid":"2.5.4.789.1"},{"text":{"value":"City=El Paso"},"oid":"2.5.4.789.1"}],"id":"24e7d7be-f337-47f7-a1af-9a4dbdaeb69d"},{"subject":[{"uuid":{"value":"c6b37600-c05f-4131-afd7-69b1212b24a7"}}],"attribute":[{"text":{"value":"Country=CA"},"oid":"2.5.4.789.1"},{"text":{"value":"Province=Quebec"},"oid":"2.5.4.789.1"},{"text":{"value":"City=Quebec City"},"oid":"2.5.4.789.1"}],"name":"Canada"}]}
15:56:22.660 [main] DEBUG test.xml.ReadXmlTest - /selection-xml-examples/selection10.xml: {"selection":[{"attribute":[{"text":{"value":"Country=US"},"oid":"2.5.4.789.1"},{"text":{"value":"State=NY"},"oid":"2.5.4.789.1"},{"text":{"value":"City=New York"},"oid":"2.5.4.789.1"}],"id":"0b52784b-4588-4c73-900d-a1bac622dde1","name":"New York"},{"subject":[{"ip":{"value":"192.168.1.101"}}],"attribute":[{"text":{"value":"Country=US"},"oid":"2.5.4.789.1"},{"text":{"value":"State=CA"},"oid":"2.5.4.789.1"},{"text":{"value":"City=Folsom"},"oid":"2.5.4.789.1"},{"text":{"value":"City=Santa Clara"},"oid":"2.5.4.789.1"}],"id":"8ed9140b-e6a1-41b2-a8d4-258948633153","name":"California"},{"subject":[{"uuid":{"value":"21c26a6e-5401-41de-a168-d637e1e1b154"}},{"uuid":{"value":"f98cabf1-6ab1-47c2-8b5c-e3be3d6865ad"}}],"attribute":[{"text":{"value":"Country=US"},"oid":"2.5.4.789.1"},{"text":{"value":"State=TX"},"oid":"2.5.4.789.1"},{"text":{"value":"City=El Paso"},"oid":"2.5.4.789.1"}],"id":"24e7d7be-f337-47f7-a1af-9a4dbdaeb69d","name":"Texas"},{"subject":[{"ip":{"value":"192.168.1.103"}},{"ip":{"value":"192.168.1.104"}},{"ip":{"value":"192.168.1.105"}}],"attribute":[{"text":{"value":"Country=MX"},"oid":"2.5.4.789.1"},{"text":{"value":"City=Mexico City"},"oid":"2.5.4.789.1"},{"text":{"value":"Dept=Finance"},"oid":"2.5.4.789.1"}],"id":"c6b37600-c05f-4131-afd7-69b1212b24a7","name":"Mexico"}]}
15:56:22.686 [main] DEBUG test.xml.ReadXmlTest - /selection-xml-examples/selection11.xml: {"selection":[{"name":"California"}]}
15:56:22.712 [main] DEBUG test.xml.ReadXmlTest - /selection-xml-examples/selection12.xml: {"selection":[{"id":"8ed9140b-e6a1-41b2-a8d4-258948633153"}]}
        */
}
Also used : ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Aggregations

ObjectMapper (org.codehaus.jackson.map.ObjectMapper)356 IOException (java.io.IOException)75 Test (org.junit.Test)58 JsonNode (org.codehaus.jackson.JsonNode)47 ArrayList (java.util.ArrayList)43 HashMap (java.util.HashMap)43 Test (org.testng.annotations.Test)37 Map (java.util.Map)33 List (java.util.List)25 StringWriter (java.io.StringWriter)22 File (java.io.File)21 JSONObject (org.json.JSONObject)18 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)15 Version (org.codehaus.jackson.Version)14 SimpleModule (org.codehaus.jackson.map.module.SimpleModule)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 JsonFactory (org.codehaus.jackson.JsonFactory)13 JSONObject (org.codehaus.jettison.json.JSONObject)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)11