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;
}
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;
}
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;
}
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();
}
}
}
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, "");
}
}
Aggregations