Package org.opensaml.messaging.encoder

# Class AbstractMessageEncoder

java.lang.Object
net.shibboleth.shared.component.AbstractInitializableComponent
org.opensaml.messaging.encoder.AbstractMessageEncoder

All Implemented Interfaces:
  `Component`, `DestructableComponent`, `InitializableComponent`, `UnmodifiableComponent`, `MessageEncoder`

Direct Known Subclasses:
  `AbstractHttpClientRequestMessageEncoder`, `AbstractHttpServletResponseMessageEncoder`

public abstract class AbstractMessageEncoder extends AbstractInitializableComponent implements MessageEncoder, UnmodifiableComponent
Abstract message encoder.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`BASE_PROTOCOL_MESSAGE_LOGGER_CATEGORY`
Logging category for protocol messages.
`private final org.slf4j.Logger`
`log`
Class logger.
`private MessageContext`
`messageContext`
The message context.
`private org.slf4j.Logger`
`protocolMessageLog`
Used to log protocol messages.
`private String`
`protocolMessageLoggerSubCategory`
The configured logging sub-category for protocol messages.

## Constructor Summary

Constructors
Constructor
Description
`AbstractMessageEncoder()`

## Method Summary

Modifier and Type
Method
Description
`protected abstract void`
`doEncode()`
Performs the encoding logic.
`protected void`
`doInitialize()`
`void`
`encode()`
Encode the `MessageContext` supplied via `MessageEncoder.setMessageContext(MessageContext)` to the sink.
`protected MessageContext`
`getMessageContext()`
Get the message context.
`protected Object`
`getMessageToLog()`
Get the XMLObject which will be logged as the protocol message.
`protected org.slf4j.Logger`
`getProtocolMessageLogger()`
Get the protocol message logger.
`protected String`
`getProtocolMessageLoggerSubCategory()`
Get the configured logging sub-category for protocol messages.
`protected void`
`logEncodedMessage()`
Log the encoded message to the protocol message logger.
`void`
`prepareContext()`
This method should prepare the message context by creating and populating any binding-specific data structures required in the MessageContext, prior to actually encoding.
`protected String`
`serializeMessageForLogging(Object message)`
Serialize the message for logging purposes.
`void`
`setMessageContext(MessageContext context)`
Set the `MessageContext` which is to be encoded.
`protected void`
`setProtocolMessageLoggerSubCategory(String category)`
Set the configured logging sub-category for protocol messages.

### Methods inherited from class net.shibboleth.shared.component.AbstractInitializableComponent

`checkComponentActive, checkSetterPreconditions, destroy, doDestroy, ifDestroyedThrowDestroyedComponentException, ifInitializedThrowUnmodifiabledComponentException, ifNotInitializedThrowUninitializedComponentException, initialize, isDestroyed, isInitialized`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface net.shibboleth.shared.component.DestructableComponent

`destroy, isDestroyed`

### Methods inherited from interface net.shibboleth.shared.component.InitializableComponent

`initialize, isInitialized`

## Field Details

### BASE_PROTOCOL_MESSAGE_LOGGER_CATEGORY

@Nonnull public static final String BASE_PROTOCOL_MESSAGE_LOGGER_CATEGORY
Logging category for protocol messages.

See Also:
  - Constant Field Values

### protocolMessageLog

@Nonnull private org.slf4j.Logger protocolMessageLog
Used to log protocol messages.

### log

@Nonnull private final org.slf4j.Logger log
Class logger.

### messageContext

@NonnullAfterInit private MessageContext messageContext
The message context.

### protocolMessageLoggerSubCategory

@Nullable private String protocolMessageLoggerSubCategory
The configured logging sub-category for protocol messages.

## Constructor Details

### AbstractMessageEncoder

public AbstractMessageEncoder()

## Method Details

### setMessageContext

public void setMessageContext(@Nullable MessageContext context)
Set the `MessageContext` which is to be encoded.

Specified by:
  `setMessageContext` in interface `MessageEncoder`
Parameters:
  `context` - the message context

### prepareContext

public void prepareContext() throws MessageEncodingException
This method should prepare the message context by creating and populating any binding-specific data structures required in the MessageContext, prior to actually encoding.

This method should be called after the MessageContext has been set, and before any binding-specific Handler or HandlerChains are invoked.

Example: For a SOAP encoder, this method would create and store the basic SOAP Envelope structure in the message context, so that Handlers that are invoked have a place to which to add headers.

This method may be a no-op if not required by the binding, or if the message type of the context implies that the binding-specific structures have already been created elsewhere (e.g. message-oriented code where the calling code already knows its SOAP, and is operating on the raw SOAP envelope anyway).

. Default implementation is a no-op.

Specified by:
  `prepareContext` in interface `MessageEncoder`
Throws:
  `MessageEncodingException` - if there is a problem preparing the message context for encoding

### getMessageContext

@NonnullAfterInit protected MessageContext getMessageContext()
Get the message context.

Returns:
  the message context.

### doInitialize

protected void doInitialize() throws ComponentInitializationException

Overrides:
  `doInitialize` in class `AbstractInitializableComponent`
Throws:
  `ComponentInitializationException`

### encode

public void encode() throws MessageEncodingException
Encode the `MessageContext` supplied via `MessageEncoder.setMessageContext(MessageContext)` to the sink.

Specified by:
  `encode` in interface `MessageEncoder`
Throws:
  `MessageEncodingException` - if there is a problem encoding the message context

### getProtocolMessageLogger

@Nonnull protected org.slf4j.Logger getProtocolMessageLogger()
Get the protocol message logger.

Returns:
  The protocol message logger

### getProtocolMessageLoggerSubCategory

@Nullable protected String getProtocolMessageLoggerSubCategory()
Get the configured logging sub-category for protocol messages.

Returns:
  the logging sub-category

### setProtocolMessageLoggerSubCategory

protected void setProtocolMessageLoggerSubCategory(@Nullable String category)
Set the configured logging sub-category for protocol messages.

Parameters:
  `category` - the logging sub-category

### logEncodedMessage

protected void logEncodedMessage()
Log the encoded message to the protocol message logger.

### getMessageToLog

@Nullable protected Object getMessageToLog()
Get the XMLObject which will be logged as the protocol message.

Returns:
  the XMLObject message considered to be the protocol message for logging purposes

### serializeMessageForLogging

@Nullable protected String serializeMessageForLogging(@Nullable Object message)
Serialize the message for logging purposes.

Default implementation is to return the message object's `Object.toString()`, but subclasses should override if a better message-specific serialization mechanism exists.

Parameters:
  `message` - the message to serialize
Returns:
  the serialized message, or null if message can not be serialized

### doEncode

protected abstract void doEncode() throws MessageEncodingException
Performs the encoding logic. By the time this is called, this encoder has already been initialized and checked to ensure that it has not been destroyed.

Throws:
  `MessageEncodingException` - thrown if there is a problem encoding the message