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.jaxws.interceptors;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import org.apache.cxf.interceptor.Fault;
024: import org.apache.cxf.message.Exchange;
025: import org.apache.cxf.message.Message;
026: import org.apache.cxf.message.MessageContentsList;
027: import org.apache.cxf.phase.AbstractPhaseInterceptor;
028: import org.apache.cxf.phase.Phase;
029: import org.apache.cxf.service.model.BindingMessageInfo;
030: import org.apache.cxf.service.model.BindingOperationInfo;
031: import org.apache.cxf.service.model.MessageInfo;
032: import org.apache.cxf.service.model.MessagePartInfo;
033: import org.apache.cxf.service.model.OperationInfo;
034:
035: public class WrapperClassOutInterceptor extends
036: AbstractPhaseInterceptor<Message> {
037: public WrapperClassOutInterceptor() {
038: super (Phase.PRE_LOGICAL);
039: }
040:
041: public void handleMessage(Message message) throws Fault {
042: Exchange ex = message.getExchange();
043: BindingOperationInfo bop = ex.get(BindingOperationInfo.class);
044:
045: MessageInfo messageInfo = message.get(MessageInfo.class);
046: if (messageInfo == null || bop == null || !bop.isUnwrapped()) {
047: return;
048: }
049:
050: BindingOperationInfo newbop = bop.getWrappedOperation();
051: MessageInfo wrappedMsgInfo;
052: if (Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE))) {
053: wrappedMsgInfo = newbop.getInput().getMessageInfo();
054: } else {
055: wrappedMsgInfo = newbop.getOutput().getMessageInfo();
056: }
057:
058: Class<?> wrapped = null;
059: List<MessagePartInfo> parts = wrappedMsgInfo.getMessageParts();
060: if (parts.size() > 0) {
061: wrapped = parts.get(0).getTypeClass();
062: }
063:
064: if (wrapped != null) {
065: MessageContentsList objs = MessageContentsList
066: .getContentsList(message);
067:
068: WrapperHelper helper = parts.get(0).getProperty(
069: "WRAPPER_CLASS", WrapperHelper.class);
070: if (helper == null) {
071: List<String> partNames = new ArrayList<String>();
072: List<String> elTypeNames = new ArrayList<String>();
073: List<Class<?>> partClasses = new ArrayList<Class<?>>();
074:
075: for (MessagePartInfo p : messageInfo.getMessageParts()) {
076: ensureSize(partNames, p.getIndex());
077: ensureSize(elTypeNames, p.getIndex());
078: ensureSize(partClasses, p.getIndex());
079:
080: partNames.set(p.getIndex(), p.getName()
081: .getLocalPart());
082:
083: String elementType = null;
084: if (p.isElement()) {
085: elementType = p.getElementQName()
086: .getLocalPart();
087: } else {
088: if (p.getTypeQName() == null) {
089: // handling anonymous complex type
090: elementType = null;
091: } else {
092: elementType = p.getTypeQName()
093: .getLocalPart();
094: }
095: }
096:
097: elTypeNames.set(p.getIndex(), elementType);
098: partClasses.set(p.getIndex(), p.getTypeClass());
099: }
100: helper = WrapperHelper.createWrapperHelper(wrapped,
101: partNames, elTypeNames, partClasses);
102:
103: parts.get(0).setProperty("WRAPPER_CLASS", helper);
104: }
105: try {
106: Object o2 = helper.createWrapperObject(objs);
107: objs.clear();
108: objs.put(parts.get(0), o2);
109: } catch (Exception e) {
110: throw new Fault(e);
111: }
112:
113: // we've now wrapped the object, so use the wrapped binding op
114: ex.put(BindingOperationInfo.class, newbop);
115: ex.put(OperationInfo.class, newbop.getOperationInfo());
116:
117: if (messageInfo == bop.getOperationInfo().getInput()) {
118: message.put(MessageInfo.class, newbop
119: .getOperationInfo().getInput());
120: message
121: .put(BindingMessageInfo.class, newbop
122: .getInput());
123: } else if (messageInfo == bop.getOperationInfo()
124: .getOutput()) {
125: message.put(MessageInfo.class, newbop
126: .getOperationInfo().getOutput());
127: message.put(BindingMessageInfo.class, newbop
128: .getOutput());
129: }
130: }
131: }
132:
133: private void ensureSize(List<?> lst, int idx) {
134: while (idx >= lst.size()) {
135: lst.add(null);
136: }
137: }
138: }
|