package com.sun.xml.internal.ws.api.message;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.soap.SOAPBinding;
import com.sun.istack.internal.NotNull;
import com.sun.xml.internal.ws.addressing.WsaTubeHelper;
import com.sun.xml.internal.ws.api.SOAPVersion;
import com.sun.xml.internal.ws.api.WSBinding;
import com.sun.xml.internal.ws.api.addressing.AddressingPropertySet;
import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
import com.sun.xml.internal.ws.api.addressing.OneWayFeature;
import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
import com.sun.xml.internal.ws.message.RelatesToHeader;
import com.sun.xml.internal.ws.message.StringHeader;
import com.sun.xml.internal.ws.resources.AddressingMessages;
import com.sun.xml.internal.ws.resources.ClientMessages;
public class AddressingUtils {
public static void (MessageHeaders headers, Packet packet, AddressingVersion av, SOAPVersion sv, boolean oneway, String action) {
fillRequestAddressingHeaders(headers, packet, av, sv, oneway, action, false);
}
public static void (MessageHeaders headers, Packet packet, AddressingVersion av, SOAPVersion sv, boolean oneway, String action, boolean mustUnderstand) {
fillCommonAddressingHeaders(headers, packet, av, sv, action, mustUnderstand);
if (!oneway) {
WSEndpointReference epr = av.anonymousEpr;
if (headers.get(av.replyToTag, false) == null) {
headers.add(epr.createHeader(av.replyToTag));
}
if (headers.get(av.faultToTag, false) == null) {
headers.add(epr.createHeader(av.faultToTag));
}
if (packet.getMessage().getHeaders().get(av.messageIDTag, false) == null) {
if (headers.get(av.messageIDTag, false) == null) {
Header h = new StringHeader(av.messageIDTag, Message.generateMessageID());
headers.add(h);
}
}
}
}
public static void (MessageHeaders headers, WSDLPort wsdlPort, WSBinding binding, Packet packet) {
if (binding == null) {
throw new IllegalArgumentException(AddressingMessages.NULL_BINDING());
}
if (binding.isFeatureEnabled(SuppressAutomaticWSARequestHeadersFeature.class)) {
return;
}
MessageHeaders hl = packet.getMessage().getHeaders();
String action = AddressingUtils.getAction(hl, binding.getAddressingVersion(), binding.getSOAPVersion());
if (action != null) {
return;
}
AddressingVersion addressingVersion = binding.getAddressingVersion();
WsaTubeHelper wsaHelper = addressingVersion.getWsaHelper(wsdlPort, null, binding);
String effectiveInputAction = wsaHelper.getEffectiveInputAction(packet);
if (effectiveInputAction == null || effectiveInputAction.equals("") && binding.getSOAPVersion() == SOAPVersion.SOAP_11) {
throw new WebServiceException(ClientMessages.INVALID_SOAP_ACTION());
}
boolean oneway = !packet.expectReply;
if (wsdlPort != null) {
if (!oneway && packet.getMessage() != null && packet.getWSDLOperation() != null) {
WSDLBoundOperation wbo = wsdlPort.getBinding().get(packet.getWSDLOperation());
if (wbo != null && wbo.getAnonymous() == WSDLBoundOperation.ANONYMOUS.prohibited) {
throw new WebServiceException(AddressingMessages.WSAW_ANONYMOUS_PROHIBITED());
}
}
}
OneWayFeature oneWayFeature = binding.getFeature(OneWayFeature.class);
final AddressingPropertySet addressingPropertySet = packet.getSatellite(AddressingPropertySet.class);
oneWayFeature = addressingPropertySet == null ? oneWayFeature : new OneWayFeature(addressingPropertySet, addressingVersion);
if (oneWayFeature == null || !oneWayFeature.isEnabled()) {
fillRequestAddressingHeaders(headers, packet, addressingVersion, binding.getSOAPVersion(), oneway, effectiveInputAction, AddressingVersion.isRequired(binding));
} else {
fillRequestAddressingHeaders(headers, packet, addressingVersion, binding.getSOAPVersion(), oneWayFeature, oneway, effectiveInputAction);
}
}
public static String (@NotNull MessageHeaders headers, @NotNull AddressingVersion av, @NotNull SOAPVersion sv) {
if (av == null) {
throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
}
String action = null;
Header h = getFirstHeader(headers, av.actionTag, true, sv);
if (h != null) {
action = h.getStringContent();
}
return action;
}
public static WSEndpointReference (@NotNull MessageHeaders headers, @NotNull AddressingVersion av, @NotNull SOAPVersion sv) {
if (av == null) {
throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
}
Header h = getFirstHeader(headers, av.faultToTag, true, sv);
WSEndpointReference faultTo = null;
if (h != null) {
try {
faultTo = h.readAsEPR(av);
} catch (XMLStreamException e) {
throw new WebServiceException(AddressingMessages.FAULT_TO_CANNOT_PARSE(), e);
}
}
return faultTo;
}
public static String (@NotNull MessageHeaders headers, @NotNull AddressingVersion av, @NotNull SOAPVersion sv) {
if (av == null) {
throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
}
Header h = getFirstHeader(headers, av.messageIDTag, true, sv);
String messageId = null;
if (h != null) {
messageId = h.getStringContent();
}
return messageId;
}
public static String (@NotNull MessageHeaders headers, @NotNull AddressingVersion av, @NotNull SOAPVersion sv) {
if (av == null) {
throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
}
Header h = getFirstHeader(headers, av.relatesToTag, true, sv);
String relatesTo = null;
if (h != null) {
relatesTo = h.getStringContent();
}
return relatesTo;
}
public static WSEndpointReference (@NotNull MessageHeaders headers, @NotNull AddressingVersion av, @NotNull SOAPVersion sv) {
if (av == null) {
throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
}
Header h = getFirstHeader(headers, av.replyToTag, true, sv);
WSEndpointReference replyTo;
if (h != null) {
try {
replyTo = h.readAsEPR(av);
} catch (XMLStreamException e) {
throw new WebServiceException(AddressingMessages.REPLY_TO_CANNOT_PARSE(), e);
}
} else {
replyTo = av.anonymousEpr;
}
return replyTo;
}
public static String (MessageHeaders headers, AddressingVersion av, SOAPVersion sv) {
if (av == null) {
throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
}
Header h = getFirstHeader(headers, av.toTag, true, sv);
String to;
if (h != null) {
to = h.getStringContent();
} else {
to = av.anonymousUri;
}
return to;
}
public static Header (MessageHeaders headers, QName name, boolean markUnderstood, SOAPVersion sv) {
if (sv == null) {
throw new IllegalArgumentException(AddressingMessages.NULL_SOAP_VERSION());
}
Iterator<Header> iter = headers.getHeaders(name.getNamespaceURI(), name.getLocalPart(), markUnderstood);
while (iter.hasNext()) {
Header h = iter.next();
if (h.getRole(sv).equals(sv.implicitRole)) {
return h;
}
}
return null;
}
private static void (@NotNull MessageHeaders headers, @NotNull Packet packet, @NotNull AddressingVersion av, @NotNull SOAPVersion sv, @NotNull OneWayFeature oneWayFeature, boolean oneway, @NotNull String action) {
if (!oneway&&!oneWayFeature.isUseAsyncWithSyncInvoke() && Boolean.TRUE.equals(packet.isSynchronousMEP)) {
fillRequestAddressingHeaders(headers, packet, av, sv, oneway, action);
} else {
fillCommonAddressingHeaders(headers, packet, av, sv, action, false);
boolean isMessageIdAdded = false;
if (headers.get(av.replyToTag, false) == null) {
WSEndpointReference replyToEpr = oneWayFeature.getReplyTo();
if (replyToEpr != null) {
headers.add(replyToEpr.createHeader(av.replyToTag));
if (packet.getMessage().getHeaders().get(av.messageIDTag, false) == null) {
String newID = oneWayFeature.getMessageId() == null ? Message.generateMessageID() : oneWayFeature.getMessageId();
headers.add(new StringHeader(av.messageIDTag, newID));
isMessageIdAdded = true;
}
}
}
final String messageId = oneWayFeature.getMessageId();
if (!isMessageIdAdded && messageId != null) {
headers.add(new StringHeader(av.messageIDTag, messageId));
}
if (headers.get(av.faultToTag, false) == null) {
WSEndpointReference faultToEpr = oneWayFeature.getFaultTo();
if (faultToEpr != null) {
headers.add(faultToEpr.createHeader(av.faultToTag));
if (headers.get(av.messageIDTag, false) == null) {
headers.add(new StringHeader(av.messageIDTag, Message.generateMessageID()));
}
}
}
if (oneWayFeature.getFrom() != null) {
headers.addOrReplace(oneWayFeature.getFrom().createHeader(av.fromTag));
}
if (oneWayFeature.getRelatesToID() != null) {
headers.addOrReplace(new RelatesToHeader(av.relatesToTag, oneWayFeature.getRelatesToID()));
}
}
}
private static void (MessageHeaders headers, Packet packet, @NotNull AddressingVersion av, @NotNull SOAPVersion sv, @NotNull String action, boolean mustUnderstand) {
if (packet == null) {
throw new IllegalArgumentException(AddressingMessages.NULL_PACKET());
}
if (av == null) {
throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
}
if (sv == null) {
throw new IllegalArgumentException(AddressingMessages.NULL_SOAP_VERSION());
}
if (action == null && !sv.httpBindingId.equals(SOAPBinding.SOAP12HTTP_BINDING)) {
throw new IllegalArgumentException(AddressingMessages.NULL_ACTION());
}
if (headers.get(av.toTag, false) == null) {
StringHeader h = new StringHeader(av.toTag, packet.endpointAddress.toString());
headers.add(h);
}
if (action != null) {
packet.soapAction = action;
if (headers.get(av.actionTag, false) == null) {
StringHeader h = new StringHeader(av.actionTag, action, sv, mustUnderstand);
headers.add(h);
}
}
}
}