package com.sun.xml.internal.ws.server.sei;
import com.oracle.webservices.internal.api.databinding.JavaCallInfo;
import com.sun.xml.internal.ws.api.SOAPVersion;
import com.sun.xml.internal.ws.api.WSBinding;
import com.sun.xml.internal.ws.api.databinding.EndpointCallBridge;
import com.sun.xml.internal.ws.api.message.Message;
import com.sun.xml.internal.ws.api.message.MessageContextFactory;
import com.sun.xml.internal.ws.api.message.Packet;
import com.sun.xml.internal.ws.api.model.JavaMethod;
import com.sun.xml.internal.ws.fault.SOAPFaultBuilder;
import com.sun.xml.internal.ws.message.jaxb.JAXBMessage;
import com.sun.xml.internal.ws.model.JavaMethodImpl;
import com.sun.xml.internal.ws.model.ParameterImpl;
import com.sun.xml.internal.ws.model.WrapperParameter;
import com.sun.xml.internal.ws.wsdl.DispatchException;
import javax.jws.WebParam.Mode;
import javax.xml.bind.JAXBException;
import javax.xml.stream.XMLStreamException;
import javax.xml.ws.Holder;
import javax.xml.ws.ProtocolException;
import javax.xml.ws.WebServiceException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
final public class TieHandler implements EndpointCallBridge {
private final SOAPVersion soapVersion;
private final Method method;
private final int noOfArgs;
private final JavaMethodImpl javaMethodModel;
private final Boolean isOneWay;
private final EndpointArgumentsBuilder argumentsBuilder;
private final EndpointResponseMessageBuilder bodyBuilder;
private final MessageFiller[] outFillers;
protected MessageContextFactory packetFactory;
public TieHandler(JavaMethodImpl method, WSBinding binding, MessageContextFactory mcf) {
this.soapVersion = binding.getSOAPVersion();
this.method = method.getMethod();
this.javaMethodModel = method;
argumentsBuilder = createArgumentsBuilder();
List<MessageFiller> fillers = new ArrayList<MessageFiller>();
bodyBuilder = createResponseMessageBuilder(fillers);
this.outFillers = fillers.toArray(new MessageFiller[fillers.size()]);
this.isOneWay = method.getMEP().isOneWay();
this.noOfArgs = this.method.getParameterTypes().length;
packetFactory = mcf;
}
private EndpointArgumentsBuilder createArgumentsBuilder() {
EndpointArgumentsBuilder argsBuilder;
List<ParameterImpl> rp = javaMethodModel.getRequestParameters();
List<EndpointArgumentsBuilder> builders = new ArrayList<EndpointArgumentsBuilder>();
for( ParameterImpl param : rp ) {
EndpointValueSetter setter = EndpointValueSetter.get(param);
switch(param.getInBinding().kind) {
case BODY:
if(param.isWrapperStyle()) {
if(param.getParent().getBinding().isRpcLit())
builders.add(new EndpointArgumentsBuilder.RpcLit((WrapperParameter)param));
else
builders.add(new EndpointArgumentsBuilder.DocLit((WrapperParameter)param, Mode.OUT));
} else {
builders.add(new EndpointArgumentsBuilder.Body(param.getXMLBridge(),setter));
}
break;
case HEADER:
builders.add(new EndpointArgumentsBuilder.Header(soapVersion, param, setter));
break;
case ATTACHMENT:
builders.add(EndpointArgumentsBuilder.AttachmentBuilder.createAttachmentBuilder(param, setter));
break;
case UNBOUND:
builders.add(new EndpointArgumentsBuilder.NullSetter(setter,
EndpointArgumentsBuilder.getVMUninitializedValue(param.getTypeInfo().type)));
break;
default:
throw new AssertionError();
}
}
List<ParameterImpl> resp = javaMethodModel.getResponseParameters();
for( ParameterImpl param : resp ) {
if (param.isWrapperStyle()) {
WrapperParameter wp = (WrapperParameter)param;
List<ParameterImpl> children = wp.getWrapperChildren();
for (ParameterImpl p : children) {
if (p.isOUT() && p.getIndex() != -1) {
EndpointValueSetter setter = EndpointValueSetter.get(p);
builders.add(new EndpointArgumentsBuilder.NullSetter(setter, null));
}
}
} else if (param.isOUT() && param.getIndex() != -1) {
EndpointValueSetter setter = EndpointValueSetter.get(param);
builders.add(new EndpointArgumentsBuilder.NullSetter(setter, null));
}
}
switch(builders.size()) {
case 0:
argsBuilder = EndpointArgumentsBuilder.NONE;
break;
case 1:
argsBuilder = builders.get(0);
break;
default:
argsBuilder = new EndpointArgumentsBuilder.Composite(builders);
}
return argsBuilder;
}
private EndpointResponseMessageBuilder createResponseMessageBuilder(List<MessageFiller> fillers) {
EndpointResponseMessageBuilder tmpBodyBuilder = null;
List<ParameterImpl> rp = javaMethodModel.getResponseParameters();
for (ParameterImpl param : rp) {
ValueGetter getter = ValueGetter.get(param);
switch(param.getOutBinding().kind) {
case BODY:
if(param.isWrapperStyle()) {
if(param.getParent().getBinding().isRpcLit()) {
tmpBodyBuilder = new EndpointResponseMessageBuilder.RpcLit((WrapperParameter)param,
soapVersion);
} else {
tmpBodyBuilder = new EndpointResponseMessageBuilder.DocLit((WrapperParameter)param,
soapVersion);
}
} else {
tmpBodyBuilder = new EndpointResponseMessageBuilder.Bare(param, soapVersion);
}
break;
case HEADER:
fillers.add(new MessageFiller.Header(param.getIndex(), param.getXMLBridge(), getter ));
break;
case ATTACHMENT:
fillers.add(MessageFiller.AttachmentFiller.createAttachmentFiller(param, getter));
break;
case UNBOUND:
break;
default:
throw new AssertionError();
}
}
if (tmpBodyBuilder == null) {
switch(soapVersion) {
case SOAP_11:
tmpBodyBuilder = EndpointResponseMessageBuilder.EMPTY_SOAP11;
break;
case SOAP_12:
tmpBodyBuilder = EndpointResponseMessageBuilder.EMPTY_SOAP12;
break;
default:
throw new AssertionError();
}
}
return tmpBodyBuilder;
}
public Object[] readRequest(Message reqMsg) {
Object[] args = new Object[noOfArgs];
try {
argumentsBuilder.readRequest(reqMsg,args);
} catch (JAXBException e) {
throw new WebServiceException(e);
} catch (XMLStreamException e) {
throw new WebServiceException(e);
}
return args;
}
public Message createResponse(JavaCallInfo call) {
Message responseMessage;
if (call.getException() == null) {
responseMessage = isOneWay ? null : createResponseMessage(call.getParameters(), call.getReturnValue());
} else {
Throwable e = call.getException();
Throwable serviceException = getServiceException(e);
if (e instanceof InvocationTargetException || serviceException != null) {
if (serviceException != null) {
LOGGER.log(Level.FINE, serviceException.getMessage(), serviceException);
responseMessage = SOAPFaultBuilder.createSOAPFaultMessage(soapVersion,
javaMethodModel.getCheckedException(serviceException.getClass()), serviceException);
} else {
Throwable cause = e.getCause();
if (cause instanceof ProtocolException) {
LOGGER.log(Level.FINE, cause.getMessage(), cause);
} else {
LOGGER.log(Level.SEVERE, cause.getMessage(), cause);
}
responseMessage = SOAPFaultBuilder.createSOAPFaultMessage(soapVersion, null, cause);
}
} else if (e instanceof DispatchException) {
responseMessage = ((DispatchException)e).fault;
} else {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
responseMessage = SOAPFaultBuilder.createSOAPFaultMessage(soapVersion, null, e);
}
}
return responseMessage;
}
Throwable getServiceException(Throwable throwable) {
if (javaMethodModel.getCheckedException(throwable.getClass()) != null) return throwable;
if (throwable.getCause() != null) {
Throwable cause = throwable.getCause();
if (javaMethodModel.getCheckedException(cause.getClass()) != null) return cause;
}
return null;
}
private Message createResponseMessage(Object[] args, Object returnValue) {
Message msg = bodyBuilder.createMessage(args, returnValue);
for (MessageFiller filler : outFillers)
filler.fillIn(args, returnValue, msg);
return msg;
}
public Method getMethod() {
return method;
}
private static final Logger LOGGER = Logger.getLogger(TieHandler.class.getName());
@Override
public JavaCallInfo deserializeRequest(Packet req) {
com.sun.xml.internal.ws.api.databinding.JavaCallInfo call = new com.sun.xml.internal.ws.api.databinding.JavaCallInfo();
call.setMethod(this.getMethod());
Object[] args = this.readRequest(req.getMessage());
call.setParameters(args);
return call;
}
@Override
public Packet serializeResponse(JavaCallInfo call) {
Message msg = this.createResponse(call);
Packet p = (msg == null) ? (Packet)packetFactory.createContext() : (Packet)packetFactory.createContext(msg);
p.setState(Packet.State.ServerResponse);
return p;
}
@Override
public JavaMethod getOperationModel() {
return javaMethodModel;
}
}