How can we upload files to the pipeline when using JBoss FSW 6?

Solution Verified - Updated -

Environment

  • Red Hat JBoss Fuse Service Works (FSW)
    • 6.x

Issue

  • We would like to upload a file to be processed by FSW pipeline. What could be used to achieve that?

  • We are developing a Switchyard REST application. We have no issue in consuming "text/plain". But when we try to consume "multipart/form-data", we are getting the following error: "Could not find message body reader for type".

Resolution

It is possible to use a REST Binding to receive an HTTP body as an input stream:

  • Create the REST Resource:
/**
 * 
 * REST resource to receive the binary content. You could receive in another
 * ways, like use Multipart. See resteasy 2.3.x docs for more information
 * 
 * @author wsiqueir
 * 
@Path("/byte")
public interface ByteResource {

    @POST
    @Path("/")
    public void receiveBytes(InputStream is);

}
  • Make the Service:
import java.io.InputStream;

public interface ByteService {
    public void receiveBytes(InputStream is);
}
  • And an implementation for this service, for example:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.activation.DataSource;
import org.switchyard.component.bean.Service;

@Service(ByteService.class)
public class ByteServiceBean implements ByteService {

    @Override
    public void receiveBytes(InputStream is) {
        System.out.println("RECEIVING DATA...");
        // WARNING: not the best impl.., but works as a test
        try {
            File f = File.createTempFile("switchyard", "");
            FileOutputStream fos = new FileOutputStream(f);
            byte buffer[] = new byte[2048];
            while ((is.read(buffer) != -1)) {
                fos.write(buffer);
            }
            System.out.println("-- Data written on " + f.getAbsolutePath());
            fos.close();
            is.close();
        } catch (IOException e) {
            throw new Error(e);
        }
    }
}
  • Now use the REST Resource as a gateway for your service:
<switchyard xmlns="urn:switchyard-config:switchyard:1.0" xmlns:bean="urn:switchyard-component-config:1.0" xmlns:resteasy="urn:switchyard-component-config:1.0" xmlns:bean_1="urn:switchyard-component-bean:config:1.0" xmlns:resteasy_1="urn:switchyard-component-resteasy:config:1.0" xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912" xmlns:soap="urn:switchyard-component-soap:config:1.0" xmlns:transform="urn:switchyard-config:transform:1.0" xmlns:validate="urn:switchyard-config:validate:1.0" name="ByteService" targetNamespace="urn:switchyard-quickstart:bean-service:0.1.0">
  <sca:composite name="ByteService" targetNamespace="urn:switchyard-quickstart:bean-service:0.1.0">
    <sca:component name="ByteServiceBean">
      <bean_1:implementation.bean class="com.redhat.gss.fsw.ByteServiceBean"/>
      <sca:service name="ByteService">
        <sca:interface.java interface="com.redhat.gss.fsw.ByteService"/>
      </sca:service>
    </sca:component>
    <sca:service name="ByteService" promote="ByteServiceBean/ByteService">
      <sca:interface.java interface="com.redhat.gss.fsw.ByteService"/>
      <resteasy_1:binding.rest name="rest">
        <resteasy_1:interfaces>com.redhat.gss.fsw.ByteResource</resteasy_1:interfaces>
        <resteasy_1:contextPath>rest</resteasy_1:contextPath>
      </resteasy_1:binding.rest>
    </sca:service>
  </sca:composite>
</switchyard>

Attached are two sample applications which demonstrate how to receive a file in Switchyard, and how to handle multipart form requests.

Attachments

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.