use of org.openntf.domino.rest.json.JsonGraphFactory 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;
}
use of org.openntf.domino.rest.json.JsonGraphFactory 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;
}
use of org.openntf.domino.rest.json.JsonGraphFactory 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;
}
use of org.openntf.domino.rest.json.JsonGraphFactory in project org.openntf.domino by OpenNTF.
the class FramedCollectionResource 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;
JsonGraphFactory factory = JsonGraphFactory.instance;
StringWriter sw = new StringWriter();
JsonGraphWriter writer = new JsonGraphWriter(sw, graph, pm, false, true, true);
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;
}
} catch (Exception e) {
e.printStackTrace();
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, request.getMethod());
writer.endArrayItem();
} else {
System.err.println("Expected a JsonJavaObject but instead we got a " + raw.getClass().getName());
}
}
writer.endArray();
} else if (jsonItems != null) {
processJsonUpdate(jsonItems, graph, writer, pm, request.getMethod());
}
String jsonEntity = sw.toString();
ResponseBuilder berg = getBuilder(jsonEntity, new Date(), false, request);
Response response = berg.build();
return response;
}
use of org.openntf.domino.rest.json.JsonGraphFactory in project org.openntf.domino by OpenNTF.
the class FramedResource 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 {
// Factory.println("Processing a POST for " + namespace);
DFramedTransactionalGraph graph = this.getGraph(namespace);
ParamMap pm = Parameters.toParamMap(uriInfo);
StringWriter sw = new StringWriter();
JsonGraphWriter writer = new JsonGraphWriter(sw, graph, pm, false, true, false);
JsonJavaObject jsonItems = null;
List<Object> jsonArray = null;
JsonGraphFactory factory = JsonGraphFactory.instance;
Object jsonRaw = null;
try {
StringReader reader = new StringReader(requestEntity);
try {
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 (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));
}
// Map<Object, Object> results = new LinkedHashMap<Object, Object>();
if (jsonArray != null) {
writer.startArray();
for (Object raw : jsonArray) {
if (raw instanceof JsonJavaObject) {
writer.startArrayItem();
processJsonObject((JsonJavaObject) raw, graph, writer, pm);
writer.endArrayItem();
}
}
writer.endArray();
} else if (jsonItems != null) {
processJsonObject(jsonItems, graph, writer, pm);
} else {
System.out.println("ALERT Got a jsonRaw of type " + (jsonRaw != null ? jsonRaw.getClass().getName() : "null") + ". Value is: " + String.valueOf(requestEntity));
}
String jsonEntity = sw.toString();
ResponseBuilder berg = getBuilder(jsonEntity, new Date(), false, null);
Response response = berg.build();
return response;
}
Aggregations