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 java.io.Serializable;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  import org.springframework.extensions.surf.FrameworkUtil;
27  import org.springframework.extensions.surf.RequestContext;
28  import org.springframework.extensions.surf.UserFactory;
29  import org.springframework.extensions.surf.WebFrameworkConstants;
30  import org.springframework.extensions.surf.site.AuthenticationUtil;
31  import org.springframework.extensions.surf.studio.WebStudioService;
32  import org.springframework.extensions.surf.util.WebStudioUtil;
33  
34  /**
35   * @author muzquiano
36   */
37  public final class ScriptWebStudio implements Serializable
38  {
39      protected RequestContext context = null;
40  
41      public ScriptWebStudio(RequestContext context)
42      {
43          this.context = context;
44      }
45      
46      protected WebStudioService getWebStudioService()
47      {
48          return WebStudioService.getInstance();
49      }
50  
51      protected HttpServletRequest getHttpServletRequest()
52      {
53          return context.getRequest();
54      }
55  
56      public String getCurrentWebProjectId()
57      {
58          return WebStudioUtil.getCurrentWebProjectId(getHttpServletRequest());
59      }
60  
61      public void setCurrentWebProjectId(String webProjectId)
62      {
63          WebStudioUtil.setCurrentWebProjectId(getHttpServletRequest(),
64                  webProjectId);
65  
66          // TODO: reset to the staging store?
67      }
68  
69      // TODO: this method has to go
70      public String getCurrentSandboxId()
71      {
72          return WebStudioUtil.getCurrentSandboxId();
73      }
74  
75      // TODO: this method has to go
76      public void setCurrentSandboxId(String sandboxId)
77      {
78          WebStudioUtil.setCurrentSandboxId(getHttpServletRequest(), sandboxId);
79      }
80  
81      public String getCurrentStoreId()
82      {
83          return WebStudioUtil.getCurrentStoreId();
84      }
85  
86      public void setCurrentStoreId(String storeId)
87      {
88          WebStudioUtil.setCurrentStoreId(getHttpServletRequest(), storeId);
89  
90          // bind the web framework to the store
91          /*
92          getHttpServletRequest().getSession(true).setAttribute(
93                  WebFrameworkConstants.STORE_ID_SESSION_ATTRIBUTE_NAME, storeId);
94          */
95  
96          // set to the ROOT webapp if none set
97          String webappId = getCurrentWebappId();
98          if (webappId == null)
99          {
100             setCurrentWebappId("ROOT");
101         }
102     }
103 
104     public String getCurrentWebappId()
105     {
106         return WebStudioUtil.getCurrentWebappId();
107     }
108 
109     public void setCurrentWebappId(String webappId)
110     {
111         WebStudioUtil.setCurrentWebappId(getHttpServletRequest(), webappId);
112 
113         // bind the web framework to the webapp
114         getHttpServletRequest().getSession(true).setAttribute(
115                 WebFrameworkConstants.WEBAPP_ID_SESSION_ATTRIBUTE_NAME,
116                 webappId);
117     }
118 
119     public String getCurrentUserId()
120     {
121         return WebStudioUtil.getCurrentUserId(getHttpServletRequest());
122     }
123 
124     public void setCurrentUserId(String currentUserId)
125     {
126         WebStudioUtil.setCurrentUserId(getHttpServletRequest(), currentUserId);
127     }
128 
129     public void setModel(Map<String, Object> model)
130     {
131         this.model = model;
132     }
133 
134     public Map<String, Object> getModel()
135     {
136         return this.model;
137     }
138 
139     protected Map<String, Object> model;
140 
141     public boolean login(String username, String password) throws Exception
142     {
143         boolean success = false;
144 
145         HttpServletRequest request = this.getHttpServletRequest();
146 
147         UserFactory userFactory = FrameworkUtil.getServiceRegistry().getUserFactory();
148 
149         // see if we can authenticate the user
150         boolean authenticated = userFactory.authenticate(request, username,
151                 password);
152         if (authenticated)
153         {
154             // this will fully reset all connector sessions
155             RequestContext context = FrameworkUtil.getCurrentRequestContext();
156             AuthenticationUtil.login(request, username);
157 
158             // store the user
159             setCurrentUserId(username);
160 
161             // mark the fact that we succeeded
162             success = true;
163         }
164 
165         return success;
166     }
167 
168     public ScriptImporter getImporter()
169     {
170         return new ScriptImporter(context);
171     }
172 }