// verify charset
Charset charset = null;
if (charSet != null) {
if (Charset.isSupported(charSet)) {
charset = Charset.forName(charSet);
}
else {
_log.warn("charset ["+charSet+"] is not recognized ");
charset = null;
}
}
HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE);
ServletContext servletContext = (ServletContext) invocation.getInvocationContext().get(SERVLET_CONTEXT);
if (charset != null) {
response.setContentType("text/plain; charset="+charSet);
}
else {
response.setContentType("text/plain");
}
response.setHeader("Content-Disposition", "inline");
PrintWriter writer = response.getWriter();
InputStreamReader reader = null;
try {
if (charset != null) {
reader = new InputStreamReader(servletContext.getResourceAsStream(finalLocation), charset);
}
else {
reader = new InputStreamReader(servletContext.getResourceAsStream(finalLocation));
}
if (reader == null) {
_log.warn("resource at location ["+finalLocation+"] cannot be obtained (return null) from ServletContext !!! ");
}
else {
char[] buffer = new char[BUFFER_SIZE];
int charRead = 0;
while((charRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, charRead);
}
}
}
finally {
if (reader != null)
reader.close();
if (writer != null) {
writer.flush();
writer.close();
}
}