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.InputStream;
021: import java.lang.reflect.InvocationHandler;
022: import java.lang.reflect.Proxy;
023: import java.lang.reflect.UndeclaredThrowableException;
024:
025: import javax.activation.DataHandler;
026: import javax.mail.util.ByteArrayDataSource;
027: import javax.xml.namespace.QName;
028: import javax.xml.ws.BindingProvider;
029: import javax.xml.ws.Holder;
030: import javax.xml.ws.soap.SOAPBinding;
031:
032: import org.apache.cxf.Bus;
033: import org.apache.cxf.BusFactory;
034: import org.apache.cxf.endpoint.Client;
035: import org.apache.cxf.endpoint.ClientImpl;
036: import org.apache.cxf.jaxws.JaxWsClientProxy;
037: import org.apache.cxf.jaxws.binding.soap.SOAPBindingImpl;
038: import org.apache.cxf.jaxws.support.JaxWsEndpointImpl;
039: import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean;
040: import org.apache.cxf.mime.TestMtom;
041: import org.apache.cxf.service.Service;
042: import org.apache.cxf.service.factory.ReflectionServiceFactoryBean;
043: import org.apache.cxf.service.model.EndpointInfo;
044: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
045: import org.junit.BeforeClass;
046: import org.junit.Test;
047:
048: public class ClientMtomXopTest extends AbstractBusClientServerTestBase {
049:
050: public static final QName MTOM_PORT = new QName(
051: "http://cxf.apache.org/mime", "TestMtomPort");
052: public static final QName MTOM_SERVICE = new QName(
053: "http://cxf.apache.org/mime", "TestMtomService");
054:
055: @BeforeClass
056: public static void startServers() throws Exception {
057: assertTrue("server did not launch correctly",
058: launchServer(Server.class));
059: }
060:
061: /*
062: @Test
063: @Ignore
064: public void testMtomSWA() throws Exception {
065: TestMtom mtomPort = createPort(MTOM_SERVICE, MTOM_PORT, TestMtom.class);
066: try {
067: InputStream pre = this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl");
068: long fileSize = 0;
069: for (int i = pre.read(); i != -1; i = pre.read()) {
070: fileSize++;
071: }
072:
073: ByteArrayDataSource bads = new ByteArrayDataSource(this.getClass().getResourceAsStream(
074: "/wsdl/mtom_xop.wsdl"), "application/octet-stream");
075: DataHandler dh = new DataHandler(bads);
076: DataHandler dhResp = mtomPort.testSWA(dh);
077: DataSource ds = dhResp.getDataSource();
078: InputStream in = ds.getInputStream();
079:
080: long count = 0;
081: for (int i = in.read(); i != -1; i = in.read()) {
082: count++;
083: }
084: assertEquals("attachemnt length different", fileSize, count);
085: } catch (UndeclaredThrowableException ex) {
086: throw (Exception) ex.getCause();
087: }
088: }
089: */
090:
091: @Test
092: public void testMtomXop() throws Exception {
093: TestMtom mtomPort = createPort(MTOM_SERVICE, MTOM_PORT,
094: TestMtom.class, true);
095: try {
096: InputStream pre = this .getClass().getResourceAsStream(
097: "/wsdl/mtom_xop.wsdl");
098: long fileSize = 0;
099: for (int i = pre.read(); i != -1; i = pre.read()) {
100: fileSize++;
101: }
102: Holder<DataHandler> param = new Holder<DataHandler>();
103: byte[] data = new byte[(int) fileSize];
104: this .getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl")
105: .read(data);
106:
107: param.value = new DataHandler(new ByteArrayDataSource(data,
108: "application/octet-stream"));
109: Holder<String> name = new Holder<String>("call detail");
110: mtomPort.testXop(name, param);
111: assertEquals("name unchanged",
112: "return detail + call detail", name.value);
113: assertNotNull(param.value);
114: } catch (UndeclaredThrowableException ex) {
115: throw (Exception) ex.getCause();
116: }
117: }
118:
119: private static <T> T createPort(QName serviceName, QName portName,
120: Class<T> serviceEndpointInterface, boolean enableMTOM)
121: throws Exception {
122: Bus bus = BusFactory.getDefaultBus();
123: ReflectionServiceFactoryBean serviceFactory = new JaxWsServiceFactoryBean();
124: serviceFactory.setBus(bus);
125: serviceFactory.setServiceName(serviceName);
126: serviceFactory.setServiceClass(serviceEndpointInterface);
127: serviceFactory.setWsdlURL(ClientMtomXopTest.class
128: .getResource("/wsdl/mtom_xop.wsdl"));
129: Service service = serviceFactory.create();
130: EndpointInfo ei = service.getEndpointInfo(portName);
131: JaxWsEndpointImpl jaxwsEndpoint = new JaxWsEndpointImpl(bus,
132: service, ei);
133: SOAPBinding jaxWsSoapBinding = new SOAPBindingImpl(ei
134: .getBinding());
135: jaxWsSoapBinding.setMTOMEnabled(enableMTOM);
136:
137: jaxwsEndpoint.getBinding().getInInterceptors().add(
138: new TestMultipartMessageInterceptor());
139: jaxwsEndpoint.getBinding().getOutInterceptors().add(
140: new TestAttachmentOutInterceptor());
141:
142: Client client = new ClientImpl(bus, jaxwsEndpoint);
143: InvocationHandler ih = new JaxWsClientProxy(client,
144: jaxwsEndpoint.getJaxwsBinding());
145: Object obj = Proxy.newProxyInstance(serviceEndpointInterface
146: .getClassLoader(), new Class[] {
147: serviceEndpointInterface, BindingProvider.class }, ih);
148: return serviceEndpointInterface.cast(obj);
149: }
150: }
|