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