package com.sun.xml.internal.ws.handler;
import com.sun.istack.internal.Nullable;
import com.sun.xml.internal.ws.api.WSBinding;
import com.sun.xml.internal.ws.api.handler.MessageHandler;
import com.sun.xml.internal.ws.api.message.Attachment;
import com.sun.xml.internal.ws.api.message.AttachmentSet;
import com.sun.xml.internal.ws.api.message.Packet;
import com.sun.xml.internal.ws.api.model.SEIModel;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
import com.sun.xml.internal.ws.api.pipe.Tube;
import com.sun.xml.internal.ws.api.pipe.TubeCloner;
import com.sun.xml.internal.ws.api.pipe.helper.AbstractFilterTubeImpl;
import com.sun.xml.internal.ws.binding.BindingImpl;
import com.sun.xml.internal.ws.client.HandlerConfiguration;
import com.sun.xml.internal.ws.message.DataHandlerAttachment;
import javax.activation.DataHandler;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.Handler;
import java.util.*;
import java.util.Map.Entry;
public class ClientMessageHandlerTube extends HandlerTube {
private SEIModel seiModel;
private Set<String> roles;
public ClientMessageHandlerTube(@Nullable SEIModel seiModel, WSBinding binding, WSDLPort port, Tube next) {
super(next, port, binding);
this.seiModel = seiModel;
}
private ClientMessageHandlerTube(ClientMessageHandlerTube that, TubeCloner cloner) {
super(that, cloner);
this.seiModel = that.seiModel;
}
public AbstractFilterTubeImpl copy(TubeCloner cloner) {
return new ClientMessageHandlerTube(this, cloner);
}
void callHandlersOnResponse(MessageUpdatableContext context, boolean handleFault) {
try {
processor.callHandlersResponse(HandlerProcessor.Direction.INBOUND, context, handleFault);
} catch (WebServiceException wse) {
throw wse;
} catch (RuntimeException re) {
throw new WebServiceException(re);
}
}
boolean callHandlersOnRequest(MessageUpdatableContext context, boolean isOneWay) {
boolean handlerResult;
Map<String, DataHandler> atts = (Map<String, DataHandler>) context.get(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS);
AttachmentSet attSet = context.packet.getMessage().getAttachments();
for (Entry<String, DataHandler> entry : atts.entrySet()) {
String cid = entry.getKey();
if (attSet.get(cid) == null) {
Attachment att = new DataHandlerAttachment(cid, atts.get(cid));
attSet.add(att);
}
}
try {
handlerResult = processor.callHandlersRequest(HandlerProcessor.Direction.OUTBOUND, context, !isOneWay);
} catch (WebServiceException wse) {
remedyActionTaken = true;
throw wse;
} catch (RuntimeException re) {
remedyActionTaken = true;
throw new WebServiceException(re);
}
if (!handlerResult) {
remedyActionTaken = true;
}
return handlerResult;
}
void closeHandlers(MessageContext mc) {
closeClientsideHandlers(mc);
}
void setUpProcessor() {
if (handlers == null) {
handlers = new ArrayList<Handler>();
HandlerConfiguration handlerConfig = ((BindingImpl) getBinding()).getHandlerConfig();
List<MessageHandler> msgHandlersSnapShot= handlerConfig.getMessageHandlers();
if (!msgHandlersSnapShot.isEmpty()) {
handlers.addAll(msgHandlersSnapShot);
roles = new HashSet<String>();
roles.addAll(handlerConfig.getRoles());
processor = new SOAPHandlerProcessor(true, this, getBinding(), handlers);
}
}
}
MessageUpdatableContext getContext(Packet p) {
MessageHandlerContextImpl context = new MessageHandlerContextImpl(seiModel, getBinding(), port, p, roles);
return context;
}
}