Search in sources :

Example 6 with JsonException

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

the class FramedCollectionResource method createFramedObject.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createFramedObject(final String requestEntity, @Context final UriInfo uriInfo, @PathParam(Routes.NAMESPACE) final String namespace, @Context final Request request) throws JsonException, IOException {
    // org.apache.wink.common.internal.registry.metadata.ResourceMetadataCollector
    // rc;
    DFramedTransactionalGraph graph = this.getGraph(namespace);
    ParamMap pm = Parameters.toParamMap(uriInfo);
    StringWriter sw = new StringWriter();
    JsonGraphWriter writer = new JsonGraphWriter(sw, graph, pm, false, true, true);
    // System.out.println("TEMP DEBUG Starting new POST...");
    JsonJavaObject jsonItems = null;
    List<Object> jsonArray = null;
    JsonGraphFactory factory = JsonGraphFactory.instance;
    try {
        StringReader reader = new StringReader(requestEntity);
        try {
            Object jsonRaw = JsonParser.fromJson(factory, reader);
            if (jsonRaw instanceof JsonJavaObject) {
                jsonItems = (JsonJavaObject) jsonRaw;
            } else if (jsonRaw instanceof List) {
                jsonArray = (List<Object>) jsonRaw;
            // System.out.println("TEMP DEBUG processing a POST with an
            // array of size " + jsonArray.size());
            } else {
            // System.out.println("TEMP DEBUG Got an unexpected object
            // from parser "
            // + (jsonRaw == null ? "null" :
            // jsonRaw.getClass().getName()));
            }
        } catch (Exception e) {
            throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
        } finally {
            reader.close();
        }
    } catch (Exception ex) {
        throw new WebApplicationException(ErrorHelper.createErrorResponse(ex, Response.Status.INTERNAL_SERVER_ERROR));
    }
    boolean committed = true;
    Map<Object, Object> results = new LinkedHashMap<Object, Object>();
    if (jsonArray != null) {
        writer.startArray();
        for (Object raw : jsonArray) {
            if (raw instanceof JsonJavaObject) {
                writer.startArrayItem();
                committed = processJsonObject((JsonJavaObject) raw, graph, writer, results);
                writer.endArrayItem();
            } else {
                System.err.println("Raw array member isn't a JsonJavaObject. It's a " + (raw == null ? "null" : raw.getClass().getName()));
            }
        }
        writer.endArray();
    } else if (jsonItems != null) {
        committed = processJsonObject(jsonItems, graph, writer, results);
    } else {
    // System.out.println("TEMP DEBUG Nothing to POST. No JSON items
    // found.");
    }
    String jsonEntity = sw.toString();
    ResponseBuilder berg = getBuilder(jsonEntity, new Date(), false, request);
    Response response = berg.build();
    if (!committed) {
        graph.rollback();
    }
    return response;
}
Also used : ParamMap(org.openntf.domino.rest.service.Parameters.ParamMap) WebApplicationException(javax.ws.rs.WebApplicationException) JsonGraphWriter(org.openntf.domino.rest.json.JsonGraphWriter) DFramedTransactionalGraph(org.openntf.domino.graph2.impl.DFramedTransactionalGraph) CaseInsensitiveString(org.openntf.domino.types.CaseInsensitiveString) JsonException(com.ibm.commons.util.io.json.JsonException) WebApplicationException(javax.ws.rs.WebApplicationException) UserAccessException(org.openntf.domino.exceptions.UserAccessException) IOException(java.io.IOException) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) Response(javax.ws.rs.core.Response) StringWriter(java.io.StringWriter) JsonGraphFactory(org.openntf.domino.rest.json.JsonGraphFactory) StringReader(java.io.StringReader) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) FramedEdgeList(org.openntf.domino.graph2.annotations.FramedEdgeList) List(java.util.List) ArrayList(java.util.ArrayList) FramedVertexList(org.openntf.domino.graph2.annotations.FramedVertexList) MixedFramedVertexList(org.openntf.domino.graph2.annotations.MixedFramedVertexList) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 7 with JsonException

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

the class FramedResource method deleteFramedObject.

@SuppressWarnings("resource")
@DELETE
@Produces(MediaType.APPLICATION_JSON)
public Response deleteFramedObject(final String requestEntity, @Context final UriInfo uriInfo, @PathParam(Routes.NAMESPACE) final String namespace, @Context final Request request) throws JsonException, IOException {
    DFramedTransactionalGraph graph = this.getGraph(namespace);
    String jsonEntity = null;
    ParamMap pm = Parameters.toParamMap(uriInfo);
    StringWriter sw = new StringWriter();
    JsonGraphWriter writer = new JsonGraphWriter(sw, graph, pm, false, true, false);
    @SuppressWarnings("unused") JsonGraphFactory factory = JsonGraphFactory.instance;
    Map<String, String> report = new HashMap<String, String>();
    List<String> ids = pm.get(Parameters.ID);
    if (ids.size() == 0) {
        throw new WebApplicationException(ErrorHelper.createErrorResponse("No id specified for DELETE", Response.Status.INTERNAL_SERVER_ERROR));
    } else {
        for (String id : ids) {
            try {
                NoteCoordinate nc = NoteCoordinate.Utils.getNoteCoordinate(id.trim());
                Object element = graph.getElement(nc, null);
                if (element instanceof Element) {
                    ((Element) element).remove();
                } else if (element instanceof VertexFrame) {
                    graph.removeVertexFrame((VertexFrame) element);
                } else if (element instanceof EdgeFrame) {
                    graph.removeEdgeFrame((EdgeFrame) element);
                } else {
                    if (element != null) {
                        throw new WebApplicationException(ErrorHelper.createErrorResponse("Graph returned unexpected object type " + element.getClass().getName(), Response.Status.INTERNAL_SERVER_ERROR));
                    }
                }
                report.put(id, "deleted");
            } catch (Exception e) {
                throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
            }
        }
        graph.commit();
    }
    try {
        writer.outObject(report);
    } catch (JsonException e) {
        throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } catch (IOException e) {
        throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    }
    jsonEntity = sw.toString();
    ResponseBuilder bob = getBuilder(jsonEntity, new Date(), false, null);
    Response response = bob.build();
    return response;
}
Also used : NoteCoordinate(org.openntf.domino.big.NoteCoordinate) JsonException(com.ibm.commons.util.io.json.JsonException) DEdgeFrame(org.openntf.domino.graph2.builtin.DEdgeFrame) EdgeFrame(com.tinkerpop.frames.EdgeFrame) ParamMap(org.openntf.domino.rest.service.Parameters.ParamMap) WebApplicationException(javax.ws.rs.WebApplicationException) JsonGraphWriter(org.openntf.domino.rest.json.JsonGraphWriter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Element(com.tinkerpop.blueprints.Element) DFramedTransactionalGraph(org.openntf.domino.graph2.impl.DFramedTransactionalGraph) CaseInsensitiveString(org.openntf.domino.types.CaseInsensitiveString) IOException(java.io.IOException) KeyNotFoundException(org.openntf.domino.graph2.impl.DEdgeEntryList.KeyNotFoundException) JsonException(com.ibm.commons.util.io.json.JsonException) WebApplicationException(javax.ws.rs.WebApplicationException) UserAccessException(org.openntf.domino.exceptions.UserAccessException) IOException(java.io.IOException) Date(java.util.Date) Response(javax.ws.rs.core.Response) DVertexFrame(org.openntf.domino.graph2.builtin.DVertexFrame) VertexFrame(com.tinkerpop.frames.VertexFrame) StringWriter(java.io.StringWriter) JsonGraphFactory(org.openntf.domino.rest.json.JsonGraphFactory) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces)

Example 8 with JsonException

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

the class FramedResource method updateFrameByMetaid.

protected Response updateFrameByMetaid(final String requestEntity, final String namespace, final String ifUnmodifiedSince, final ParamMap pm, final boolean isPut, final Request request) throws JsonException, IOException {
    @SuppressWarnings("unused") Response result = null;
    DFramedTransactionalGraph<?> graph = this.getGraph(namespace);
    JsonJavaObject jsonItems = null;
    List<Object> jsonArray = null;
    // ResponseBuilder builder = null;
    JsonGraphFactory factory = JsonGraphFactory.instance;
    StringWriter sw = new StringWriter();
    JsonGraphWriter writer = new JsonGraphWriter(sw, graph, pm, false, true, false);
    try {
        StringReader reader = new StringReader(requestEntity);
        try {
            Object jsonRaw = JsonParser.fromJson(factory, reader);
            if (jsonRaw instanceof JsonJavaObject) {
                jsonItems = (JsonJavaObject) jsonRaw;
            } else if (jsonRaw instanceof List) {
                jsonArray = (List) jsonRaw;
            } else if (jsonRaw instanceof String) {
                // NTF reparse
                reader = new StringReader((String) jsonRaw);
                Object jsonRaw2 = JsonParser.fromJson(factory, reader);
                if (jsonRaw2 instanceof JsonJavaObject) {
                    jsonItems = (JsonJavaObject) jsonRaw2;
                } else if (jsonRaw2 instanceof List) {
                    jsonArray = (List) jsonRaw2;
                } else {
                    System.out.println("ALERT Got a jsonRaw2 of type " + (jsonRaw2 != null ? jsonRaw2.getClass().getName() : "null") + ".  Value is: " + String.valueOf(jsonRaw));
                }
            }
        } catch (UserAccessException uae) {
            throw new WebApplicationException(ErrorHelper.createErrorResponse("User " + Factory.getSession(SessionType.CURRENT).getEffectiveUserName() + " is not authorized to update this resource", Response.Status.UNAUTHORIZED));
        } catch (Exception e) {
            throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
        } finally {
            reader.close();
        }
    } catch (Exception ex) {
        throw new WebApplicationException(ErrorHelper.createErrorResponse(ex, Response.Status.INTERNAL_SERVER_ERROR));
    }
    if (jsonArray != null) {
        writer.startArray();
        for (Object raw : jsonArray) {
            if (raw instanceof JsonJavaObject) {
                writer.startArrayItem();
                processJsonUpdate((JsonJavaObject) raw, graph, writer, pm, isPut);
                writer.endArrayItem();
            }
        }
        writer.endArray();
    } else if (jsonItems != null) {
        processJsonUpdate(jsonItems, graph, writer, pm, isPut);
    } else {
        System.out.println("ALERT! Received a JSON object that was not expected!");
    }
    String jsonEntity = sw.toString();
    ResponseBuilder berg = getBuilder(jsonEntity, new Date(), false, request);
    Response response = berg.build();
    return response;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) JsonGraphWriter(org.openntf.domino.rest.json.JsonGraphWriter) CaseInsensitiveString(org.openntf.domino.types.CaseInsensitiveString) UserAccessException(org.openntf.domino.exceptions.UserAccessException) KeyNotFoundException(org.openntf.domino.graph2.impl.DEdgeEntryList.KeyNotFoundException) JsonException(com.ibm.commons.util.io.json.JsonException) WebApplicationException(javax.ws.rs.WebApplicationException) UserAccessException(org.openntf.domino.exceptions.UserAccessException) IOException(java.io.IOException) Date(java.util.Date) Response(javax.ws.rs.core.Response) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) JsonGraphFactory(org.openntf.domino.rest.json.JsonGraphFactory) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) List(java.util.List) ArrayList(java.util.ArrayList) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 9 with JsonException

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

the class FramedResource method processJsonObject.

@SuppressWarnings({ "resource", "unlikely-arg-type" })
private void processJsonObject(final JsonJavaObject jsonItems, final DFramedTransactionalGraph graph, final JsonGraphWriter writer, final ParamMap pm) /* , Map<Object, Object> resultMap */
{
    Map<CaseInsensitiveString, Object> cisMap = new HashMap<CaseInsensitiveString, Object>();
    for (String jsonKey : jsonItems.keySet()) {
        CaseInsensitiveString cis = new CaseInsensitiveString(jsonKey);
        cisMap.put(cis, jsonItems.get(jsonKey));
    }
    List<String> ids = pm.get(Parameters.ID);
    boolean commit = true;
    if (ids == null) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("error", "Cannot POST to frame without an id parameter");
        try {
            writer.outObject(map);
        } catch (JsonException e) {
            throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
        } catch (IOException e) {
            throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
        }
    } else {
        if (ids.size() == 0) {
            System.out.println("Cannot POST to frame without an id parameter");
            Map<String, String> map = new HashMap<String, String>();
            map.put("error", "Cannot POST to frame without an id parameter");
            try {
                writer.outObject(map);
            } catch (JsonException e) {
                throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
            } catch (IOException e) {
                throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
            }
        } else {
            for (String id : ids) {
                // System.out.println("TEMP DEBUG POSTing to " + id);
                Class<?> type = null;
                if (pm.getTypes() != null) {
                    List<CharSequence> types = pm.getTypes();
                    String typename = types.get(0).toString();
                    type = graph.getTypeRegistry().findClassByName(typename);
                }
                NoteCoordinate nc = NoteCoordinate.Utils.getNoteCoordinate(id.trim());
                Object element = graph.getElement(nc, type);
                if (element instanceof VertexFrame) {
                    VertexFrame parVertex = (VertexFrame) element;
                    Map<CaseInsensitiveString, Method> adders = graph.getTypeRegistry().getAdders(parVertex.getClass());
                    CaseInsensitiveString rawLabel = new CaseInsensitiveString(jsonItems.getAsString("@label"));
                    Method method = adders.get(rawLabel);
                    if (method == null) {
                        method = adders.get(rawLabel + "In");
                    }
                    if (method != null) {
                        String rawId = jsonItems.getAsString("@id");
                        NoteCoordinate othernc = NoteCoordinate.Utils.getNoteCoordinate(rawId);
                        Object otherElement = graph.getElement(othernc, null);
                        if (otherElement instanceof VertexFrame) {
                            VertexFrame otherVertex = (VertexFrame) otherElement;
                            try {
                                Object result = method.invoke(parVertex, otherVertex);
                                if (result == null) {
                                    System.out.println("Invokation of method " + method.getName() + " on a vertex of type " + DGraphUtils.findInterface(parVertex) + " with an argument of type " + DGraphUtils.findInterface(otherVertex) + " resulted in null when we expected an Edge");
                                }
                                JsonFrameAdapter adapter = new JsonFrameAdapter(graph, (EdgeFrame) result, null, false);
                                Iterator<String> frameProperties = adapter.getJsonProperties();
                                CaseInsensitiveString actionName = null;
                                CaseInsensitiveString preactionName = null;
                                for (CaseInsensitiveString cis : cisMap.keySet()) {
                                    if (cis.equals("%preaction")) {
                                        preactionName = new CaseInsensitiveString(String.valueOf(cisMap.get(cis)));
                                    }
                                }
                                if (preactionName != null) {
                                    commit = adapter.runAction(preactionName);
                                }
                                if (commit) {
                                    while (frameProperties.hasNext()) {
                                        CaseInsensitiveString key = new CaseInsensitiveString(frameProperties.next());
                                        if (!key.startsWith("@")) {
                                            Object value = cisMap.get(key);
                                            if (value != null) {
                                                adapter.putJsonProperty(key.toString(), value);
                                                cisMap.remove(key);
                                            }
                                        }
                                    }
                                    for (CaseInsensitiveString cis : cisMap.keySet()) {
                                        if (cis.equals("%action")) {
                                            actionName = new CaseInsensitiveString(String.valueOf(cisMap.get(cis)));
                                        } else if (!cis.startsWith("@")) {
                                            Object value = cisMap.get(cis);
                                            if (value != null) {
                                                adapter.putJsonProperty(cis.toString(), value);
                                            }
                                        }
                                    }
                                    adapter.updateReadOnlyProperties();
                                    if (actionName != null) {
                                        commit = adapter.runAction(actionName);
                                    }
                                }
                                writer.outObject(result);
                            } catch (Exception e) {
                                throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
                            }
                        } else {
                            throw new WebApplicationException(ErrorHelper.createErrorResponse("otherElement is not a VertexFrame. It's a " + (otherElement == null ? "null" : DGraphUtils.findInterface(otherElement).getName()), Response.Status.INTERNAL_SERVER_ERROR));
                        }
                    } else {
                        Class[] interfaces = element.getClass().getInterfaces();
                        String intList = "";
                        for (Class inter : interfaces) {
                            intList = intList + inter.getName() + ", ";
                        }
                        String methList = "";
                        for (CaseInsensitiveString key : adders.keySet()) {
                            methList = methList + key.toString() + ", ";
                        }
                        throw new WebApplicationException(ErrorHelper.createErrorResponse("No method found for " + rawLabel + " on element " + intList + ": " + ((VertexFrame) element).asVertex().getId() + " methods " + methList, Response.Status.INTERNAL_SERVER_ERROR));
                    }
                } else {
                    throw new WebApplicationException(ErrorHelper.createErrorResponse("Element is null", Response.Status.INTERNAL_SERVER_ERROR));
                }
            }
        }
        if (commit) {
            graph.commit();
        } else {
            graph.rollback();
        }
    }
}
Also used : JsonException(com.ibm.commons.util.io.json.JsonException) NoteCoordinate(org.openntf.domino.big.NoteCoordinate) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CaseInsensitiveString(org.openntf.domino.types.CaseInsensitiveString) IOException(java.io.IOException) Method(java.lang.reflect.Method) CaseInsensitiveString(org.openntf.domino.types.CaseInsensitiveString) KeyNotFoundException(org.openntf.domino.graph2.impl.DEdgeEntryList.KeyNotFoundException) JsonException(com.ibm.commons.util.io.json.JsonException) WebApplicationException(javax.ws.rs.WebApplicationException) UserAccessException(org.openntf.domino.exceptions.UserAccessException) IOException(java.io.IOException) DVertexFrame(org.openntf.domino.graph2.builtin.DVertexFrame) VertexFrame(com.tinkerpop.frames.VertexFrame) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject)

Example 10 with JsonException

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

the class JsonGraphWriter method outDominoValue.

public void outDominoValue(final Object paramObject) throws IOException {
    try {
        if (paramObject == null) {
            outNull();
            return;
        }
        if (paramObject instanceof String) {
            outStringLiteral((String) paramObject);
            return;
        }
        if (paramObject instanceof Number) {
            outNumberLiteral(((Number) paramObject).doubleValue());
            return;
        }
        if (paramObject instanceof Boolean) {
            outBooleanLiteral(((Boolean) paramObject).booleanValue());
            return;
        }
        if (paramObject instanceof Date) {
            outStringLiteral(dateToString((Date) paramObject, true));
            return;
        }
        if (paramObject instanceof DateTime) {
            if (((DateTime) paramObject).getDateOnly().length() == 0) {
                outStringLiteral(timeOnlyToString(((DateTime) paramObject).toJavaDate()));
                return;
            }
            if (((DateTime) paramObject).getTimeOnly().length() == 0) {
                outStringLiteral(dateOnlyToString(((DateTime) paramObject).toJavaDate()));
                return;
            }
            outStringLiteral(dateToString(((DateTime) paramObject).toJavaDate(), true));
            return;
        }
        if (paramObject instanceof Vector) {
            startArray();
            Vector localVector = (Vector) paramObject;
            int i = localVector.size();
            for (int j = 0; j < i; ++j) {
                startArrayItem();
                outDominoValue(localVector.get(j));
                endArrayItem();
            }
            endArray();
            return;
        }
        outStringLiteral("???");
    } catch (JsonException localJsonException) {
        throw new AbstractIOException(localJsonException, "");
    }
}
Also used : JsonException(com.ibm.commons.util.io.json.JsonException) AbstractIOException(com.ibm.commons.util.AbstractIOException) Vector(java.util.Vector) Date(java.util.Date) DateTime(org.openntf.domino.DateTime)

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