Search in sources :

Example 16 with JsonException

use of com.ibm.commons.util.io.json.JsonException in project org.openntf.domino by OpenNTF.

the class ViewEntry method toJson.

public String toJson(boolean compact) {
    StringWriter sw = new StringWriter();
    JsonWriter jw = new JsonWriter(sw, compact);
    // $NON-NLS-1$
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    try {
        jw.startObject();
        // $NON-NLS-1$
        jw.outStringProperty("@unid", getUniversalID());
        // $NON-NLS-1$
        jw.outStringProperty("@noteid", getNoteID());
        // $NON-NLS-1$
        jw.outStringProperty("@replicaid", getAncestorDatabase().getReplicaID());
        // $NON-NLS-1$
        jw.outStringProperty("@metaversalid", getMetaversalID());
        boolean resetPreferDates = false;
        if (!isPreferJavaDates()) {
            setPreferJavaDates(true);
            resetPreferDates = true;
        }
        Map<String, Object> colVals = getColumnValuesMap();
        Set<String> keys = colVals.keySet();
        for (String key : keys) {
            Object colVal = colVals.get(key);
            if (colVal instanceof java.util.Date) {
                jw.outProperty(key, sdf.format((java.util.Date) colVal));
            } else {
                jw.outProperty(key, colVal.toString());
            }
        }
        if (resetPreferDates) {
            setPreferJavaDates(false);
        }
        jw.endObject();
        jw.flush();
    } catch (IOException e) {
        DominoUtils.handleException(e, this.getDocument());
        return null;
    } catch (JsonException e) {
        DominoUtils.handleException(e, this.getDocument());
        return null;
    }
    return sw.toString();
}
Also used : JsonException(com.ibm.commons.util.io.json.JsonException) StringWriter(java.io.StringWriter) IOException(java.io.IOException) JsonWriter(com.ibm.commons.util.io.json.util.JsonWriter) SimpleDateFormat(java.text.SimpleDateFormat)

Example 17 with JsonException

use of com.ibm.commons.util.io.json.JsonException in project org.openntf.nsfodp by OpenNTF.

the class StockComponentsServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setBufferSize(0);
    // $NON-NLS-1$ //$NON-NLS-2$
    resp.setHeader("Content-Type", "text/json");
    ServletOutputStream os = resp.getOutputStream();
    try {
        if (componentInfo == null) {
            SharableRegistryImpl facesRegistry = new SharableRegistryImpl(getClass().getPackage().getName());
            // $NON-NLS-1$
            List<Object> libraries = ExtensionManager.findServices((List<Object>) null, LibraryServiceLoader.class, "com.ibm.xsp.Library");
            libraries.stream().filter(lib -> lib instanceof XspLibrary).map(XspLibrary.class::cast).map(lib -> new LibraryWrapper(lib.getLibraryId(), lib)).map(wrapper -> {
                SimpleRegistryProvider provider = new SimpleRegistryProvider();
                provider.init(wrapper);
                return provider;
            }).map(XspRegistryProvider::getRegistry).forEach(facesRegistry::addDepend);
            facesRegistry.refreshReferences();
            componentInfo = new JsonJavaObject();
            JsonArray defs = new JsonJavaArray();
            facesRegistry.findComponentDefs().stream().filter(FacesComponentDefinition::isTag).map(def -> {
                try {
                    JsonObject defObj = new JsonJavaObject();
                    // $NON-NLS-1$
                    defObj.putJsonProperty("namespaceUri", def.getNamespaceUri());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("tagName", def.getTagName());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("class", def.getJavaClass().getName());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("since", def.getSince());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("defaultPrefix", def.getFirstDefaultPrefix());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("componentFamily", def.getComponentFamily());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("componentType", def.getComponentType());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("id", def.getId());
                    JsonArray facetNames = new JsonJavaArray();
                    for (String facetName : def.getFacetNames()) {
                        facetNames.add(facetName);
                    }
                    // $NON-NLS-1$
                    defObj.putJsonProperty("facetNames", facetNames);
                    FacesProperty defaultProp = def.getDefaultFacesProperty();
                    if (defaultProp != null) {
                        JsonObject defaultPropObj = new JsonJavaObject();
                        // $NON-NLS-1$
                        defaultPropObj.putJsonProperty("name", defaultProp.getName());
                        // $NON-NLS-1$
                        defaultPropObj.putJsonProperty("since", defaultProp.getSince());
                        // $NON-NLS-1$
                        defaultPropObj.putJsonProperty("class", defaultProp.getJavaClass().getName());
                        // $NON-NLS-1$
                        defaultPropObj.putJsonProperty("required", defaultProp.isRequired());
                        // $NON-NLS-1$
                        defaultPropObj.putJsonProperty("attribute", defaultProp.isAttribute());
                    }
                    JsonArray properties = new JsonJavaArray();
                    for (String propName : def.getPropertyNames()) {
                        FacesProperty prop = def.getProperty(propName);
                        JsonObject propObj = new JsonJavaObject();
                        // $NON-NLS-1$
                        propObj.putJsonProperty("name", propName);
                        // $NON-NLS-1$
                        propObj.putJsonProperty("class", prop.getJavaClass().getName());
                        // $NON-NLS-1$
                        propObj.putJsonProperty("since", prop.getSince());
                        // $NON-NLS-1$
                        propObj.putJsonProperty("required", prop.isRequired());
                        // $NON-NLS-1$
                        propObj.putJsonProperty("attribute", prop.isAttribute());
                        properties.add(propObj);
                    }
                    // $NON-NLS-1$
                    defObj.putJsonProperty("properties", properties);
                    return defObj;
                } catch (JsonException e) {
                    throw new RuntimeException(e);
                }
            }).forEach(defObj -> {
                try {
                    defs.add(defObj);
                } catch (JsonException e) {
                    throw new RuntimeException(e);
                }
            });
            // $NON-NLS-1$
            componentInfo.putJsonProperty("components", defs);
        }
        os.print(JsonGenerator.toJson(JsonJavaFactory.instance, componentInfo, false));
    } catch (Throwable e) {
        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter out = new PrintWriter(baos);
        e.printStackTrace(out);
        out.flush();
        os.println(baos.toString());
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletException(javax.servlet.ServletException) ExtensionManager(com.ibm.commons.extension.ExtensionManager) SimpleRegistryProvider(com.ibm.xsp.registry.config.SimpleRegistryProvider) JsonGenerator(com.ibm.commons.util.io.json.JsonGenerator) XspLibrary(com.ibm.xsp.library.XspLibrary) HttpServletRequest(javax.servlet.http.HttpServletRequest) JsonJavaArray(com.ibm.commons.util.io.json.JsonJavaArray) FacesComponentDefinition(com.ibm.xsp.registry.FacesComponentDefinition) JsonJavaFactory(com.ibm.commons.util.io.json.JsonJavaFactory) ServletOutputStream(javax.servlet.ServletOutputStream) JsonArray(com.ibm.commons.util.io.json.JsonArray) LibraryWrapper(com.ibm.xsp.library.LibraryWrapper) PrintWriter(java.io.PrintWriter) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) SharableRegistryImpl(com.ibm.xsp.registry.SharableRegistryImpl) HttpServlet(javax.servlet.http.HttpServlet) JsonObject(com.ibm.commons.util.io.json.JsonObject) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) FacesProperty(com.ibm.xsp.registry.FacesProperty) WebServlet(javax.servlet.annotation.WebServlet) LibraryServiceLoader(com.ibm.xsp.library.LibraryServiceLoader) List(java.util.List) JsonException(com.ibm.commons.util.io.json.JsonException) XspRegistryProvider(com.ibm.xsp.registry.config.XspRegistryProvider) JsonException(com.ibm.commons.util.io.json.JsonException) JsonJavaArray(com.ibm.commons.util.io.json.JsonJavaArray) LibraryWrapper(com.ibm.xsp.library.LibraryWrapper) ServletOutputStream(javax.servlet.ServletOutputStream) XspLibrary(com.ibm.xsp.library.XspLibrary) JsonObject(com.ibm.commons.util.io.json.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SharableRegistryImpl(com.ibm.xsp.registry.SharableRegistryImpl) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) JsonArray(com.ibm.commons.util.io.json.JsonArray) FacesComponentDefinition(com.ibm.xsp.registry.FacesComponentDefinition) SimpleRegistryProvider(com.ibm.xsp.registry.config.SimpleRegistryProvider) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) JsonObject(com.ibm.commons.util.io.json.JsonObject) FacesProperty(com.ibm.xsp.registry.FacesProperty) PrintWriter(java.io.PrintWriter)

Aggregations

JsonException (com.ibm.commons.util.io.json.JsonException)17 IOException (java.io.IOException)15 StringWriter (java.io.StringWriter)11 WebApplicationException (javax.ws.rs.WebApplicationException)11 UserAccessException (org.openntf.domino.exceptions.UserAccessException)11 JsonJavaObject (com.ibm.commons.util.io.json.JsonJavaObject)10 Date (java.util.Date)10 List (java.util.List)10 Response (javax.ws.rs.core.Response)10 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)10 JsonGraphWriter (org.openntf.domino.rest.json.JsonGraphWriter)9 ArrayList (java.util.ArrayList)8 CaseInsensitiveString (org.openntf.domino.types.CaseInsensitiveString)8 Produces (javax.ws.rs.Produces)7 DFramedTransactionalGraph (org.openntf.domino.graph2.impl.DFramedTransactionalGraph)7 ParamMap (org.openntf.domino.rest.service.Parameters.ParamMap)7 LinkedHashMap (java.util.LinkedHashMap)6 KeyNotFoundException (org.openntf.domino.graph2.impl.DEdgeEntryList.KeyNotFoundException)6 GET (javax.ws.rs.GET)5 JsonGraphFactory (org.openntf.domino.rest.json.JsonGraphFactory)5