1. Project Clover database Sat Feb 2 2019 06:45:20 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 209 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: dbcf5793673c3d3a7f54bd3b68b02f6aa7fdf649 $
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  1106 toggle public SafeMessageConverter(SafeXStream xstream)
57    {
58  1106 super(xstream);
59    }
60   
 
61  2705 toggle @Override
62    public boolean canConvert(Class type)
63    {
64  2712 return type == Message.class;
65    }
66   
 
67  4422 toggle @Override
68    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context)
69    {
70  4422 Message message = (Message) source;
71   
72    // Message
73  4422 XStreamUtils.serializeField(FIELD_MESSAGE, String.class, message.getMessage(), writer, context, mapper());
74   
75    // Marker
76  4422 XStreamUtils.serializeField(FIELD_MARKER, Marker.class, message.getMarker(), writer, context, mapper());
77   
78    // Log arguments
79  4422 if (message.getArgumentArray() != null) {
80  4025 writer.startNode(FIELD_ARGUMENTARRAY);
81  4025 for (Object argument : message.getArgumentArray()) {
82  4962 if (isSerializable(argument)) {
83  1671 writeCompleteItem(argument, context, writer);
84    } else {
85  3291 writeCompleteItem(argument.toString(), context, writer);
86    }
87    }
88  4024 writer.endNode();
89    }
90   
91    // Throwable
92  4422 XStreamUtils.serializeField(FIELD_THROWABLE, Throwable.class, message.getThrowable(), writer, context,
93    mapper());
94    }
95   
 
96  4962 toggle protected boolean isSerializable(Object argument)
97    {
98  4962 if (argument == null) {
99  247 return true;
100    }
101   
102  4715 Serializable serializable = argument.getClass().getAnnotation(Serializable.class);
103  4715 if (serializable != null) {
104  12 return serializable.value();
105    } else {
106  4703 return argument instanceof java.io.Serializable;
107    }
108   
109    // TODO: Add white list or is Serializable interface and @Serializable annotation enough ?
110    }
111   
 
112  23 toggle @Override
113    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context)
114    {
115  23 Marker marker = null;
116  23 String message = "";
117  23 List<Object> arguments = Collections.emptyList();
118  23 Throwable throwable = null;
119   
120  92 while (reader.hasMoreChildren()) {
121  69 reader.moveDown();
122  69 switch (reader.getNodeName()) {
123  23 case FIELD_MARKER:
124  23 marker = read(Marker.class, reader, context);
125  23 break;
126  23 case FIELD_MESSAGE:
127  23 message = reader.getValue();
128  23 break;
129  0 case FIELD_THROWABLE:
130  0 throwable = read(Throwable.class, reader, context);
131  0 break;
132  23 case FIELD_ARGUMENTARRAY:
133  23 arguments = unmarshalArgumentArray(reader, context);
134  23 break;
135  0 default:
136  0 break;
137    }
138  69 reader.moveUp();
139    }
140   
141  23 return new Message(marker, message, arguments.toArray(), throwable);
142    }
143   
 
144  137 toggle protected List<Object> unmarshalArgumentArray(HierarchicalStreamReader reader, UnmarshallingContext context)
145    {
146  137 List<Object> arguments = new ArrayList<>();
147   
148  276 while (reader.hasMoreChildren()) {
149  139 reader.moveDown();
150  139 Object argument = readBareItem(reader, context, null);
151  139 arguments.add(argument);
152  139 reader.moveUp();
153    }
154   
155  137 return arguments;
156    }
157   
 
158  159 toggle protected <T> T read(Class<T> defaultType, HierarchicalStreamReader reader, UnmarshallingContext context)
159    {
160  159 String classAttribute = HierarchicalStreams.readClassAttribute(reader, mapper());
161   
162  159 return (T) context.convertAnother(null,
163  159 classAttribute != null ? mapper().realClass(classAttribute) : defaultType);
164    }
165    }