View Javadoc
1   /*
2    * Copyright 2011-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5    * the License. You may obtain a copy of the License at
6    *
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11   * specific language governing permissions and limitations under the License.
12   */
13  package org.springframework.security.oauth2.http.converter.jaxb;
14  
15  import java.io.IOException;
16  
17  import javax.xml.bind.JAXBContext;
18  import javax.xml.bind.JAXBElement;
19  import javax.xml.bind.JAXBException;
20  import javax.xml.bind.MarshalException;
21  import javax.xml.bind.Marshaller;
22  import javax.xml.bind.UnmarshalException;
23  import javax.xml.bind.Unmarshaller;
24  import javax.xml.transform.Result;
25  import javax.xml.transform.Source;
26  
27  import org.springframework.http.HttpHeaders;
28  import org.springframework.http.converter.HttpMessageConversionException;
29  import org.springframework.http.converter.HttpMessageNotReadableException;
30  import org.springframework.http.converter.HttpMessageNotWritableException;
31  import org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter;
32  
33  /**
34   * <p>
35   * Used as a convenience for converting an external object into an Object that can be marshalled using JAXB.
36   * </p>
37   * <p>
38   * Note that the existing {@link AbstractXmlHttpMessageConverter} implementations will not work due to final methods
39   * preventing the modification of the {@link Marshaller}.
40   * </p>
41   * 
42   * @author Rob Winch
43   * 
44   * @param <I> The internal representation of the object that can be safely marshalled/unmarshalled using JAXB.
45   * @param <E> The external representation of the object that is exposed externally but cannot be marshalled/unmarshalled using JAXB.
46   */
47  abstract class AbstractJaxbMessageConverter<I, E> extends AbstractXmlHttpMessageConverter<E> {
48  
49  	private final Class<I> internalClass;
50  
51  	private final Class<E> externalClass;
52  	
53  	private final JAXBContext context;
54  
55  	public AbstractJaxbMessageConverter(Class<I> internalClass, Class<E> externalClass) {
56  		this.internalClass = internalClass;
57  		this.externalClass = externalClass;
58  		try {
59  			context = JAXBContext.newInstance(this.internalClass);
60  		}
61  		catch (JAXBException e) {
62  			throw new RuntimeException(e);
63  		}
64  	}
65  
66  	@Override
67  	protected final E readFromSource(Class<? extends E> clazz, HttpHeaders headers, Source source) throws IOException {
68  		try {
69  			JAXBElement<? extends I> jaxbElement = createUnmarshaller().unmarshal(source, internalClass);
70  			return convertToExternal(jaxbElement.getValue());
71  		}
72  		catch (UnmarshalException ex) {
73  			throw new HttpMessageNotReadableException("Could not unmarshal to [" + clazz + "]: " + ex.getMessage(), ex);
74  		}
75  		catch (JAXBException ex) {
76  			throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
77  		}
78  	}
79  
80  	@Override
81  	protected final void writeToResult(E accessToken, HttpHeaders headers, Result result) throws IOException {
82  		I convertedAccessToken = convertToInternal(accessToken);
83  		try {
84  			createMarshaller().marshal(convertedAccessToken, result);
85  		}
86  		catch (MarshalException ex) {
87  			throw new HttpMessageNotWritableException("Could not marshal [" + accessToken + "]: " + ex.getMessage(), ex);
88  		}
89  		catch (JAXBException ex) {
90  			throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
91  		}
92  	}
93  
94  	@Override
95  	protected final boolean supports(Class<?> clazz) {
96  		return this.externalClass.isAssignableFrom(clazz);
97  	}
98  
99  	protected abstract E convertToExternal(I internalValue);
100 
101 	protected abstract I convertToInternal(E externalValue);
102 	
103 	private Unmarshaller createUnmarshaller() throws JAXBException {
104 		return context.createUnmarshaller();
105 	}
106 	
107 	private Marshaller createMarshaller() throws JAXBException {
108 		Marshaller marshaller = context.createMarshaller();
109 		marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
110 		return marshaller;
111 	}
112 }