LibraryLink ToToggle FramesPrintFeedback

Working with Contexts in a Service Implementation

Context information is made available to service implementations using the WebServiceContext interface. From the WebServiceContext object you can obtain a MessageContext object that is populated with the current request's context properties in the application scope. You can manipulate the values of the properties, and they are propagated back through the response chain.

[Note]Note

The MessageContext interface inherits from the java.util.Map interface. Its contents can be manipulated using the Map interface's methods.

To obtain the message context in a service implementation do the following:

Example 20.2 shows code for obtaining a context object.


Once you have obtained the MessageContext object for your implementation, you can access the properties stored there using the get() method shown in Example 20.3.


[Note]Note

This get() is inherited from the Map interface.

The key parameter is the string representing the property you want to retrieve from the context. The get() returns an object that must be cast to the proper type for the property. Table 20.1 lists a number of the properties that are available in a service implementation's context.

[Important]Important

Changing the values of the object returned from the context also changes the value of the property in the context.

Example 20.4 shows code for getting the name of the WSDL operation element that represents the invoked operation.


Once you have obtained the MessageContext object for your implementation, you can set properties, and change existing properties, using the put() method shown in Example 20.5.


If the property being set already exists in the message context, the put() method replaces the existing value with the new value and returns the old value. If the property does not already exist in the message context, the put() method sets the property and returns null.

Example 20.6 shows code for setting the response code for an HTTP request.


Table 20.1 lists the properties accessible through the context in a service implementation object.

Table 20.1. Properties Available in the Service Implementation Context

Base Class
Property NameDescription
org.apache.cxf.message.Message
PROTOCOL_HEADERS[a]Specifies the transport specific header information. The value is stored as a java.util.Map<String, List<String>>.
RESPONSE_CODE[a]Specifies the response code returned to the consumer. The value is stored as an Integer object.
ENDPOINT_ADDRESSSpecifies the address of the service provider. The value is stored as a String.
HTTP_REQUEST_METHOD[a]Specifies the HTTP verb sent with a request. The value is stored as a String.
PATH_INFO[a]

Specifies the path of the resource being requested. The value is stored as a String.

The path is the portion of the URI after the hostname and before any query string. For example, if an endpoint's URI is http://cxf.apache.org/demo/widgets the path is /demo/widgets.

QUERY_STRING[a]

Specifies the query, if any, attached to the URI used to invoke the request. The value is stored as a String.

Queries appear at the end of the URI after a ?. For example, if a request is made to http://cxf.apache.org/demo/widgets?color the query is color.

MTOM_ENABLEDSpecifies whether or not the service provider can use MTOM for SOAP attachments. The value is stored as a Boolean.
SCHEMA_VALIDATION_ENABLEDSpecifies whether or not the service provider validates messages against a schema. The value is stored as a Boolean.
FAULT_STACKTRACE_ENABLEDSpecifies if the runtime provides a stack trace along with a fault message. The value is stored as a Boolean.
CONTENT_TYPESpecifies the MIME type of the message. The value is stored as a String.
BASE_PATH

Specifies the path of the resource being requested. The value is stored as a java.net.URL.

The path is the portion of the URI after the hostname and before any query string. For example, if an endpoint's URL is http://cxf.apache.org/demo/widgets the base path is /demo/widgets.

ENCODINGSpecifies the encoding of the message. The value is stored as a String.
FIXED_PARAMETER_ORDERSpecifies whether the parameters must appear in the message in a particular order. The value is stored as a Boolean.
MAINTAIN_SESSIONSpecifies if the consumer wants to maintain the current session for future requests. The value is stored as a Boolean.
WSDL_DESCRIPTION[a]Specifies the WSDL document that defines the service being implemented. The value is stored as a org.xml.sax.InputSource object.
WSDL_SERVICE[a]Specifies the qualified name of the wsdl:service element that defines the service being implemented. The value is stored as a QName.
WSDL_PORT[a]Specifies the qualified name of the wsdl:port element that defines the endpoint used to access the service. The value is stored as a QName.
WSDL_INTERFACE[a]Specifies the qualified name of the wsdl:portType element that defines the service being implemented. The value is stored as a QName.
WSDL_OPERATION[a]Specifies the qualified name of the wsdl:operation element that corresponds to the operation invoked by the consumer. The value is stored as a QName.
javax.xml.ws.handler.MessageContext
MESSAGE_OUTBOUND_PROPERTYSpecifies if a message is outbound. The value is stored as a Boolean. true specifies that a message is outbound.
INBOUND_MESSAGE_ATTACHMENTS

Contains any attachments included in the request message. The value is stored as a java.util.Map<String, DataHandler>.

The key value for the map is the MIME Content-ID for the header.

OUTBOUND_MESSAGE_ATTACHMENTS

Contains any attachments for the response message. The value is stored as a java.util.Map<String, DataHandler>.

The key value for the map is the MIME Content-ID for the header.

WSDL_DESCRIPTIONSpecifies the WSDL document that defines the service being implemented. The value is stored as a org.xml.sax.InputSource object.
WSDL_SERVICESpecifies the qualified name of the wsdl:service element that defines the service being implemented. The value is stored as a QName.
WSDL_PORTSpecifies the qualified name of the wsdl:port element that defines the endpoint used to access the service. The value is stored as a QName.
WSDL_INTERFACESpecifies the qualified name of the wsdl:portType element that defines the service being implemented. The value is stored as a QName.
WSDL_OPERATIONSpecifies the qualified name of the wsdl:operation element that corresponds to the operation invoked by the consumer. The value is stored as a QName.
HTTP_RESPONSE_CODESpecifies the response code returned to the consumer. The value is stored as an Integer object.
HTTP_REQUEST_HEADERSSpecifies the HTTP headers on a request. The value is stored as a java.util.Map<String, List<String>>.
HTTP_RESPONSE_HEADERSSpecifies the HTTP headers for the response. The value is stored as a java.util.Map<String, List<String>>.
HTTP_REQUEST_METHODSpecifies the HTTP verb sent with a request. The value is stored as a String.
SERVLET_REQUESTContains the servlet's request object. The value is stored as a javax.servlet.http.HttpServletRequest.
SERVLET_RESPONSEContains the servlet's response object. The value is stored as a javax.servlet.http.HttpResponse.
SERVLET_CONTEXTContains the servlet's context object. The value is stored as a javax.servlet.ServletContext.
PATH_INFO

Specifies the path of the resource being requested. The value is stored as a String.

The path is the portion of the URI after the hostname and before any query string. For example, if an endpoint's URL is http://cxf.apache.org/demo/widgets the path is /demo/widgets.

QUERY_STRING

Specifies the query, if any, attached to the URI used to invoke the request. The value is stored as a String.

Queries appear at the end of the URI after a ?. For example, if a request is made to http://cxf.apache.org/demo/widgets?color the query string is color.

REFERENCE_PARAMETERSSpecifies the WS-Addressing reference parameters. This includes all of the SOAP headers whose wsa:IsReferenceParameter attribute is set to true. The value is stored as a java.util.List.
org.apache.cxf.transport.jms.JMSConstants
JMS_SERVER_HEADERSContains the JMS message headers. For more information see Working with JMS Message Properties.

[a] When using HTTP this property is the same as the standard JAX-WS defined property.