package org.jboss.axis.handlers.soap;
import org.jboss.axis.AxisEngine;
import org.jboss.axis.AxisFault;
import org.jboss.axis.Constants;
import org.jboss.axis.Handler;
import org.jboss.axis.MessageContext;
import org.jboss.axis.SimpleTargetedChain;
import org.jboss.axis.attachments.Attachments;
import org.jboss.axis.description.ServiceDesc;
import org.jboss.axis.encoding.TypeMappingRegistry;
import org.jboss.axis.enums.Style;
import org.jboss.axis.enums.Use;
import org.jboss.axis.handlers.BasicHandler;
import org.jboss.axis.handlers.HandlerChainImpl;
import org.jboss.axis.handlers.HandlerInfoChainFactory;
import org.jboss.axis.message.SOAPEnvelopeAxisImpl;
import org.jboss.axis.message.SOAPHeaderElementAxisImpl;
import org.jboss.axis.providers.BasicProvider;
import org.jboss.axis.soap.SOAPConstants;
import org.jboss.axis.utils.LockableHashtable;
import org.jboss.axis.utils.Messages;
import org.jboss.axis.utils.XMLUtils;
import org.jboss.logging.Logger;
import org.w3c.dom.Document;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
public class SOAPService extends SimpleTargetedChain
{
private static Logger log = Logger.getLogger(SOAPService.class.getName());
private Vector validTransports = null;
private boolean highFidelityRecording = true;
private int sendType = Attachments.SEND_TYPE_NOTSET;
private ServiceDesc serviceDescription = new ServiceDesc();
private AxisEngine engine;
ArrayList actors = new ArrayList();
public ArrayList getServiceActors()
{
return actors;
}
public ArrayList getActors()
{
ArrayList acts = (ArrayList)actors.clone();
if (engine != null)
{
acts.addAll(engine.getActorURIs());
}
return acts;
}
private class SOAPResponseHandler extends BasicHandler
{
public SOAPResponseHandler()
{
}
public void invoke(MessageContext msgContext) throws AxisFault
{
if (log.isDebugEnabled())
{
log.debug(Messages.getMessage("semanticCheck00"));
}
checkMustUnderstand(msgContext);
}
private void checkMustUnderstand(MessageContext msgContext) throws AxisFault
{
String svcActor = "";
SOAPEnvelopeAxisImpl env = msgContext.getRequestMessage().getSOAPEnvelope();
Vector headers = env.getHeaders(); Vector misunderstoodHeaders = null;
Enumeration en = headers.elements();
while (en.hasMoreElements())
{
SOAPHeaderElementAxisImpl header = (SOAPHeaderElementAxisImpl)en.
nextElement();
if (header.getActor() != null && header.getMustUnderstand() &&
header.getActor() != svcActor)
{
if (misunderstoodHeaders == null)
misunderstoodHeaders = new Vector();
misunderstoodHeaders.addElement(header);
}
}
SOAPConstants soapConstants = msgContext.getSOAPConstants();
if (misunderstoodHeaders != null)
{
AxisFault fault =
new AxisFault(soapConstants.getMustunderstandFaultQName(),
null, null,
null, null,
null);
StringBuffer whatWasMissUnderstood = new StringBuffer(256);
if (soapConstants == SOAPConstants.SOAP12_CONSTANTS)
{
en = misunderstoodHeaders.elements();
while (en.hasMoreElements())
{
SOAPHeaderElementAxisImpl badHeader = (SOAPHeaderElementAxisImpl)en.
nextElement();
QName badQName = new QName(badHeader.getNamespaceURI(),
badHeader.getName());
if (whatWasMissUnderstood.length() != 0) whatWasMissUnderstood.append(", ");
whatWasMissUnderstood.append(badQName.toString());
SOAPHeaderElementAxisImpl newHeader = new
SOAPHeaderElementAxisImpl(Constants.URI_SOAP12_ENV,
Constants.ELEM_NOTUNDERSTOOD);
newHeader.addAttribute(null,
Constants.ATTR_QNAME,
badQName);
fault.addHeader(newHeader);
}
}
fault.setFaultString(Messages.getMessage("noUnderstand00",
whatWasMissUnderstood.toString()));
throw fault;
}
}
}
public SOAPService()
{
setOptionsLockable(true);
initHashtable();
actors.add("");
}
public SOAPService(Handler reqHandler, Handler pivHandler,
Handler respHandler)
{
this();
init(reqHandler, null, pivHandler, new SOAPResponseHandler(), respHandler);
}
public TypeMappingRegistry getTypeMappingRegistry()
{
return serviceDescription.getTypeMappingRegistry();
}
public SOAPService(Handler serviceHandler)
{
init(null, null, serviceHandler, new SOAPResponseHandler(), null);
}
public void setEngine(AxisEngine engine)
{
if (engine == null)
throw new IllegalArgumentException(Messages.getMessage("nullEngine"));
this.engine = engine;
getTypeMappingRegistry().delegate(engine.getTypeMappingRegistry());
}
public AxisEngine getEngine()
{
return engine;
}
public boolean availableFromTransport(String transportName)
{
if (validTransports != null)
{
for (int i = 0; i < validTransports.size(); i++)
{
if (validTransports.elementAt(i).equals(transportName))
return true;
}
return false;
}
return true;
}
public Style getStyle()
{
return serviceDescription.getStyle();
}
public void setStyle(Style style)
{
serviceDescription.setStyle(style);
}
public Use getUse()
{
return serviceDescription.getUse();
}
public void setUse(Use style)
{
serviceDescription.setUse(style);
}
public ServiceDesc getServiceDescription()
{
return serviceDescription;
}
public synchronized ServiceDesc getInitializedServiceDesc(MessageContext msgContext)
throws AxisFault
{
if (serviceDescription.getImplClass() == null)
{
if (pivotHandler instanceof BasicProvider)
{
((BasicProvider)pivotHandler).initServiceDesc(this, msgContext);
}
}
return serviceDescription;
}
public void setServiceDescription(ServiceDesc serviceDescription)
{
if (serviceDescription == null)
{
return;
}
this.serviceDescription = serviceDescription;
}
public void setPropertyParent(Hashtable parent)
{
if (options == null)
{
options = new LockableHashtable();
}
((LockableHashtable)options).setParent(parent);
}
public void generateWSDL(MessageContext msgContext) throws AxisFault
{
if (serviceDescription == null ||
serviceDescription.getWSDLFile() == null)
{
super.generateWSDL(msgContext);
return;
}
InputStream instream = null;
try
{
String filename = serviceDescription.getWSDLFile();
File file = new File(filename);
if (file.exists())
{
instream = new FileInputStream(filename);
}
else
{
ClassLoader classLoader = msgContext.getClassLoader();
if (classLoader == null)
classLoader = getClass().getClassLoader();
instream = classLoader.getResourceAsStream(filename);
if (instream == null)
{
String errorText = Messages.getMessage("wsdlFileMissing", filename);
throw new AxisFault(errorText);
}
}
Document doc = XMLUtils.newDocument(instream);
msgContext.setProperty("WSDL", doc);
}
catch (Exception e)
{
throw AxisFault.makeFault(e);
}
finally
{
if (instream != null)
{
try
{
instream.close();
}
catch (IOException e)
{
}
}
}
}
public void start()
{
}
public void stop()
{
}
public void enableTransport(String transportName)
{
if (log.isDebugEnabled())
{
log.debug(Messages.getMessage("enableTransport00", "" + this, transportName));
}
if (validTransports == null)
validTransports = new Vector();
validTransports.addElement(transportName);
}
public void disableTransport(String transportName)
{
if (validTransports != null)
{
validTransports.removeElement(transportName);
}
}
public boolean needsHighFidelityRecording()
{
return highFidelityRecording;
}
public void setHighFidelityRecording(boolean highFidelityRecording)
{
this.highFidelityRecording = highFidelityRecording;
}
public int getSendType()
{
return sendType;
}
public void setSendType(int sendType)
{
this.sendType = sendType;
}
public void invoke(MessageContext msgContext) throws AxisFault
{
HandlerInfoChainFactory handlerFactory = (HandlerInfoChainFactory)this.getOption(Constants.ATTR_HANDLERINFOCHAIN);
HandlerChainImpl handlerImpl = null;
if (handlerFactory != null) handlerImpl = (HandlerChainImpl)handlerFactory.createHandlerChain();
boolean result = true;
if (handlerImpl != null)
{
result = handlerImpl.handleRequest(msgContext);
}
if (result)
{
super.invoke(msgContext);
}
else
{
msgContext.setPastPivot(true);
}
if (handlerImpl != null)
{
handlerImpl.handleResponse(msgContext);
handlerImpl.destroy();
}
}
}