1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.ws.message.decoder;
18
19 import java.io.InputStream;
20
21 import org.opensaml.ws.message.MessageContext;
22 import org.opensaml.ws.security.SecurityPolicy;
23 import org.opensaml.ws.security.SecurityPolicyResolver;
24 import org.opensaml.xml.Configuration;
25 import org.opensaml.xml.XMLObject;
26 import org.opensaml.xml.io.Unmarshaller;
27 import org.opensaml.xml.io.UnmarshallingException;
28 import org.opensaml.xml.parse.BasicParserPool;
29 import org.opensaml.xml.parse.ParserPool;
30 import org.opensaml.xml.parse.XMLParserException;
31 import org.opensaml.xml.security.SecurityException;
32 import org.opensaml.xml.util.XMLHelper;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37
38
39
40
41 public abstract class BaseMessageDecoder implements MessageDecoder {
42
43
44 private Logger protocolMessageLog = LoggerFactory.getLogger("PROTOCOL_MESSAGE");
45
46
47 private final Logger log = LoggerFactory.getLogger(BaseMessageDecoder.class);
48
49
50 private ParserPool parserPool;
51
52
53 public BaseMessageDecoder() {
54 parserPool = new BasicParserPool();
55 }
56
57
58
59
60
61
62 public BaseMessageDecoder(ParserPool pool) {
63 if (pool == null) {
64 throw new IllegalArgumentException("Parser pool may not be null");
65 }
66
67 parserPool = pool;
68 }
69
70
71 public void decode(MessageContext messageContext) throws MessageDecodingException, SecurityException {
72 log.debug("Beginning to decode message from inbound transport of type: {}", messageContext
73 .getInboundMessageTransport().getClass().getName());
74
75 doDecode(messageContext);
76
77 logDecodedMessage(messageContext);
78
79 processSecurityPolicy(messageContext);
80
81 log.debug("Successfully decoded message.");
82 }
83
84
85
86
87
88
89 protected void logDecodedMessage(MessageContext messageContext) {
90 if(protocolMessageLog.isDebugEnabled() && messageContext.getInboundMessage() != null){
91 protocolMessageLog.debug("\n" + XMLHelper.prettyPrintXML(messageContext.getInboundMessage().getDOM()));
92 }
93 }
94
95
96
97
98
99
100
101 protected void processSecurityPolicy(MessageContext messageContext) throws SecurityException {
102 SecurityPolicyResolver policyResolver = messageContext.getSecurityPolicyResolver();
103 if (policyResolver != null) {
104 Iterable<SecurityPolicy> securityPolicies = policyResolver.resolve(messageContext);
105 if (securityPolicies != null) {
106 for (SecurityPolicy policy : securityPolicies) {
107 if (policy != null) {
108 log.debug("Evaluating security policy of type '{}' for decoded message", policy.getClass()
109 .getName());
110 policy.evaluate(messageContext);
111 }
112 }
113 } else {
114 log.debug("No security policy resolved for this message context, no security policy evaluation attempted");
115 }
116 } else {
117 log.debug("No security policy resolver attached to this message context, no security policy evaluation attempted");
118 }
119 }
120
121
122
123
124
125
126
127
128 protected abstract void doDecode(MessageContext messageContext) throws MessageDecodingException;
129
130
131
132
133
134
135 protected ParserPool getParserPool() {
136 return parserPool;
137 }
138
139
140
141
142
143
144 protected void setParserPool(ParserPool pool) {
145 if (pool == null) {
146 throw new IllegalArgumentException("Parser pool may not be null");
147 }
148 parserPool = pool;
149 }
150
151
152
153
154
155
156
157
158
159
160 protected XMLObject unmarshallMessage(InputStream messageStream) throws MessageDecodingException {
161 log.debug("Parsing message stream into DOM document");
162
163 try {
164 Document messageDoc = parserPool.parse(messageStream);
165 Element messageElem = messageDoc.getDocumentElement();
166
167 if (log.isTraceEnabled()) {
168 log.trace("Resultant DOM message was:\n{}", XMLHelper.nodeToString(messageElem));
169 }
170
171 log.debug("Unmarshalling message DOM");
172 Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(messageElem);
173 if (unmarshaller == null) {
174 log.error("Unable to unmarshall message, no unmarshaller registered for message element "
175 + XMLHelper.getNodeQName(messageElem));
176 throw new MessageDecodingException(
177 "Unable to unmarshall message, no unmarshaller registered for message element "
178 + XMLHelper.getNodeQName(messageElem));
179 }
180
181 XMLObject message = unmarshaller.unmarshall(messageElem);
182
183 log.debug("Message succesfully unmarshalled");
184 return message;
185 } catch (XMLParserException e) {
186 log.error("Encountered error parsing message into its DOM representation", e);
187 throw new MessageDecodingException("Encountered error parsing message into its DOM representation", e);
188 } catch (UnmarshallingException e) {
189 log.error("Encountered error unmarshalling message from its DOM representation", e);
190 throw new MessageDecodingException("Encountered error unmarshalling message from its DOM representation", e);
191 }
192 }
193 }