Writing the marshaler class
We now have to write the marshaler class
Creating the marshaler class
Preparing an Eclipse project
In order to ease the development we will now make use of Mavens ability to create Eclipse projects. From the http-consumer-su main folder run the following comand:
This will cleanup any existing Eclipse config (eclipse:clean) and create a new eclipse project (eclipse:eclipse).
When done it should post something like:
Importing the project into Eclipse
Now it's time to start up Eclipse workbench. Choose File > Import > Existing Projects into Workspace.
Then select the projects root directory and press OK. A project named http-consumer-su should now be visible inside the Import window's projects list. Make sure it's checkbox is selected and press finish.
The project should now appear in your Package Explorer view.
Be sure you have specified the M2_REPO classpath variable to point on your local Maven repository which is located in your home folder.
For example: /home/lhe/.m2/repository
If you don't know how to specify this variable refer to the Eclipse documentation or Google it.
Create the marshaler class file
Until now we have just used our service unit to configure the behaviour of the JBI http-bc. Now we are going to extend the functionlity of this http binding component. To do this we will implement our own marshaler, which will handle incoming and outgoing messages.
For this select the src/main folder in your project. Now create a new folder under main called java. Add this new folder to your source folders.
If you did it correctly the new folder should show up as src/main/java right below the src/main/resources folder.
Now select the new folder and select File > New > Class. In the dialog enter the following:
*
*
Then hit Finish to create the class.
About the DefaultHttpConsumerMarshaler
The class DefaultHttpConsumerMarshaler implements the interface HttpConsumerMarshaler and is used as default marshaler on http:consumer endpoints
if nothing else is defined.
The interface HttpConsumerMarshaler contains the following method declarations:
The createExchange method is invoked on incoming data that is accepted. So this will be one method to override in our marshaler.
Another method which is interesting for us is the sendOut method. This method is used to send an answer back to the client and will be overridden as well.
All other methods are out of scope of this tutorial and will be untouched.
HTTPMarshaler
As said above we just subclass the DefaultHttpConsumerMarshaler to have only to code as less as possible.
We just override now the methods createExchange and sendOut to fit our needs.
createExchange()
We will use the commons-fileupload lib to have a multipart-formdata parser out-of-box.
The below code shows how it is checked if the request is a multipart-formdata request and the data is extracted.
Finally we create a message exchange and fill it with a dummy content and the uploaded file in the attachment.
sendOut()
In this method we just take the first attachment in the message exchange and write it to a temporary file.
Afterwards the response header is set and the file is posted as stream into the response's output stream.
Notice that we make use of the sendError method of our super class to handle exceptions.
The finished class
Feel free to play around with this class after you ran it once sucessfully. There is enough room for improvements.
Now the consumer SU is ready for work. Let's move on to the http-handler SU which handles the JBI messages from our consumer.
Proceed to the next step