package com.sun.xml.internal.ws.wsdl;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import com.sun.xml.internal.ws.api.WSBinding;
import com.sun.xml.internal.ws.api.model.SEIModel;
import com.sun.xml.internal.ws.api.model.WSDLOperationMapping;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation;
import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
import com.sun.xml.internal.ws.api.message.AddressingUtils;
import com.sun.xml.internal.ws.api.message.Message;
import com.sun.xml.internal.ws.api.message.MessageHeaders;
import com.sun.xml.internal.ws.api.message.Messages;
import com.sun.xml.internal.ws.api.message.Packet;
import com.sun.xml.internal.ws.model.AbstractSEIModelImpl;
import com.sun.xml.internal.ws.model.JavaMethodImpl;
import static com.sun.xml.internal.ws.wsdl.PayloadQNameBasedOperationFinder.*;
import com.sun.xml.internal.ws.resources.AddressingMessages;
import javax.xml.namespace.QName;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
final class ActionBasedOperationFinder extends WSDLOperationFinder {
private static final Logger LOGGER = Logger.getLogger(ActionBasedOperationFinder.class.getName());
private final Map<ActionBasedOperationSignature, WSDLOperationMapping> uniqueOpSignatureMap;
private final Map<String, WSDLOperationMapping> actionMap;
private final @NotNull AddressingVersion av;
public ActionBasedOperationFinder(WSDLPort wsdlModel, WSBinding binding, @Nullable SEIModel seiModel) {
super(wsdlModel, binding, seiModel);
assert binding.getAddressingVersion() != null;
av = binding.getAddressingVersion();
uniqueOpSignatureMap = new HashMap<ActionBasedOperationSignature, WSDLOperationMapping>();
actionMap = new HashMap<String,WSDLOperationMapping>();
if (seiModel != null) {
for (JavaMethodImpl m : ((AbstractSEIModelImpl) seiModel).getJavaMethods()) {
if(m.getMEP().isAsync)
continue;
String action = m.getInputAction();
QName payloadName = m.getRequestPayloadName();
if (payloadName == null)
payloadName = EMPTY_PAYLOAD;
if (action == null || action.equals("")) {
if (m.getOperation() != null) action = m.getOperation().getOperation().getInput().getAction();
}
if (action != null) {
ActionBasedOperationSignature opSignature = new ActionBasedOperationSignature(action, payloadName);
if(uniqueOpSignatureMap.get(opSignature) != null) {
LOGGER.warning(AddressingMessages.NON_UNIQUE_OPERATION_SIGNATURE(
uniqueOpSignatureMap.get(opSignature),m.getOperationQName(),action,payloadName));
}
uniqueOpSignatureMap.put(opSignature, wsdlOperationMapping(m));
actionMap.put(action,wsdlOperationMapping(m));
}
}
} else {
for (WSDLBoundOperation wsdlOp : wsdlModel.getBinding().getBindingOperations()) {
QName payloadName = wsdlOp.getRequestPayloadName();
if (payloadName == null)
payloadName = EMPTY_PAYLOAD;
String action = wsdlOp.getOperation().getInput().getAction();
ActionBasedOperationSignature opSignature = new ActionBasedOperationSignature(
action, payloadName);
if(uniqueOpSignatureMap.get(opSignature) != null) {
LOGGER.warning(AddressingMessages.NON_UNIQUE_OPERATION_SIGNATURE(
uniqueOpSignatureMap.get(opSignature),wsdlOp.getName(),action,payloadName));
}
uniqueOpSignatureMap.put(opSignature, wsdlOperationMapping(wsdlOp));
actionMap.put(action,wsdlOperationMapping(wsdlOp));
}
}
}
public WSDLOperationMapping getWSDLOperationMapping(Packet request) throws DispatchException {
MessageHeaders hl = request.getMessage().getHeaders();
String action = AddressingUtils.getAction(hl, av, binding.getSOAPVersion());
if (action == null)
return null;
Message message = request.getMessage();
QName payloadName;
String localPart = message.getPayloadLocalPart();
if (localPart == null) {
payloadName = EMPTY_PAYLOAD;
} else {
String nsUri = message.getPayloadNamespaceURI();
if (nsUri == null)
nsUri = EMPTY_PAYLOAD_NSURI;
payloadName = new QName(nsUri, localPart);
}
WSDLOperationMapping opMapping = uniqueOpSignatureMap.get(new ActionBasedOperationSignature(action, payloadName));
if (opMapping != null)
return opMapping;
opMapping = actionMap.get(action);
if (opMapping != null)
return opMapping;
Message result = Messages.create(action, av, binding.getSOAPVersion());
throw new DispatchException(result);
}
}