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.io.IOException;
021: import java.io.InputStream;
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import javax.activation.DataSource;
026: import javax.mail.util.ByteArrayDataSource;
027: import javax.xml.soap.SOAPMessage;
028: import javax.xml.stream.XMLStreamReader;
029:
030: import org.w3c.dom.Node;
031:
032: import org.apache.cxf.databinding.DataReader;
033: import org.apache.cxf.endpoint.Endpoint;
034: import org.apache.cxf.interceptor.AbstractInDatabindingInterceptor;
035: import org.apache.cxf.interceptor.Fault;
036: import org.apache.cxf.message.Exchange;
037: import org.apache.cxf.message.Message;
038: import org.apache.cxf.phase.Phase;
039: import org.apache.cxf.service.Service;
040: import org.apache.cxf.service.model.BindingOperationInfo;
041: import org.apache.cxf.staxutils.W3CDOMStreamReader;
042:
043: public class ProviderInDatabindingInterceptor extends
044: AbstractInDatabindingInterceptor {
045:
046: private Class type;
047:
048: public ProviderInDatabindingInterceptor(Class type) {
049: super (Phase.PRE_STREAM);
050: this .type = type;
051: }
052:
053: public Class getType() {
054: return type;
055: }
056:
057: public void handleMessage(Message message) throws Fault {
058: Exchange ex = message.getExchange();
059: Endpoint e = ex.get(Endpoint.class);
060:
061: if (e.getEndpointInfo().getBinding().getOperations().iterator()
062: .hasNext()) {
063: BindingOperationInfo bop = e.getEndpointInfo().getBinding()
064: .getOperations().iterator().next();
065: ex.put(BindingOperationInfo.class, bop);
066: getMessageInfo(message, bop);
067: }
068:
069: List<Object> params = new ArrayList<Object>();
070:
071: if (isGET(message)) {
072: params.add(null);
073: message.setContent(Object.class, params);
074: return;
075: }
076:
077: Service s = ex.get(Service.class);
078:
079: if (SOAPMessage.class.equals(type)) {
080: SOAPMessage msg = message.getContent(SOAPMessage.class);
081: params.add(msg);
082: } else if (DataSource.class.equals(type)) {
083: InputStream is = message.getContent(InputStream.class);
084: try {
085: params.add(new ByteArrayDataSource(is, (String) message
086: .get(Message.CONTENT_TYPE)));
087: } catch (IOException e1) {
088: e1.printStackTrace();
089: }
090: } else {
091:
092: XMLStreamReader r = message
093: .getContent(XMLStreamReader.class);
094: if (r != null) {
095: if (r instanceof W3CDOMStreamReader) {
096: Node nd = ((W3CDOMStreamReader) r)
097: .getCurrentElement();
098: DataReader<Node> reader = s.getDataBinding()
099: .createReader(Node.class);
100: Object object = reader.read(null, nd, type);
101: params.add(object);
102: } else {
103: DataReader<XMLStreamReader> reader = s
104: .getDataBinding().createReader(
105: XMLStreamReader.class);
106:
107: Object object = reader.read(null, r, type);
108: params.add(object);
109: }
110: }
111: }
112: message.setContent(Object.class, params);
113: }
114: }
|