1
2
3
4
5
6
7
8
9
10
11
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
35
36
37
38
39
40
41
42
43
44
45
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 }