// set proper contentType
String mimeType = context.getMimeType(pathInfo);
response.setContentType(mimeType);
// Modified for DFS support
FileSystem fs = ServiceLocator.getInstance().getFileSystem();
/*
* Shanti: Do not try and get image path here. Delegate to filesystem
// look for file in default location such as WEB-INF
String imagePath = WebappUtil.getArtifactLocalionDir() + pathInfo;
*/
// Strip leading slash from pathInfo
String imagePath;
if (pathInfo.charAt(0) == '/')
imagePath = pathInfo.substring(1);
else
imagePath = pathInfo;
logger.finer("Image path = " + imagePath);
File imageFile = new File(imagePath);
/* Assume image exists -- This was done to reduce FileSystem interaction
if(!fs.exists(imagePath)) {
System.out.println ("Could not find file - " + imagePath);
// not in default location, look in upload location
imageFile=new File(context.getRealPath("artifacts/" + pathInfo));
if (!fs.exists(imageFile.getCanonicalPath())) {
WebappUtil.getLogger().log(Level.SEVERE, "image_does_not_exist", imageFile.getCanonicalPath());
return null;
}
}
** End mod */
FileInputStream fis = null;
FileChannel in = null;
WritableByteChannel out = null;
// serve up image from proper location
// Use local file system if the image is in default location.
logger.finer("AcessArtifact -- imagePath = " + imagePath);
InputStream is = null;
try {
is = fs.open(imagePath);
} catch (FileNotFoundException nfe) {
logger.severe("File not found - imagePath = " + imagePath);
} catch (IOException ioe) {
logger.severe("IOException - imagePath = " + imagePath);
}
if (is == null) {
logger.severe("Could not open image: " + imagePath);
} else {
if (is instanceof FileInputStream) {
logger.finer("Streaming from file --");
try {
// NIO transferTo takes the slow route since we are passing along a wrapped
// buffer rather than the Socket output stream.
// From experiments we did, it looks like
// Using traditional I/O is more efficient than using NIO in our case - for
// small files.
/*
fis = (FileInputStream) (is);
in = fis.getChannel();
out = Channels.newChannel(response.getOutputStream());
in.transferTo(0, in.size(), out);
* */
byte[] buf = tlBuf.get();
int count;
while ((count = is.read(buf)) != -1) {
response.getOutputStream().write(buf, 0, count);
}
} finally {
WebappUtil.closeIgnoringException(in);
WebappUtil.closeIgnoringException(fis);
WebappUtil.closeIgnoringException(out);
}
} else {
if (bDebug) {
System.out.println("Not a FileInputStream");
}
InputStream iStream = null;
try {
OutputStream oStream = response.getOutputStream();
iStream = fs.open(imageFile.getCanonicalPath());
// With the current implementation, we only support FS, so this is a plcae holder.
// TODO - Optimize this (if required) when DFS is supported
byte[] buffer = new byte[4096];
int len;
while ((len = iStream.read(buffer)) != -1) {