|
9 | 9 | */ |
10 | 10 | package org.openmrs.module.reporting.serializer; |
11 | 11 |
|
12 | | -import java.io.OutputStream; |
13 | | -import java.io.OutputStreamWriter; |
14 | | -import java.io.UnsupportedEncodingException; |
15 | | -import java.lang.reflect.Method; |
16 | | - |
17 | | -import org.openmrs.api.APIException; |
18 | | -import org.openmrs.api.context.Context; |
19 | | -import org.openmrs.module.VersionComparator; |
20 | | -import org.openmrs.module.serialization.xstream.XStreamShortSerializer; |
21 | | -import org.openmrs.module.serialization.xstream.mapper.CGLibMapper; |
22 | | -import org.openmrs.module.serialization.xstream.mapper.HibernateCollectionMapper; |
23 | | -import org.openmrs.module.serialization.xstream.mapper.JavassistMapper; |
24 | | -import org.openmrs.module.serialization.xstream.mapper.NullValueMapper; |
25 | | -import org.openmrs.serialization.SerializationException; |
26 | | -import org.openmrs.serialization.SimpleXStreamSerializer; |
27 | | - |
28 | 12 | import com.thoughtworks.xstream.XStream; |
29 | 13 | import com.thoughtworks.xstream.converters.ConverterLookup; |
30 | 14 | import com.thoughtworks.xstream.converters.DataHolder; |
|
33 | 17 | import com.thoughtworks.xstream.io.xml.DomDriver; |
34 | 18 | import com.thoughtworks.xstream.mapper.Mapper; |
35 | 19 | import com.thoughtworks.xstream.mapper.MapperWrapper; |
36 | | -import org.openmrs.util.OpenmrsConstants; |
| 20 | +import org.apache.commons.logging.Log; |
| 21 | +import org.apache.commons.logging.LogFactory; |
| 22 | +import org.openmrs.api.context.Context; |
| 23 | +import org.openmrs.module.serialization.xstream.XStreamShortSerializer; |
| 24 | +import org.openmrs.module.serialization.xstream.mapper.CGLibMapper; |
| 25 | +import org.openmrs.module.serialization.xstream.mapper.HibernateCollectionMapper; |
| 26 | +import org.openmrs.module.serialization.xstream.mapper.JavassistMapper; |
| 27 | +import org.openmrs.module.serialization.xstream.mapper.NullValueMapper; |
| 28 | +import org.openmrs.serialization.SerializationException; |
| 29 | +import org.openmrs.serialization.SimpleXStreamSerializer; |
37 | 30 |
|
| 31 | +import java.io.OutputStream; |
| 32 | +import java.io.OutputStreamWriter; |
| 33 | +import java.io.UnsupportedEncodingException; |
| 34 | +import java.lang.reflect.Method; |
38 | 35 |
|
39 | 36 | public class ReportingSerializer extends XStreamShortSerializer { |
40 | 37 |
|
41 | 38 | private static ThreadLocal<DataHolder> cache = new ThreadLocal<DataHolder>(); |
| 39 | + |
| 40 | + private final Log log = LogFactory.getLog(this.getClass()); |
| 41 | + |
| 42 | + private boolean xstreamSecuritySetup = false; |
42 | 43 |
|
43 | 44 | /** |
44 | 45 | * @throws SerializationException |
@@ -87,15 +88,14 @@ public Object unmarshal(HierarchicalStreamReader reader, Object root) { |
87 | 88 | xstream.registerConverter(new IndicatorConverter(mapper, converterLookup)); |
88 | 89 |
|
89 | 90 | xstream.registerConverter(new ReportDefinitionConverter(mapper, converterLookup)); |
90 | | - |
91 | | - // Only setup XStreamSecurity only on versions that are after 2.7.0 |
92 | | - if (new VersionComparator().compare(OpenmrsConstants.OPENMRS_VERSION, "2.7.0") >= 0) { |
93 | | - setupXStreamSecurity(xstream); |
94 | | - } |
95 | 91 | } |
96 | 92 |
|
97 | 93 | @Override |
98 | 94 | synchronized public <T> T deserialize(String serializedObject, Class<? extends T> clazz) throws SerializationException { |
| 95 | + if (!xstreamSecuritySetup) { |
| 96 | + setupXStreamSecurity(); |
| 97 | + xstreamSecuritySetup = true; |
| 98 | + } |
99 | 99 | boolean cacheOwner = cache.get() == null; |
100 | 100 | if (cacheOwner) { |
101 | 101 | cache.set(new MapBackedDataHolder()); |
@@ -123,21 +123,31 @@ public void serializeToStream(Object object, OutputStream out) { |
123 | 123 | } |
124 | 124 | } |
125 | 125 |
|
126 | | - private void setupXStreamSecurity(XStream xstream) throws SerializationException { |
| 126 | + /** |
| 127 | + * Sets up xstream security on the Reporting Serializer to match the OpenMRS core security configuration |
| 128 | + */ |
| 129 | + public void setupXStreamSecurity() throws SerializationException { |
| 130 | + log.debug("Setting up xstream security on ReportingSerializer"); |
| 131 | + SimpleXStreamSerializer serializer = null; |
127 | 132 | try { |
128 | | - SimpleXStreamSerializer serializer = Context.getRegisteredComponent("simpleXStreamSerializer", SimpleXStreamSerializer.class); |
129 | | - if (serializer != null) { |
130 | | - try { |
131 | | - Method method = serializer.getClass().getMethod("initXStream", XStream.class); |
132 | | - method.invoke(serializer, xstream); |
133 | | - } |
134 | | - catch (Exception ex) { |
135 | | - throw new SerializationException("Failed to set up XStream Security", ex); |
136 | | - } |
137 | | - } |
| 133 | + serializer = Context.getRegisteredComponent("simpleXStreamSerializer", SimpleXStreamSerializer.class); |
| 134 | + } |
| 135 | + catch (Exception ignored) { |
| 136 | + } |
| 137 | + if (serializer == null) { |
| 138 | + log.debug("Not setting up XStream security as no simpleXStreamSerializer component is found"); |
| 139 | + return; |
| 140 | + } |
| 141 | + try { |
| 142 | + Method method = serializer.getClass().getMethod("initXStream", XStream.class); |
| 143 | + method.invoke(serializer, xstream); |
| 144 | + log.info("XStream security initialized on ReportingSerializer"); |
| 145 | + } |
| 146 | + catch (NoSuchMethodException ignored) { |
| 147 | + log.debug("Not setting up XStream Security as no initXStream method found on SimpleXStreamSerializer"); |
138 | 148 | } |
139 | | - catch (APIException ex) { |
140 | | - //Ignore APIException("Error during getting registered component) for platform versions below 2.7.0 |
| 149 | + catch (Exception e) { |
| 150 | + throw new SerializationException("Failed to set up XStream Security on Reporting Serializer", e); |
141 | 151 | } |
142 | 152 | } |
143 | 153 | } |
0 commit comments