package org.jboss.webservice.client;
import org.jboss.axis.Message;
import org.jboss.logging.Logger;
import org.jboss.webservice.deployment.OperationDescription;
import org.jboss.webservice.deployment.ServiceDescription;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.URLDataSource;
import javax.mail.internet.MimeMultipart;
import javax.xml.soap.AttachmentPart;
import javax.xml.transform.Source;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class CallImpl extends org.jboss.axis.client.Call
{
private static final Logger log = Logger.getLogger(CallImpl.class);
private ServiceImpl jaxrpcService;
private OperationDescription opDescription;
private Map attachments = new HashMap();
public CallImpl(ServiceImpl service)
{
super(service);
this.jaxrpcService = service;
}
public CallImpl(Object url) throws MalformedURLException
{
this(new ServiceImpl());
setTargetEndpointAddress(new URL(url.toString()));
}
public void setOperation(String javaOpName)
{
super.setOperation(javaOpName);
org.jboss.axis.description.OperationDesc axisOp = getOperation();
String wsdlOpName = getOperationName().getLocalPart();
String portName = (getPortName() != null ? getPortName().getLocalPart() : null);
ServiceDescription serviceDesc = jaxrpcService.getServiceDescription(portName);
Properties callProperties = serviceDesc.getCallProperties();
if (callProperties != null)
{
Iterator keys = callProperties.keySet().iterator();
while (keys.hasNext())
{
String key = (String)keys.next();
String value = callProperties.getProperty(key);
this.setProperty(key, value);
}
}
this.opDescription = null;
Iterator itOp = serviceDesc.getOperations();
while (opDescription == null && itOp.hasNext())
{
OperationDescription operation = (OperationDescription)itOp.next();
if (operation.getWsdlName().equals(wsdlOpName))
opDescription = operation;
}
if (opDescription != null)
{
if (serviceDesc.getStyle().equals(axisOp.getStyle()) == false)
{
log.debug("Fixing style: [was=" + axisOp.getStyle() + ",is=" + serviceDesc.getStyle() + "]");
axisOp.setStyle(serviceDesc.getStyle());
}
if (serviceDesc.getUse().equals(axisOp.getUse()) == false)
{
log.debug("Fixing use: [was=" + axisOp.getUse() + ",is=" + serviceDesc.getUse() + "]");
axisOp.setUse(serviceDesc.getUse());
}
}
else
{
log.warn("Cannot find operation description for: " + wsdlOpName);
}
}
protected String getWsdlOpName(String javaOpName)
{
String wsdlOpName = javaOpName;
ServiceDescription serviceDesc = getServiceDescription();
if (serviceDesc != null)
{
Iterator it = serviceDesc.getOperations();
while (it.hasNext())
{
OperationDescription operation = (OperationDescription)it.next();
if (javaOpName.equals(operation.getJavaName()))
{
if (wsdlOpName.equals(operation.getWsdlName()) == false)
{
wsdlOpName = operation.getWsdlName();
log.debug("Replacing operation name '" + javaOpName + "' with '" + wsdlOpName + "'");
}
}
}
}
return wsdlOpName;
}
private ServiceDescription getServiceDescription()
{
ServiceDescription serviceDesc = null;
if (jaxrpcService != null)
{
String portName = (getPortName() != null ? getPortName().getLocalPart() : null);
serviceDesc = jaxrpcService.getServiceDescription(portName);
}
return serviceDesc;
}
public void addAttachment(String contentID, Object mimepart)
{
attachments.put(contentID, mimepart);
}
public Iterator getAttachmentIdentifiers()
{
return Collections.unmodifiableSet(attachments.keySet()).iterator();
}
public Object getAttachment(String contentID)
{
return attachments.get(contentID);
}
public void removeAttachment(String contentID)
{
attachments.remove(contentID);
}
protected void addAttachmentParts(Message msg)
{
Iterator it = getAttachmentIdentifiers();
while (it.hasNext())
{
String contentID = (String)it.next();
Object part = getAttachment(contentID);
AttachmentPart ap = null;
if (part instanceof String)
{
ap = msg.createAttachmentPart(part, "text/plain");
}
else if (part instanceof Source)
{
ap = msg.createAttachmentPart(part, "application/xml");
}
else if (part instanceof URL)
{
DataSource ds = new URLDataSource((URL)part);
ap = msg.createAttachmentPart(new DataHandler(ds));
}
else if (part instanceof DataHandler)
{
ap = msg.createAttachmentPart((DataHandler)part);
}
else if (part instanceof MimeMultipart)
{
ap = msg.createAttachmentPart((MimeMultipart)part, "multipart/mixed");
}
if (ap == null)
throw new IllegalArgumentException("Unsupported attachment part: " + part);
ap.setContentId(contentID);
attachmentParts.add(ap);
}
super.addAttachmentParts(msg);
attachments.clear();
}
public Object invoke(Object[] params) throws RemoteException
{
try
{
if (opDescription != null && opDescription.isOneWay())
{
log.debug("Using one-way call semantics for: " + getOperationName());
super.invokeOneWay(params);
return null;
}
else
{
return super.invoke(params);
}
}
finally
{
msgContext = null;
clearHeaders();
}
}
}