Search in sources :

Example 86 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project hbase by apache.

the class RowResourceBase method checkValueJSON.

protected static void checkValueJSON(String table, String row, String column, String value) throws IOException, JAXBException {
    Response response = getValueJson(table, row, column);
    assertEquals(response.getCode(), 200);
    assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
    ObjectMapper mapper = new JacksonJaxbJsonProvider().locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
    CellSetModel cellSet = mapper.readValue(response.getBody(), CellSetModel.class);
    RowModel rowModel = cellSet.getRows().get(0);
    CellModel cell = rowModel.getCells().get(0);
    assertEquals(Bytes.toString(cell.getColumn()), column);
    assertEquals(Bytes.toString(cell.getValue()), value);
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) JacksonJaxbJsonProvider(org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 87 with ObjectMapper

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

the class ESSink method doSerialize.

private void doSerialize(Event event) throws IOException {
    Map<String, String> headers = event.getHeaders();
    String content = null;
    String serviceName = "";
    if (!headers.containsKey("InsTime")) {
        long insTime = System.currentTimeMillis();
        headers.put("InsTime", String.valueOf(insTime));
    }
    if (headers.containsKey("ServiceName")) {
        serviceName = headers.get("ServiceName");
        int pos = serviceName.indexOf(".");
        if (pos > 0) {
            serviceName = serviceName.substring(0, pos);
        }
    }
    ObjectMapper objectMapper = new ObjectMapper();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonGenerator jgen = null;
    try {
        jgen = objectMapper.getJsonFactory().createJsonGenerator(baos, JsonEncoding.UTF8);
        jgen.writeStartObject();
        for (String headerKey : headers.keySet()) {
            String headerValue = headers.get(headerKey);
            if (headerValue != null && !headerValue.isEmpty()) {
                jgen.writeStringField(headerKey, headerValue);
            }
        }
        content = new String(event.getBody());
        content = content.replace('\t', ' ').replace('\n', ' ');
        if (content.length() > maxContentLength) {
            content = content.substring(0, maxContentLength - 15) + "<..truncated..>";
        }
        jgen.writeStringField("body", content);
        jgen.writeEndObject();
        jgen.flush();
        jgen = null;
        //outputStream.write((baos.toString() + "\n").getBytes());
        String curIndexName = getCurrentIndexName(serviceName.toLowerCase());
        //LOG.info("index: (" + curIndexName + "," + indexType + ") source: " + baos.toString());
        esThreadRequest.sourceList.add(baos.toString());
        esThreadRequest.indexNameList.add(curIndexName);
        esThreadRequest.indexTypeList.add(indexType);
        if (esThreadRequest.sourceList.size() >= bulkNum) {
            submitESRequest(esThreadRequest);
            esThreadRequest = new ESClientThread.ESThreadRequest();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    LOG.info("ES sink process: " + content + " " + headers.get("IP") + " " + headers.get("Level") + " " + headers.get("RPCName"));
}
Also used : JsonGenerator(org.codehaus.jackson.JsonGenerator) String(java.lang.String) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 88 with ObjectMapper

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

the class TestClient method main.

public static void main(String[] args) {
    Options options = new Options();
    Option option = new Option("t", "type", true, "the type of this request: query/add/del/get/graph");
    option.setRequired(true);
    options.addOption(option);
    option = new Option("f", "field", true, "required if add field or del field");
    option.setRequired(false);
    options.addOption(option);
    option = new Option("h", "help", false, "show help");
    options.addOption(option);
    CommandLineParser parser = new GnuParser();
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println("Parse command line failed.");
        e.printStackTrace();
        return;
    }
    if (commandLine.hasOption('h')) {
        new HelpFormatter().printHelp("LogsysProxy Client", options, true);
        return;
    }
    String reqType = commandLine.getOptionValue('t');
    String fieldName = commandLine.getOptionValue("f");
    if (!reqType.equals("query") && !reqType.equals("add") && !reqType.equals("del") && !reqType.equals("get") && !reqType.equals("graph")) {
        System.out.println("Invalid type " + reqType);
        return;
    }
    if ((reqType.equals("add") || reqType.equals("del")) && (fieldName == null || fieldName.isEmpty())) {
        System.out.println("No fieldname specified.");
        return;
    }
    try {
        Socket socket = new Socket("localhost", 30150);
        //�ֽ������
        OutputStream os = socket.getOutputStream();
        org.msec.LogsysReq req = new org.msec.LogsysReq();
        if (reqType.equals("query")) {
            req.queryLogReq = new org.msec.LogsysReq.QueryLogReq();
            setRequest(req.queryLogReq);
        }
        if (reqType.equals("add")) {
            req.modifyFieldsReq = new org.msec.LogsysReq.ModifyFieldsReq();
            req.modifyFieldsReq.setAppName("App");
            req.modifyFieldsReq.setFieldName(fieldName);
            req.modifyFieldsReq.setFieldType("String");
            req.modifyFieldsReq.setOperator("ADD");
        }
        if (reqType.equals("del")) {
            req.modifyFieldsReq = new org.msec.LogsysReq.ModifyFieldsReq();
            req.modifyFieldsReq.setAppName("TestApp");
            req.modifyFieldsReq.setFieldName(fieldName);
            req.modifyFieldsReq.setFieldType("String");
            req.modifyFieldsReq.setOperator("DEL");
        }
        if (reqType.equals("get")) {
            req.getFieldsReq = new org.msec.LogsysReq.GetFieldsReq();
            req.getFieldsReq.setAppName("TestApp");
        }
        if (reqType.equals("graph")) {
            req.callGraphReq = new org.msec.LogsysReq.CallGraphReq();
            req.callGraphReq.setAppName("JavaSample.Jecho");
            //req.callGraphReq.addFilterField(new LogField("RPCName", "", "rpc814"));
            req.callGraphReq.setStartDate("2016-07-04");
            req.callGraphReq.setEndDate("2016-07-04");
            req.callGraphReq.setStartTime("00:00:00");
            req.callGraphReq.setEndTime("23:59:00");
        }
        ObjectMapper objectMapper = new ObjectMapper();
        String reqJsonStr = objectMapper.writeValueAsString(req);
        String reqJsonLen = String.format("%-10d", reqJsonStr.length());
        System.out.println(reqJsonLen + reqJsonStr);
        os.write(reqJsonLen.getBytes());
        os.write(reqJsonStr.getBytes());
        //�ر������
        socket.shutdownOutput();
        InputStream is = socket.getInputStream();
        byte[] buff = new byte[1024];
        int readLen = 0;
        //ѭ����ȡ
        while ((readLen = is.read(buff)) > 0) {
            System.out.println(new String(buff));
        }
        is.close();
        os.close();
        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Socket(java.net.Socket) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 89 with ObjectMapper

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

the class CarryOutReleasePlan method exec.

public JsonRPCResponseBase exec(ReleasePlan request) {
    Logger logger = Logger.getLogger(CarryOutReleasePlan.class);
    JsonRPCResponseBase resp = new JsonRPCResponseBase();
    plan_id = request.getPlan_id();
    flsn = request.getFirst_level_service_name();
    slsn = request.getSecond_level_service_name();
    logger.info(String.format("carry out plan begin\n [%s.%s][%s]\n", flsn, slsn, plan_id));
    if (flsn == null || flsn.length() < 1 || slsn == null || slsn.length() < 1 || plan_id == null || plan_id.length() < 1) {
        resp.setStatus(100);
        resp.setMessage("service name/plan id invalid!");
        return resp;
    }
    String result = checkIdentity();
    if (!result.equals("success")) {
        resp.setStatus(99);
        resp.setMessage(result);
        return resp;
    }
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        resp.setStatus(100);
        resp.setMessage("db connect failed!");
        return resp;
    }
    try {
        String sql = "select dest_ip_list, memo,release_type from t_release_plan where plan_id=?";
        ArrayList<Object> params = new ArrayList<Object>();
        params.add(request.getPlan_id());
        Map<String, Object> res = util.findSimpleResult(sql, params);
        if (res.get("dest_ip_list") == null) {
            resp.setStatus(100);
            resp.setMessage("query dest ip list failed.");
            return resp;
        }
        String dest_ip_list = (String) (res.get("dest_ip_list"));
        release_memo = (String) (res.get("memo"));
        release_type = (String) (res.get("release_type"));
        release_memo = plan_id.substring(4, 12) + release_memo;
        ObjectMapper objectMapper = new ObjectMapper();
        IPPortPair[] ips = objectMapper.readValue(dest_ip_list, IPPortPair[].class);
        logger.info("destination IP number;" + ips.length);
        //每一个目标ip执行耗时估计30s,
        long gap = ips.length * 60;
        long lastCarryoutTime = getLastCarryoutTime(util);
        checkTime(util, gap, lastCarryoutTime);
        logger.info("check last carry out time OK");
        //对每个IP下发安装包
        String tarFile = PackReleaseFile.getPackedFile(request.getPlan_id());
        logger.info("release package path:" + tarFile);
        getHttpResponse().setContentType("text/html");
        getHttpResponse().setCharacterEncoding("UTF-8");
        ServletOutputStream out = getHttpResponse().getOutputStream();
        safeWrite("<html>", out);
        safeWrite("<head>", out);
        safeWrite("<title>release result</title>", out);
        safeWrite("</head>", out);
        safeWrite("<body>", out);
        //这个标签用于原样输出
        safeWrite("<pre>", out);
        updatePlanStatus(util, "carrying out");
        String dev_lang = getDevLang(util, flsn, slsn);
        doRelease(tarFile, ips, out, lastCarryoutTime == 0, dev_lang, util);
        safeWrite("</pre>", out);
        safeWrite("</body>", out);
        safeWrite("</html>", out);
        //update plan status
        updatePlanStatus(util, "carry out successfully");
        try {
            out.close();
        } catch (Exception e) {
        }
        return null;
    } catch (Exception e) {
        resp.setStatus(100);
        resp.setMessage(e.getMessage());
        e.printStackTrace();
        try {
            updatePlanStatus(util, "failed to carry out");
        } catch (Exception e1) {
        }
        return resp;
    } finally {
        util.releaseConn();
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) IPPortPair(beans.request.IPPortPair) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 90 with ObjectMapper

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

the class JsonRPCServlet method service.

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Logger logger = Logger.getLogger(JsonRPCServlet.class);
    req.setCharacterEncoding("UTF-8");
    //从http请求里获取完整的json字符串
    //json字符串是http请求的一个form字段
    String request_string = req.getParameter("request_string");
    int maxLen = request_string.length();
    if (maxLen > 10240) {
        maxLen = 10240;
    }
    logger.info("request_string:" + request_string.substring(0, maxLen));
    //ObjectManager和JSONObject两个都是 json与java类之间的转换工具
    ObjectMapper objectMapper = new ObjectMapper();
    JSONObject jsonObject = new JSONObject(request_string);
    String handleClassStr = jsonObject.getString("handleClass");
    JSONObject requestBodyObj = jsonObject.getJSONObject("requestBody");
    logger.info("requestBody:" + requestBodyObj.toString());
    try {
        //加载handleClass字段指定的类,并创建它的实例
        Class<?> clazz = Class.forName(handleClassStr);
        JsonRPCHandler handler = (JsonRPCHandler) clazz.newInstance();
        //将http的上下文信息传给该实例,以方便在业务逻辑处理的时候访问
        handler.setHttpRequest(req);
        handler.setHttpResponse(resp);
        handler.setServlet(this);
        //获取类的所有成员方法,遍历找到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的参数列表,要求只能有一个参数
                Class<?>[] paramTypes = methods[i].getParameterTypes();
                if (paramTypes.length != 1) {
                    errorResponse(resp, "handle class's exec() method's param number is not 1!");
                    return;
                }
                //将json字符串的requestBody部分映射到一个java类的实例,作为exec函数的参数
                Object exec_request = objectMapper.readValue(requestBodyObj.toString(), paramTypes[0]);
                //特殊场景下,例如文件下载等,exec 会返回null,不需要给前端返回json字符串
                try {
                    Object exec_result = methods[i].invoke(handler, exec_request);
                    if (exec_result != null) {
                        //�����ص�bean���л�Ϊjson�ַ���
                        String s = objectMapper.writeValueAsString(exec_result);
                        resp.setCharacterEncoding("UTF-8");
                        resp.setContentType("application/json; charset=utf-8");
                        PrintWriter out = resp.getWriter();
                        out.println(s);
                        maxLen = s.length();
                        if (maxLen > 10240) {
                            maxLen = 10240;
                        }
                        logger.info("response:" + s.substring(0, maxLen));
                        out.close();
                    }
                    return;
                } catch (InvocationTargetException e) {
                    logger.error(e.getMessage());
                    e.printStackTrace();
                }
            }
        }
        if (!execFound) {
            errorResponse(resp, "handleClass has NOT method named exec()");
            return;
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        errorResponse(resp, "ClassNotFoundException:" + e.toString());
        return;
    } catch (InstantiationException e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        errorResponse(resp, "InstantiationException:" + e.toString());
        ;
        return;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        errorResponse(resp, "IllegalAccessException:" + e.toString());
        return;
    }
}
Also used : Method(java.lang.reflect.Method) Logger(org.apache.log4j.Logger) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) PrintWriter(java.io.PrintWriter)

Aggregations

ObjectMapper (org.codehaus.jackson.map.ObjectMapper)386 IOException (java.io.IOException)85 Test (org.junit.Test)61 JsonNode (org.codehaus.jackson.JsonNode)50 HashMap (java.util.HashMap)46 ArrayList (java.util.ArrayList)45 Test (org.testng.annotations.Test)37 Map (java.util.Map)34 List (java.util.List)26 File (java.io.File)22 StringWriter (java.io.StringWriter)22 JSONObject (org.json.JSONObject)18 SimpleModule (org.codehaus.jackson.map.module.SimpleModule)17 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 Version (org.codehaus.jackson.Version)14 JsonFactory (org.codehaus.jackson.JsonFactory)13 JSONObject (org.codehaus.jettison.json.JSONObject)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 JsonGenerator (org.codehaus.jackson.JsonGenerator)11