KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > app > servlet > ExternalAccessServlet


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.app.servlet;
18
19 import java.io.IOException JavaDoc;
20 import java.text.MessageFormat JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import javax.faces.application.NavigationHandler;
24 import javax.faces.context.FacesContext;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.alfresco.repo.webdav.WebDAVServlet;
30 import org.alfresco.service.ServiceRegistry;
31 import org.alfresco.service.cmr.repository.NodeRef;
32 import org.alfresco.service.cmr.repository.StoreRef;
33 import org.alfresco.service.cmr.security.AccessStatus;
34 import org.alfresco.service.cmr.security.PermissionService;
35 import org.alfresco.web.app.Application;
36 import org.alfresco.web.bean.BrowseBean;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 /**
41  * Servlet allowing external URL access to various global JSF views in the Web Client.
42  * <p>
43  * The servlet accepts a well formed URL that can easily be generated from a Content or Space NodeRef.
44  * The URL also specifies the JSF "outcome" to be executed which provides the correct JSF View to be
45  * displayed. The JSF "outcome" must equate to a global navigation rule or it will not be displayed.
46  * Servlet URL is of the form:
47  * <p>
48  * <code>http://&lt;server&gt;/alfresco/navigate/&lt;outcome&gt;[/&lt;workspace&gt;/&lt;store&gt;/&lt;nodeId&gt;]</code> or <br/>
49  * <code>http://&lt;server&gt;/alfresco/navigate/&lt;outcome&gt;[/webdav/&lt;path/to/node&gt;]</code>
50  * <p>
51  * Like most Alfresco servlets, the URL may be followed by a valid 'ticket' argument for authentication:
52  * ?ticket=1234567890
53  * <p>
54  * And/or also followed by the "?guest=true" argument to force guest access login for the URL.
55  *
56  * @author Kevin Roast
57  */

58 public class ExternalAccessServlet extends BaseServlet
59 {
60    private static final long serialVersionUID = -4118907921337237802L;
61    
62    private static Log logger = LogFactory.getLog(ExternalAccessServlet.class);
63    
64    private final static String JavaDoc OUTCOME_DOCDETAILS = "showDocDetails";
65    private final static String JavaDoc OUTCOME_SPACEDETAILS = "showSpaceDetails";
66    private final static String JavaDoc OUTCOME_BROWSE = "browse";
67    private final static String JavaDoc OUTCOME_LOGOUT = "logout";
68    
69    private static final String JavaDoc ARG_TEMPLATE = "template";
70    
71    /**
72     * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
73     */

74    protected void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
75       throws ServletException JavaDoc, IOException JavaDoc
76    {
77       String JavaDoc uri = req.getRequestURI();
78       
79       if (logger.isDebugEnabled())
80          logger.debug("Processing URL: " + uri + (req.getQueryString() != null ? ("?" + req.getQueryString()) : ""));
81       
82       AuthenticationStatus status = servletAuthenticate(req, res);
83       if (status == AuthenticationStatus.Failure)
84       {
85          return;
86       }
87       
88       StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(uri, "/");
89       int count = t.countTokens();
90       if (count < 3)
91       {
92          throw new IllegalArgumentException JavaDoc("Externally addressable URL did not contain all required args: " + uri);
93       }
94       t.nextToken(); // skip web app name
95
t.nextToken(); // skip servlet name
96

97       String JavaDoc outcome = t.nextToken();
98       
99       // get rest of the tokens arguments
100
String JavaDoc[] args = new String JavaDoc[count - 3];
101       for (int i=0; i<count - 3; i++)
102       {
103          args[i] = t.nextToken();
104       }
105       
106       if (logger.isDebugEnabled())
107          logger.debug("External outcome found: " + outcome);
108       
109       // we almost always need this bean reference
110
FacesContext fc = FacesHelper.getFacesContext(req, res, getServletContext());
111       BrowseBean browseBean = (BrowseBean)FacesHelper.getManagedBean(fc, "BrowseBean");
112       
113       // get services we need
114
ServiceRegistry serviceRegistry = getServiceRegistry(getServletContext());
115       PermissionService permissionService = serviceRegistry.getPermissionService();
116       
117       // setup is required for certain outcome requests
118
if (OUTCOME_DOCDETAILS.equals(outcome))
119       {
120          NodeRef nodeRef = null;
121          
122          if (args[0].equals(WebDAVServlet.WEBDAV_PREFIX))
123          {
124             nodeRef = resolveWebDAVPath(fc, args);
125          }
126          else if (args.length == 3)
127          {
128             StoreRef storeRef = new StoreRef(args[0], args[1]);
129             nodeRef = new NodeRef(storeRef, args[2]);
130          }
131          
132          if (nodeRef != null)
133          {
134             // check that the user has at least READ access - else redirect to the login page
135
if (permissionService.hasPermission(nodeRef, PermissionService.READ) == AccessStatus.DENIED)
136             {
137                if (logger.isDebugEnabled())
138                   logger.debug("User does not have permissions to READ NodeRef: " + nodeRef.toString());
139                redirectToLoginPage(req, res, getServletContext());
140                return;
141             }
142             
143             // setup the Document on the browse bean
144
browseBean.setupContentAction(nodeRef.getId(), true);
145          }
146          
147          // perform the appropriate JSF navigation outcome
148
NavigationHandler navigationHandler = fc.getApplication().getNavigationHandler();
149          navigationHandler.handleNavigation(fc, null, outcome);
150       }
151       else if (OUTCOME_SPACEDETAILS.equals(outcome))
152       {
153          NodeRef nodeRef = null;
154          
155          if (args[0].equals(WebDAVServlet.WEBDAV_PREFIX))
156          {
157             nodeRef = resolveWebDAVPath(fc, args);
158          }
159          else if (args.length == 3)
160          {
161             StoreRef storeRef = new StoreRef(args[0], args[1]);
162             nodeRef = new NodeRef(storeRef, args[2]);
163          }
164          
165          if (nodeRef != null)
166          {
167             // check that the user has at least READ access - else redirect to the login page
168
if (permissionService.hasPermission(nodeRef, PermissionService.READ) == AccessStatus.DENIED)
169             {
170                if (logger.isDebugEnabled())
171                   logger.debug("User does not have permissions to READ NodeRef: " + nodeRef.toString());
172                redirectToLoginPage(req, res, getServletContext());
173                return;
174             }
175             
176             // setup the Space on the browse bean
177
browseBean.setupSpaceAction(nodeRef.getId(), true);
178          }
179          
180          // perform the appropriate JSF navigation outcome
181
NavigationHandler navigationHandler = fc.getApplication().getNavigationHandler();
182          navigationHandler.handleNavigation(fc, null, outcome);
183       }
184       else if (OUTCOME_BROWSE.equals(outcome))
185       {
186          if (args != null && args.length >= 3)
187          {
188             NodeRef nodeRef = null;
189             int offset = 0;
190
191             offset = args.length - 3;
192             StoreRef storeRef = new StoreRef(args[0+offset], args[1+offset]);
193             nodeRef = new NodeRef(storeRef, args[2+offset]);
194             
195             // check that the user has at least READ access - else redirect to the login page
196
if (permissionService.hasPermission(nodeRef, PermissionService.READ) == AccessStatus.DENIED)
197             {
198                if (logger.isDebugEnabled())
199                   logger.debug("User does not have permissions to READ NodeRef: " + nodeRef.toString());
200                redirectToLoginPage(req, res, getServletContext());
201                return;
202             }
203             
204             // this call sets up the current node Id, and updates or initialises the
205
// breadcrumb component with the selected node as appropriate.
206
browseBean.updateUILocation(nodeRef);
207             
208             // force a "late" refresh of the BrowseBean to handle external servlet access URL
209
browseBean.externalAccessRefresh();
210             
211             // check for view mode first argument
212
if (args[0].equals(ARG_TEMPLATE))
213             {
214                browseBean.setDashboardView(true);
215             }
216             
217             // the above calls into BrowseBean setup the NavigationHandler automatically
218
}
219          else
220          {
221             // perform the appropriate JSF navigation outcome
222
NavigationHandler navigationHandler = fc.getApplication().getNavigationHandler();
223             navigationHandler.handleNavigation(fc, null, outcome);
224          }
225       }
226       else if (OUTCOME_LOGOUT.equals(outcome))
227       {
228          // special case for logout
229
req.getSession().invalidate();
230          res.sendRedirect(req.getContextPath() + FACES_SERVLET + Application.getLoginPage(getServletContext()));
231          return;
232       }
233       
234       // perform the forward to the page processed by the Faces servlet
235
String JavaDoc viewId = fc.getViewRoot().getViewId();
236       getServletContext().getRequestDispatcher(FACES_SERVLET + viewId).forward(req, res);
237    }
238    
239    /**
240     * Generate a URL to the External Access Servlet.
241     * Allows access to JSF views (via an "outcome" ID) from external URLs.
242     *
243     * @param outcome
244     * @param args
245     *
246     * @return URL
247     */

248    public final static String JavaDoc generateExternalURL(String JavaDoc outcome, String JavaDoc args)
249    {
250       if (args == null)
251       {
252          return MessageFormat.format(EXTERNAL_URL, new Object JavaDoc[] {outcome} );
253       }
254       else
255       {
256          return MessageFormat.format(EXTERNAL_URL_ARGS, new Object JavaDoc[] {outcome, args} );
257       }
258    }
259    
260    // example: http://<server>/alfresco/navigate/<outcome>[/<workspace>/<store>/<nodeId>]
261
private static final String JavaDoc EXTERNAL_URL = "/navigate/{0}";
262    private static final String JavaDoc EXTERNAL_URL_ARGS = "/navigate/{0}/{1}";
263 }
264
Popular Tags