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;
}
}
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";
}
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();
}
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;
}
}
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"}]}
*/
}
Aggregations