Package org.json.simple

Examples of org.json.simple.JSONAware


    }

    @Override
    @SuppressWarnings({ "PMD.AvoidCatchingThrowable", "PMD.AvoidInstanceofChecksInCatchClause" })
    public void handle(HttpExchange pExchange) throws IOException {
        JSONAware json = null;
        URI uri = pExchange.getRequestURI();
        ParsedUri parsedUri = new ParsedUri(uri,context);
        try {
            // Check access policy
            InetSocketAddress address = pExchange.getRemoteAddress();
            requestHandler.checkClientIPAccess(address.getHostName(),address.getAddress().getHostAddress());
            String method = pExchange.getRequestMethod();

            // Dispatch for the proper HTTP request method
            if ("GET".equalsIgnoreCase(method)) {
                json = executeGetRequest(parsedUri);
            } else if ("POST".equalsIgnoreCase(method)) {
                json = executePostRequest(pExchange, parsedUri);
            } else {
                throw new IllegalArgumentException("HTTP Method " + method + " is not supported.");
            }
            if (backendManager.isDebug()) {
                backendManager.info("Response: " + json);
            }
        } catch (Throwable exp) {
            JSONObject error = requestHandler.handleThrowable(
                    exp instanceof RuntimeMBeanException ? ((RuntimeMBeanException) exp).getTargetException() : exp);
            json = error;
        } finally {
            sendResponse(pExchange,parsedUri, json.toJSONString());
        }
    }
View Full Code Here


            throws IOException {
        if (backendManager.isDebug()) {
            logHandler.debug("URI: " + pUri);
        }

        JSONAware jsonRequest = extractJsonRequest(pInputStream,pEncoding);
        if (jsonRequest instanceof List) {
            List<JmxRequest> jmxRequests = JmxRequestFactory.createPostRequests((List) jsonRequest,pParameterMap);

            JSONArray responseList = new JSONArray();
            for (JmxRequest jmxReq : jmxRequests) {
                if (backendManager.isDebug()) {
                    logHandler.debug("Request: " + jmxReq.toString());
                }
                // Call handler and retrieve return value
                JSONObject resp = executeRequest(jmxReq);
                responseList.add(resp);
            }
            return responseList;
        } else if (jsonRequest instanceof Map) {
            JmxRequest jmxReq = JmxRequestFactory.createPostRequest((Map<String, ?>) jsonRequest,pParameterMap);
            return executeRequest(jmxReq);
        } else {
            throw new IllegalArgumentException("Invalid JSON Request " + jsonRequest.toJSONString());
        }
    }
View Full Code Here

        handle(httpPostHandler,req,resp);
    }

    @SuppressWarnings({ "PMD.AvoidCatchingThrowable", "PMD.AvoidInstanceofChecksInCatchClause" })
    private void handle(ServletRequestHandler pReqHandler,HttpServletRequest pReq, HttpServletResponse pResp) throws IOException {
        JSONAware json = null;
        try {
            // Check access policy
            requestHandler.checkClientIPAccess(pReq.getRemoteHost(),pReq.getRemoteAddr());

            // Dispatch for the proper HTTP request method
            json = pReqHandler.handleRequest(pReq,pResp);
            if (backendManager.isDebug()) {
                backendManager.debug("Response: " + json);
            }
        } catch (Throwable exp) {
            JSONObject error = requestHandler.handleThrowable(
                    exp instanceof RuntimeMBeanException ? ((RuntimeMBeanException) exp).getTargetException() : exp);
            json = error;
        } finally {
            String callback = pReq.getParameter(ConfigKey.CALLBACK.getKeyValue());
            if (callback != null) {
                // Send a JSONP response
                sendResponse(pResp, "text/javascript",callback + "(" + json.toJSONString() ");");
            } else {
                sendResponse(pResp, "text/plain",json.toJSONString());
            }
        }
    }
View Full Code Here

    /** {@inheritDoc} */
    @Override
    Object convertToObject(CompositeType pType, Object pFrom) {
        // break down the composite type to its field and recurse for converting each field
        JSONAware value = toJSON(pFrom);
        if (!(value instanceof JSONObject)) {
            throw new IllegalArgumentException(
                    "Conversion of " + value + " to " +
                    pType + " failed because provided JSON type " + value.getClass() + " is not a JSONObject");
        }

        Map<String, Object> givenValues = (JSONObject) value;
        Map<String, Object> compositeValues = new HashMap<String, Object>();

View Full Code Here

    }

    /** {@inheritDoc} */
    @Override
    public Object convertToObject(ArrayType type, Object pFrom) {
        JSONAware value = toJSON(pFrom);
        // prepare each value in the array and then process the array of values
        if (!(value instanceof JSONArray)) {
            throw new IllegalArgumentException(
                    "Can not convert " + value + " to type " +
                    type + " because JSON object type " + value.getClass() + " is not a JSONArray");

        }

        JSONArray jsonArray = (JSONArray) value;
        OpenType elementOpenType = type.getElementOpenType();
View Full Code Here

        }
    }

    @SuppressWarnings({ "PMD.AvoidCatchingThrowable", "PMD.AvoidInstanceofChecksInCatchClause" })
    private void handle(ServletRequestHandler pReqHandler,HttpServletRequest pReq, HttpServletResponse pResp) throws IOException {
        JSONAware json = null;
        try {
            // Check access policy
            requestHandler.checkAccess(pReq.getRemoteHost(), pReq.getRemoteAddr(),
                                       getOriginOrReferer(pReq));

            // Remember the agent URL upon the first request. Needed for discovery
            updateAgentUrlIfNeeded(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 {
View Full Code Here

    public void doHandle(HttpExchange pExchange) throws IOException {
        if (requestHandler == null) {
            throw new IllegalStateException("Handler not yet started");
        }

        JSONAware json = null;
        URI uri = pExchange.getRequestURI();
        ParsedUri parsedUri = new ParsedUri(uri,context);
        try {
            // Check access policy
            InetSocketAddress address = pExchange.getRemoteAddress();
View Full Code Here

  public int dispatch(final RequestAndResponse req) {
    return runWithContextClassLoader(new PrivilegedAction<Integer>() {
      public Integer run() {
        int returnCode = 200;
        JSONAware json = null;
        try {
          switch (req.getMode()) {
          case RequestAndResponse.GET_REQUEST:
            json = handler.handleGetRequest(req.getUri(),
                req.getPath(), req.getParameters());
            break;
          case RequestAndResponse.POST_REQUEST:
            json = handler.handlePostRequest(req.getUri(),
                new ByteArrayInputStream(req.getBody()),
                req.getEncoding(), req.getParameters());

            break;
          }
        } catch (Throwable exp) {
          json = handler
              .handleThrowable(exp instanceof RuntimeMBeanException ? ((RuntimeMBeanException) exp)
                  .getTargetException() : exp);
        } finally {
          String callback = req.getParameter(ConfigKey.CALLBACK
              .getKeyValue());
          String answer = json != null ? json.toJSONString()
              : handler
                  .handleThrowable(
                      new Exception(
                          "Internal error while handling an exception"))
                  .toJSONString();
View Full Code Here

TOP

Related Classes of org.json.simple.JSONAware

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.