| | |
| | import javax.servlet.ServletException; |
| | - |
| | import javax.servlet.http.HttpServlet; |
| | import javax.servlet.http.HttpServletRequest; |
 |
| | import javax.xml.transform.TransformerFactory; |
| | import javax.xml.transform.Transformer; |
| | - |
| | -import org.apache.shiro.subject.Subject; |
| | -import org.apache.shiro.SecurityUtils; |
| | -import org.apache.shiro.authc.AuthenticationToken; |
| | -import org.apache.shiro.authc.UsernamePasswordToken; |
| | -import org.apache.shiro.web.util.SavedRequest; |
| | -import org.apache.shiro.web.util.WebUtils; |
| | |
| | import to.discuss.Base; |
 |
| | * Main HTTP request handler for /app. |
| | */ |
| | -@SuppressWarnings("serial") |
| | public class App extends HttpServlet implements Base { |
| | - private static final Logger LOG = LoggerFactory.getLogger( App.class ); |
| | - |
| | - private HttpServletRequest request; |
| | - private HttpServletResponse response; |
| | - |
| | - private int responseStatus = SC_OK; |
| | + protected static final Logger LOG = LoggerFactory.getLogger( App.class ); |
| | |
| | public App() { |
 |
| | HttpServletResponse response ) throws ServletException |
| | { |
| | - setRequest( request ); |
| | - setResponse( response ); |
| | - |
| | try { |
| | - sendHeader(); |
| | - sendContent(); |
| | + // Ensure the finally block is called if this throws an exception. |
| | + if( preprocess( request, response ) ) { |
| | + sendHeader( response ); |
| | + sendContent( response ); |
| | + } |
| | } |
| | catch( Exception e ) { |
| | throw new ServletException( e ); |
| | + } |
| | + finally { |
| | + postprocess( request, response ); |
| | } |
| | + } |
| | + |
| | + /** |
| | + * Called before the header is sent, but after the request and response |
| | + * variable have been set. This will return false to indicate that |
| | + * content should not be sent (usually in the case of a redirect). |
| | + * |
| | + * @return true Send the header and content (via XSL transformation). |
| | + */ |
| | + protected boolean preprocess( |
| | + HttpServletRequest request, |
| | + HttpServletResponse response ) throws ServletException { |
| | + |
| | + return true; |
| | + } |
| | + |
| | + /** |
| | + * Called after the content is sent. This is guaranteed to be called, even |
| | + * if the servlet throws an exception during preprocessing. |
| | + */ |
| | + protected void postprocess( |
| | + HttpServletRequest request, |
| | + HttpServletResponse response ) throws ServletException { |
| | } |
| | |
| | /** |
| | * Writes the header information. |
| | */ |
| | - protected void sendHeader() throws Exception { |
| | - getResponse().setContentType( getContentType() ); |
| | - getResponse().setStatus( getResponseStatus() ); |
| | + protected void sendHeader( HttpServletResponse response ) throws Exception { |
| | + response.setContentType( getContentType() ); |
| | + response.setStatus( getResponseStatus() ); |
| | } |
| | |
| | /** |
| | * Returns the default content type. |
| | * |
| | * @return "text/html" |
| | */ |
| | protected String getContentType() { |
| | return "text/html"; |
| | + } |
| | + |
| | + /** |
| | + * Returns the default response status. |
| | + * |
| | + * @return 200 OK |
| | + */ |
| | + protected int getResponseStatus() { |
| | + return SC_OK; |
| | } |
| | |
 |
| | * content to the client. |
| | */ |
| | - private void sendContent() throws Exception { |
| | - //Subject user = SecurityUtils.getSubject(); |
| | - |
| | + private void sendContent( HttpServletResponse response ) throws Exception { |
| | Transformer transformer = getTransformer(); |
| | transformer.setOutputProperty( OutputKeys.ENCODING, getEncoding() ); |
| | - transformer.transform( getDocument(), getResultStream() ); |
| | + transformer.transform( getDocument(), getResultStream( response ) ); |
| | } |
| | |
| | - protected Result getResultStream() throws IOException { |
| | - return new StreamResult( getResponse().getOutputStream() ); |
| | + protected Result getResultStream( HttpServletResponse response ) |
| | + throws IOException { |
| | + return new StreamResult( response.getOutputStream() ); |
| | } |
| | |
 |
| | public String getAppName() { |
| | return getClass().getSimpleName().toLowerCase(); |
| | - } |
| | - |
| | - private void setRequest( HttpServletRequest request ) { |
| | - this.request = request; |
| | - } |
| | - |
| | - private HttpServletRequest getRequest() { |
| | - return this.request; |
| | - } |
| | - |
| | - private void setResponse( HttpServletResponse response ) { |
| | - this.response = response; |
| | - } |
| | - |
| | - private HttpServletResponse getResponse() { |
| | - return this.response; |
| | - } |
| | - |
| | - /** |
| | - * Return HTTP response status to return to the client. |
| | - * |
| | - * @return An HTTP status code (200 OK by default). |
| | - */ |
| | - private int getResponseStatus() { |
| | - return this.responseStatus; |
| | - } |
| | - |
| | - /** |
| | - * Set HTTP response status to return to the client. |
| | - * |
| | - * @param responseStatus The status code to send to the client. |
| | - */ |
| | - private void setResponseStatus( int responseStatus ) { |
| | - this.responseStatus = responseStatus; |
| | } |
| | } |