PageRenderTime 24ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/sitebricks/src/main/java/com/google/sitebricks/SitebricksFilter.java

http://github.com/dhanji/sitebricks
Java | 98 lines | 70 code | 17 blank | 11 comment | 12 complexity | 667c2fb43f68ea91b769ac24a51fab4a MD5 | raw file
 1package com.google.sitebricks;
 2
 3import java.io.IOException;
 4import java.net.URLEncoder;
 5
 6import javax.servlet.Filter;
 7import javax.servlet.FilterChain;
 8import javax.servlet.FilterConfig;
 9import javax.servlet.ServletException;
10import javax.servlet.ServletRequest;
11import javax.servlet.ServletResponse;
12import javax.servlet.http.HttpServletRequest;
13import javax.servlet.http.HttpServletResponse;
14
15import net.jcip.annotations.Immutable;
16
17import com.google.inject.Inject;
18import com.google.inject.Provider;
19import com.google.inject.Singleton;
20import com.google.sitebricks.headless.Reply;
21import com.google.sitebricks.headless.ReplyBasedHeadlessRenderer;
22import com.google.sitebricks.headless.Request;
23import com.google.sitebricks.routing.RoutingDispatcher;
24import com.google.sitebricks.routing.RoutingDispatcher.Events;
25
26/**
27 * @author Dhanji R. Prasanna (dhanji@gmail.com)
28 */
29@Immutable
30@Singleton
31class SitebricksFilter implements Filter {
32  private final RoutingDispatcher dispatcher;
33  private final Provider<Bootstrapper> bootstrapper;
34  private final Provider<Shutdowner> teardowner;
35
36  private final Provider<Request> requestProvider;
37  private final ReplyBasedHeadlessRenderer headlessRenderer;
38
39  @Inject
40  public SitebricksFilter(RoutingDispatcher dispatcher, Provider<Bootstrapper> bootstrapper,
41                          Provider<Shutdowner> teardowner, Provider<Request> requestProvider,
42                          ReplyBasedHeadlessRenderer headlessRenderer) {
43    this.dispatcher = dispatcher;
44    this.bootstrapper = bootstrapper;
45    this.teardowner = teardowner;
46    this.requestProvider = requestProvider;
47    this.headlessRenderer = headlessRenderer;
48  }
49
50  public void init(FilterConfig filterConfig) throws ServletException {
51    bootstrapper.get().start();
52  }
53
54  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
55                       FilterChain filterChain)
56      throws IOException, ServletException {
57
58    HttpServletRequest request = (HttpServletRequest) servletRequest;
59    HttpServletResponse response = (HttpServletResponse) servletResponse;
60
61    //dispatch
62    Object respondObject = dispatcher.dispatch(this.requestProvider.get(), Events.DURING);
63
64    //was there any matching page? (if it was a headless response, we don't need to do anything).
65    // Also we do not do anything if the page elected to do nothing.
66    if (null != respondObject && null == request.getAttribute(Reply.NO_REPLY_ATTR)) {
67
68      // Only use the string rendering pipeline if this is not a headless request.
69      if (respondObject instanceof Respond) {
70        Respond respond = (Respond) respondObject;
71      
72        //do we need to redirect or was this a successful render?
73        final String redirect = respond.getRedirect();
74        if (null != redirect) {
75            response.sendRedirect(redirect);
76            } else { //successful render
77
78          // by checking if a content type was set, we allow users to override content-type
79          //  on an arbitrary basis
80          if (null == response.getContentType()) {
81            response.setContentType(respond.getContentType());
82          }
83
84          response.getWriter().write(respond.toString());
85        }
86      } else { // It must be a headless Reply. Render the headless response.
87        headlessRenderer.render(response, respondObject);
88      }
89    } else {
90      //continue down filter-chain
91      filterChain.doFilter(request, response);
92    }
93  }
94
95  public void destroy() {
96    teardowner.get().shutdown();
97  }
98}