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.util.HashSet;
022: import java.util.Set;
023:
024: import javax.activation.DataHandler;
025: import javax.imageio.ImageIO;
026: import javax.xml.transform.stream.StreamSource;
027:
028: import org.apache.cxf.binding.soap.SoapMessage;
029: import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
030: import org.apache.cxf.binding.soap.model.SoapBodyInfo;
031: import org.apache.cxf.interceptor.Fault;
032: import org.apache.cxf.message.Attachment;
033: import org.apache.cxf.message.MessageContentsList;
034: import org.apache.cxf.phase.Phase;
035: import org.apache.cxf.service.model.BindingMessageInfo;
036: import org.apache.cxf.service.model.BindingOperationInfo;
037: import org.apache.cxf.service.model.MessagePartInfo;
038:
039: public class SwAInInterceptor extends AbstractSoapInterceptor {
040:
041: public SwAInInterceptor() {
042: super (Phase.PRE_INVOKE);
043: getBefore().add(HolderInInterceptor.class.getName());
044: }
045:
046: public void handleMessage(SoapMessage message) throws Fault {
047: BindingOperationInfo bop = message.getExchange().get(
048: BindingOperationInfo.class);
049: if (bop == null) {
050: return;
051: }
052:
053: if (bop.isUnwrapped()) {
054: bop = bop.getWrappedOperation();
055: }
056:
057: boolean client = isRequestor(message);
058: BindingMessageInfo bmi = client ? bop.getOutput() : bop
059: .getInput();
060:
061: if (bmi == null) {
062: return;
063: }
064:
065: SoapBodyInfo sbi = bmi.getExtensor(SoapBodyInfo.class);
066:
067: if (sbi == null || sbi.getAttachments() == null
068: || sbi.getAttachments().size() == 0) {
069: return;
070: }
071:
072: Set<Integer> foundAtts = new HashSet<Integer>();
073: MessageContentsList inObjects = MessageContentsList
074: .getContentsList(message);
075:
076: for (MessagePartInfo mpi : sbi.getAttachments()) {
077: String partName = mpi.getConcreteName().getLocalPart();
078:
079: String start = partName + "=";
080: boolean found = false;
081:
082: if (foundAtts.contains(mpi.getIndex())) {
083: continue;
084: }
085: foundAtts.add(mpi.getIndex());
086:
087: for (Attachment a : message.getAttachments()) {
088: if (a.getId().startsWith(start)) {
089: DataHandler dh = a.getDataHandler();
090: String ct = dh.getContentType();
091: Object o = null;
092:
093: if (DataHandler.class.isAssignableFrom(mpi
094: .getTypeClass())) {
095: o = dh;
096: } else if (ct.startsWith("image/")) {
097: try {
098: o = ImageIO.read(dh.getInputStream());
099: } catch (IOException e) {
100: throw new Fault(e);
101: }
102: } else if (ct.startsWith("text/xml")
103: || ct.startsWith("application/xml")) {
104: try {
105: o = new StreamSource(dh.getInputStream());
106: } catch (IOException e) {
107: throw new Fault(e);
108: }
109: } else {
110: o = dh;
111: }
112:
113: inObjects.put(mpi, o);
114: found = true;
115: break;
116: }
117: }
118:
119: if (!found) {
120: inObjects.put(mpi, null);
121: }
122: }
123: }
124: }
|