001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.binding.coloc;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.SortedSet;
023: import java.util.TreeSet;
024: import java.util.logging.Level;
025: import java.util.logging.Logger;
026:
027: import javax.xml.namespace.QName;
028:
029: import org.apache.cxf.Bus;
030: import org.apache.cxf.binding.Binding;
031: import org.apache.cxf.endpoint.Endpoint;
032: import org.apache.cxf.interceptor.Interceptor;
033: import org.apache.cxf.interceptor.InterceptorChain;
034: import org.apache.cxf.message.Exchange;
035: import org.apache.cxf.message.ExchangeImpl;
036: import org.apache.cxf.message.Message;
037: import org.apache.cxf.message.MessageImpl;
038: import org.apache.cxf.phase.Phase;
039: import org.apache.cxf.phase.PhaseManager;
040: import org.apache.cxf.service.Service;
041: import org.apache.cxf.service.model.BindingInfo;
042: import org.apache.cxf.service.model.BindingOperationInfo;
043: import org.apache.cxf.service.model.MessageInfo;
044: import org.apache.cxf.service.model.OperationInfo;
045: import org.apache.cxf.transport.ChainInitiationObserver;
046:
047: public class ColocMessageObserver extends ChainInitiationObserver {
048: private static final Logger LOG = Logger
049: .getLogger(ColocMessageObserver.class.getName());
050: private static final String COLOCATED = Message.class.getName()
051: + ".COLOCATED";
052:
053: public ColocMessageObserver(Endpoint endpoint, Bus bus) {
054: super (endpoint, bus);
055: }
056:
057: public void onMessage(Message m) {
058: if (LOG.isLoggable(Level.FINER)) {
059: LOG
060: .finer("Processing Message at collocated endpoint. Request message: "
061: + m);
062: }
063: Exchange ex = new ExchangeImpl();
064: setExchangeProperties(ex, m);
065:
066: Message inMsg = endpoint.getBinding().createMessage();
067: MessageImpl.copyContent(m, inMsg);
068:
069: //Copy Request Context to Server inBound Message
070: //TODO a Context Filter Strategy required.
071: inMsg.putAll(m);
072:
073: inMsg.put(COLOCATED, Boolean.TRUE);
074: inMsg.put(Message.REQUESTOR_ROLE, Boolean.FALSE);
075: inMsg.put(Message.INBOUND_MESSAGE, Boolean.TRUE);
076: OperationInfo oi = ex.get(OperationInfo.class);
077: if (oi != null) {
078: inMsg.put(MessageInfo.class, oi.getInput());
079: }
080: ex.setInMessage(inMsg);
081: inMsg.setExchange(ex);
082:
083: if (LOG.isLoggable(Level.FINEST)) {
084: LOG.finest("Build inbound interceptor chain.");
085: }
086:
087: //Add all interceptors between USER_LOGICAL and INVOKE.
088: SortedSet<Phase> phases = new TreeSet<Phase>(bus.getExtension(
089: PhaseManager.class).getInPhases());
090: ColocUtil.setPhases(phases, Phase.USER_LOGICAL, Phase.INVOKE);
091: InterceptorChain chain = ColocUtil.getInInterceptorChain(ex,
092: phases);
093: chain.add(addColocInterceptors());
094: inMsg.setInterceptorChain(chain);
095:
096: chain.doIntercept(inMsg);
097:
098: //Set Server OutBound Message onto InBound Exchange.
099: setOutBoundMessage(ex, m.getExchange());
100: }
101:
102: protected void setOutBoundMessage(Exchange from, Exchange to) {
103: if (from.getOutFaultMessage() != null) {
104: to.setInFaultMessage(from.getOutFaultMessage());
105: } else {
106: to.setInMessage(from.getOutMessage());
107: }
108: }
109:
110: protected void setExchangeProperties(Exchange exchange, Message m) {
111: exchange.put(Bus.class, bus);
112: exchange.put(Endpoint.class, endpoint);
113: exchange.put(Service.class, endpoint.getService());
114: exchange.put(Binding.class, endpoint.getBinding());
115:
116: //Setup the BindingOperationInfo
117: QName opName = (QName) m.get(Message.WSDL_OPERATION);
118: BindingInfo bi = endpoint.getEndpointInfo().getBinding();
119: BindingOperationInfo boi = bi.getOperation(opName);
120: if (boi != null && boi.isUnwrapped()) {
121: boi = boi.getWrappedOperation();
122: }
123:
124: exchange.put(BindingInfo.class, bi);
125: exchange.put(BindingOperationInfo.class, boi);
126: exchange.put(OperationInfo.class, boi.getOperationInfo());
127: }
128:
129: protected List<Interceptor> addColocInterceptors() {
130: List<Interceptor> list = new ArrayList<Interceptor>();
131: list.add(new ColocInInterceptor());
132: return list;
133: }
134: }
|