View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts.servlet.mvc;
20  
21  import java.util.Locale;
22  
23  import javax.servlet.ServletException;
24  
25  import org.springframework.context.ApplicationContext;
26  import org.springframework.context.ApplicationEvent;
27  import org.springframework.context.ApplicationListener;
28  import org.springframework.context.event.ContextRefreshedEvent;
29  import org.springframework.extensions.config.ConfigService;
30  import org.springframework.extensions.webscripts.Match;
31  import org.springframework.extensions.webscripts.RuntimeContainer;
32  import org.springframework.extensions.webscripts.servlet.ServletAuthenticatorFactory;
33  import org.springframework.web.servlet.view.AbstractUrlBasedView;
34  import org.springframework.web.servlet.view.UrlBasedViewResolver;
35  
36  /**
37   * Resolves views to Web Scripts
38   * 
39   * @author muzquiano
40   */
41  public class WebScriptViewResolver extends UrlBasedViewResolver implements ApplicationListener 
42  {
43      protected RuntimeContainer container;
44      
45      protected ConfigService configService;
46      protected ServletAuthenticatorFactory authenticatorFactory;
47      
48      public WebScriptViewResolver() 
49      {
50          Class viewClass = requiredViewClass();
51          setViewClass(viewClass);
52      }
53  
54      /**
55       * Sets the container.
56       * 
57       * @param container the new container
58       */
59      public void setContainer(RuntimeContainer container)
60      {
61          this.container = container;
62      }
63  
64      /**
65       * Sets the authenticator factory.
66       * 
67       * @param authenticatorFactory the new authenticator factory
68       */
69      public void setAuthenticatorFactory(ServletAuthenticatorFactory authenticatorFactory)
70      {
71          this.authenticatorFactory = authenticatorFactory;
72      }
73      
74      /* (non-Javadoc)
75       * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
76       */
77      public void onApplicationEvent(ApplicationEvent event)
78      {
79          if (event instanceof ContextRefreshedEvent)
80          {
81              ContextRefreshedEvent refreshEvent = (ContextRefreshedEvent)event;            
82              ApplicationContext refreshContext = refreshEvent.getApplicationContext();
83              if (refreshContext != null && refreshContext.equals(getApplicationContext()))
84              {
85                  onBootstrap();
86              }
87          }
88      }
89          
90      /**
91       * Initializes the view resolver
92       * 
93       * @throws ServletException
94       */
95      public void onBootstrap()
96      {
97          ApplicationContext context = this.getApplicationContext();
98          configService = (ConfigService)context.getBean("web.config");
99          
100         // ensure that we have a container
101         if (container == null)
102         {
103             container = (RuntimeContainer)context.getBean("webscripts.container");
104         }        
105         
106         String authenticatorId = null;
107         if (authenticatorFactory != null)
108         {
109             authenticatorId = authenticatorFactory.getClass().getName();
110         }
111         
112         if (logger.isDebugEnabled())
113             logger.debug("Initialised Web Script View Resolver (authenticator='" + authenticatorId + "')");
114     }
115     
116     
117     
118     /* (non-Javadoc)
119      * @see org.springframework.web.servlet.view.UrlBasedViewResolver#canHandle(java.lang.String, java.util.Locale)
120      */
121     protected boolean canHandle(String viewName, Locale locale) 
122     {
123         String uri = viewName;
124         
125         // path corrections
126         if (uri != null)
127         {
128             if (!uri.startsWith("/"))
129             {
130                 uri = "/" + uri;
131             }
132         }
133         
134         // check the web script registry to see if this web script exists
135         Match match = container.getRegistry().findWebScript("get", uri);
136         return (match != null && match.getWebScript() != null);
137     }
138     
139 
140     /* (non-Javadoc)
141      * @see org.springframework.web.servlet.view.UrlBasedViewResolver#buildView(java.lang.String)
142      */
143     protected AbstractUrlBasedView buildView(String viewName) throws Exception 
144     {
145         AbstractUrlBasedView view = null;
146         
147         String uri = viewName;
148         
149         // path corrections
150         if (uri != null)
151         {
152             if (!uri.startsWith("/"))
153             {
154                 uri = "/" + uri;
155             }
156         }
157         
158         // check the web script registry to see if this web script exists
159         Match match = container.getRegistry().findWebScript("get", uri);
160         if (match != null && match.getWebScript() != null)
161         {
162             view = new WebScriptView(container, authenticatorFactory, configService);     
163             view.setUrl(uri);            
164         }
165         
166         return view;        
167     }
168 }