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.util.Collection;
025:
026: import org.w3c.dom.Node;
027:
028: import org.apache.cxf.Bus;
029: import org.apache.cxf.BusException;
030: import org.apache.cxf.BusFactory;
031: import org.apache.cxf.attachment.AttachmentDeserializer;
032: import org.apache.cxf.helpers.DOMUtils;
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.Message;
037: import org.apache.cxf.message.MessageImpl;
038: import org.apache.cxf.service.model.EndpointInfo;
039: import org.apache.cxf.test.AbstractCXFTest;
040: import org.apache.cxf.transport.Conduit;
041: import org.apache.cxf.transport.ConduitInitiator;
042: import org.apache.cxf.transport.ConduitInitiatorManager;
043: import org.apache.cxf.ws.policy.PolicyEngine;
044: import org.apache.cxf.ws.policy.WSPolicyFeature;
045: import org.apache.cxf.ws.policy.selector.FirstAlternativeSelector;
046: import org.junit.Test;
047:
048: public class MtomPolicyTest extends AbstractCXFTest {
049: String address = "http://localhost:9036/EchoService";
050:
051: @Test
052: public void testRequiredMtom() throws Exception {
053: setupServer(true);
054:
055: sendMtomMessage(address);
056:
057: Node res = invoke(address,
058: "http://schemas.xmlsoap.org/soap/http", "nonmtom.xml");
059:
060: assertValid(
061: "//faultstring[text()='None of the policy alternatives can be satisfied.']",
062: res);
063: }
064:
065: @Test
066: public void testOptionalMtom() throws Exception {
067: setupServer(false);
068:
069: sendMtomMessage(address);
070:
071: Node res = invoke(address,
072: "http://schemas.xmlsoap.org/soap/http", "nonmtom.xml");
073:
074: assertNoFault(res);
075: }
076:
077: public void setupServer(boolean mtomRequired) throws Exception {
078: getBus().getExtension(PolicyEngine.class)
079: .setAlternativeSelector(new FirstAlternativeSelector());
080: JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
081: sf.setServiceBean(new EchoService());
082: sf.setBus(getBus());
083: sf.setAddress(address);
084:
085: WSPolicyFeature policy = new WSPolicyFeature();
086: if (mtomRequired) {
087: policy.getPolicyElements().add(
088: DOMUtils.readXml(
089: getClass().getResourceAsStream(
090: "mtom-policy.xml"))
091: .getDocumentElement());
092: } else {
093: policy.getPolicyElements().add(
094: DOMUtils.readXml(
095: getClass().getResourceAsStream(
096: "mtom-policy-optional.xml"))
097: .getDocumentElement());
098: }
099:
100: sf.getFeatures().add(policy);
101:
102: sf.create();
103: }
104:
105: private void sendMtomMessage(String a) throws Exception {
106: EndpointInfo ei = new EndpointInfo(null,
107: "http://schemas.xmlsoap.org/wsdl/http");
108: ei.setAddress(a);
109:
110: ConduitInitiatorManager conduitMgr = getBus().getExtension(
111: ConduitInitiatorManager.class);
112: ConduitInitiator conduitInit = conduitMgr
113: .getConduitInitiator("http://schemas.xmlsoap.org/soap/http");
114: Conduit conduit = conduitInit.getConduit(ei);
115:
116: TestMessageObserver obs = new TestMessageObserver();
117: conduit.setMessageObserver(obs);
118:
119: Message m = new MessageImpl();
120: String ct = "multipart/related; type=\"application/xop+xml\"; "
121: + "start=\"<soap.xml@xfire.codehaus.org>\"; "
122: + "start-info=\"text/xml; charset=utf-8\"; "
123: + "boundary=\"----=_Part_4_701508.1145579811786\"";
124:
125: m.put(Message.CONTENT_TYPE, ct);
126: conduit.prepare(m);
127:
128: OutputStream os = m.getContent(OutputStream.class);
129: InputStream is = getResourceAsStream("request");
130: if (is == null) {
131: throw new RuntimeException("Could not find resource "
132: + "request");
133: }
134:
135: IOUtils.copy(is, os);
136:
137: os.flush();
138: is.close();
139: os.close();
140:
141: byte[] res = obs.getResponseStream().toByteArray();
142: MessageImpl resMsg = new MessageImpl();
143: resMsg.setContent(InputStream.class, new ByteArrayInputStream(
144: res));
145: resMsg.put(Message.CONTENT_TYPE, obs.getResponseContentType());
146: AttachmentDeserializer deserializer = new AttachmentDeserializer(
147: resMsg);
148: deserializer.initializeAttachments();
149:
150: Collection<Attachment> attachments = resMsg.getAttachments();
151: assertNotNull(attachments);
152: assertEquals(1, attachments.size());
153:
154: Attachment inAtt = attachments.iterator().next();
155: ByteArrayOutputStream out = new ByteArrayOutputStream();
156: IOUtils.copy(inAtt.getDataHandler().getInputStream(), out);
157: out.close();
158: assertEquals(37448, out.size());
159: }
160:
161: @Override
162: protected Bus createBus() throws BusException {
163: return BusFactory.getDefaultBus();
164: }
165:
166: }
|