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.surf;
20  
21  import javax.servlet.http.HttpSession;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.springframework.extensions.surf.exception.ConnectorProviderException;
26  import org.springframework.extensions.surf.exception.ConnectorServiceException;
27  import org.springframework.extensions.surf.support.ThreadLocalRequestContext;
28  import org.springframework.extensions.webscripts.connector.Connector;
29  import org.springframework.extensions.webscripts.connector.ConnectorProvider;
30  import org.springframework.extensions.webscripts.connector.ConnectorService;
31  import org.springframework.extensions.webscripts.connector.User;
32  
33  /**
34   * An implementation of connector provider that provides access to the
35   * Web Framework request context to build connectors
36   *
37   * @author Kevin Roast
38   * @author muzquiano
39   */
40  public class WebFrameworkConnectorProvider implements ConnectorProvider
41  {    
42      private static final Log logger = LogFactory.getLog(WebFrameworkConnectorProvider.class);
43  
44      private ConnectorService connectorService;
45      
46      /**
47       * Sets the connector service.
48       * 
49       * @param connectorService
50       */
51      public void setConnectorService(ConnectorService connectorService)
52      {
53          this.connectorService = connectorService;
54      }
55      
56      /**
57       * Implementation of the contract to provide a Connector for our remote store.
58       * This allows lazy providing of the Connector object only if the remote store actually needs
59       * it. Otherwise acquiring the Connector when rarely used is an expensive overhead as most
60       * objects are cached by the persister in which case the remote store isn't actually called.
61       */
62      public Connector provide(String endpoint) throws ConnectorProviderException
63      {
64          Connector conn = null;
65          RequestContext rc = ThreadLocalRequestContext.getRequestContext();
66          
67          if (rc != null)
68          {
69              try
70              {
71                  // check whether we have a current user
72                  User user = rc.getUser();
73                  if (user == null || rc.getCredentialVault() == null)
74                  {
75                      if (logger.isDebugEnabled())
76                          logger.debug("No user was found, creating unauthenticated connector");
77                      
78                      // return the non-credential'ed connector to this endpoint
79                      conn = connectorService.getConnector(endpoint);                       
80                  }
81                  else
82                  {
83                      if (logger.isDebugEnabled())
84                          logger.debug("User '" + user.getId() + "' was found, creating authenticated connector");
85                      
86                      // return the credential'ed connector to this endpoint
87                      HttpSession httpSession = rc.getRequest().getSession(true);
88                      conn = connectorService.getConnector(endpoint, rc.getUserId(), httpSession);
89                  }
90              }
91              catch (ConnectorServiceException cse)
92              {
93                  throw new ConnectorProviderException("Unable to provision connector for endpoint: " + endpoint, cse);
94              }
95          }
96          else
97          {
98              // if we don't have a request context, we can still provision a connector with no credentials
99              try
100             {
101                 conn = connectorService.getConnector(endpoint);
102             }
103             catch (ConnectorServiceException cse)
104             {
105                 throw new ConnectorProviderException("Unable to provision non-credential'd connector for endpoint: " + endpoint, cse);
106             }
107         }
108         
109         return conn;
110     }
111 }