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