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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
No comments:
Post a Comment