Appfuse and Servlets
I needed to use a servlet, in my appfuse based application, to handle the download of certain files to the web client. Basically users will upload files, and users satisfying certain conditions will be able to download those files. I need to use a servlet because I want to avoid users linking to the file directly, and user based authentication is not appropriate in this case.
So I set about implementing a servlet to do the job. The download action establishes whether the user should be given the file and which file to give them, then stores the name of the file and the path to the file, in the request object. I set a global forward mapping to the servlet in metadata\web\global-forwards.xml. So I just forward my action to that forward mapping to send control over to the servlet. The servlet reads 4 Kb blocks from file and posts them out to the servletoutput stream.
The code in the servlet for reading and posting file data looks like this:
String fileName = (String) request.getAttribute("fileName");
String filePath = (String) request.getAttribute("filePath");
//new method
if (filePath != null)
{
//Get handle on file
File file = new File(filePath);
FileInputStream fileIn = new FileInputStream(file);
//Get and set content type
String contentType = getServletContext().getMimeType( filePath );
if ( contentType != null ){
response.setContentType ( contentType ) ;
}else{
response.setContentType ( "application/octet-stream" ) ;
}
response.addHeader( "Content-Disposition", "attachment; filename=" + fileName) ;
//Gets servlet output stream
ServletOutputStream outStream = response.getOutputStream();
//4kbuffer
byte[] buffer = new byte[4 * 1024];
//Read file bytes into buffer and send to client
int readCount =0;
while ((readCount=fileIn.read(buffer))!=-1){
//outStream.write(buffer,sent,file.length());
outStream.write(buffer,0,readCount);
}
//Clean up
outStream.flush();
outStream.close();
}
Integrating the servlet into appfuse, meant bypassing the documented way of doing things. I had to set up serlet mappings in web.xml directly, because the webdoclet tags didn't work and neither did putting settings in the appfuses documented servlets config file. shrug.
I needed to use a servlet, in my appfuse based application, to handle the download of certain files to the web client. Basically users will upload files, and users satisfying certain conditions will be able to download those files. I need to use a servlet because I want to avoid users linking to the file directly, and user based authentication is not appropriate in this case.
So I set about implementing a servlet to do the job. The download action establishes whether the user should be given the file and which file to give them, then stores the name of the file and the path to the file, in the request object. I set a global forward mapping to the servlet in metadata\web\global-forwards.xml. So I just forward my action to that forward mapping to send control over to the servlet. The servlet reads 4 Kb blocks from file and posts them out to the servletoutput stream.
The code in the servlet for reading and posting file data looks like this:
String fileName = (String) request.getAttribute("fileName");
String filePath = (String) request.getAttribute("filePath");
//new method
if (filePath != null)
{
//Get handle on file
File file = new File(filePath);
FileInputStream fileIn = new FileInputStream(file);
//Get and set content type
String contentType = getServletContext().getMimeType( filePath );
if ( contentType != null ){
response.setContentType ( contentType ) ;
}else{
response.setContentType ( "application/octet-stream" ) ;
}
response.addHeader( "Content-Disposition", "attachment; filename=" + fileName) ;
//Gets servlet output stream
ServletOutputStream outStream = response.getOutputStream();
//4kbuffer
byte[] buffer = new byte[4 * 1024];
//Read file bytes into buffer and send to client
int readCount =0;
while ((readCount=fileIn.read(buffer))!=-1){
//outStream.write(buffer,sent,file.length());
outStream.write(buffer,0,readCount);
}
//Clean up
outStream.flush();
outStream.close();
}
Integrating the servlet into appfuse, meant bypassing the documented way of doing things. I had to set up serlet mappings in web.xml directly, because the webdoclet tags didn't work and neither did putting settings in the appfuses documented servlets config file. shrug.