View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
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.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   * Base class for message decoders.
40   */
41  public abstract class BaseMessageDecoder implements MessageDecoder {
42      
43      /** Used to log protocol messages. */
44      private Logger protocolMessageLog = LoggerFactory.getLogger("PROTOCOL_MESSAGE");
45  
46      /** Class logger. */
47      private final Logger log = LoggerFactory.getLogger(BaseMessageDecoder.class);
48  
49      /** Parser pool used to deserialize the message. */
50      private ParserPool parserPool;
51  
52      /** Constructor. */
53      public BaseMessageDecoder() {
54          parserPool = new BasicParserPool();
55      }
56  
57      /**
58       * Constructor.
59       * 
60       * @param pool parser pool used to deserialize messages
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      /** {@inheritDoc} */
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          doDecode(messageContext);
75  
76          SecurityPolicyResolver policyResolver = messageContext.getSecurityPolicyResolver();
77          if (policyResolver != null) {
78              Iterable<SecurityPolicy> securityPolicies = policyResolver.resolve(messageContext);
79              if (securityPolicies != null) {
80                  for (SecurityPolicy policy : securityPolicies) {
81                      if (policy != null) {
82                          log.debug("Evaluating security policy of type '{}' for decoded message", policy.getClass()
83                                  .getName());
84                          policy.evaluate(messageContext);
85                      }
86                  }
87              } else {
88                  log.debug("No security policy resolved for this message context, no security policy evaluation attempted");
89              }
90          } else {
91              log.debug("No security policy resolver attached to this message context, no security policy evaluation attempted");
92          }
93  
94          log.debug("Successfully decoded message.");
95          if(protocolMessageLog.isDebugEnabled() && messageContext.getInboundMessage() != null){
96              protocolMessageLog.debug("\n" + XMLHelper.prettyPrintXML(messageContext.getInboundMessage().getDOM()));
97          }
98      }
99  
100     /**
101      * Decodes a message, updating the message context. Security policy evaluation is handled outside this method.
102      * 
103      * @param messageContext current message context
104      * 
105      * @throws MessageDecodingException thrown if there is a problem decoding the message
106      */
107     protected abstract void doDecode(MessageContext messageContext) throws MessageDecodingException;
108 
109     /**
110      * Gets the parser pool used to deserialize incomming messages.
111      * 
112      * @return parser pool used to deserialize incomming messages
113      */
114     protected ParserPool getParserPool() {
115         return parserPool;
116     }
117 
118     /**
119      * Sets the parser pool used to deserialize incomming messages.
120      * 
121      * @param pool parser pool used to deserialize incomming messages
122      */
123     protected void setParserPool(ParserPool pool) {
124         if (pool == null) {
125             throw new IllegalArgumentException("Parser pool may not be null");
126         }
127         parserPool = pool;
128     }
129 
130     /**
131      * Helper method that deserializes and unmarshalls the message from the given stream.
132      * 
133      * @param messageStream input stream containing the message
134      * 
135      * @return the inbound message
136      * 
137      * @throws MessageDecodingException thrown if there is a problem deserializing and unmarshalling the message
138      */
139     protected XMLObject unmarshallMessage(InputStream messageStream) throws MessageDecodingException {
140         log.debug("Parsing message stream into DOM document");
141 
142         try {
143             Document messageDoc = parserPool.parse(messageStream);
144             Element messageElem = messageDoc.getDocumentElement();
145 
146             if (log.isTraceEnabled()) {
147                 log.trace("Resultant DOM message was:\n{}", XMLHelper.nodeToString(messageElem));
148             }
149 
150             log.debug("Unmarshalling message DOM");
151             Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(messageElem);
152             if (unmarshaller == null) {
153                 log.error("Unable to unmarshall message, no unmarshaller registered for message element "
154                         + XMLHelper.getNodeQName(messageElem));
155                 throw new MessageDecodingException(
156                         "Unable to unmarshall message, no unmarshaller registered for message element "
157                                 + XMLHelper.getNodeQName(messageElem));
158             }
159 
160             XMLObject message = unmarshaller.unmarshall(messageElem);
161 
162             log.debug("Message succesfully unmarshalled");
163             return message;
164         } catch (XMLParserException e) {
165             log.error("Encountered error parsing message into its DOM representation", e);
166             throw new MessageDecodingException("Encountered error parsing message into its DOM representation", e);
167         } catch (UnmarshallingException e) {
168             log.error("Encountered error unmarshalling message from its DOM representation", e);
169             throw new MessageDecodingException("Encountered error unmarshalling message from its DOM representation", e);
170         }
171     }
172 }