Search in sources :

Example 1 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project hbase by apache.

the class JSONBean method open.

public Writer open(final PrintWriter writer) throws IOException {
    final JsonGenerator jg = jsonFactory.createJsonGenerator(writer);
    jg.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
    jg.useDefaultPrettyPrinter();
    jg.writeStartObject();
    return new Writer() {

        @Override
        public void flush() throws IOException {
            jg.flush();
        }

        @Override
        public void close() throws IOException {
            jg.close();
        }

        @Override
        public void write(String key, String value) throws JsonGenerationException, IOException {
            jg.writeStringField(key, value);
        }

        @Override
        public int write(MBeanServer mBeanServer, ObjectName qry, String attribute, boolean description) throws IOException {
            return JSONBean.write(jg, mBeanServer, qry, attribute, description);
        }
    };
}
Also used : JsonGenerator(org.codehaus.jackson.JsonGenerator) PrintWriter(java.io.PrintWriter) StringWriter(java.io.StringWriter) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 2 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project hive by apache.

the class JMXJsonServlet method doGet.

/**
   * Process a GET request for the specified resource.
   * 
   * @param request
   *          The servlet request we are processing
   * @param response
   *          The servlet response we are creating
   */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
    try {
        if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(), request, response)) {
            return;
        }
        JsonGenerator jg = null;
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            response.setContentType("application/json; charset=utf8");
            response.setHeader(ACCESS_CONTROL_ALLOW_METHODS, "GET");
            response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
            jg = jsonFactory.createJsonGenerator(writer);
            jg.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
            jg.useDefaultPrettyPrinter();
            jg.writeStartObject();
            // query per mbean attribute
            String getmethod = request.getParameter("get");
            if (getmethod != null) {
                String[] splitStrings = getmethod.split("\\:\\:");
                if (splitStrings.length != 2) {
                    jg.writeStringField("result", "ERROR");
                    jg.writeStringField("message", "query format is not as expected.");
                    jg.flush();
                    response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                    return;
                }
                listBeans(jg, new ObjectName(splitStrings[0]), splitStrings[1], response);
                return;
            }
            // query per mbean
            String qry = request.getParameter("qry");
            if (qry == null) {
                qry = "*:*";
            }
            listBeans(jg, new ObjectName(qry), null, response);
        } finally {
            if (jg != null) {
                jg.close();
            }
            if (writer != null) {
                writer.close();
            }
        }
    } catch (IOException e) {
        LOG.error("Caught an exception while processing JMX request", e);
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    } catch (MalformedObjectNameException e) {
        LOG.error("Caught an exception while processing JMX request", e);
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) JsonGenerator(org.codehaus.jackson.JsonGenerator) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) ObjectName(javax.management.ObjectName)

Example 3 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator 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 4 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator 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 5 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project WSPerfLab by Netflix-Skunkworks.

the class ServiceResponseBuilder method buildTestAResponse.

public static ByteArrayOutputStream buildTestAResponse(JsonFactory jsonFactory, BackendResponse responseA, BackendResponse responseB, BackendResponse responseC, BackendResponse responseD, BackendResponse responseE) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(bos);
    jsonGenerator.writeStartObject();
    // multiplication of C, D, E responseKey
    jsonGenerator.writeNumberField("responseKey", responseC.getResponseKey() + responseD.getResponseKey() + responseE.getResponseKey());
    // delay values of each response
    jsonGenerator.writeArrayFieldStart("delay");
    writeTuple(jsonGenerator, "a", responseA.getDelay());
    writeTuple(jsonGenerator, "b", responseB.getDelay());
    writeTuple(jsonGenerator, "c", responseC.getDelay());
    writeTuple(jsonGenerator, "d", responseD.getDelay());
    writeTuple(jsonGenerator, "e", responseE.getDelay());
    jsonGenerator.writeEndArray();
    // itemSize values of each response
    jsonGenerator.writeArrayFieldStart("itemSize");
    writeTuple(jsonGenerator, "a", responseA.getItemSize());
    writeTuple(jsonGenerator, "b", responseB.getItemSize());
    writeTuple(jsonGenerator, "c", responseC.getItemSize());
    writeTuple(jsonGenerator, "d", responseD.getItemSize());
    writeTuple(jsonGenerator, "e", responseE.getItemSize());
    jsonGenerator.writeEndArray();
    // numItems values of each response
    jsonGenerator.writeArrayFieldStart("numItems");
    writeTuple(jsonGenerator, "a", responseA.getNumItems());
    writeTuple(jsonGenerator, "b", responseB.getNumItems());
    writeTuple(jsonGenerator, "c", responseC.getNumItems());
    writeTuple(jsonGenerator, "d", responseD.getNumItems());
    writeTuple(jsonGenerator, "e", responseE.getNumItems());
    jsonGenerator.writeEndArray();
    // all items from responses
    jsonGenerator.writeArrayFieldStart("items");
    addItemsFromResponse(jsonGenerator, responseA);
    addItemsFromResponse(jsonGenerator, responseB);
    addItemsFromResponse(jsonGenerator, responseC);
    addItemsFromResponse(jsonGenerator, responseD);
    addItemsFromResponse(jsonGenerator, responseE);
    jsonGenerator.writeEndArray();
    jsonGenerator.writeEndObject();
    jsonGenerator.close();
    return bos;
}
Also used : JsonGenerator(org.codehaus.jackson.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

JsonGenerator (org.codehaus.jackson.JsonGenerator)52 JsonFactory (org.codehaus.jackson.JsonFactory)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 IOException (java.io.IOException)11 StringWriter (java.io.StringWriter)10 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)9 JsonProcessingException (org.codehaus.jackson.JsonProcessingException)8 RpcException (cz.metacentrum.perun.core.api.exceptions.RpcException)6 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)5 JsonNode (org.codehaus.jackson.JsonNode)5 OutputStreamWriter (java.io.OutputStreamWriter)4 File (java.io.File)3 HashMap (java.util.HashMap)3 GET (javax.ws.rs.GET)3 Response (javax.ws.rs.core.Response)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 BufferedWriter (java.io.BufferedWriter)2 DataOutputStream (java.io.DataOutputStream)2 OutputStream (java.io.OutputStream)2 PrintWriter (java.io.PrintWriter)2