use of com.ibm.commons.util.io.json.JsonException in project org.openntf.xsp.jakartaee by OpenNTF.
the class TestBundleBeanResource method getBean.
private Map<String, Object> getBean(String base) throws JsonException {
Client client = getAnonymousClient();
WebTarget target = client.target(base + "/bean");
Response response = target.request().get();
String output = response.readEntity(String.class);
try {
Map<String, Object> obj = (Map<String, Object>) JsonParser.fromJson(JsonJavaFactory.instance, output);
return obj;
} catch (Exception e) {
fail("Exception parsing JSON: " + output, e);
return null;
}
}
use of com.ibm.commons.util.io.json.JsonException in project org.openntf.nsfodp by OpenNTF.
the class StockComponentsServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setBufferSize(0);
// $NON-NLS-1$ //$NON-NLS-2$
resp.setHeader("Content-Type", "text/json");
ServletOutputStream os = resp.getOutputStream();
try {
if (componentInfo == null) {
SharableRegistryImpl facesRegistry = new SharableRegistryImpl(getClass().getPackage().getName());
// $NON-NLS-1$
List<Object> libraries = ExtensionManager.findServices((List<Object>) null, LibraryServiceLoader.class, "com.ibm.xsp.Library");
libraries.stream().filter(lib -> lib instanceof XspLibrary).map(XspLibrary.class::cast).map(lib -> new LibraryWrapper(lib.getLibraryId(), lib)).map(wrapper -> {
SimpleRegistryProvider provider = new SimpleRegistryProvider();
provider.init(wrapper);
return provider;
}).map(XspRegistryProvider::getRegistry).forEach(facesRegistry::addDepend);
facesRegistry.refreshReferences();
componentInfo = new JsonJavaObject();
JsonArray defs = new JsonJavaArray();
facesRegistry.findComponentDefs().stream().filter(FacesComponentDefinition::isTag).map(def -> {
try {
JsonObject defObj = new JsonJavaObject();
// $NON-NLS-1$
defObj.putJsonProperty("namespaceUri", def.getNamespaceUri());
// $NON-NLS-1$
defObj.putJsonProperty("tagName", def.getTagName());
// $NON-NLS-1$
defObj.putJsonProperty("class", def.getJavaClass().getName());
// $NON-NLS-1$
defObj.putJsonProperty("since", def.getSince());
// $NON-NLS-1$
defObj.putJsonProperty("defaultPrefix", def.getFirstDefaultPrefix());
// $NON-NLS-1$
defObj.putJsonProperty("componentFamily", def.getComponentFamily());
// $NON-NLS-1$
defObj.putJsonProperty("componentType", def.getComponentType());
// $NON-NLS-1$
defObj.putJsonProperty("id", def.getId());
JsonArray facetNames = new JsonJavaArray();
for (String facetName : def.getFacetNames()) {
facetNames.add(facetName);
}
// $NON-NLS-1$
defObj.putJsonProperty("facetNames", facetNames);
FacesProperty defaultProp = def.getDefaultFacesProperty();
if (defaultProp != null) {
JsonObject defaultPropObj = new JsonJavaObject();
// $NON-NLS-1$
defaultPropObj.putJsonProperty("name", defaultProp.getName());
// $NON-NLS-1$
defaultPropObj.putJsonProperty("since", defaultProp.getSince());
// $NON-NLS-1$
defaultPropObj.putJsonProperty("class", defaultProp.getJavaClass().getName());
// $NON-NLS-1$
defaultPropObj.putJsonProperty("required", defaultProp.isRequired());
// $NON-NLS-1$
defaultPropObj.putJsonProperty("attribute", defaultProp.isAttribute());
}
JsonArray properties = new JsonJavaArray();
for (String propName : def.getPropertyNames()) {
FacesProperty prop = def.getProperty(propName);
JsonObject propObj = new JsonJavaObject();
// $NON-NLS-1$
propObj.putJsonProperty("name", propName);
// $NON-NLS-1$
propObj.putJsonProperty("class", prop.getJavaClass().getName());
// $NON-NLS-1$
propObj.putJsonProperty("since", prop.getSince());
// $NON-NLS-1$
propObj.putJsonProperty("required", prop.isRequired());
// $NON-NLS-1$
propObj.putJsonProperty("attribute", prop.isAttribute());
properties.add(propObj);
}
// $NON-NLS-1$
defObj.putJsonProperty("properties", properties);
return defObj;
} catch (JsonException e) {
throw new RuntimeException(e);
}
}).forEach(defObj -> {
try {
defs.add(defObj);
} catch (JsonException e) {
throw new RuntimeException(e);
}
});
// $NON-NLS-1$
componentInfo.putJsonProperty("components", defs);
}
os.print(componentInfo.toString());
} catch (Throwable e) {
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter out = new PrintWriter(baos);
e.printStackTrace(out);
out.flush();
os.println(LineDelimitedJsonProgressMonitor.message(// $NON-NLS-1$ //$NON-NLS-2$
"type", // $NON-NLS-1$ //$NON-NLS-2$
"error", // $NON-NLS-1$
"stackTrace", // $NON-NLS-1$
baos.toString()));
}
}
use of com.ibm.commons.util.io.json.JsonException in project org.openntf.domino by OpenNTF.
the class Document method toJson.
@Override
public String toJson(final boolean compact) {
StringWriter sw = new StringWriter();
JsonWriter jw = new JsonWriter(sw, compact);
try {
jw.startObject();
jw.outStringProperty("@unid", getUniversalID());
jw.outStringProperty("@noteid", getNoteID());
jw.outStringProperty("@replicaid", getParentDatabase().getReplicaID());
jw.outStringProperty("@metaversalid", getMetaversalID());
try {
jw.outStringProperty("@created", getCreated().toGMTISO());
jw.outStringProperty("@lastmodified", getLastModified().toGMTISO());
jw.outStringProperty("@lastaccessed", getLastAccessed().toGMTISO());
} catch (Exception e) {
DominoUtils.handleException(e, "Exception trying to index Dates.");
}
Set<String> keys = keySet();
for (String key : keys) {
Item currItem = getFirstItem(key);
// A beer to anyone who can work out how this could happen, except for the person who identified it!
if (null != currItem) {
Type itemType = currItem.getTypeEx();
try {
if (itemType == Type.ATTACHMENT) {
jw.outProperty(key, "ATTACHMENT");
} else if (itemType == Type.AUTHORS || itemType == Type.READERS || itemType == Type.NAMES || itemType == Type.TEXT || itemType == Type.NUMBERS) {
Vector<Object> values = currItem.getValues();
if (values.size() == 1) {
jw.outProperty(key, values.elementAt(0));
} else {
jw.outProperty(key, values);
}
} else if (itemType == Type.DATETIMES) {
Vector<DateTime> values = currItem.getValueDateTimeArray();
if (values.size() == 1) {
jw.outProperty(key, values.get(0).toGMTISO());
} else {
jw.outProperty(key, TypeUtils.toStrings(values));
}
} else if (itemType == Type.EMBEDDEDOBJECT) {
jw.outProperty(key, "EMBEDDED_OBJECT");
} else if (itemType == Type.RICHTEXT) {
RichTextItem rtItem = (RichTextItem) currItem;
jw.outProperty(key, rtItem.getUnformattedText());
} else if (itemType == Type.MIME_PART) {
MIMEEntity mimeEntity = currItem.getMIMEEntity();
if (mimeEntity != null) {
jw.outProperty(key, mimeEntity.getContentAsText());
} else {
jw.outProperty(key, "MIME_PART null");
}
}
} catch (Exception e) {
DominoUtils.handleException(e, this);
// NTF - temporary
e.printStackTrace();
}
}
// if (currItem.getMIMEEntity() == null) {
// jw.outProperty(key, currItem.getText());
// } else {
// String abstractedText = currItem.abstractText(0, false, false);
// if (null == abstractedText) {
// jw.outProperty(key, "**MIME ITEM, VALUE CANNOT BE DECODED TO JSON**");
// } else {
// jw.outProperty(key, abstractedText);
// }
// }
// Now output attachments
jw.outProperty("@attachments", getAttachmentNames());
}
jw.endObject();
jw.flush();
} catch (IOException e) {
DominoUtils.handleException(e, this);
return null;
} catch (JsonException e) {
DominoUtils.handleException(e, this);
return null;
}
return sw.toString();
}
use of com.ibm.commons.util.io.json.JsonException 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 com.ibm.commons.util.io.json.JsonException 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;
}
Aggregations