|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.oxm.xstream; |
| 18 | + |
| 19 | +import com.thoughtworks.xstream.converters.Converter; |
| 20 | +import com.thoughtworks.xstream.converters.MarshallingContext; |
| 21 | +import com.thoughtworks.xstream.converters.UnmarshallingContext; |
| 22 | +import com.thoughtworks.xstream.io.HierarchicalStreamReader; |
| 23 | +import com.thoughtworks.xstream.io.HierarchicalStreamWriter; |
| 24 | + |
| 25 | +/** |
| 26 | + * XStream {@link Converter} that supports all classes, but throws exceptions for |
| 27 | + * (un)marshalling. |
| 28 | + * <p>Main purpose of this class is to |
| 29 | + * {@linkplain com.thoughtworks.xstream.XStream#registerConverter(com.thoughtworks.xstream.converters.Converter, int) register} |
| 30 | + * this converter as a catchall last converter with a |
| 31 | + * {@linkplain com.thoughtworks.xstream.XStream#PRIORITY_NORMAL normal} |
| 32 | + * or higher priority, in addition to converters that explicitly support the domain |
| 33 | + * classes that should be supported. As a result, default XStream converters with lower |
| 34 | + * priorities and possible security vulnerabilities do not get invoked. |
| 35 | + * <p>For instance:</p> |
| 36 | + * <pre class="code"> |
| 37 | + * XStreamMarshaller unmarshaller = new XStreamMarshaller(); |
| 38 | + * unmarshaller.getXStream().registerConverter(new MyDomainClassConverter(), XStream.PRIORITY_VERY_HIGH); |
| 39 | + * unmarshaller.getXStream().registerConverter(new CatchAllConverter(), XStream.PRIORITY_NORMAL); |
| 40 | + * MyDomainClass o = unmarshaller.unmarshal(source); |
| 41 | + * </pre |
| 42 | + * |
| 43 | + * @author Arjen Poutsma |
| 44 | + * @since 4.0 |
| 45 | + */ |
| 46 | +public class CatchAllConverter implements Converter { |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean canConvert(Class type) { |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void marshal(Object source, HierarchicalStreamWriter writer, |
| 55 | + MarshallingContext context) { |
| 56 | + throw new UnsupportedOperationException("marshalling not supported"); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Object unmarshal(HierarchicalStreamReader reader, |
| 61 | + UnmarshallingContext context) { |
| 62 | + throw new UnsupportedOperationException("unmarshalling not supported"); |
| 63 | + } |
| 64 | +} |
0 commit comments