001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.cxfbc.interceptors;
018:
019: import javax.xml.namespace.QName;
020: import javax.xml.stream.XMLStreamReader;
021:
022: import org.apache.cxf.binding.soap.model.SoapBindingInfo;
023: import org.apache.cxf.binding.soap.model.SoapBodyInfo;
024: import org.apache.cxf.endpoint.Endpoint;
025: import org.apache.cxf.interceptor.Fault;
026: import org.apache.cxf.interceptor.URIMappingInterceptor;
027: import org.apache.cxf.message.Message;
028: import org.apache.cxf.phase.AbstractPhaseInterceptor;
029: import org.apache.cxf.phase.Phase;
030: import org.apache.cxf.service.model.BindingMessageInfo;
031: import org.apache.cxf.service.model.BindingOperationInfo;
032: import org.apache.cxf.service.model.OperationInfo;
033: import org.apache.cxf.service.model.ServiceModelUtil;
034: import org.apache.cxf.staxutils.DepthXMLStreamReader;
035: import org.apache.cxf.staxutils.StaxUtils;
036:
037: public class JbiOperationInterceptor extends
038: AbstractPhaseInterceptor<Message> {
039:
040: public JbiOperationInterceptor() {
041: super (Phase.UNMARSHAL);
042: addAfter(URIMappingInterceptor.class.getName());
043: }
044:
045: public void handleMessage(Message message) {
046: if (isGET(message)) {
047: return;
048: }
049: if (message.getExchange().get(BindingOperationInfo.class) != null) {
050: return;
051: }
052:
053: DepthXMLStreamReader xmlReader = getXMLStreamReader(message);
054:
055: BindingOperationInfo operation = null;
056: if (!StaxUtils.toNextElement(xmlReader)) {
057: message.setContent(Exception.class, new RuntimeException(
058: "There must be a method name element."));
059: }
060: String opName = xmlReader.getLocalName();
061: if (isRequestor(message) && opName.endsWith("Response")) {
062: opName = opName.substring(0, opName.length() - 8);
063: }
064: QName opQName = new QName(xmlReader.getNamespaceURI(), opName);
065: SoapBindingInfo binding = (SoapBindingInfo) message
066: .getExchange().get(Endpoint.class).getEndpointInfo()
067: .getBinding();
068: for (BindingOperationInfo op : binding.getOperations()) {
069: String style = binding.getStyle(op.getOperationInfo());
070: if (style == null) {
071: style = binding.getStyle();
072: }
073: if ("document".equals(style)) {
074: BindingMessageInfo msg = !isRequestor(message) ? op
075: .getInput() : op.getOutput();
076: if (msg.getExtensor(SoapBodyInfo.class).getParts().get(
077: 0).getElementQName().equals(opQName)) {
078: operation = op;
079: break;
080: }
081: } else {
082: if (opQName.equals(op.getName())) {
083: operation = op;
084: break;
085: }
086: }
087: }
088: if (operation != null) {
089: message.getExchange().put(BindingOperationInfo.class,
090: operation);
091: message.getExchange().put(OperationInfo.class,
092: operation.getOperationInfo());
093: message.getExchange().setOneWay(
094: operation.getOperationInfo().isOneWay());
095: }
096: }
097:
098: protected DepthXMLStreamReader getXMLStreamReader(Message message) {
099:
100: XMLStreamReader xr = message.getContent(XMLStreamReader.class);
101: if (xr instanceof DepthXMLStreamReader) {
102: return (DepthXMLStreamReader) xr;
103: }
104: DepthXMLStreamReader dr = new DepthXMLStreamReader(xr);
105: message.setContent(XMLStreamReader.class, dr);
106: return dr;
107: }
108:
109: protected BindingOperationInfo getOperation(Message message,
110: QName opName) {
111: BindingOperationInfo op = ServiceModelUtil.getOperation(message
112: .getExchange(), opName);
113: if (op == null) {
114: throw new Fault(new Exception("Unrecognized operation"));
115: }
116: return op;
117: }
118:
119: protected boolean isRequestor(Message message) {
120: return Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE));
121: }
122:
123: }
|