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.springframework.extensions.surf.FrameworkUtil;
22  import org.springframework.extensions.surf.RequestContext;
23  import org.springframework.extensions.surf.exception.ConnectorServiceException;
24  import org.springframework.extensions.surf.studio.WebStudioService;
25  import org.springframework.extensions.surf.task.ImportTask;
26  import org.springframework.extensions.surf.task.Task;
27  import org.springframework.extensions.webscripts.ScriptBase;
28  import org.springframework.extensions.webscripts.ScriptableMap;
29  import org.springframework.extensions.webscripts.connector.Connector;
30  
31  /**
32   * Utility for importing Surf assets from a remote location into the
33   * Surf environment
34   * 
35   * @author muzquiano
36   */
37  public final class ScriptImporter extends ScriptBase
38  {
39      /**
40       * Constructs a new ScriptImporter object.
41       * 
42       * @param context The RequestContext instance for the current
43       *            request
44       */
45      public ScriptImporter(RequestContext context)
46      {
47          super(context);
48      }
49  
50      /*
51       * (non-Javadoc)
52       * 
53       * @see org.alfresco.web.scripts.ScriptBase#buildProperties()
54       */
55      protected ScriptableMap buildProperties()
56      {
57          if (this.properties == null)
58          {
59          }
60  
61          return this.properties;
62      }
63  
64      // --------------------------------------------------------------
65      // JavaScript Properties
66      //    
67  
68      /**
69       * Imports a Surf archive into the given store from a URL location
70       * 
71       * A task id is returned so that the task can be monitored asynchrously 
72       * 
73       * @param store
74       * @param webappId empty or webapp id
75       * @param url
76       * 
77       * @return task id
78       */
79      public String importArchive(String store, String webappId, String url)
80      {
81          String taskId = null;
82          
83          String taskName = "Import from: " + url;
84  
85          // allow for resolution of relative url's
86          if (url.startsWith("/"))
87          {
88              String baseUrl = context.getRequest().getScheme() + "://"
89                      + context.getRequest().getServerName();
90  
91              if (context.getRequest().getServerPort() != 80)
92              {
93                  baseUrl += ":" + context.getRequest().getServerPort();
94              }            
95              baseUrl += context.getRequest().getContextPath();
96              
97              url = baseUrl + url;
98          }
99          
100         // create the alfresco connector ahead of time
101         Connector alfrescoConnector = null;
102         try
103         {
104             alfrescoConnector = FrameworkUtil.getConnector(context, "alfresco");        
105 
106             // create the task
107             ImportTask task = new ImportTask(taskName);
108             task.setAlfrescoConnector(alfrescoConnector);
109             task.setStoreId(store);
110             task.setWebappId(webappId);
111             task.setUrl(url);
112             
113             // add the task to the task manager
114             taskId = WebStudioService.getInstance().getTaskManager().addTask(task);
115             
116         }
117         catch(ConnectorServiceException cse)
118         {
119             FrameworkUtil.getLogger().warn("Unable to load 'alfresco' endpoint connector for script import", cse);
120         }
121         
122         return taskId;
123     }
124     
125     /**
126      * Returns a task with the given task id
127      * 
128      * @param taskId
129      * 
130      * @return task
131      */
132     public Task getTask(String taskId)
133     {
134         return WebStudioService.getInstance().getTaskManager().getTask(taskId);
135     }
136 }