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          log.debug("Successfully encoded message.");
53          if(protocolMessageLog.isDebugEnabled() && messageContext.getOutboundMessage() != null){
54              protocolMessageLog.debug("\n" + XMLHelper.prettyPrintXML(messageContext.getOutboundMessage().getDOM()));
55          }
56      }
57  
58      /**
59       * Encodes the outbound message onto the outbound transport.
60       * 
61       * @param messageContext current message context
62       * 
63       * @throws MessageEncodingException thrown if there is a problem encoding the message
64       */
65      protected abstract void doEncode(MessageContext messageContext) throws MessageEncodingException;
66  
67      /**
68       * Helper method that marshalls the given message.
69       * 
70       * @param message message the marshall and serialize
71       * 
72       * @return marshalled message
73       * 
74       * @throws MessageEncodingException thrown if the give message can not be marshalled into its DOM representation
75       */
76      protected Element marshallMessage(XMLObject message) throws MessageEncodingException {
77          log.debug("Marshalling message");
78  
79          try {
80              Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(message);
81              if (marshaller == null) {
82                  log.error("Unable to marshall message, no marshaller registered for message object: "
83                          + message.getElementQName());
84              }
85              Element messageElem = marshaller.marshall(message);
86              if (log.isTraceEnabled()) {
87                  log.trace("Marshalled message into DOM:\n{}", XMLHelper.nodeToString(messageElem));
88              }
89              return messageElem;
90          } catch (MarshallingException e) {
91              log.error("Encountered error marshalling message to its DOM representation", e);
92              throw new MessageEncodingException("Encountered error marshalling message into its DOM representation", e);
93          }
94      }
95  }