About:
In my previous post, I explained on how to create a Restful service using which File Upload can be performed without using Multipart Form data. In this blog, i will be explaining on how to consume such a service in RestDataControl in ADF.
Pre-requisites:
1. Lets create a jersey rest service as per this blog and deploy it to the server
2. Now create a XSD file for the service as shown below
<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="fileData">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="fileInfo" type="xsd:base64Binary"/>
<xsd:element name="fileName" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Description:
Create a Fusion Web Application and using the url of the rest service deployed above create a Rest Data Control from New -> Gallery -> WebService DataControl and select Type as REST.
Now create a jspx page and backing bean in the view controller. Then create pagedef for the page by right clicking on the jspx page and selecting Go to PageDef . In the Page Definition add the methodAction from the DC
/**
* Method to Upload the file and post it to the rest service
*/
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("posting");
try {
byte[] buffer = readFully(file.getInputStream());
String encodedStr = new sun.misc.BASE64Encoder().encode(buffer);
if(op!=null){
Map tempMap = new HashMap();
tempMap.put("fileInfo",encodedStr);
tempMap.put("fileName",file.getFilename());
op.getParamsMap().put("fileData", tempMap);
op.execute();
}
} catch (IOException e) {
System.out.println(e);
}
}
}
/**
* Method to convert the InputStream to ByteArray
*/
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 the Page create a InputFileComponent and bind the ValuechangeListener to the fileUploaded method in the backing bean. Now add Command button for Submit.
That's it we are ready to transfer the file, run the jspx page and select the file to be transferred using the browse button and click on submit.




Very useful. Thanks Alex!
ReplyDelete