1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.job.internal.xstream

File SafeMessageConverter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

14
51
7
1
165
107
18
0.35
7.29
7
2.57

Classes

Class Line # Actions
SafeMessageConverter 43 51 0% 18 5
0.930555693.1%
 

Contributing tests

This file is covered by 189 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.job.internal.xstream;
21   
22    import java.util.ArrayList;
23    import java.util.Collections;
24    import java.util.List;
25   
26    import org.slf4j.Marker;
27    import org.xwiki.job.annotation.Serializable;
28    import org.xwiki.logging.Message;
29    import org.xwiki.logging.event.LogEvent;
30   
31    import com.thoughtworks.xstream.converters.MarshallingContext;
32    import com.thoughtworks.xstream.converters.UnmarshallingContext;
33    import com.thoughtworks.xstream.core.util.HierarchicalStreams;
34    import com.thoughtworks.xstream.io.HierarchicalStreamReader;
35    import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
36   
37    /**
38    * Filter {@link LogEvent} arguments allowed to be serialized.
39    *
40    * @version $Id: cb71811dda8ae5774921e2c4283126d2499d12b6 $
41    * @since 8.4RC1
42    */
 
43    public class SafeMessageConverter extends SafeArrayConverter
44    {
45    protected static final String FIELD_MESSAGE = "message";
46   
47    protected static final String FIELD_MARKER = "marker";
48   
49    protected static final String FIELD_ARGUMENTARRAY = "argumentArray";
50   
51    protected static final String FIELD_THROWABLE = "throwable";
52   
53    /**
54    * @param xstream the {@link com.thoughtworks.xstream.XStream} instance to use to isolate array element marshaling
55    */
 
56  926 toggle public SafeMessageConverter(SafeXStream xstream)
57    {
58  926 super(xstream);
59    }
60   
 
61  1807 toggle @Override
62    public boolean canConvert(Class type)
63    {
64  1808 return type == Message.class;
65    }
66   
 
67  912 toggle @Override
68    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context)
69    {
70  912 Message message = (Message) source;
71   
72    // Message
73  912 XStreamUtils.serializeField(FIELD_MESSAGE, String.class, message.getMessage(), writer, context, mapper());
74   
75    // Marker
76  912 XStreamUtils.serializeField(FIELD_MARKER, Marker.class, message.getMarker(), writer, context, mapper());
77   
78    // Log arguments
79  912 if (message.getArgumentArray() != null) {
80  800 writer.startNode(FIELD_ARGUMENTARRAY);
81  800 for (Object argument : message.getArgumentArray()) {
82  1014 if (isSerializable(argument)) {
83  606 writeItem(argument, context, writer);
84    } else {
85  408 writeItem(argument.toString(), context, writer);
86    }
87    }
88  800 writer.endNode();
89    }
90   
91    // Throwable
92  912 XStreamUtils.serializeField(FIELD_THROWABLE, Throwable.class, message.getThrowable(), writer, context,
93    mapper());
94    }
95   
 
96  1014 toggle protected boolean isSerializable(Object argument)
97    {
98  1014 if (argument == null) {
99  108 return true;
100    }
101   
102  906 Serializable serializable = argument.getClass().getAnnotation(Serializable.class);
103  906 if (serializable != null) {
104  12 return serializable.value();
105    } else {
106  894 return argument instanceof java.io.Serializable;
107    }
108   
109    // TODO: Add white list or is Serializable interface and @Serializable annotation enough ?
110    }
111   
 
112  24 toggle @Override
113    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context)
114    {
115  24 Marker marker = null;
116  24 String message = "";
117  24 List<Object> arguments = Collections.emptyList();
118  24 Throwable throwable = null;
119   
120  96 while (reader.hasMoreChildren()) {
121  72 reader.moveDown();
122  72 switch (reader.getNodeName()) {
123  24 case FIELD_MARKER:
124  24 marker = read(Marker.class, reader, context);
125  24 break;
126  24 case FIELD_MESSAGE:
127  24 message = reader.getValue();
128  24 break;
129  0 case FIELD_THROWABLE:
130  0 throwable = read(Throwable.class, reader, context);
131  0 break;
132  24 case FIELD_ARGUMENTARRAY:
133  24 arguments = unmarshalArgumentArray(reader, context);
134  24 break;
135  0 default:
136  0 break;
137    }
138  72 reader.moveUp();
139    }
140   
141  24 return new Message(marker, message, arguments.toArray(), throwable);
142    }
143   
 
144  138 toggle protected List<Object> unmarshalArgumentArray(HierarchicalStreamReader reader, UnmarshallingContext context)
145    {
146  138 List<Object> arguments = new ArrayList<>();
147   
148  278 while (reader.hasMoreChildren()) {
149  140 reader.moveDown();
150  140 Object argument = readItem(reader, context, null);
151  140 arguments.add(argument);
152  140 reader.moveUp();
153    }
154   
155  138 return arguments;
156    }
157   
 
158  160 toggle protected <T> T read(Class<T> defaultType, HierarchicalStreamReader reader, UnmarshallingContext context)
159    {
160  160 String classAttribute = HierarchicalStreams.readClassAttribute(reader, mapper());
161   
162  160 return (T) context.convertAnother(null,
163  160 classAttribute != null ? mapper().realClass(classAttribute) : defaultType);
164    }
165    }