001: package org.objectweb.celtix.bindings;
002:
003: import java.io.IOException;
004: import java.util.logging.Level;
005: import java.util.logging.Logger;
006:
007: import javax.xml.ws.ProtocolException;
008: import javax.xml.ws.handler.MessageContext;
009:
010: import org.objectweb.celtix.common.logging.LogUtils;
011: import org.objectweb.celtix.context.InputStreamMessageContext;
012: import org.objectweb.celtix.context.ObjectMessageContext;
013: import org.objectweb.celtix.handlers.HandlerInvoker;
014:
015: import static org.objectweb.celtix.context.ObjectMessageContext.CORRELATION_IN;
016:
017: public class Response {
018:
019: private static final Logger LOG = LogUtils
020: .getL7dLogger(Response.class);
021:
022: AbstractBindingBase binding;
023: ObjectMessageContext objectCtx;
024: HandlerInvoker handlerInvoker;
025: MessageContext bindingCtx;
026:
027: public Response(Request request) {
028: binding = request.getBinding();
029: objectCtx = request.getObjectMessageContext();
030: bindingCtx = request.getBindingContext();
031: handlerInvoker = request.getHandlerInvoker();
032: }
033:
034: public Response(AbstractBindingBase b, HandlerInvoker h) {
035: binding = b;
036: handlerInvoker = h;
037: }
038:
039: public String getCorrelationId() {
040: if (null != bindingCtx) {
041: return (String) bindingCtx.get(CORRELATION_IN);
042: }
043: return null;
044: }
045:
046: public ObjectMessageContext getObjectMessageContext() {
047: return objectCtx;
048: }
049:
050: public MessageContext getBindingMessageContext() {
051: return bindingCtx;
052: }
053:
054: public void setObjectMessageContext(ObjectMessageContext o) {
055: objectCtx = o;
056: if (null != bindingCtx) {
057: objectCtx.putAll(bindingCtx);
058: }
059: }
060:
061: public void setHandlerInvoker(HandlerInvoker h) {
062: h.setInbound();
063: handlerInvoker = h;
064: }
065:
066: public HandlerInvoker getHandlerInvoker() {
067: return handlerInvoker;
068: }
069:
070: /**
071: * Handle an incoming message at the stream and protocol level.
072: *
073: * @param istreamCtx the inut stream messsage context
074: */
075: public void processProtocol(InputStreamMessageContext istreamCtx) {
076:
077: // Output Message For Client
078:
079: handlerInvoker.setInbound();
080: handlerInvoker.setFault(istreamCtx.isFault());
081: handlerInvoker.invokeStreamHandlers(istreamCtx);
082:
083: if (null == bindingCtx) {
084: bindingCtx = binding.getBindingImpl()
085: .createBindingMessageContext(istreamCtx);
086: } else {
087: bindingCtx.putAll(istreamCtx);
088: }
089: if (null == bindingCtx) {
090: bindingCtx = istreamCtx;
091: }
092:
093: bindingCtx
094: .put(ObjectMessageContext.MESSAGE_INPUT, Boolean.TRUE);
095:
096: safeRead(istreamCtx, bindingCtx);
097:
098: handlerInvoker.invokeProtocolHandlers(true, bindingCtx);
099: }
100:
101: /**
102: * Unmarshal and process an incoming message at the logical level.
103: *
104: * @param callback the data binding callback
105: */
106:
107: public void processLogical(DataBindingCallback callback) {
108: assert null != bindingCtx;
109:
110: if (null != objectCtx) {
111: objectCtx.putAll(bindingCtx);
112: } else {
113: objectCtx = binding.createObjectContext();
114: callback.initObjectContext(objectCtx);
115: objectCtx.putAll(bindingCtx);
116: }
117:
118: if (!BindingContextUtils.retrieveDecoupledResponse(bindingCtx)) {
119: if (!binding.getBindingImpl().hasFault(bindingCtx)) {
120: binding.getBindingImpl().unmarshal(bindingCtx,
121: objectCtx, callback);
122: } else {
123: binding.getBindingImpl().unmarshalFault(bindingCtx,
124: objectCtx, callback);
125: }
126: }
127:
128: objectCtx.put(ObjectMessageContext.MESSAGE_INPUT, Boolean.TRUE);
129: handlerInvoker.invokeLogicalHandlers(true, objectCtx);
130: }
131:
132: private void safeRead(InputStreamMessageContext inContext,
133: MessageContext msgContext) {
134: try {
135: binding.getBindingImpl().read(inContext, msgContext);
136: } catch (IOException ex) {
137: LOG.log(Level.SEVERE, "READ_IO_FAILURE_MSG", ex);
138: throw new ProtocolException(ex);
139: }
140: }
141: }
|