use of org.openntf.domino.rest.json.JsonGraphWriter in project org.openntf.domino by OpenNTF.
the class InfoResource method performRequest.
@GET
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("nls")
public Response performRequest(@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> items = pm.get(Parameters.ITEM);
if (items != null && items.size() > 0) {
IGraphFactory factory = this.getService().getFactory(namespace);
if (factory != null) {
for (String item : items) {
Object curResult = factory.processRequest(namespace, item, 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 SearchResource method getSearchObject.
@SuppressWarnings("unchecked")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getSearchObject(@Context final UriInfo uriInfo, @PathParam(Routes.NAMESPACE) final String namespace, @Context final Request request) throws JsonException, IOException {
@SuppressWarnings("rawtypes") DFramedTransactionalGraph graph = this.getGraph(namespace);
ParamMap pm = Parameters.toParamMap(uriInfo);
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;
}
Object elem = graph.getElement(nc, null);
if (elem == null) {
throw new WebApplicationException(ErrorHelper.createErrorResponse("Graph element not found for id " + id, Response.Status.NOT_FOUND));
}
if (elem instanceof DVertexFrame && getLastMod) {
lastModified = ((DVertexFrame) elem).getModified();
}
if (elem instanceof DEdgeFrame && getLastMod) {
lastModified = ((DEdgeFrame) elem).getModified();
}
writer.outObject(elem);
} else {
List<Value> valueResults = null;
List<RichTextReference> rtRefResults = null;
for (String id : ids) {
NoteCoordinate nc = NoteCoordinate.Utils.getNoteCoordinate(id.trim());
Object rawterm = graph.getElement(nc, null);
// System.out.println("TEMP DEBUG got a " + DGraphUtils.getInterfaceList(rawterm) + " from id " + nc);
Term term = null;
if (rawterm instanceof Term) {
term = (Term) rawterm;
} else {
// System.out.println("TEMP DEBUG didn't get a Term!");
}
List<Value> values = term.getValues();
// System.out.println("TEMP DEBUG found " + values.size() + " values for term " + term.getValue() + " in a " + values.getClass().getName());
List<RichTextReference> rtRefs = term.getRichTextReferences();
// System.out.println("TEMP DEBUG found " + rtRefs.size() + " rtrefs for term " + term.getValue() + " in a " + rtRefs.getClass().getName());
if (valueResults == null) {
valueResults = values;
} else {
valueResults.retainAll(values);
// System.out.println("TEMP DEBUG retained " + valueResults.size() + " values for term " + term.getValue());
}
if (rtRefResults == null) {
rtRefResults = rtRefs;
} else {
rtRefResults.retainAll(rtRefs);
// System.out.println("TEMP DEBUG retained " + rtRefResults.size() + " rtrefs for term " + term.getValue());
}
}
List<Object> combinedResults = new ArrayList<Object>();
combinedResults.addAll(valueResults);
combinedResults.addAll(rtRefResults);
writer.outArrayLiteral(combinedResults);
}
} else if (pm.getKeys() != null) {
Class<?> type = Term.class;
DKeyResolver resolver = graph.getKeyResolver(type);
List<CharSequence> keys = pm.getKeys();
if (keys.size() == 0) {
writer.outNull();
} else {
List<Value> valueResults = null;
List<RichTextReference> rtRefResults = null;
String key = URLDecoder.decode(String.valueOf(keys.get(0)), "UTF-8");
String[] array = key.split(" ");
for (String cur : array) {
NoteCoordinate nc = resolver.resolveKey(type, cur);
Object rawterm = graph.getElement(nc, null);
Term term = null;
if (rawterm instanceof Term) {
term = (Term) rawterm;
} else {
}
List<Value> values = term.getValues();
List<RichTextReference> rtRefs = term.getRichTextReferences();
if (valueResults == null) {
valueResults = values;
} else {
valueResults.retainAll(values);
}
if (rtRefResults == null) {
rtRefResults = rtRefs;
} else {
rtRefResults.retainAll(rtRefs);
}
}
List<Object> combinedResults = new ArrayList<Object>();
combinedResults.addAll(valueResults);
combinedResults.addAll(rtRefResults);
writer.outArrayLiteral(combinedResults);
}
graph.rollback();
} else {
// MultivaluedMap<String, String> mvm = uriInfo.getQueryParameters();
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;
}
use of org.openntf.domino.rest.json.JsonGraphWriter in project org.openntf.domino by OpenNTF.
the class TermsResource method getTermsObject.
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getTermsObject(@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 {
List<CharSequence> types = new ArrayList<CharSequence>();
types.add("org.openntf.domino.graph2.builtin.search.Term");
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) {
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);
}
} 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 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);
}
}
} 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 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.JsonGraphWriter 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;
}
Aggregations