Example usage for org.json.simple JSONAware toJSONString

List of usage examples for org.json.simple JSONAware toJSONString

Introduction

In this page you can find the example usage for org.json.simple JSONAware toJSONString.

Prototype

public String toJSONString();

Source Link

Usage

From source file:com.nesscomputing.jmx.jolokia.JolokiaServlet.java

private void handle(final ServletRequestHandler reqHandler, final HttpServletRequest req,
        final HttpServletResponse resp) throws IOException {
    JSONAware json = null;
    try {/*from   w ww. ja v  a  2s.com*/
        // Check access policy
        requestHandler.checkClientIPAccess(req.getRemoteHost(), req.getRemoteAddr());

        // Dispatch for the proper HTTP request method
        json = reqHandler.handleRequest(req, resp);

        if (backendManager.isDebug()) {
            backendManager.debug("Response: " + json);
        }
    } catch (RuntimeMBeanException rme) {
        json = requestHandler.handleThrowable(rme.getTargetException());
    } catch (Throwable exp) {
        json = requestHandler.handleThrowable(exp);
    } finally {
        final String callback = req.getParameter(ConfigKey.CALLBACK.getKeyValue());
        if (callback != null) {
            // Send a JSONP response
            sendResponse(resp, "text/javascript", callback + "(" + json.toJSONString() + ");");
        } else {
            sendResponse(resp, "application/json", json.toJSONString());
        }
    }
}

From source file:org.jolokia.http.AgentServlet.java

@SuppressWarnings({ "PMD.AvoidCatchingThrowable", "PMD.AvoidInstanceofChecksInCatchClause" })
private void handle(ServletRequestHandler pReqHandler, HttpServletRequest pReq, HttpServletResponse pResp)
        throws IOException {
    JSONAware json = null;
    try {/*from  w  ww . jav a2 s .c o m*/
        // Check access policy
        requestHandler.checkAccess(allowDnsReverseLookup ? pReq.getRemoteHost() : null, pReq.getRemoteAddr(),
                getOriginOrReferer(pReq));

        // Remember the agent URL upon the first request. Needed for discovery
        updateAgentDetailsIfNeeded(pReq);

        // Dispatch for the proper HTTP request method
        json = handleSecurely(pReqHandler, pReq, pResp);
    } catch (Throwable exp) {
        json = requestHandler.handleThrowable(
                exp instanceof RuntimeMBeanException ? ((RuntimeMBeanException) exp).getTargetException()
                        : exp);
    } finally {
        setCorsHeader(pReq, pResp);

        String callback = pReq.getParameter(ConfigKey.CALLBACK.getKeyValue());
        String answer = json != null ? json.toJSONString()
                : requestHandler.handleThrowable(new Exception("Internal error while handling an exception"))
                        .toJSONString();
        if (callback != null) {
            // Send a JSONP response
            sendResponse(pResp, "text/javascript", callback + "(" + answer + ");");
        } else {
            sendResponse(pResp, getMimeType(pReq), answer);
        }
    }
}

From source file:org.jolokia.jvmagent.handler.JolokiaHttpHandler.java

private void sendAllJSON(HttpExchange pExchange, ParsedUri pParsedUri, JSONAware pJson) throws IOException {
    OutputStream out = null;/*from   w  w  w  . ja  v a 2  s. co m*/
    try {
        Headers headers = pExchange.getResponseHeaders();
        if (pJson != null) {
            headers.set("Content-Type", getMimeType(pParsedUri) + "; charset=utf-8");
            String json = pJson.toJSONString();
            String callback = pParsedUri.getParameter(ConfigKey.CALLBACK.getKeyValue());
            String content = callback == null ? json : callback + "(" + json + ");";
            byte[] response = content.getBytes("UTF8");
            pExchange.sendResponseHeaders(200, response.length);
            out = pExchange.getResponseBody();
            out.write(response);
        } else {
            headers.set("Content-Type", "text/plain");
            pExchange.sendResponseHeaders(200, -1);
        }
    } finally {
        if (out != null) {
            // Always close in order to finish the request.
            // Otherwise the thread blocks.
            out.close();
        }
    }
}

From source file:org.jolokia.jvmagent.JolokiaHttpHandler.java

private void sendResponse(HttpExchange pExchange, ParsedUri pParsedUri, JSONAware pJson) throws IOException {
    OutputStream out = null;//w  ww. j av a2  s .  c  om
    try {
        Headers headers = pExchange.getResponseHeaders();
        if (pJson != null) {
            headers.set("Content-Type", getMimeType(pParsedUri) + "; charset=utf-8");
            String json = pJson.toJSONString();
            String callback = pParsedUri.getParameter(ConfigKey.CALLBACK.getKeyValue());
            String content = callback == null ? json : callback + "(" + json + ");";
            byte[] response = content.getBytes("UTF8");
            pExchange.sendResponseHeaders(200, response.length);
            out = pExchange.getResponseBody();
            out.write(response);
        } else {
            headers.set("Content-Type", "text/plain");
            pExchange.sendResponseHeaders(200, -1);
        }
    } finally {
        if (out != null) {
            // Always close in order to finish the request.
            // Otherwise the thread blocks.
            out.close();
        }
    }
}

From source file:uk.ac.susx.tag.method51.webapp.jetty.results.JsonHandler.java

@Override
public void handle(JSONAware result, String target, Request baseRequest, HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    baseRequest.setHandled(true);//from  www.j a  v a 2 s .com
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/json");
    response.getWriter().write(result.toJSONString());
}