SyntaxHighlighter

Friday, September 21, 2012

WebSphere Gotcha - Streaming in WAS 8

WebSphere Gotcha - Streaming in WAS 8

here's a little gotcha in WebSphere 8; if you're using Spring MVC and you decide to serve non-jsp content from behind the WEB-INF directory (e.g. a javascript file), the ViewResolver must return it as a jsp page, otherwise WAS will block it.

In tomcat or other servlet container it will work, but in WebSphere, I can't get it to.

My workaround

create a controller that will stream it back in the HttpServletResponse object

package org.incompletecode.spring.web.controller;
import java.io.InputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/service")
public class StreamController implements ApplicationContextAware {
private ApplicationContext applicationContext;
private String defaultLocation = "WEB-INF/jsp/";
@RequestMapping("/stream")
public void stream(@RequestParam("fileName")String fileName,HttpServletResponse response) throws Exception {
//retrieve the file from the location
InputStream in = applicationContext.getResource(defaultLocation + fileName).getInputStream();
//stream it out
int i = -1;
while ((i = in.read()) > 0) {
response.getOutputStream().write(i);
}//end while
//flush
response.getOutputStream().flush();
//close
response.getOutputStream().close();//done
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
view raw gistfile1.java hosted with ❤ by GitHub

No comments:

Post a Comment