use of org.openntf.domino.rest.json.JsonGraphWriter 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.JsonGraphWriter in project org.openntf.domino by OpenNTF.
the class CommandResource method performCommand.
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response performCommand(@Context final UriInfo uriInfo, @PathParam(Routes.NAMESPACE) final String namespace) throws JsonException, IOException {
String jsonEntity = null;
ResponseBuilder builder = Response.ok();
ParamMap pm = Parameters.toParamMap(uriInfo);
StringWriter sw = new StringWriter();
DFramedTransactionalGraph<?> graph = this.getGraph(namespace);
JsonGraphWriter writer = new JsonGraphWriter(sw, graph, pm, false, true, false);
// writer.outObject(null);
List<String> commands = pm.get(Parameters.COMMAND);
if (commands != null && commands.size() > 0) {
IGraphFactory factory = this.getService().getFactory(namespace);
if (factory != null) {
for (String command : commands) {
// System.out.println("TEMP DEBUG processing command " +
// command + " in namespace " + namespace);
Object curResult = factory.processCommand(namespace, command, uriInfo.getQueryParameters());
writer.outObject(curResult);
}
} else {
System.err.println("No Factory found for namespace: " + namespace);
}
}
jsonEntity = sw.toString();
builder.type(MediaType.APPLICATION_JSON_TYPE).entity(jsonEntity);
Response response = builder.build();
return response;
}
use of org.openntf.domino.rest.json.JsonGraphWriter in project org.openntf.domino by OpenNTF.
the class FramedCollectionResource method getFramedObject.
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getFramedObject(@Context final UriInfo uriInfo, @PathParam(Routes.NAMESPACE) final String namespace, @Context final Request request) throws JsonException, IOException {
DFramedTransactionalGraph graph = this.getGraph(namespace);
ParamMap pm = Parameters.toParamMap(uriInfo);
StringWriter sw = new StringWriter();
JsonGraphWriter writer = new JsonGraphWriter(sw, graph, pm, false, true, true);
try {
if (pm.getTypes() != null) {
List<CharSequence> types = pm.getTypes();
List<CharSequence> filterkeys = pm.getFilterKeys();
List<CharSequence> filtervalues = pm.getFilterValues();
List<CharSequence> partialkeys = pm.getPartialKeys();
List<CharSequence> partialvalues = pm.getPartialValues();
List<CharSequence> startskeys = pm.getStartsKeys();
List<CharSequence> startsvalues = pm.getStartsValues();
if (types.size() == 0) {
writer.outNull();
} else if (types.size() == 1) {
long startTime = new Date().getTime();
CharSequence typename = types.get(0);
Iterable<?> elements = null;
if (filterkeys != null) {
elements = graph.getFilteredElements(typename.toString(), filterkeys, filtervalues);
} else if (partialkeys != null) {
elements = graph.getFilteredElementsPartial(typename.toString(), partialkeys, partialvalues);
} else if (startskeys != null) {
elements = graph.getFilteredElementsStarts(typename.toString(), startskeys, startsvalues);
} else {
elements = graph.getElements(typename.toString());
}
if (elements instanceof FramedEdgeList) {
List<?> result = sortAndLimitList((List<?>) elements, pm);
writer.outArrayLiteral(result);
} else if (elements instanceof FramedVertexList) {
List<?> result = sortAndLimitList((List<?>) elements, pm);
writer.outArrayLiteral(result);
} else {
List<Object> maps = new ArrayList<Object>();
for (Object element : elements) {
maps.add(element);
}
writer.outArrayLiteral(maps);
}
long endTime = new Date().getTime();
System.out.println("TEMP DEBUG Output for " + typename + " took " + (endTime - startTime) + "ms");
} else {
MixedFramedVertexList vresult = null;
FramedEdgeList eresult = null;
for (CharSequence typename : types) {
Iterable<?> elements = null;
if (filterkeys != null) {
elements = graph.getFilteredElements(typename.toString(), filterkeys, filtervalues);
} else if (partialkeys != null) {
elements = graph.getFilteredElementsPartial(typename.toString(), partialkeys, partialvalues);
} else if (startskeys != null) {
elements = graph.getFilteredElementsStarts(typename.toString(), startskeys, startsvalues);
} else {
elements = graph.getElements(typename.toString());
}
/*
* if (elements != null) { System.out.
* println("TEMP DEBUG found elements for type " +
* typename.toString()); } else {
* System.out.println("TEMP DEBUG NO elements for type "
* + typename.toString()); }
*/
if (elements instanceof FramedVertexList) {
if (vresult == null) {
vresult = new MixedFramedVertexList(graph, null, (FramedVertexList) elements);
} else {
vresult.addAll((List<?>) elements);
}
} else if (elements instanceof FramedEdgeList) {
if (eresult == null) {
eresult = (FramedEdgeList) elements;
} else {
eresult.addAll((FramedEdgeList) elements);
}
}
}
if (vresult != null) {
List<?> result = sortAndLimitList(vresult, pm);
writer.outArrayLiteral(result);
}
if (eresult != null) {
List<?> result = sortAndLimitList(eresult, pm);
writer.outArrayLiteral(result);
}
}
} else {
// System.out.println("TEMP DEBUG: ID was null therefore we
// can't report...");
Map<String, Object> jsonMap = new LinkedHashMap<String, Object>();
jsonMap.put("namespace", namespace);
jsonMap.put("status", "active");
writer.outObject(jsonMap);
}
} catch (UserAccessException uae) {
throw new WebApplicationException(ErrorHelper.createErrorResponse(uae, Response.Status.UNAUTHORIZED));
} catch (Exception e) {
throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
String jsonEntity = sw.toString();
ResponseBuilder berg = getBuilder(jsonEntity, new Date(), true, request);
Response response = berg.build();
return response;
}
use of org.openntf.domino.rest.json.JsonGraphWriter 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.JsonGraphWriter in project org.openntf.domino by OpenNTF.
the class FramedResource method getFramedObject.
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getFramedObject(@Context final UriInfo uriInfo, @PathParam(Routes.NAMESPACE) final String namespace, @Context final Request request) throws JsonException, IOException {
DFramedTransactionalGraph graph = this.getGraph(namespace);
ParamMap pm = Parameters.toParamMap(uriInfo);
if (pm.getVersion() != null) {
System.out.println("TEMP DEBUG Version number parameter detected " + pm.getVersion().get(0));
}
StringWriter sw = new StringWriter();
JsonGraphWriter writer = new JsonGraphWriter(sw, graph, pm, false, true, false);
Date lastModified = new Date();
boolean getLastMod = false;
try {
if (pm.get(Parameters.ID) != null) {
List<String> ids = pm.get(Parameters.ID);
if (ids.size() == 0) {
writer.outNull();
} else if (ids.size() == 1) {
String id = ids.get(0).trim();
NoteCoordinate nc = null;
if (id.startsWith("E")) {
nc = ViewEntryCoordinate.Utils.getViewEntryCoordinate(id);
} else if (id.startsWith("V")) {
nc = ViewEntryCoordinate.Utils.getViewEntryCoordinate(id);
} else {
nc = NoteCoordinate.Utils.getNoteCoordinate(id);
getLastMod = true;
// System.out.println("TEMP DEBUG isIcon: " +
// String.valueOf(nc.isIcon()));
}
if (nc == null) {
// writer.outStringProperty("message", "NoteCoordinate
// is null for id " + id);
}
if (graph == null) {
// writer.outStringProperty("message", "Graph is null
// for namespace " + namespace);
}
NoteCoordinate versionNC = null;
if (pm.getVersion() != null) {
String versionString = pm.getVersion().get(0).toString();
System.out.println("Version parameter detected: " + versionString);
SimpleDateFormat sdf = TypeUtils.getDefaultDateFormat();
Date versionDate = sdf.parse(versionString);
try {
Session sess = Factory.getSession(SessionType.CURRENT);
Document doc = sess.getDocumentByMetaversalID(nc.toString());
Database db = doc.getAncestorDatabase();
List<DocumentBackupContributor> contributors = Factory.findApplicationServices(DocumentBackupContributor.class);
if (contributors != null) {
for (DocumentBackupContributor contributor : contributors) {
Optional<Document> versionDoc = contributor.createSidecarDocument(db, doc.getUniversalID(), versionDate);
if (versionDoc.isPresent()) {
versionNC = versionDoc.get().getNoteCoordinate();
break;
}
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}
Object elem = null;
if (versionNC != null) {
elem = graph.getElement(versionNC, null);
// System.out.println("Got an element from graph with id " + ((Element)elem).getId());
} else {
elem = graph.getElement(nc, null);
}
if (elem == null) {
// builder = Response.status(Status.NOT_FOUND);
// writer.outStringProperty("currentUsername",
// Factory.getSession(SessionType.CURRENT).getEffectiveUserName());
// throw new IllegalStateException();
Response response = ErrorHelper.createErrorResponse("Graph element not found for id " + String.valueOf(id), Response.Status.NOT_FOUND);
throw new WebApplicationException(response);
}
try {
if (elem instanceof DVertexFrame && getLastMod) {
lastModified = ((DVertexFrame) elem).getModified();
}
if (elem instanceof DEdgeFrame && getLastMod) {
lastModified = ((DEdgeFrame) elem).getModified();
}
} catch (UserAccessException uae) {
return ErrorHelper.createErrorResponse("User " + Factory.getSession(SessionType.CURRENT).getEffectiveUserName() + " is not authorized to access this resource", Response.Status.UNAUTHORIZED);
} catch (Exception e) {
throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
writer.outObject(elem);
} else {
List<Object> maps = new ArrayList<Object>();
for (String id : ids) {
NoteCoordinate nc = NoteCoordinate.Utils.getNoteCoordinate(id.trim());
maps.add(graph.getElement(nc, null));
}
writer.outArrayLiteral(maps);
}
} else if (pm.getKeys() != null) {
Class<?> type = null;
if (pm.getTypes() != null) {
List<CharSequence> types = pm.getTypes();
String typename = types.get(0).toString();
type = graph.getTypeRegistry().findClassByName(typename);
}
DKeyResolver resolver = graph.getKeyResolver(type);
List<CharSequence> keys = pm.getKeys();
if (keys.size() == 0) {
writer.outNull();
} else if (keys.size() == 1) {
CharSequence id = keys.get(0);
NoteCoordinate nc = resolver.resolveKey(type, URLDecoder.decode(String.valueOf(id), "UTF-8"));
Object elem = null;
if (nc != null) {
// is null for id " + id);
try {
elem = graph.getElement(nc);
} catch (Exception e) {
// NOOP NTF - this is possible and not an error condition. That's why we have .handleMissingKey.
}
}
if (elem == null) {
elem = resolver.handleMissingKey(type, id);
if (elem == null) {
Response response = ErrorHelper.createErrorResponse("Graph element not found for key " + id, Response.Status.NOT_FOUND);
throw new WebApplicationException(response);
}
}
if (elem instanceof Vertex) {
// System.out.println("TEMP DEBUG Framing a vertex of
// type "
// + elem.getClass().getName());
VertexFrame vf = (VertexFrame) graph.frame((Vertex) elem, type);
if (vf instanceof DVertexFrame) {
lastModified = ((DVertexFrame) vf).getModified();
}
writer.outObject(vf);
} else if (elem instanceof Edge) {
EdgeFrame ef = (EdgeFrame) graph.frame((Edge) elem, type);
if (ef instanceof DEdgeFrame) {
lastModified = ((DEdgeFrame) ef).getModified();
}
writer.outObject(ef);
}
} else {
List<Object> maps = new ArrayList<Object>();
for (CharSequence id : keys) {
NoteCoordinate nc = resolver.resolveKey(type, id);
maps.add(graph.getElement(nc, null));
}
writer.outArrayLiteral(maps);
}
graph.rollback();
} else {
MultivaluedMap<String, String> mvm = uriInfo.getQueryParameters();
for (@SuppressWarnings("unused") String key : mvm.keySet()) {
// System.out.println("TEMP DEBUG: " + key + ": " +
// mvm.getFirst(key));
}
Map<String, Object> jsonMap = new LinkedHashMap<String, Object>();
jsonMap.put("namespace", namespace);
jsonMap.put("status", "active");
writer.outObject(jsonMap);
}
} catch (UserAccessException uae) {
return ErrorHelper.createErrorResponse("User " + Factory.getSession(SessionType.CURRENT).getEffectiveUserName() + " is not authorized to access this resource", Response.Status.UNAUTHORIZED);
} catch (KeyNotFoundException knfe) {
ResponseBuilder rb = Response.noContent();
return rb.build();
} catch (Exception e) {
throw new WebApplicationException(ErrorHelper.createErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
String jsonEntity = sw.toString();
ResponseBuilder berg = getBuilder(jsonEntity, lastModified, true, request);
Response response = berg.build();
return response;
}
Aggregations