IBM Support

How to upload a file using JSF hx:fileupload and EGL

Question & Answer


Question

How do you use the JavaServer Faces (JSF) component hx:fileupload to upload a file with Enterprise Generation Language (EGL) and IBM Rational Business Developer v8.0.1?

Cause

When using the hx:fileupload component with EGL, a question is how to map the actual value to an EGL data type.

Answer

Disclaimer

All source code and/or binaries attached to this document are referred to here as "the Program". IBM is not providing program services of any kind for the Program. IBM is providing the Program on an "AS IS" basis without warranty of any kind. IBM WILL NOT BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF IBM, OR ITS RESELLER, HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

A possible solution is to use the string type.

To store the content of the uploaded file, JSF will encode it with a Base64 encoding.

It is possible to decode it using the com.ibm.faces.util.Base64 class and the EGL ExternalType, for example:


Package test;

import java.io.FileOutputStream;
import java.io.IOException;

import com.ibm.faces.util.Base64;

public class MyBase64Decoder {

String encoded;
byte[] decoded;

public MyBase64Decoder(String value){
encoded=value;
}

public void writeToFile(String fileName) throws IOException{
decode();
FileOutputStream out = new FileOutputStream(fileName);
out.write(decoded);
out.close();
}

public int getSize(){
decode();
return decoded.length;
}

private void decode(){
if (decoded==null)
decoded=Base64.decode(encoded);
}
}

  • The EGL type could be something like:


ExternalType Base64Decoder type JavaObject
{
  JavaName = "MyBase64Decoder",
  PackageName = "test"
}
function writeToFile(fileName string in);
function getSize() returns(int);

constructor(encoded string in);
end

  • And the EGL page handler like:


handler TestUpload type JSFHandler{onConstructionFunction = onConstruction, onPrerenderFunction = onPrerender, view = "TestUpload.jsp", viewRootVar = viewRoot}

    viewRoot UIViewRoot;
    file string;
    fileName string;

    // Function Declarations

    function onConstruction()
    end

    function onPrerender()
    end

    function btnUploadAction()
        try
        decoder Base64Decoder = new Base64Decoder(file);
            SysLib.writeStdout("file received, name="+fileName+",
                  size="+decoder.getSize());            
decoder.writeToFile("c:\\temp\\uploaded2.dat");

        onException(ex AnyException)
        SysLib.writeStderr("error="+ex.message);
        end
    end
end


  • And finally the JSF page:


<hx:fileupload styleClass="fileupload" id="fileupload1"
value="#{TestUpload.file}">
<hx:fileProp name="fileName" value="#{TestUpload.fileName}" />
</hx:fileupload>



Note on how to limit the size of the upload:

The upload size can be limited at the JSF level by parameter com.ibm.faces.MAX_REQUEST_CONTENT_SIZE in web.xml.

This parameter will not prevent the application server from uploading the complete file before pssaing it the its Web container and finally JSF. This means for example that a file of 1Gb will be uploaded completely before being rejected by JSF so it can possibly cause OutOfMemory exceptions.

Another possibility is then to limit the size at the application server level. See the link in the below section on how to do it with Websphere Application Server:

  • either using PostSizeLimit
    or
  • HTTP request chunking



To test the attached project:

  1. Import it into RBD 8.0.1 using File > Import > General > Existing Projects into Workspace and then select from archive file

  2. Add the missing jar files under WEB-INF\lib: fda7.jar, jsf-ibm.jar, odc-jsf.jar and rte.jar which has been removed to reduce the size of the zip file (they can be copied over from a new EGL JSF Web project).

  3. Create the missing JavaSource folders using menu File > New > Folder

  4. Publish it on a WebSphere Application Server 7.0

  5. On the project Explorer, right-click on page WebContent/MyUploadPage.jsp and select Run As..Run on Server

eglfileupload.zipeglfileupload.zip
eglfileupload (zip)

[{"Product":{"code":"SSMQ79","label":"Rational Business Developer"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Component":"General Information","Platform":[{"code":"PF033","label":"Windows"}],"Version":"7.1;7.1.0.1;8.0.1;8.0.1.1;8.0.1.2","Edition":"","Line of Business":{"code":"LOB35","label":"Mainframe SW"}}]

Document Information

Modified date:
02 August 2018

UID

swg21318912