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.encoder;
18  
19  import org.opensaml.ws.message.MessageContext;
20  import org.opensaml.xml.Configuration;
21  import org.opensaml.xml.XMLObject;
22  import org.opensaml.xml.io.Marshaller;
23  import org.opensaml.xml.io.MarshallingException;
24  import org.opensaml.xml.util.XMLHelper;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.w3c.dom.Element;
28  
29  /**
30   * Base class for message decoders.
31   */
32  public abstract class BaseMessageEncoder implements MessageEncoder {
33      
34      /** Used to log protocol messages. */
35      private Logger protocolMessageLog = LoggerFactory.getLogger("PROTOCOL_MESSAGE");
36  
37      /** Class logger. */
38      private final Logger log = LoggerFactory.getLogger(BaseMessageEncoder.class);
39  
40      /** Constructor. */
41      public BaseMessageEncoder() {
42  
43      }
44  
45      /** {@inheritDoc} */
46      public void encode(MessageContext messageContext) throws MessageEncodingException {
47          log.debug("Beginning encode message to outbound transport of type: {}", messageContext
48                  .getOutboundMessageTransport().getClass().getName());
49  
50          doEncode(messageContext);
51  
52          logEncodedMessage(messageContext);
53          
54          log.debug("Successfully encoded message.");
55      }
56  
57      /**
58       * Log the encoded message to the protocol message logger.
59       * 
60       * @param messageContext the message context to process
61       */
62      protected void logEncodedMessage(MessageContext messageContext) {
63          if(protocolMessageLog.isDebugEnabled() && messageContext.getOutboundMessage() != null){
64              protocolMessageLog.debug("\n" + XMLHelper.prettyPrintXML(messageContext.getOutboundMessage().getDOM()));
65          }
66      }
67  
68      /**
69       * Encodes the outbound message onto the outbound transport.
70       * 
71       * @param messageContext current message context
72       * 
73       * @throws MessageEncodingException thrown if there is a problem encoding the message
74       */
75      protected abstract void doEncode(MessageContext messageContext) throws MessageEncodingException;
76  
77      /**
78       * Helper method that marshalls the given message.
79       * 
80       * @param message message the marshall and serialize
81       * 
82       * @return marshalled message
83       * 
84       * @throws MessageEncodingException thrown if the give message can not be marshalled into its DOM representation
85       */
86      protected Element marshallMessage(XMLObject message) throws MessageEncodingException {
87          log.debug("Marshalling message");
88  
89          try {
90              Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(message);
91              if (marshaller == null) {
92                  log.error("Unable to marshall message, no marshaller registered for message object: "
93                          + message.getElementQName());
94              }
95              Element messageElem = marshaller.marshall(message);
96              if (log.isTraceEnabled()) {
97                  log.trace("Marshalled message into DOM:\n{}", XMLHelper.nodeToString(messageElem));
98              }
99              return messageElem;
100         } catch (MarshallingException e) {
101             log.error("Encountered error marshalling message to its DOM representation", e);
102             throw new MessageEncodingException("Encountered error marshalling message into its DOM representation", e);
103         }
104     }
105 }