/**
* <p> This method is responsible for generating the content and
* returning an InputStream to that content. It is also
* responsible for setting any attribute values in the
* {@link DownloadServlet#Context}, such as
* {@link DownloadServlet#EXTENSION} or
* {@link DownloadServlet#CONTENT_TYPE}.</p>
*/
public InputStream getInputStream(DownloadServlet.Context ctx) {
HttpServletRequest request =
(HttpServletRequest) ctx.getServletRequest();
String pathInfo = request.getPathInfo();
if (pathInfo == null || pathInfo.length() == 0) {
return null;
}
ServletContext sc = ctx.getServletConfig().getServletContext();
String servletPath = request.getServletPath();
String realPath = sc.getRealPath(servletPath);
File file = new File(realPath+pathInfo);
InputStream is = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException ex) {
//try classpath
is = getClass().getClassLoader().getResourceAsStream(servletPath.substring(1)+pathInfo);
}
int index = pathInfo.lastIndexOf('.');
// Set the extension so it can be mapped to a MIME type
String extnsn = pathInfo.substring(index + 1);
ctx.setAttribute(DownloadServlet.EXTENSION, extnsn); // NOI18N
ctx.setAttribute("filePath", is);
// Return an InputStream to the tmpFile
return is;
}