View Javadoc

1   /*
2    * Copyright 2009 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.handler;
18  
19  import org.opensaml.ws.message.MessageContext;
20  import org.opensaml.ws.message.decoder.BaseMessageDecoder;
21  import org.opensaml.ws.message.decoder.MessageDecodingException;
22  import org.opensaml.xml.parse.ParserPool;
23  import org.opensaml.xml.security.SecurityException;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * Base class for message decoders which are capable of processing the message context's inbound {@link HandlerChain}.
29   */
30  public abstract class BaseHandlerChainAwareMessageDecoder extends BaseMessageDecoder implements HandlerChainAware {
31      
32      /** Class logger. */
33      private final Logger log = LoggerFactory.getLogger(BaseHandlerChainAwareMessageDecoder.class);
34      
35      /**
36       * Constructor.
37       *
38       */
39      public BaseHandlerChainAwareMessageDecoder() {
40          super();
41      }
42  
43      /**
44       * Constructor.
45       *
46       * @param pool parser pool used to deserialize messages
47       */
48      public BaseHandlerChainAwareMessageDecoder(ParserPool pool) {
49          super(pool);
50      }
51  
52      /** {@inheritDoc} */
53      public void decode(MessageContext messageContext) throws MessageDecodingException, SecurityException {
54          log.debug("Beginning to decode message from inbound transport of type: {}", messageContext
55                  .getInboundMessageTransport().getClass().getName());
56          
57          doDecode(messageContext);
58          
59          logDecodedMessage(messageContext);
60  
61          processPreSecurityInboundHandlerChain(messageContext);
62          log.debug("Successfully processed pre-SecurityPolicy inbound handler chain.");
63          
64          processSecurityPolicy(messageContext);
65  
66          processPostSecurityInboundHandlerChain(messageContext);
67          log.debug("Successfully processed post-SecurityPolicy inbound handler chain.");
68          
69          log.debug("Successfully decoded message.");
70      }
71      
72      /**
73       * Process the pre-SecurityPolicy inbound {@link HandlerChain} for the message context, if any.
74       * 
75       * @param messageContext the message context to process
76       * @throws MessageDecodingException thrown if a handler indicates a problem handling the message
77       */
78      protected void processPreSecurityInboundHandlerChain(MessageContext messageContext)
79              throws MessageDecodingException {
80          HandlerChainResolver inboundHandlerChainResolver = messageContext.getPreSecurityInboundHandlerChainResolver();
81          if (inboundHandlerChainResolver != null) {
82              log.debug("Invoking pre-SecurityPolicy inbound handler chain on message context");
83              try {
84                  for (HandlerChain inboundHandlerChain : inboundHandlerChainResolver.resolve(messageContext)) {
85                      if (inboundHandlerChain != null) {
86                          invokeHandlerChain(inboundHandlerChain, messageContext);
87                      }
88                  }
89              } catch (HandlerException e) {
90                  log.error("Encountered pre-SecurityPolicy HandlerException when decoding message: {}", e.getMessage());
91                  throw new MessageDecodingException("Pre-SecurityPolicy Handler exception while decoding message", e);
92              }
93          }
94      }
95      
96      /**
97       * Process the post-SecurityPolicy inbound {@link HandlerChain} for the message context, if any.
98       * 
99       * @param messageContext the message context to process
100      * @throws MessageDecodingException thrown if a handler indicates a problem handling the message
101      */
102     protected void processPostSecurityInboundHandlerChain(MessageContext messageContext)
103             throws MessageDecodingException {
104         HandlerChainResolver inboundHandlerChainResolver = messageContext.getPostSecurityInboundHandlerChainResolver();
105         if (inboundHandlerChainResolver != null) {
106             log.debug("Invoking post-SecurityPolicy inbound handler chain on message context");
107             try {
108                 for (HandlerChain inboundHandlerChain : inboundHandlerChainResolver.resolve(messageContext)) {
109                     if (inboundHandlerChain != null) {
110                         invokeHandlerChain(inboundHandlerChain, messageContext);
111                     }
112                 }
113             } catch (HandlerException e) {
114                 log.error("Encountered post-SecurityPolicy HandlerException when decoding message: {}", e.getMessage());
115                 throw new MessageDecodingException("Handler exception while decoding message", e);
116             }
117         }
118     }
119     
120     /**
121      * Invoke a handler chain on the specified message context.
122      * 
123      * @param handlerChain the handle chain to invoke
124      * @param messageContext the message context to process
125      * 
126      * @throws HandlerException if handler chain encountered a problem handling the message context
127      */
128     protected void invokeHandlerChain(HandlerChain handlerChain, MessageContext messageContext)
129             throws HandlerException {
130         if (handlerChain != null && messageContext != null) {
131             handlerChain.invoke(messageContext);
132         }
133     }
134 
135 }