/*=============================================================================*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*=============================================================================*/
package org.apache.ws.resource.handler.axis;
import org.apache.axis.AxisFault;
import org.apache.axis.Constants;
import org.apache.axis.Handler;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.encoding.SerializationContext;
import org.apache.axis.handlers.soap.SOAPService;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.resource.ResourceContext;
import org.apache.ws.resource.handler.ResourceHandler;
import org.apache.ws.resource.i18n.Keys;
import org.apache.ws.resource.i18n.MessagesImpl;
import org.apache.ws.util.i18n.Messages;
import org.apache.xmlbeans.XmlObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.namespace.QName;
import javax.xml.rpc.handler.soap.SOAPMessageContext;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPMessage;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
/**
* LOG-DONE Axis provider that dispatches requests to WS-Resources.
*
* @author Ian Springer, Sal Campana
*/
public class ResourceProvider
extends ResourceHandler
implements Handler
{
private static final Log LOG = LogFactory.getLog( ResourceProvider.class );
private static final Messages MSG = MessagesImpl.getInstance();
public static final String SYSPROP_AXIS_FAULT_DEBUG = "axis.fault.debug";
private GenericAxisHandler m_axisHelperHandler = new GenericAxisHandler( this );
public void init()
{
m_axisHelperHandler.setOptions( getOptions() );
m_axisHelperHandler.init();
}
/**
* DOCUMENT_ME
*
* @param doc DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public Element getDeploymentData( Document doc )
{
return m_axisHelperHandler.getDeploymentData( doc );
}
/**
* DOCUMENT_ME
*
* @param name DOCUMENT_ME
*/
public void setName( String name )
{
m_axisHelperHandler.setName( name );
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public String getName()
{
return m_axisHelperHandler.getName();
}
/**
* DOCUMENT_ME
*
* @param name DOCUMENT_ME
* @param value DOCUMENT_ME
*/
public void setOption( String name,
Object value )
{
m_axisHelperHandler.setOption( name, value );
}
/**
* DOCUMENT_ME
*
* @param name DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public Object getOption( String name )
{
return m_axisHelperHandler.getOption( name );
}
/**
* DOCUMENT_ME
*
* @param opts DOCUMENT_ME
*/
public void setOptions( Hashtable opts )
{
m_axisHelperHandler.setOptions( opts );
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public Hashtable getOptions()
{
return m_axisHelperHandler.getOptions();
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public List getUnderstoodHeaders()
{
return m_axisHelperHandler.getUnderstoodHeaders();
}
/**
* DOCUMENT_ME
*
* @param qname DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public boolean canHandleBlock( QName qname )
{
return m_axisHelperHandler.canHandleBlock( qname );
}
/**
* DOCUMENT_ME
*/
public void cleanup()
{
m_axisHelperHandler.cleanup();
}
/**
* DOCUMENT_ME
*
* @param msgContext DOCUMENT_ME
*
* @throws AxisFault DOCUMENT_ME
*/
public void generateWSDL( MessageContext msgContext )
throws AxisFault
{
SOAPService service = msgContext.getService();
LOG.debug( MSG.getMessage( Keys.RECEIVED_WSDL_REQUEST, service.getName() ) );
service.generateWSDL( msgContext );
}
/**
* Invokes a Web service operation.
*
* @param msgContext Axis SOAP message context
*
* @throws AxisFault if a SOAP fault needs to be returned to the client
*/
public void invoke( MessageContext msgContext )
throws AxisFault
{
try
{
try
{
handleRequest( msgContext );
handleResponse( msgContext );
}
catch ( RuntimeException re )
{
throw AxisFault.makeFault( re );
}
}
catch ( AxisFault axisFault )
{
// only include hostname and stack trace in fault Detail if sysprop axis.fault.debug="true"
if ( ! Boolean.getBoolean( SYSPROP_AXIS_FAULT_DEBUG ) )
{
axisFault.removeHostname();
axisFault.removeFaultDetail( Constants.QNAME_FAULTDETAIL_STACKTRACE );
}
if ( LOG.isDebugEnabled() )
{
try
{
LOG.debug( "Sending SOAP fault: \n" + toXML( axisFault, msgContext ) );
}
catch ( Exception e )
{
LOG.error( "Failed to serialize AxisFault: " + e );
}
}
throw axisFault;
}
}
/**
* DOCUMENT_ME
*
* @param msgContext DOCUMENT_ME
*/
public void onFault( MessageContext msgContext )
{
m_axisHelperHandler.onFault( msgContext );
}
/**
* DOCUMENT_ME
*
* @param soapMsgContext DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
protected SOAPMessage getResponseMessage( SOAPMessageContext soapMsgContext )
{
MessageContext axisMsgContext = ( (MessageContext) soapMsgContext );
Message responseMsg = axisMsgContext.getResponseMessage();
if ( responseMsg == null )
{
responseMsg = createMessage( axisMsgContext );
axisMsgContext.setResponseMessage( responseMsg );
}
return responseMsg;
}
/**
* DOCUMENT_ME
*
* @param soapMsgContext DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
protected ResourceContext createResourceContext( SOAPMessageContext soapMsgContext )
{
return new AxisResourceContext( soapMsgContext );
}
/**
* DOCUMENT_ME
*
* @param responseBodyElem DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
protected SOAPBodyElement[] createSOAPBodyElements( XmlObject responseBodyElem )
{
List bodyElems = new ArrayList();
Node node = responseBodyElem.newDomNode();
NodeList childNodes = node.getChildNodes();
for ( int i = 0; i < childNodes.getLength(); i++ )
{
Node child = childNodes.item( i );
//add all child elements
if ( child.getNodeType() == Node.ELEMENT_NODE )
{
bodyElems.add( new org.apache.axis.message.SOAPBodyElement( (Element) child ) );
}
}
return (SOAPBodyElement[]) bodyElems.toArray( new SOAPBodyElement[0] );
}
private Message createMessage( MessageContext axisMsgContext )
{
SOAPEnvelope envelope =
new SOAPEnvelope( axisMsgContext.getSOAPConstants(),
axisMsgContext.getSchemaVersion() );
return new Message( envelope );
}
private String toXML( AxisFault axisFault, MessageContext msgContext ) throws Exception
{
Writer writer = new StringWriter();
axisFault.output( new SerializationContext( writer, msgContext ) );
String xml = writer.toString();
writer.close();
return xml;
}
}