package com.sun.xml.internal.ws.addressing;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import com.sun.xml.internal.ws.addressing.model.ActionNotSupportedException;
import com.sun.xml.internal.ws.addressing.model.InvalidAddressingHeaderException;
import com.sun.xml.internal.ws.api.EndpointAddress;
import com.sun.xml.internal.ws.api.SOAPVersion;
import com.sun.xml.internal.ws.api.WSBinding;
import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
import com.sun.xml.internal.ws.api.addressing.NonAnonymousResponseProcessor;
import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
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.api.model.wsdl.WSDLBoundOperation;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
import com.sun.xml.internal.ws.api.pipe.*;
import com.sun.xml.internal.ws.api.server.WSEndpoint;
import com.sun.xml.internal.ws.client.Stub;
import com.sun.xml.internal.ws.developer.JAXWSProperties;
import com.sun.xml.internal.ws.fault.SOAPFaultBuilder;
import com.sun.xml.internal.ws.message.FaultDetailHeader;
import com.sun.xml.internal.ws.resources.AddressingMessages;
import javax.xml.soap.SOAPFault;
import javax.xml.ws.WebServiceException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
public class WsaServerTube extends WsaTube {
private WSEndpoint endpoint;
private WSEndpointReference replyTo;
private WSEndpointReference faultTo;
private boolean isAnonymousRequired = false;
protected boolean isEarlyBackchannelCloseAllowed = true;
private WSDLBoundOperation wbo;
public WsaServerTube(WSEndpoint endpoint, @NotNull WSDLPort wsdlPort, WSBinding binding, Tube next) {
super(wsdlPort, binding, next);
this.endpoint = endpoint;
}
public WsaServerTube(WsaServerTube that, TubeCloner cloner) {
super(that, cloner);
endpoint = that.endpoint;
}
@Override
public WsaServerTube copy(TubeCloner cloner) {
return new WsaServerTube(this, cloner);
}
@Override
public @NotNull NextAction processRequest(Packet request) {
Message msg = request.getMessage();
if (msg == null) {
return doInvoke(next,request);
}
request.addSatellite(new WsaPropertyBag(addressingVersion,soapVersion,request));
MessageHeaders hl = request.getMessage().getHeaders();
String msgId;
try {
replyTo = AddressingUtils.getReplyTo(hl, addressingVersion, soapVersion);
faultTo = AddressingUtils.getFaultTo(hl, addressingVersion, soapVersion);
msgId = AddressingUtils.getMessageID(hl, addressingVersion, soapVersion);
} catch (InvalidAddressingHeaderException e) {
LOGGER.log(Level.WARNING, addressingVersion.getInvalidMapText()+", Problem header:" + e.getProblemHeader()+ ", Reason: "+ e.getSubsubcode(),e);
hl.remove(e.getProblemHeader());
SOAPFault soapFault = helper.createInvalidAddressingHeaderFault(e, addressingVersion);
if ((wsdlPort!=null) && request.getMessage().isOneWay(wsdlPort)) {
Packet response = request.createServerResponse(null, wsdlPort, null, binding);
return doReturnWith(response);
}
Message m = Messages.create(soapFault);
if (soapVersion == SOAPVersion.SOAP_11) {
FaultDetailHeader s11FaultDetailHeader = new FaultDetailHeader(addressingVersion, addressingVersion.problemHeaderQNameTag.getLocalPart(), e.getProblemHeader());
m.getHeaders().add(s11FaultDetailHeader);
}
Packet response = request.createServerResponse(m, wsdlPort, null, binding);
return doReturnWith(response);
}
if (replyTo == null) {
replyTo = addressingVersion.anonymousEpr;
}
if (faultTo == null) {
faultTo = replyTo;
}
request.put(WsaPropertyBag.WSA_REPLYTO_FROM_REQUEST, replyTo);
request.put(WsaPropertyBag.WSA_FAULTTO_FROM_REQUEST, faultTo);
request.put(WsaPropertyBag.WSA_MSGID_FROM_REQUEST, msgId);
wbo = getWSDLBoundOperation(request);
isAnonymousRequired = isAnonymousRequired(wbo);
Packet p = validateInboundHeaders(request);
if (p.getMessage() == null) {
return doReturnWith(p);
}
if (p.getMessage().isFault()) {
if (isEarlyBackchannelCloseAllowed &&
!(isAnonymousRequired) &&
!faultTo.isAnonymous() && request.transportBackChannel != null) {
request.transportBackChannel.close();
}
return processResponse(p);
}
if (isEarlyBackchannelCloseAllowed &&
!(isAnonymousRequired) &&
!replyTo.isAnonymous() && !faultTo.isAnonymous() &&
request.transportBackChannel != null) {
request.transportBackChannel.close();
}
return doInvoke(next,p);
}
protected boolean isAnonymousRequired(@Nullable WSDLBoundOperation wbo) {
return false;
}
protected void checkAnonymousSemantics(WSDLBoundOperation wbo, WSEndpointReference replyTo, WSEndpointReference faultTo) {
}
@Override
public @NotNull NextAction processException(Throwable t) {
final Packet response = Fiber.current().getPacket();
ThrowableContainerPropertySet tc = response.getSatellite(ThrowableContainerPropertySet.class);
if (tc == null) {
tc = new ThrowableContainerPropertySet(t);
response.addSatellite(tc);
} else if (t != tc.getThrowable()) {
tc.setThrowable(t);
}
return processResponse(response.endpoint.createServiceResponseForException(tc, response, soapVersion, wsdlPort,
response.endpoint.getSEIModel(),
binding));
}
@Override
public @NotNull NextAction processResponse(Packet response) {
Message msg = response.getMessage();
if (msg ==null) {
return doReturnWith(response);
}
String to = AddressingUtils.getTo(msg.getHeaders(),
addressingVersion, soapVersion);
if (to != null) {
replyTo = faultTo = new WSEndpointReference(to, addressingVersion);
}
if (replyTo == null) {
replyTo = (WSEndpointReference)response.
get(WsaPropertyBag.WSA_REPLYTO_FROM_REQUEST);
}
if (faultTo == null) {
faultTo = (WSEndpointReference)response.
get(WsaPropertyBag.WSA_FAULTTO_FROM_REQUEST);
}
WSEndpointReference target = msg.isFault() ? faultTo : replyTo;
if (target == null && response.proxy instanceof Stub) {
target = ((Stub) response.proxy).getWSEndpointReference();
}
if (target == null || target.isAnonymous() || isAnonymousRequired) {
return doReturnWith(response);
}
if (target.isNone()) {
response.setMessage(null);
return doReturnWith(response);
}
if ((wsdlPort!=null) && response.getMessage().isOneWay(wsdlPort)) {
LOGGER.fine(AddressingMessages.NON_ANONYMOUS_RESPONSE_ONEWAY());
return doReturnWith(response);
}
if (wbo != null || response.soapAction == null) {
String action = response.getMessage().isFault() ?
helper.getFaultAction(wbo, response) :
helper.getOutputAction(wbo);
if (response.soapAction == null ||
(action != null &&
!action.equals(AddressingVersion.UNSET_OUTPUT_ACTION))) {
response.soapAction = action;
}
}
response.expectReply = false;
EndpointAddress adrs;
try {
adrs = new EndpointAddress(URI.create(target.getAddress()));
} catch (NullPointerException e) {
throw new WebServiceException(e);
} catch (IllegalArgumentException e) {
throw new WebServiceException(e);
}
response.endpointAddress = adrs;
if (response.isAdapterDeliversNonAnonymousResponse) {
return doReturnWith(response);
}
return doReturnWith(NonAnonymousResponseProcessor.getDefault().process(response));
}
@Override
protected void validateAction(Packet packet) {
WSDLBoundOperation wsdlBoundOperation = getWSDLBoundOperation(packet);
if (wsdlBoundOperation == null) {
return;
}
String gotA = AddressingUtils.getAction(
packet.getMessage().getHeaders(),
addressingVersion, soapVersion);
if (gotA == null) {
throw new WebServiceException(AddressingMessages.VALIDATION_SERVER_NULL_ACTION());
}
String expected = helper.getInputAction(packet);
String soapAction = helper.getSOAPAction(packet);
if (helper.isInputActionDefault(packet) && (soapAction != null && !soapAction.equals(""))) {
expected = soapAction;
}
if (expected != null && !gotA.equals(expected)) {
throw new ActionNotSupportedException(gotA);
}
}
@Override
protected void checkMessageAddressingProperties(Packet packet) {
super.checkMessageAddressingProperties(packet);
WSDLBoundOperation wsdlBoundOperation = getWSDLBoundOperation(packet);
checkAnonymousSemantics(wsdlBoundOperation, replyTo, faultTo);
checkNonAnonymousAddresses(replyTo,faultTo);
}
@SuppressWarnings("ResultOfObjectAllocationIgnored")
private void checkNonAnonymousAddresses(WSEndpointReference replyTo, WSEndpointReference faultTo) {
if (!replyTo.isAnonymous()) {
try {
new EndpointAddress(URI.create(replyTo.getAddress()));
} catch (Exception e) {
throw new InvalidAddressingHeaderException(addressingVersion.replyToTag, addressingVersion.invalidAddressTag);
}
}
}
public static final String REQUEST_MESSAGE_ID = "com.sun.xml.internal.ws.addressing.request.messageID";
private static final Logger LOGGER = Logger.getLogger(WsaServerTube.class.getName());
}