Example usage for org.json XML toJSONObject

List of usage examples for org.json XML toJSONObject

Introduction

In this page you can find the example usage for org.json XML toJSONObject.

Prototype

public static JSONObject toJSONObject(String string) throws JSONException 

Source Link

Document

Convert a well-formed (but not necessarily valid) XML string into a JSONObject.

Usage

From source file:com.portfolio.rest.RestServicePortfolio.java

@Path("/portfolios/portfolio/code/{code}")
@GET//from  w  w w  .j a v  a2  s  . c o m
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })

public String getPortfolioByCode(@CookieParam("user") String user, @CookieParam("credential") String token,
        @QueryParam("group") int groupId, @PathParam("code") String code, @Context ServletConfig sc,
        @Context HttpServletRequest httpServletRequest, @HeaderParam("Accept") String accept,
        @QueryParam("user") Integer userId, @QueryParam("group") Integer group,
        @QueryParam("resources") String resources) {
    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        String returnValue = dataProvider
                .getPortfolioByCode(new MimeType("text/xml"), code, ui.userId, groupId, resources, ui.subId)
                .toString();
        if (returnValue.equals("faux")) {

            throw new RestWebApplicationException(Status.FORBIDDEN, "Vous n'avez pas les droits necessaires");
        }
        if (accept.equals(MediaType.APPLICATION_JSON))
            returnValue = XML.toJSONObject(returnValue).toString();

        logRestRequest(httpServletRequest, null, returnValue, Status.OK.getStatusCode());

        return returnValue;
    } catch (RestWebApplicationException ex) {
        throw new RestWebApplicationException(Status.FORBIDDEN, ex.getResponse().getEntity().toString());
    } catch (SQLException ex) {
        logRestRequest(httpServletRequest, null, "Portfolio code = " + code + " not found",
                Status.NOT_FOUND.getStatusCode());

        throw new RestWebApplicationException(Status.NOT_FOUND, "Portfolio code = " + code + " not found");
    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, null, ex.getMessage() + "\n\n" + ex.getStackTrace(),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}

From source file:com.portfolio.rest.RestServicePortfolio.java

@Path("/portfolios")
@GET//  ww w .java2  s  . c o  m
@Consumes(MediaType.APPLICATION_XML)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public String getPortfolios(@CookieParam("user") String user, @CookieParam("credential") String token,
        @QueryParam("group") int groupId, @Context ServletConfig sc,
        @Context HttpServletRequest httpServletRequest, @HeaderParam("Accept") String accept,
        @QueryParam("active") String active, @QueryParam("user") Integer userId,
        @QueryParam("code") String code, @QueryParam("portfolio") String portfolioUuid) {
    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        if (portfolioUuid != null) {
            String returnValue = dataProvider.getPortfolio(new MimeType("text/xml"), portfolioUuid, ui.userId,
                    groupId, this.label, null, null, ui.subId).toString();
            if (accept.equals(MediaType.APPLICATION_JSON))
                returnValue = XML.toJSONObject(returnValue).toString();

            logRestRequest(httpServletRequest, null, returnValue, Status.OK.getStatusCode());

            return returnValue;

        } else {
            String portfolioCode = null;
            String returnValue = "";
            Boolean portfolioActive;
            try {
                if (active.equals("false") || active.equals("0"))
                    portfolioActive = false;
                else
                    portfolioActive = true;
            } catch (Exception ex) {
                portfolioActive = true;
            }
            ;

            try {
                portfolioCode = code;
            } catch (Exception ex) {
            }
            ;
            if (portfolioCode != null) {
                returnValue = dataProvider.getPortfolioByCode(new MimeType("text/xml"), portfolioCode,
                        ui.userId, groupId, null, ui.subId).toString();
            } else {
                if (userId != null && credential.isAdmin(ui.userId)) {
                    returnValue = dataProvider
                            .getPortfolios(new MimeType("text/xml"), userId, groupId, portfolioActive, ui.subId)
                            .toString();
                } else {
                    returnValue = dataProvider.getPortfolios(new MimeType("text/xml"), ui.userId, groupId,
                            portfolioActive, ui.subId).toString();
                }

                if (accept.equals(MediaType.APPLICATION_JSON))
                    returnValue = XML.toJSONObject(returnValue).toString();
            }
            logRestRequest(httpServletRequest, null, returnValue, Status.OK.getStatusCode());

            return returnValue;
        }
    } catch (RestWebApplicationException ex) {
        logRestRequest(httpServletRequest, null, null, Status.FORBIDDEN.getStatusCode());

        throw new RestWebApplicationException(Status.FORBIDDEN, ex.getResponse().getEntity().toString());
    } catch (SQLException ex) {
        logRestRequest(httpServletRequest, null, null, Status.NOT_FOUND.getStatusCode());

        throw new RestWebApplicationException(Status.NOT_FOUND, "Portfolios  not found");
    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, null, ex.getMessage() + "\n\n" + javaUtils.getCompleteStackTrace(ex),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}

From source file:com.portfolio.rest.RestServicePortfolio.java

@Path("/nodes/node/{node-id}")
@GET/*  w  w w  . j  a  va2 s.  com*/
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes(MediaType.APPLICATION_XML)
public String getNode(@CookieParam("user") String user, @CookieParam("credential") String token,
        @QueryParam("group") int groupId, @PathParam("node-id") String nodeUuid, @Context ServletConfig sc,
        @Context HttpServletRequest httpServletRequest, @HeaderParam("Accept") String accept,
        @QueryParam("user") Integer userId) {
    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        String returnValue = dataProvider
                .getNode(new MimeType("text/xml"), nodeUuid, false, ui.userId, groupId, this.label).toString();
        if (returnValue.length() != 0) {
            if (accept.equals(MediaType.APPLICATION_JSON))
                returnValue = XML.toJSONObject(returnValue).toString();
            logRestRequest(httpServletRequest, null, returnValue, Status.OK.getStatusCode());
        } else {
            throw new RestWebApplicationException(Status.FORBIDDEN, "Vous n'avez pas les droits necessaires");
        }

        return returnValue;
    } catch (RestWebApplicationException ex) {
        throw new RestWebApplicationException(Status.FORBIDDEN, ex.getResponse().getEntity().toString());
    } catch (SQLException ex) {
        logRestRequest(httpServletRequest, null, null, Status.NOT_FOUND.getStatusCode());

        throw new RestWebApplicationException(Status.NOT_FOUND, "Node " + nodeUuid + " not found");
    } catch (NullPointerException ex) {
        logRestRequest(httpServletRequest, null, null, Status.NOT_FOUND.getStatusCode());

        throw new RestWebApplicationException(Status.NOT_FOUND, "Node " + nodeUuid + " not found");
    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, null, ex.getMessage() + "\n\n" + javaUtils.getCompleteStackTrace(ex),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}

From source file:com.portfolio.rest.RestServicePortfolio.java

@Path("/nodes/node/{node-id}/children")
@GET//from   ww  w. j  ava  2 s.  c o  m
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes(MediaType.APPLICATION_XML)
public String getNodeWithChildren(@CookieParam("user") String user, @CookieParam("credential") String token,
        @QueryParam("group") int groupId, @PathParam("node-id") String nodeUuid, @Context ServletConfig sc,
        @Context HttpServletRequest httpServletRequest, @HeaderParam("Accept") String accept,
        @QueryParam("user") Integer userId) {
    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        String returnValue = dataProvider
                .getNode(new MimeType("text/xml"), nodeUuid, true, ui.userId, groupId, this.label).toString();
        if (returnValue.length() != 0) {
            if (accept.equals(MediaType.APPLICATION_JSON))
                returnValue = XML.toJSONObject(returnValue).toString();
            logRestRequest(httpServletRequest, null, returnValue, Status.OK.getStatusCode());

            return returnValue;
        } else {
            throw new RestWebApplicationException(Status.FORBIDDEN, "Vous n'avez pas les droits necessaires");
        }
    } catch (RestWebApplicationException ex) {
        throw new RestWebApplicationException(Status.FORBIDDEN, ex.getResponse().getEntity().toString());
    } catch (SQLException ex) {
        logRestRequest(httpServletRequest, null, null, Status.NOT_FOUND.getStatusCode());

        throw new RestWebApplicationException(Status.NOT_FOUND, "Node " + nodeUuid + " not found");
    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, null, ex.getMessage() + "\n\n" + javaUtils.getCompleteStackTrace(ex),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}

From source file:com.portfolio.rest.RestServicePortfolio.java

@Path("/nodes/node/{nodeid}/metadatawad")
@GET//from  ww  w . j  a  v  a 2  s .c o  m
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes(MediaType.APPLICATION_XML)
public String getNodeMetadataWad(@CookieParam("user") String user, @CookieParam("credential") String token,
        @QueryParam("group") int groupId, @PathParam("nodeid") String nodeUuid, @Context ServletConfig sc,
        @Context HttpServletRequest httpServletRequest, @HeaderParam("Accept") String accept,
        @QueryParam("user") Integer userId) {
    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        String returnValue = dataProvider
                .getNodeMetadataWad(new MimeType("text/xml"), nodeUuid, true, ui.userId, groupId, this.label)
                .toString();
        if (returnValue.length() != 0) {
            if (accept.equals(MediaType.APPLICATION_JSON))
                returnValue = XML.toJSONObject(returnValue).toString();
            logRestRequest(httpServletRequest, null, returnValue, Status.OK.getStatusCode());

            return returnValue;
        } else {
            throw new RestWebApplicationException(Status.FORBIDDEN, "Vous n'avez pas les droits necessaires");
        }
    } catch (RestWebApplicationException ex) {
        throw new RestWebApplicationException(Status.FORBIDDEN, ex.getResponse().getEntity().toString());
    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, null, ex.getMessage() + "\n\n" + javaUtils.getCompleteStackTrace(ex),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}

From source file:com.portfolio.rest.RestServicePortfolio.java

@Path("/nodes/node/{node-id}/rights")
@GET/* w  ww . j  a  v  a2s.  c  om*/
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes(MediaType.APPLICATION_XML)
public String getNodeRights(@CookieParam("user") String user, @CookieParam("credential") String token,
        @QueryParam("group") int groupId, @PathParam("node-id") String nodeUuid, @Context ServletConfig sc,
        @Context HttpServletRequest httpServletRequest, @HeaderParam("Accept") String accept,
        @QueryParam("user") Integer userId) {
    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        String returnValue = dataProvider.getNodeRights(nodeUuid, ui.userId, groupId);
        if (returnValue.length() != 0) {
            if (accept.equals(MediaType.APPLICATION_JSON))
                returnValue = XML.toJSONObject(returnValue).toString();
            logRestRequest(httpServletRequest, null, returnValue, Status.OK.getStatusCode());
        } else {
            throw new RestWebApplicationException(Status.FORBIDDEN, "Vous n'avez pas les droits necessaires");
        }

        return returnValue;
    } catch (RestWebApplicationException ex) {
        throw new RestWebApplicationException(Status.FORBIDDEN, ex.getResponse().getEntity().toString());
    } catch (SQLException ex) {
        logRestRequest(httpServletRequest, null, null, Status.NOT_FOUND.getStatusCode());

        throw new RestWebApplicationException(Status.NOT_FOUND, "Node " + nodeUuid + " not found");
    } catch (NullPointerException ex) {
        logRestRequest(httpServletRequest, null, null, Status.NOT_FOUND.getStatusCode());

        throw new RestWebApplicationException(Status.NOT_FOUND, "Node " + nodeUuid + " not found");
    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, null, ex.getMessage() + "\n\n" + javaUtils.getCompleteStackTrace(ex),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}

From source file:com.portfolio.rest.RestServicePortfolio.java

/*****************************/

@Path("/resources/resource/{node-parent-id}")
@GET/* w  w w.ja v a2 s. c om*/
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes(MediaType.APPLICATION_XML)
public String getResource(@CookieParam("user") String user, @CookieParam("credential") String token,
        @QueryParam("group") int groupId, @PathParam("node-parent-id") String nodeParentUuid,
        @Context ServletConfig sc, @Context HttpServletRequest httpServletRequest,
        @HeaderParam("Accept") String accept, @QueryParam("user") Integer userId) {
    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        String returnValue = dataProvider
                .getResource(new MimeType("text/xml"), nodeParentUuid, ui.userId, groupId).toString();
        if (returnValue.equals("faux")) {

            throw new RestWebApplicationException(Status.FORBIDDEN, "Vous n'avez pas les droits necessaires");
        }
        if (accept.equals(MediaType.APPLICATION_JSON))
            returnValue = XML.toJSONObject(returnValue).toString();
        logRestRequest(httpServletRequest, null, returnValue, Status.OK.getStatusCode());

        return returnValue;
    } catch (RestWebApplicationException ex) {
        throw new RestWebApplicationException(Status.FORBIDDEN, ex.getResponse().getEntity().toString());
    } catch (SQLException ex) {
        logRestRequest(httpServletRequest, null, null, Status.NOT_FOUND.getStatusCode());

        throw new RestWebApplicationException(Status.NOT_FOUND, "Resource " + nodeParentUuid + " not found");
    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, null, ex.getMessage() + "\n\n" + javaUtils.getCompleteStackTrace(ex),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}

From source file:com.portfolio.rest.RestServicePortfolio.java

@Path("/resources/portfolios/{portfolio-id}")
@GET//from ww  w  .  j a va2 s.com
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public String getResources(@CookieParam("user") String user, @CookieParam("credential") String token,
        @QueryParam("group") int groupId, @PathParam("portfolio-id") String portfolioUuid,
        @Context ServletConfig sc, @Context HttpServletRequest httpServletRequest,
        @HeaderParam("Accept") String accept, @QueryParam("user") Integer userId) {
    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        String returnValue = dataProvider
                .getResources(new MimeType("text/xml"), portfolioUuid, ui.userId, groupId).toString();
        if (accept.equals(MediaType.APPLICATION_JSON))
            returnValue = XML.toJSONObject(returnValue).toString();
        logRestRequest(httpServletRequest, null, returnValue, Status.OK.getStatusCode());

        return returnValue;
    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, null, ex.getMessage() + "\n\n" + javaUtils.getCompleteStackTrace(ex),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}

From source file:com.portfolio.rest.RestServicePortfolio.java

@Path("/nodes/{node-id}/xsl/{xsl-file}")
@GET//  www .j av  a2  s .  c  o m
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_XML)
public String getNodeWithXSL(@CookieParam("user") String user, @CookieParam("credential") String token,
        @QueryParam("group") int groupId, @PathParam("node-id") String nodeUuid,
        @PathParam("xsl-file") String xslFile, @Context ServletConfig sc,
        @Context HttpServletRequest httpServletRequest, @HeaderParam("Accept") String accept,
        @QueryParam("user") Integer userId, @QueryParam("p1") String p1, @QueryParam("p2") String p2,
        @QueryParam("p3") String p3) {
    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        javax.servlet.http.HttpSession session = httpServletRequest.getSession(true);
        String ppath = session.getServletContext().getRealPath(File.separator);
        // TODO xslFile avec _ => repertoire_fichier
        xslFile = xslFile.replace(".", "");
        xslFile = xslFile.replace("/", "");
        String[] tmp = xslFile.split("-");

        xslFile = ppath.substring(0, ppath.lastIndexOf(File.separator)) + File.separator + "xsl"
                + File.separator + tmp[0] + File.separator + tmp[1] + ".xsl";
        String returnValue = dataProvider
                .getNodeWithXSL(new MimeType("text/xml"), nodeUuid, xslFile, ui.userId, groupId).toString();
        if (returnValue.length() != 0) {
            if (accept.equals(MediaType.APPLICATION_JSON))
                returnValue = XML.toJSONObject(returnValue).toString();
            logRestRequest(httpServletRequest, null, returnValue, Status.OK.getStatusCode());
        } else {
            throw new RestWebApplicationException(Status.FORBIDDEN, "Vous n'avez pas les droits necessaires");
        }

        return returnValue;
    } catch (RestWebApplicationException ex) {
        throw new RestWebApplicationException(Status.FORBIDDEN, "Vous n'avez pas les droits necessaires");
    }

    catch (NullPointerException ex) {
        logRestRequest(httpServletRequest, null, null, Status.NOT_FOUND.getStatusCode());

        throw new RestWebApplicationException(Status.NOT_FOUND,
                "Node " + nodeUuid + " not found or xsl not found :" + ex.getMessage());
    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, null, ex.getMessage() + "\n\n" + javaUtils.getCompleteStackTrace(ex),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}

From source file:fi.csc.emrex.ncp.JsonController.java

@RequestMapping(value = "/api/fullelmo", method = RequestMethod.GET)
@ResponseBody/*from   w w w  .  ja  v  a 2  s  . c  o  m*/
public String getFullElmoJSON() {
    log.info("getting FullELmo");
    try {

        ElmoParser parser = (ElmoParser) context.getSession().getAttribute("elmo");
        String xmlString;

        xmlString = parser.getCourseData();
        log.debug(xmlString);
        JSONObject json = XML.toJSONObject(xmlString);
        return json.toString();
    } catch (Exception e) {
        log.error(e.getMessage());
        StackTraceElement elements[] = e.getStackTrace();
        Map<String, Object> error = new HashMap<String, Object>();
        Map<String, Object> log = new HashMap<String, Object>();
        error.put("message", e.getMessage());
        for (int i = 0, n = elements.length; i < n; i++) {
            log.put(elements[i].getFileName() + " " + elements[i].getLineNumber(), elements[i].getMethodName());
        }
        error.put("stack", log);
        return new JSONObject(error).toString();
    }
}