Wednesday Mar 26, 2014

File Upload Using WSDC for an MTOM WebService

Use Case:

In this usecase I will explain about how to upload file in WSDC to the target WebService from the Backing Bean 

What is MTOM ?

MTOM or  Message Transmission Optimization Mechanism is the W3C Message Transmission Optimization Mechanism, a method of efficiently sending binary data to and from Web services.

Implementation:

Lets start by creating a Java WebService with MTOM Enabled to allow transfer of files as shown below.

In the service provide a location where the files have to be saved else create a folder called mytemp in C drive if planning to use the code below. 

package project1;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataHandler;

import javax.jws.Oneway;
import javax.jws.WebMethod;
import javax.jws.WebParam;

import javax.jws.WebService;

import javax.xml.bind.annotation.XmlMimeType;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.soap.MTOM;

@WebService(serviceName = "UploadService")
@MTOM
public class UploadService {
    public UploadService() {
        super();
    }

    @WebMethod
    @Oneway
    public void fileUpload(@WebParam(name = "arg0") String fileName, @XmlMimeType("application/octet-stream") @WebParam(name = "arg1") DataHandler data) {
    try {
       
 OutputStream os = new FileOutputStream("C:\\mytemp\\"+fileName);
        InputStream is = data.getInputStream();          
                 byte[] buffer = new byte[1024];
                 int bytesRead;
                 //read from is to buffer
                 while((bytesRead = is.read(buffer)) !=-1){
                     os.write(buffer, 0, bytesRead);
                 }
                 is.close();
                 //flush OutputStream to write any buffered data to file
                 os.flush();
                 os.close();
        
    }
    catch (Exception e)
    {
            throw new WebServiceException(e);
    }
    }
}

Now deploy the Service to Integrated WLS.

Create an ADF Fusion Application and create WebService Data Control from the New Gallery using the WSDL URL of the service deployed. After creating the DC , expand the DC palette where we can observe that the FileUpload method takes 2 arguments , the first argument is the file Name which String type and the Second Argument is Object type 

DCPalette

Now in the ViewController, create a jspx page with Backing Bean or we can create the backing bean separately and bind to the page.

In the Backing Bean add the following Two Methods

// This Method is used to read the file provided by the user and pass it to the DC
public void fileUploaded(ValueChangeEvent event)
     {
       UploadedFile file = (UploadedFile) event.getNewValue();
       if (file != null)
       {
        FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage message = new FacesMessage("Successfully uploaded file " + file.getFilename()+" (" + file.getLength()+" bytes)");
        context.addMessage(event.getComponent().getClientId(context), message);
        DCBindingContainer bc = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
        OperationBinding op = bc.getOperationBinding("fileUpload");
            try {
                //Convert the file Data into Byte Array and then pass this to the DC
                byte[] buffer = readFully(file.getInputStream());
                if(op!=null){
                   Map param = op.getParamsMap();
                   param.put("arg0",file.getFilename());
                   param.put("arg1",buffer);
                    op.execute();
                }
            } catch (IOException e) {
            }
            
       }
     }
    public byte[] readFully(InputStream input) throws IOException
    {
        byte[] buffer = new byte[8192];
        int bytesRead;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        while ((bytesRead = input.read(buffer)) != -1)
        {
            output.write(buffer, 0, bytesRead);
        }
        return output.toByteArray();
    }

In jspx page, Drag and drop the ADF Input File Component  and bind the Value Change Listener component to the fileUploaded() Method given above

pageSource

Go to the page definitions of the page, in  the bindings add the Method Action as shown below


Once after adding the method action, drag &drop a button onto the page for submit.Now when the jspx page is rendered , browse and select a file then click on Submit button 

createREST

Now we can observe that file gets transferred to the location mentioned in the Service



About

Tips and Tricks from Oracle's JDeveloper & ADF QA

Search

Archives
« March 2014
SunMonTueWedThuFriSat
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
27
28
29
30
31
     
Today