Preloader image

Help us document this example! Click the blue pencil icon in the upper right to edit this page.

AttachmentImpl

package org.superbiz.attachment;

import jakarta.activation.DataHandler;
import jakarta.activation.DataSource;
import jakarta.ejb.Stateless;
import jakarta.jws.WebService;
import jakarta.xml.ws.BindingType;
import jakarta.xml.ws.soap.SOAPBinding;
import java.io.IOException;
import java.io.InputStream;

/**
 * This is an EJB 3 style pojo stateless session bean
 * Every stateless session bean implementation must be annotated
 * using the annotation @Stateless
 * This EJB has a single interface: {@link AttachmentWs} a webservice interface.
 */
@Stateless
@WebService(
        portName = "AttachmentPort",
        serviceName = "AttachmentWsService",
        targetNamespace = "http://superbiz.org/wsdl",
        endpointInterface = "org.superbiz.attachment.AttachmentWs")
@BindingType(value = SOAPBinding.SOAP12HTTP_MTOM_BINDING)
public class AttachmentImpl implements AttachmentWs {

    public String stringFromBytes(byte[] data) {
        return new String(data);
    }

    public String stringFromDataSource(DataSource source) {

        try {
            InputStream inStr = source.getInputStream();
            int size = inStr.available();
            byte[] data = new byte[size];
            inStr.read(data);
            inStr.close();
            return new String(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public String stringFromDataHandler(DataHandler handler) {

        try {
            return (String) handler.getContent();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}

AttachmentWs

package org.superbiz.attachment;

import jakarta.activation.DataHandler;
import jakarta.jws.WebService;

/**
 * This is an EJB 3 webservice interface to send attachments throughout SAOP.
 */
@WebService(targetNamespace = "http://superbiz.org/wsdl")
public interface AttachmentWs {

    public String stringFromBytes(byte[] data);

    // Not working at the moment with SUN saaj provider and CXF
    //public String stringFromDataSource(DataSource source);

    public String stringFromDataHandler(DataHandler handler);
}

ejb-jar.xml

<ejb-jar/>

AttachmentTest

package org.superbiz.attachment;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import jakarta.activation.DataHandler;
import jakarta.activation.DataSource;
import jakarta.mail.util.ByteArrayDataSource;
import javax.xml.namespace.QName;
import jakarta.xml.ws.BindingProvider;
import jakarta.xml.ws.Service;
import jakarta.xml.ws.soap.SOAPBinding;
import java.net.URL;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@RunWith(Arquillian.class)
public class AttachmentTest {

    //START SNIPPET: setup
    @ArquillianResource
    private URL base;

    @Deployment(testable = false)
    public static WebArchive war() {
        return ShrinkWrap.create(WebArchive.class, "webservice-attachments.war")
                .addClasses(AttachmentImpl.class, AttachmentWs.class);
    }
    //END SNIPPET: setup

    /**
     * Create a webservice client using wsdl url
     *
     * @throws Exception
     */
    //START SNIPPET: webservice
    @Test
    public void testAttachmentViaWsInterface() throws Exception {
        Service service = Service.create(
                new URL(base.toExternalForm() + "webservices/AttachmentImpl?wsdl"),
                new QName("http://superbiz.org/wsdl", "AttachmentWsService"));
        assertNotNull(service);

        AttachmentWs ws = service.getPort(AttachmentWs.class);

        // retrieve the SOAPBinding
        SOAPBinding binding = (SOAPBinding) ((BindingProvider) ws).getBinding();
        binding.setMTOMEnabled(true);

        String request = "tsztelak@gmail.com";

        // Byte array
        String response = ws.stringFromBytes(request.getBytes());
        assertEquals(request, response);

        // Data Source
        DataSource source = new ByteArrayDataSource(request.getBytes(), "text/plain; charset=UTF-8");

        // not yet supported !
        //        response = ws.stringFromDataSource(source);
        //        assertEquals(request, response);

        // Data Handler
        response = ws.stringFromDataHandler(new DataHandler(source));
        assertEquals(request, response);

    }
    //END SNIPPET: webservice

}

Running

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.superbiz.attachment.AttachmentTest
INFO: Starting Servlet engine: [Apache Tomcat/11.0.25]
INFO: Version: 10.3.0-SNAPSHOT
INFO: ------------------------- localhost -> /webservice-attachments
INFO: Configuring enterprise application: /path/to/examples/webservice-attachments/target/tomee-embedded/0/webservice-attachments
INFO: Auto-deploying ejb AttachmentImpl: EjbDeployment(deployment-id=AttachmentImpl)
INFO: Created Ejb(deployment-id=AttachmentImpl, ejb-name=AttachmentImpl, container=Default Stateless Container)
INFO: Started Ejb(deployment-id=AttachmentImpl, ejb-name=AttachmentImpl, container=Default Stateless Container)
INFO: Webservice(wsdl=http://localhost:53476/webservice-attachments/webservices/AttachmentImpl, qname={http://superbiz.org/wsdl}AttachmentWsService) --> Ejb(id=AttachmentImpl)
INFO: Deployed Application(path=/path/to/examples/webservice-attachments/target/tomee-embedded/0/webservice-attachments)
INFO: Creating Service {http://superbiz.org/wsdl}AttachmentWsService from WSDL: http://localhost:53476/webservice-attachments/webservices/AttachmentImpl?wsdl
INFO: Undeploying app: /path/to/examples/webservice-attachments/target/tomee-embedded/0/webservice-attachments
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.997 s -- in org.superbiz.attachment.AttachmentTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0