001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.cxfbc.mtom;
018:
019: import java.io.InputStream;
020: import java.lang.reflect.InvocationHandler;
021: import java.lang.reflect.Proxy;
022: import java.lang.reflect.UndeclaredThrowableException;
023:
024: import javax.activation.DataHandler;
025: import javax.mail.util.ByteArrayDataSource;
026: import javax.xml.namespace.QName;
027: import javax.xml.ws.BindingProvider;
028: import javax.xml.ws.Holder;
029: import javax.xml.ws.soap.SOAPBinding;
030:
031: import org.apache.cxf.Bus;
032: import org.apache.cxf.BusFactory;
033: import org.apache.cxf.endpoint.Client;
034: import org.apache.cxf.endpoint.ClientImpl;
035: import org.apache.cxf.jaxws.JaxWsClientProxy;
036: import org.apache.cxf.jaxws.binding.soap.SOAPBindingImpl;
037: import org.apache.cxf.jaxws.support.JaxWsEndpointImpl;
038: import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean;
039: import org.apache.cxf.mime.TestMtom;
040: import org.apache.cxf.service.Service;
041: import org.apache.cxf.service.factory.ReflectionServiceFactoryBean;
042: import org.apache.cxf.service.model.EndpointInfo;
043: import org.apache.servicemix.tck.SpringTestSupport;
044: import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
045: import org.springframework.context.support.AbstractXmlApplicationContext;
046:
047: public class CxfBCMtomTest extends SpringTestSupport {
048:
049: public static final QName MTOM_PORT = new QName(
050: "http://cxf.apache.org/mime", "TestMtomPort");
051:
052: public static final QName MTOM_SERVICE = new QName(
053: "http://cxf.apache.org/mime", "TestMtomService");
054:
055: public void testMtom() throws Exception {
056: TestMtom mtomPort = createPort(MTOM_SERVICE, MTOM_PORT,
057: TestMtom.class, true);
058: try {
059:
060: Holder<DataHandler> param = new Holder<DataHandler>();
061:
062: param.value = new DataHandler(new ByteArrayDataSource(
063: "foobar".getBytes(), "application/octet-stream"));
064:
065: Holder<String> name = new Holder<String>("call detail");
066: mtomPort.testXop(name, param);
067: assertEquals("call detailfoobar", name.value);
068: assertNotNull(param.value);
069: InputStream bis = param.value.getDataSource()
070: .getInputStream();
071: byte b[] = new byte[10];
072: bis.read(b, 0, 10);
073: String attachContent = new String(b);
074: assertEquals(attachContent, "testfoobar");
075: } catch (UndeclaredThrowableException ex) {
076: throw (Exception) ex.getCause();
077: }
078: }
079:
080: @Override
081: protected AbstractXmlApplicationContext createBeanFactory() {
082: return new ClassPathXmlApplicationContext(
083: "org/apache/servicemix/cxfbc/mtom/xbean.xml");
084: }
085:
086: private <T> T createPort(QName serviceName, QName portName,
087: Class<T> serviceEndpointInterface, boolean enableMTOM)
088: throws Exception {
089: Bus bus = BusFactory.getDefaultBus();
090: ReflectionServiceFactoryBean serviceFactory = new JaxWsServiceFactoryBean();
091: serviceFactory.setBus(bus);
092: serviceFactory.setServiceName(serviceName);
093: serviceFactory.setServiceClass(serviceEndpointInterface);
094: serviceFactory.setWsdlURL(getClass().getResource(
095: "/wsdl/mtom_xop.wsdl"));
096: Service service = serviceFactory.create();
097: EndpointInfo ei = service.getEndpointInfo(portName);
098: JaxWsEndpointImpl jaxwsEndpoint = new JaxWsEndpointImpl(bus,
099: service, ei);
100: SOAPBinding jaxWsSoapBinding = new SOAPBindingImpl(ei
101: .getBinding());
102: jaxWsSoapBinding.setMTOMEnabled(enableMTOM);
103:
104: Client client = new ClientImpl(bus, jaxwsEndpoint);
105: InvocationHandler ih = new JaxWsClientProxy(client,
106: jaxwsEndpoint.getJaxwsBinding());
107: Object obj = Proxy.newProxyInstance(serviceEndpointInterface
108: .getClassLoader(), new Class[] {
109: serviceEndpointInterface, BindingProvider.class }, ih);
110: return serviceEndpointInterface.cast(obj);
111: }
112:
113: }
|