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.systest.mtom;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.net.URL;
025: import java.util.Collection;
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: import org.apache.cxf.Bus;
030: import org.apache.cxf.BusException;
031: import org.apache.cxf.BusFactory;
032: import org.apache.cxf.attachment.AttachmentDeserializer;
033: import org.apache.cxf.helpers.IOUtils;
034: import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
035: import org.apache.cxf.message.Attachment;
036: import org.apache.cxf.message.ExchangeImpl;
037: import org.apache.cxf.message.Message;
038: import org.apache.cxf.message.MessageImpl;
039: import org.apache.cxf.service.model.EndpointInfo;
040: import org.apache.cxf.test.AbstractCXFTest;
041: import org.apache.cxf.transport.Conduit;
042: import org.apache.cxf.transport.ConduitInitiator;
043: import org.apache.cxf.transport.ConduitInitiatorManager;
044: import org.apache.cxf.transport.Destination;
045: import org.apache.cxf.transport.DestinationFactory;
046: import org.apache.cxf.transport.DestinationFactoryManager;
047: import org.apache.cxf.transport.MessageObserver;
048: import org.junit.Test;
049:
050: public class MtomServerTest extends AbstractCXFTest {
051:
052: private static final String HTTP_ID = "http://schemas.xmlsoap.org/wsdl/http/";
053:
054: @Test
055: public void testMtomRequest() throws Exception {
056: JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
057: sf.setServiceBean(new EchoService());
058: sf.setBus(getBus());
059: String address = "http://localhost:9036/EchoService";
060: sf.setAddress(address);
061: Map<String, Object> props = new HashMap<String, Object>();
062: props.put(Message.MTOM_ENABLED, "true");
063: sf.setProperties(props);
064: sf.create();
065:
066: EndpointInfo ei = new EndpointInfo(null, HTTP_ID);
067: ei.setAddress(address);
068:
069: ConduitInitiatorManager conduitMgr = getBus().getExtension(
070: ConduitInitiatorManager.class);
071: ConduitInitiator conduitInit = conduitMgr
072: .getConduitInitiator("http://schemas.xmlsoap.org/soap/http");
073: Conduit conduit = conduitInit.getConduit(ei);
074:
075: TestMessageObserver obs = new TestMessageObserver();
076: conduit.setMessageObserver(obs);
077:
078: Message m = new MessageImpl();
079: String ct = "multipart/related; type=\"application/xop+xml\"; "
080: + "start=\"<soap.xml@xfire.codehaus.org>\"; "
081: + "start-info=\"text/xml\"; "
082: + "boundary=\"----=_Part_4_701508.1145579811786\"";
083:
084: m.put(Message.CONTENT_TYPE, ct);
085: conduit.prepare(m);
086:
087: OutputStream os = m.getContent(OutputStream.class);
088: InputStream is = getResourceAsStream("request");
089: if (is == null) {
090: throw new RuntimeException("Could not find resource "
091: + "request");
092: }
093:
094: IOUtils.copy(is, os);
095:
096: os.flush();
097: is.close();
098: os.close();
099:
100: byte[] res = obs.getResponseStream().toByteArray();
101: MessageImpl resMsg = new MessageImpl();
102: resMsg.setContent(InputStream.class, new ByteArrayInputStream(
103: res));
104: resMsg.put(Message.CONTENT_TYPE, obs.getResponseContentType());
105: AttachmentDeserializer deserializer = new AttachmentDeserializer(
106: resMsg);
107: deserializer.initializeAttachments();
108:
109: Collection<Attachment> attachments = resMsg.getAttachments();
110: assertNotNull(attachments);
111: assertEquals(1, attachments.size());
112:
113: Attachment inAtt = attachments.iterator().next();
114: ByteArrayOutputStream out = new ByteArrayOutputStream();
115: IOUtils.copy(inAtt.getDataHandler().getInputStream(), out);
116: out.close();
117: assertEquals(37448, out.size());
118: }
119:
120: @Test
121: public void testURLBasedAttachment() throws Exception {
122: JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
123: sf.setServiceBean(new EchoService());
124: sf.setBus(getBus());
125: String address = "http://localhost:9036/EchoService";
126: sf.setAddress(address);
127: Map<String, Object> props = new HashMap<String, Object>();
128: props.put(Message.MTOM_ENABLED, "true");
129: sf.setProperties(props);
130: sf.create();
131:
132: servStatic(getClass().getResource("mtom-policy.xml"),
133: "http://localhost:9036/policy.xsd");
134:
135: EndpointInfo ei = new EndpointInfo(null, HTTP_ID);
136: ei.setAddress(address);
137:
138: ConduitInitiatorManager conduitMgr = getBus().getExtension(
139: ConduitInitiatorManager.class);
140: ConduitInitiator conduitInit = conduitMgr
141: .getConduitInitiator("http://schemas.xmlsoap.org/soap/http");
142: Conduit conduit = conduitInit.getConduit(ei);
143:
144: TestMessageObserver obs = new TestMessageObserver();
145: conduit.setMessageObserver(obs);
146:
147: Message m = new MessageImpl();
148: String ct = "multipart/related; type=\"application/xop+xml\"; "
149: + "start=\"<soap.xml@xfire.codehaus.org>\"; "
150: + "start-info=\"text/xml; charset=utf-8\"; "
151: + "boundary=\"----=_Part_4_701508.1145579811786\"";
152:
153: m.put(Message.CONTENT_TYPE, ct);
154: conduit.prepare(m);
155:
156: OutputStream os = m.getContent(OutputStream.class);
157: InputStream is = getResourceAsStream("request-url-attachment");
158: if (is == null) {
159: throw new RuntimeException("Could not find resource "
160: + "request");
161: }
162:
163: IOUtils.copy(is, os);
164:
165: os.flush();
166: is.close();
167: os.close();
168:
169: byte[] res = obs.getResponseStream().toByteArray();
170: MessageImpl resMsg = new MessageImpl();
171: resMsg.setContent(InputStream.class, new ByteArrayInputStream(
172: res));
173: resMsg.put(Message.CONTENT_TYPE, obs.getResponseContentType());
174: AttachmentDeserializer deserializer = new AttachmentDeserializer(
175: resMsg);
176: deserializer.initializeAttachments();
177:
178: Collection<Attachment> attachments = resMsg.getAttachments();
179: assertNotNull(attachments);
180: assertEquals(1, attachments.size());
181:
182: Attachment inAtt = attachments.iterator().next();
183: ByteArrayOutputStream out = new ByteArrayOutputStream();
184: IOUtils.copy(inAtt.getDataHandler().getInputStream(), out);
185: out.close();
186: assertTrue("Wrong size: " + out.size(), out.size() > 980
187: && out.size() < 1020);
188: }
189:
190: @Override
191: protected Bus createBus() throws BusException {
192: return BusFactory.getDefaultBus();
193: }
194:
195: /**
196: * Serve static file
197: */
198: private void servStatic(final URL resource, final String add)
199: throws Exception {
200: Bus bus = getBus();
201: DestinationFactoryManager dfm = bus
202: .getExtension(DestinationFactoryManager.class);
203: DestinationFactory df = dfm
204: .getDestinationFactory("http://cxf.apache.org/transports/http/configuration");
205:
206: EndpointInfo ei = new EndpointInfo();
207: ei.setAddress(add);
208:
209: Destination d = df.getDestination(ei);
210: d.setMessageObserver(new MessageObserver() {
211:
212: public void onMessage(Message message) {
213: try {
214: // HTTP seems to need this right now...
215: ExchangeImpl ex = new ExchangeImpl();
216: ex.setInMessage(message);
217:
218: Conduit backChannel = message.getDestination()
219: .getBackChannel(message, null, null);
220:
221: MessageImpl res = new MessageImpl();
222: res.put(Message.CONTENT_TYPE, "text/xml");
223: backChannel.prepare(res);
224:
225: OutputStream out = res
226: .getContent(OutputStream.class);
227: InputStream is = resource.openStream();
228: IOUtils.copy(is, out, 2048);
229:
230: out.flush();
231:
232: out.close();
233: is.close();
234:
235: backChannel.close(res);
236: } catch (Exception e) {
237: e.printStackTrace();
238: }
239: }
240:
241: });
242: }
243: }
|