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.jbi.interceptor;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.ResourceBundle;
023: import java.util.logging.Logger;
024:
025: import javax.xml.namespace.QName;
026: import javax.xml.stream.XMLStreamConstants;
027: import javax.xml.stream.XMLStreamException;
028: import javax.xml.stream.XMLStreamReader;
029:
030: import org.apache.cxf.binding.jbi.JBIBindingInfo;
031: import org.apache.cxf.binding.jbi.JBIConstants;
032: import org.apache.cxf.binding.jbi.JBIFault;
033: import org.apache.cxf.common.logging.LogUtils;
034: import org.apache.cxf.databinding.DataReader;
035: import org.apache.cxf.endpoint.Endpoint;
036: import org.apache.cxf.interceptor.AbstractInDatabindingInterceptor;
037: import org.apache.cxf.interceptor.Fault;
038: import org.apache.cxf.message.Exchange;
039: import org.apache.cxf.message.Message;
040: import org.apache.cxf.phase.Phase;
041: import org.apache.cxf.service.model.BindingInfo;
042: import org.apache.cxf.service.model.BindingMessageInfo;
043: import org.apache.cxf.service.model.BindingOperationInfo;
044: import org.apache.cxf.service.model.MessageInfo;
045: import org.apache.cxf.service.model.MessagePartInfo;
046: import org.apache.cxf.staxutils.DepthXMLStreamReader;
047: import org.apache.cxf.staxutils.StaxUtils;
048:
049: public class JBIWrapperInInterceptor extends
050: AbstractInDatabindingInterceptor {
051:
052: private static final Logger LOG = LogUtils
053: .getL7dLogger(JBIWrapperInInterceptor.class);
054:
055: private static final ResourceBundle BUNDLE = LOG
056: .getResourceBundle();
057:
058: public JBIWrapperInInterceptor() {
059: super (Phase.UNMARSHAL);
060: }
061:
062: public void handleMessage(Message message) throws Fault {
063: if (isGET(message)) {
064: LOG
065: .info("JbiMessageInInterceptor skipped in HTTP GET method");
066: return;
067: }
068: XMLStreamReader xsr = message.getContent(XMLStreamReader.class);
069:
070: DepthXMLStreamReader reader = new DepthXMLStreamReader(xsr);
071:
072: Endpoint ep = message.getExchange().get(Endpoint.class);
073: BindingInfo binding = ep.getEndpointInfo().getBinding();
074: if (!(binding instanceof JBIBindingInfo)) {
075: throw new IllegalStateException(
076: new org.apache.cxf.common.i18n.Message(
077: "NEED_JBIBINDING", BUNDLE).toString());
078: }
079:
080: if (!StaxUtils.toNextElement(reader)) {
081: throw new Fault(new org.apache.cxf.common.i18n.Message(
082: "NO_OPERATION_ELEMENT", BUNDLE));
083: }
084:
085: Exchange ex = message.getExchange();
086: QName startQName = reader.getName();
087:
088: // handling jbi fault message
089: if (startQName.getLocalPart().equals(JBIFault.JBI_FAULT_ROOT)) {
090: message.getInterceptorChain().abort();
091:
092: if (ep.getInFaultObserver() != null) {
093: ep.getInFaultObserver().onMessage(message);
094: return;
095: }
096: }
097:
098: // handling xml normal inbound message
099: if (!startQName.equals(JBIConstants.JBI_WRAPPER_MESSAGE)) {
100: throw new Fault(new org.apache.cxf.common.i18n.Message(
101: "NO_JBI_MESSAGE_ELEMENT", BUNDLE));
102: }
103:
104: try {
105: BindingOperationInfo bop = ex
106: .get(BindingOperationInfo.class);
107: DataReader<XMLStreamReader> dr = getDataReader(message);
108: List<Object> parameters = new ArrayList<Object>();
109: reader.next();
110: BindingMessageInfo messageInfo = !isRequestor(message) ? bop
111: .getInput()
112: : bop.getOutput();
113: message
114: .put(MessageInfo.class, messageInfo
115: .getMessageInfo());
116: for (MessagePartInfo part : messageInfo.getMessageParts()) {
117: if (!StaxUtils.skipToStartOfElement(reader)) {
118: throw new Fault(
119: new org.apache.cxf.common.i18n.Message(
120: "NOT_ENOUGH_PARTS", BUNDLE));
121: }
122: startQName = reader.getName();
123: if (!startQName.equals(JBIConstants.JBI_WRAPPER_PART)) {
124: throw new Fault(
125: new org.apache.cxf.common.i18n.Message(
126: "NO_JBI_PART_ELEMENT", BUNDLE));
127: }
128: if (part.isElement()) {
129: reader.next();
130: if (!StaxUtils.toNextElement(reader)) {
131: throw new Fault(
132: new org.apache.cxf.common.i18n.Message(
133: "EXPECTED_ELEMENT_IN_PART",
134: BUNDLE));
135: }
136: }
137: parameters.add(dr.read(part, reader));
138: // skip end element
139: if (part.isElement()) {
140: reader.next();
141: }
142: }
143: int ev = reader.getEventType();
144: while (ev != XMLStreamConstants.END_ELEMENT
145: && ev != XMLStreamConstants.START_ELEMENT
146: && ev != XMLStreamConstants.END_DOCUMENT) {
147: ev = reader.next();
148: }
149: message.setContent(List.class, parameters);
150: } catch (XMLStreamException e) {
151: throw new Fault(new org.apache.cxf.common.i18n.Message(
152: "STAX_READ_EXC", BUNDLE), e);
153: }
154: }
155:
156: }
|