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;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.springframework.extensions.config.ConfigService;
24  import org.springframework.extensions.config.RemoteConfigElement;
25  import org.springframework.extensions.config.RemoteConfigElement.EndpointDescriptor;
26  import org.springframework.extensions.surf.exception.ConnectorProviderException;
27  import org.springframework.extensions.webscripts.connector.Connector;
28  import org.springframework.extensions.webscripts.connector.ConnectorProvider;
29  import org.springframework.extensions.webscripts.connector.ConnectorProviderImpl;
30  import org.springframework.extensions.webscripts.connector.Response;
31  
32  /**
33   * Root-scope class that provides useful functions for working with endpoints,
34   * connectors and credentials.
35   * 
36   * This class also implements methods from the Connector interface so as to
37   * allow application developers to use it straight away against the configured
38   * default endpoint.
39   * 
40   * @author muzquiano
41   */
42  public class ScriptRemote
43  {
44      private static final Log logger = LogFactory.getLog(ScriptRemote.class);
45  
46      private ConfigService configService;
47      private ConnectorProvider connectorProvider;
48  
49      /**
50       * Sets the configuration service.
51       * 
52       * @param configService
53       */
54      public void setConfigService(ConfigService configService)
55      {
56          this.configService = configService;
57      }
58          
59      /**
60       * Sets the connector provider.
61       * 
62       * @param connectorProvider
63       */
64      public void setConnectorProvider(ConnectorProvider connectorProvider)
65      {
66          this.connectorProvider = connectorProvider;
67      }
68  
69      /**
70       * Constructs a remote connector to a default endpoint (if configured).
71       * If a default endpoint is not configured, null will be returned.
72       * 
73       * @return the remote client
74       */
75      public ScriptRemoteConnector connect()
76      {
77          ScriptRemoteConnector remoteConnector = null;
78  
79          // Check whether a remote configuration has been provided
80          RemoteConfigElement remoteConfig = getRemoteConfig();
81          if (remoteConfig != null)
82          {
83              // See if we have a default endpoint id
84              String defaultEndpointId = remoteConfig.getDefaultEndpointId();
85              if (defaultEndpointId != null)
86              {
87                  // Construct for this endpoint id
88                  remoteConnector = connect(defaultEndpointId);
89              }
90          }
91  
92          return remoteConnector;
93      }
94  
95      /**
96       * Constructs a RemoteClient to a specific endpoint. If the endpoint does
97       * not exist, null is returned.
98       * 
99       * @param endpointId the endpoint id
100      * 
101      * @return the remote client
102      */
103     public ScriptRemoteConnector connect(String endpointId)
104     {
105         ScriptRemoteConnector remoteConnector = null;
106 
107         RemoteConfigElement remoteConfig = getRemoteConfig();
108         if (remoteConfig != null)
109         {
110             // check whether we have a descriptor for this endpoint
111             EndpointDescriptor descriptor = remoteConfig.getEndpointDescriptor(endpointId);
112             if (descriptor == null)
113             {
114                 logger.error("No EndPoint descriptor configuration found for ID: " + endpointId);
115             }
116             else
117             {
118                 // if a connector provider has not been assigned, we can use a
119                 // default provider which provides simple stateless access
120                 if (connectorProvider == null)
121                 {
122                     connectorProvider = new ConnectorProviderImpl();                    
123                 }
124                 
125                 try
126                 {
127                     // construct a connector to this endpoint
128                     Connector connector = connectorProvider.provide(endpointId);
129                     remoteConnector = new ScriptRemoteConnector(connector, descriptor);
130                 }
131                 catch (ConnectorProviderException cpe)
132                 {
133                     logger.error("Unable to provision connector for endpoint: " + endpointId);
134                 }
135             }
136         }
137 
138         return remoteConnector;
139     }
140 
141     // //////////////////////////////////////////////////////////////
142     //
143     // Connector pass-thru methods to work with default Connector
144     //
145     // //////////////////////////////////////////////////////////////
146 
147     /**
148      * Invoke a specific URI on the default endpoint
149      * 
150      * @param uri the uri
151      * 
152      * @return the response
153      */
154     public Response call(String uri)
155     {
156         return this.connect().call(uri);
157     }
158 
159     /**
160      * Returns a list of the application endpoint ids
161      * 
162      * @return
163      */
164     public String[] getEndpointIds()
165     {
166         String[] endpointIds = null;
167         
168         RemoteConfigElement remoteConfig = getRemoteConfig();
169         if(remoteConfig != null)
170         {
171             endpointIds = remoteConfig.getEndpointIds();
172         }
173         
174         return endpointIds;
175     }
176     
177     /**
178      * Returns the name of an endpoint
179      * 
180      * @param endpointId
181      * @return
182      */
183     public String getEndpointName(String endpointId)
184     {
185         String name = null;
186         
187         RemoteConfigElement remoteConfig = getRemoteConfig();
188         if(remoteConfig != null)
189         {
190             EndpointDescriptor descriptor = remoteConfig.getEndpointDescriptor(endpointId);
191             if(descriptor != null)
192             {
193                 name = descriptor.getName();
194             }
195         }
196 
197         return name;
198     }
199 
200     /**
201      * Returns the description of an endpoint
202      * 
203      * @param endpointId
204      * @return
205      */
206     public String getEndpointDescription(String endpointId)
207     {
208         String description = null;
209         
210         RemoteConfigElement remoteConfig = getRemoteConfig();
211         if(remoteConfig != null)
212         {
213             EndpointDescriptor descriptor = remoteConfig.getEndpointDescriptor(endpointId);
214             if(descriptor != null)
215             {
216                 description = descriptor.getDescription();
217             }
218         }
219 
220         return description;
221     }    
222 
223     public boolean isEndpointPersistent(String id)
224     {
225         boolean persistent = false;
226         
227         RemoteConfigElement remoteConfig = getRemoteConfig();
228         if(remoteConfig != null)
229         {
230             EndpointDescriptor descriptor = remoteConfig.getEndpointDescriptor(id);
231             if(descriptor != null)
232             {
233                 persistent = descriptor.getPersistent();
234             }
235         }
236 
237         return persistent;
238     }    
239 
240     /**
241      * Returns the configured URL for the given endpoint
242      * 
243      * @param endpointId
244      * 
245      * @return the endpoint url
246      */
247     public String getEndpointURL(String endpointId)
248     {
249         String url = null;
250         
251         RemoteConfigElement remoteConfig = getRemoteConfig();
252         if (remoteConfig != null)
253         {
254             EndpointDescriptor descriptor = remoteConfig.getEndpointDescriptor(endpointId);
255             if (descriptor != null)
256             {
257                 url = descriptor.getEndpointUrl();
258             }
259         }
260 
261         return url;
262     }
263     
264     /**
265      * @return RemoteConfigElement
266      */
267     private RemoteConfigElement getRemoteConfig()
268     {
269         RemoteConfigElement remoteConfig = (RemoteConfigElement)configService.getConfig(
270                 "Remote").getConfigElement("remote");
271         return remoteConfig;
272     }
273 }