fieldnames) {
- Field[] fields = new Field[fieldnames.size()];
- int f = 0;
- for (String s: fieldnames) {
- try {
- fields[f++] = o.getClass().getDeclaredField(s);
- } catch (SecurityException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (NoSuchFieldException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- for (int i = 0; i < fields.length; i++) {
- Field field = fields[i];
- try {
- field.setAccessible(true);
- System.out.println(field.getName()+": "+field.get(o));
- }
- catch (IllegalAccessException e) {
- System.err.println("Illegal access exception");
- } catch (NullPointerException e) {
- System.err.println("Nullpointer Exception");
- }
- }
- System.out.println();
- }
-
- /**
- * @param o
- */
- public static void logBusinessObject(Object o) {
- Field[] fields = o.getClass().getDeclaredFields();
-
- for (int i = 0; i < fields.length; i++) {
- Field field = fields[i];
- try {
- field.setAccessible(true);
- logger.info(field.getName()+": "+field.get(o));
- }
- catch (IllegalAccessException e) {
- logger.error("Illegal access exception");
- } catch (NullPointerException e) {
- logger.error("Nullpointer Exception");
- }
-
- }
- }
-
- /**
- * @param mapobject
- */
- public static void printMapObject(Map, ?> mapobject) {
- Iterator> iter = mapobject.keySet().iterator();
- while (iter.hasNext()) {
- Object keyobj = iter.next();
- Object valobj = mapobject.get(keyobj);
- System.out.println(keyobj +": "+ valobj);
- }
- }
-
- /**
- * @param mapobject
- */
- public static void logMapObject(Map, ?> mapobject) {
- Iterator> iter = mapobject.keySet().iterator();
- while (iter.hasNext()) {
- Object keyobj = iter.next();
- Object valobj = mapobject.get(keyobj);
- logger.info(keyobj +": "+ valobj);
- }
- }
-
- public static String nl2br(String text) {
- return text.replaceAll("\n\n", "").replaceAll("\n", "
");
- }
-
-
-
-}
diff --git a/src/edu/cornell/mannlib/semservices/util/TimeConverter.java b/src/edu/cornell/mannlib/semservices/util/TimeConverter.java
deleted file mode 100644
index ccd6147c..00000000
--- a/src/edu/cornell/mannlib/semservices/util/TimeConverter.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/* $This file is distributed under the terms of the license in /doc/license.txt$ */
-
-package edu.cornell.mannlib.semservices.util;
-import edu.cornell.mannlib.semservices.bo.Time;
-import java.util.Date;
-
-import org.apache.commons.beanutils.Converter;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-
-
-public class TimeConverter implements Converter {
- @SuppressWarnings("unused")
- private static final Log logger = LogFactory.getLog(TimeConverter.class);
-
- /**
- * The default value specified to our Constructor, if any.
- */
- @SuppressWarnings("unused")
- private Object defaultValue = null;
-
- /**
- * Should we return the default value on conversion errors?
- */
- @SuppressWarnings("unused")
- private boolean useDefault = true;
-
- /**
- *
- */
- public TimeConverter() {
- this.defaultValue = null;
- this.useDefault = false;
- }
-
- /**
- * @param defaultValue
- */
- public TimeConverter(Object defaultValue) {
- this.defaultValue = defaultValue;
- this.useDefault = true;
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.beanutils.Converter#convert(java.lang.Class, java.lang.Object)
- */
- @SuppressWarnings("unchecked")
- public Object convert(Class type, Object value) {
- String s = value.toString();
- return s;
- }
-
- /**
- * @param time
- * @return
- */
- public static String toFormattedString(Object time) {
- return time.toString();
- }
-
- /**
- * @param time
- * @return
- */
- public static String toUnixTime(Object time) {
- Time timeObject = (Time) time;
- Date date = timeObject.getDate();
- Long seconds = date.getTime() / 1000;
- //logger.info("unixtime: " + seconds.toString());
- return seconds.toString();
- }
-
-}
diff --git a/src/edu/cornell/mannlib/semservices/util/TimestampConverter.java b/src/edu/cornell/mannlib/semservices/util/TimestampConverter.java
deleted file mode 100644
index c4f3baa8..00000000
--- a/src/edu/cornell/mannlib/semservices/util/TimestampConverter.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/* $This file is distributed under the terms of the license in /doc/license.txt$ */
-
-package edu.cornell.mannlib.semservices.util;
-import java.sql.Timestamp;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-
-import org.apache.commons.beanutils.Converter;
-
-public class TimestampConverter implements Converter {
- /**
- * The default value specified to our Constructor, if any.
- */
- @SuppressWarnings("unused")
- private Object defaultValue = null;
-
- /**
- * Should we return the default value on conversion errors?
- */
- @SuppressWarnings("unused")
- private boolean useDefault = true;
-
-
- /**
- *
- */
- public TimestampConverter() {
- this.defaultValue = null;
- this.useDefault = false;
- }
-
- /**
- * @param defaultValue
- */
- public TimestampConverter(Object defaultValue) {
- this.defaultValue = defaultValue;
- this.useDefault = true;
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.beanutils.Converter#convert(java.lang.Class, java.lang.Object)
- */
-
- @SuppressWarnings("unchecked")
- public Object convert(Class type, Object value) {
- Timestamp ts = (Timestamp) value;
- String s = new String();
- s = new SimpleDateFormat("MMM d, h:mm a").format(ts.getTime());
- return s;
- }
-
- /**
- * @param time
- * @return
- */
- public String toUnixTime(Object time) {
- Timestamp ts = (Timestamp) time;
- Date date = new Date(ts.getTime());
- Long seconds = date.getTime() / 1000;
- //logger.info("unixtime: " + seconds.toString());
- return seconds.toString();
- }
-
-
-}
diff --git a/src/edu/cornell/mannlib/semservices/util/XMLGregorianCalendarConverter.java b/src/edu/cornell/mannlib/semservices/util/XMLGregorianCalendarConverter.java
deleted file mode 100644
index 2caaf4ce..00000000
--- a/src/edu/cornell/mannlib/semservices/util/XMLGregorianCalendarConverter.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/* $This file is distributed under the terms of the license in /doc/license.txt$ */
-
-/*
-
- * $Id: XMLGregorianCalendarConverter.java 28642 2006-10-25 13:41:54Z jdamick $
- *
- * Copyright 2006- Revolution Health Group. All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Revolution Health Group. (Confidential Information).
- * You shall not disclose such Confidential Information and shall
- * use it only in accordance with the terms of the license
- * agreement you entered into with Revolution Health Group.
- *
- */
-
-package edu.cornell.mannlib.semservices.util;
-
-import javax.xml.datatype.DatatypeFactory;
-import javax.xml.datatype.XMLGregorianCalendar;
-
-import org.apache.commons.beanutils.ConversionException;
-import org.apache.commons.beanutils.Converter;
-
-public final class XMLGregorianCalendarConverter implements Converter {
-
- // Constructors
-
- /**
- * Create a {@link Converter} that will throw a {@link ConversionException}
- * if a conversion error occurs.
- */
- public XMLGregorianCalendarConverter() {
- this.defaultValue = null;
- this.useDefault = false;
- }
-
- /**
- * Create a {@link Converter} that will return the specified default value if
- * a conversion error occurs.
- *
- * @param defaultValue
- * The default value to be returned
- */
- public XMLGregorianCalendarConverter(Object defaultValue) {
- this.defaultValue = defaultValue;
- this.useDefault = true;
- }
-
- // Instance Variables
-
- /**
- * The default value specified to our Constructor, if any.
- */
- private Object defaultValue = null;
-
- /**
- * Should we return the default value on conversion errors?
- */
- private boolean useDefault = true;
-
- // Public Methods
-
- /**
- * Convert the specified input object into an output object of the specified
- * type.
- *
- * @param type
- * XMLGregorianCalendar type to which this value should be
- * converted
- * @param value
- * The input value to be converted
- *
- * @exception ConversionException
- * if conversion cannot be performed successfully
- */
- @SuppressWarnings("unchecked")
- public Object convert(Class type, Object value) {
-
- if (value == null) {
- if (useDefault) {
- return (defaultValue);
- } else {
- throw new ConversionException("No value specified");
- }
- }
-
- if (value instanceof XMLGregorianCalendar) {
- return (value);
- }
-
- try {
- return DatatypeFactory.newInstance().newXMLGregorianCalendar(
- value.toString());
- } catch (Exception e) {
- if (useDefault) {
- return (defaultValue);
- } else {
- throw new ConversionException(e);
- }
- }
- }
-}
diff --git a/src/edu/cornell/mannlib/semservices/view/BeanToJsonSerializer.java b/src/edu/cornell/mannlib/semservices/view/BeanToJsonSerializer.java
deleted file mode 100644
index 13d4d859..00000000
--- a/src/edu/cornell/mannlib/semservices/view/BeanToJsonSerializer.java
+++ /dev/null
@@ -1,228 +0,0 @@
-/* $This file is distributed under the terms of the license in /doc/license.txt$ */
-
-/*
- * $Id: BeanToJsonSerializer.java 66350 2007-08-20 21:11:41Z xluan $
- *
- * Copyright 2006- Revolution Health Group. All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Revolution Health Group. (Confidential Information).
- * You shall not disclose such Confidential Information and shall
- * use it only in accordance with the terms of the license
- * agreement you entered into with Revolution Health Group.
- *
- */
-package edu.cornell.mannlib.semservices.view;
-
-import java.beans.PropertyDescriptor;
-import java.lang.reflect.InvocationTargetException;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-import javax.xml.datatype.XMLGregorianCalendar;
-
-import org.apache.commons.beanutils.ConvertUtils;
-import org.apache.commons.beanutils.PropertyUtils;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import net.sf.json.JSONArray;
-import net.sf.json.JSONException;
-import net.sf.json.JSONNull;
-import net.sf.json.JSONObject;
-
-import edu.cornell.mannlib.semservices.bo.Day;
-import edu.cornell.mannlib.semservices.bo.Time;
-import edu.cornell.mannlib.semservices.util.ClassUtils;
-import edu.cornell.mannlib.semservices.util.DateConverter;
-import edu.cornell.mannlib.semservices.util.DayConverter;
-import edu.cornell.mannlib.semservices.util.ObjectUtils;
-import edu.cornell.mannlib.semservices.util.TimeConverter;
-import edu.cornell.mannlib.semservices.util.TimestampConverter;
-
-@SuppressWarnings("serial")
-public class BeanToJsonSerializer {
- private static final Log logger = LogFactory.getLog(BeanToJsonSerializer.class);
-
- /**
- *
- */
- private BeanToJsonSerializer() {
- ConvertUtils.register(new DateConverter(), java.util.Date.class);
- ConvertUtils.register(new TimestampConverter(),
- String.class);
- ConvertUtils.register(new TimeConverter(), String.class);
- ConvertUtils.register(new DayConverter(), String.class);
- }
-
- /**
- *
- */
- @SuppressWarnings("unchecked")
- private static final Set CONVERTABLE_CLASSES = new HashSet() {
- {
- add(XMLGregorianCalendar.class);
- }
- };
-
- /**
- * @param bean
- * @return
- */
- public static JSONObject serializeToJsonObject(Object bean) {
- Object result = serialize(bean);
- JSONObject jsonObj = new JSONObject();
- try {
- if (ObjectUtils.isArray(bean) || result instanceof JSONArray) {
- jsonObj.put("array", result);
- } else if (result instanceof JSONObject) {
- jsonObj = (JSONObject) result;
- } else {
- jsonObj.put(bean.getClass().getSimpleName(), result);
- }
- } catch (JSONException e) {
- logger.error("JSONException ",e);
- }
- return jsonObj;
- }
-
- /**
- * @param bean
- * @return
- */
- @SuppressWarnings("unchecked")
- public static Object serialize(Object bean) {
- //String simpleName = bean.getClass().getSimpleName();
- Object result = JSONNull.getInstance();
-
- if (isObjectJson(bean)) {
- result = bean;
- } else if (bean != null && isConvertable(bean.getClass())) {
- //logger.info("Converting convertable Class: "+simpleName);
- result = ConvertUtils.convert(bean);
- } else if (bean != null && Time.class.isAssignableFrom(bean.getClass())) {
- //logger.info("Converting Time Class: "+simpleName);
- result = TimeConverter.toUnixTime((Time) bean);
- } else if (bean != null && Day.class.isAssignableFrom(bean.getClass())) {
- //logger.info("Converting Day Class: "+simpleName);
- result = DayConverter.toUnixTime((Day) bean);
- } else if (bean != null
- && java.util.Date.class.isAssignableFrom(bean.getClass())) {
- // for date consistency, use the XMLGregorianCalendar
- // result =
- // DateConverter.toXMLGregorianCalendar((java.util.Date)bean
- // ).toString();
- //logger.info("Converting Date Class: "+simpleName);
- result = DateConverter.toFormattedString((java.util.Date) bean);
- } else if (bean != null && ObjectUtils.isComplex(bean)
- && !ObjectUtils.isArray(bean) && !ObjectUtils.isMap(bean)) {
- //logger.info("Converting complex bean: "+simpleName);
- JSONObject jsonObject = new JSONObject();
- try {
- PropertyDescriptor[] pds = PropertyUtils
- .getPropertyDescriptors(bean);
- for (int i = 0; i < pds.length; i++) {
- String key = pds[i].getName();
- if ("class".equals(key)) {
- continue;
- }
-
- Class type = pds[i].getPropertyType();
- Object value = PropertyUtils.getProperty(bean, key);
-
- if (String.class.isAssignableFrom(type)) {
- jsonObject.put(key, (value == null) ? "" : value);
- } else if (ObjectUtils.isArray(value)) {
- jsonObject.put(key, serialize(value));
- } else if (value == null) {
- jsonObject.put(key, JSONNull.getInstance());
- } else if (ObjectUtils.isSimpleType(value) || type.isEnum()) {
- if (ClassUtils.isXmlEnum(type)) {
- jsonObject.put(key, ObjectUtils.getXmlEnumValue(value));
- } else {
- jsonObject.put(key, value);
- }
- } else {
- jsonObject.put(key, serialize(value));
- }
- }
-
- result = jsonObject;
- } catch (IllegalAccessException e) {
- logger.error("IllegalAccessException ", e);
- } catch (InvocationTargetException e) {
- logger.error("InvocationTargetException ", e);
- } catch (NoSuchMethodException e) {
- logger.error("NoSuchMethodException ", e);
- } catch (JSONException e) {
- logger.error("JSONException ", e);
- }
- } else if (ObjectUtils.isArray(bean)) {
- //logger.info("Converting Array bean: "+simpleName);
- Collection collection = null;
- if (bean.getClass().isArray()) {
- collection = Arrays.asList((Object[]) bean);
- } else {
- collection = (Collection) bean;
- }
-
- result = new JSONArray();
- for (Object item : collection) {
- ((JSONArray) result).add(serialize(item));
- }
- } else if (ObjectUtils.isMap(bean)) {
- //logger.info("Converting Map bean: "+simpleName);
- Map map = (Map) bean;
- result = new JSONObject();
- for (Object key : map.keySet()) {
- try {
- ((JSONObject) result).put(key.toString(),
- serialize(map.get(key)));
- } catch (JSONException e) {
- logger.error("JSONException ",e);
- }
- }
- } else if (bean != null && ClassUtils.isXmlEnum(bean.getClass())) {
- //logger.info("converting xmlEnum bean: "+simpleName);
- result = ObjectUtils.getXmlEnumValue(bean);
- } else {
- //logger.info("just returning the bean: "+simpleName);
- result = bean;
- }
- return result;
- }
-
- /**
- * @param clazz
- * @return
- */
- @SuppressWarnings("unchecked")
- private static boolean isConvertable(Class clazz) {
- boolean found = false;
- for (Class convertableClass : CONVERTABLE_CLASSES) {
- if (convertableClass.isAssignableFrom(clazz)) {
- found = true;
- break;
- } else {
- //logger.warn("Class is not convertable");
- //logger.warn("Class: " + clazz.getSimpleName());
- }
- }
- return found;
- }
-
- /**
- * @param bean
- * @return
- */
- private static boolean isObjectJson(Object bean) {
- if ((JSONNull.getInstance().equals(bean)) || (bean instanceof JSONObject)
- || (bean instanceof JSONArray)) {
- return true;
- }
- return false;
- }
-}
diff --git a/src/edu/cornell/mannlib/vitro/webapp/servlet/ConceptSearchServlet.java b/src/edu/cornell/mannlib/vitro/webapp/servlet/ConceptSearchServlet.java
index 04a0aca0..38bdf751 100644
--- a/src/edu/cornell/mannlib/vitro/webapp/servlet/ConceptSearchServlet.java
+++ b/src/edu/cornell/mannlib/vitro/webapp/servlet/ConceptSearchServlet.java
@@ -11,16 +11,16 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import net.sf.json.JSONObject;
-
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
import edu.cornell.mannlib.semservices.bo.Concept;
import edu.cornell.mannlib.semservices.bo.ConceptInfo;
import edu.cornell.mannlib.semservices.bo.SemanticServicesError;
-import edu.cornell.mannlib.semservices.view.BeanToJsonSerializer;
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.utils.ConceptSearchService.ConceptSearchServiceUtils;
@@ -58,7 +58,9 @@ public class ConceptSearchServlet extends VitroHttpServlet {
conceptInfo.setSemanticServicesError(semanticServicesError);
}
conceptInfo.setConceptList(results);
+
String json = renderJson(conceptInfo);
+
json = StringUtils.replaceChars(json, "\r\t\n", "");
PrintWriter writer = resp.getWriter();
resp.setContentType("application/json");
@@ -70,12 +72,18 @@ public class ConceptSearchServlet extends VitroHttpServlet {
}
}
+
protected String renderJson(ConceptInfo conceptInfo) {
+
+ ObjectMapper mapper = new ObjectMapper();
+ try {
+ return mapper.writeValueAsString(conceptInfo);
+ } catch (JsonProcessingException e) {
+ // TODO Auto-generated catch block
+ log.error("An error occurred in rendering conceptInfo as json ", e);
+ return null;
+ }
+ }
- JSONObject jsonObject = null;
- jsonObject = BeanToJsonSerializer.serializeToJsonObject(conceptInfo);
- log.debug(jsonObject.toString());
- return jsonObject.toString();
- }
}