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.util;
20  
21  import javax.servlet.http.HttpServletRequest;
22  
23  import org.springframework.extensions.surf.RequestContext;
24  import org.springframework.extensions.surf.ThreadLocalPreviewContext;
25  import org.springframework.extensions.surf.WebFrameworkConstants;
26  
27  /**
28   * @author muzquiano
29   */
30  public class WebStudioUtil
31  {
32      // TODO: This method must be rethought and reimplemented
33      public static String getContentEditURL(RequestContext context,
34              String endpointId, String itemRelativePath)
35      {
36          return null;
37      }
38  
39      public static Object getCachedObject(HttpServletRequest request, String key)
40      {
41          return (Object) request.getSession().getAttribute(
42                  "CACHED_RESOURCE_" + key);
43      }
44  
45      public static void setCachedObject(HttpServletRequest request, String key,
46              Object object)
47      {
48          request.getSession().setAttribute("CACHED_RESOURCE_" + key, object);
49      }
50  
51      public static String getCurrentUserId(HttpServletRequest request)
52      {
53          return (String) getCachedObject(request, "CurrentUserId");
54      }
55  
56      public static void setCurrentUserId(HttpServletRequest request,
57              String currentUserId)
58      {
59          setCachedObject(request, "CurrentUserId", currentUserId);
60      }
61  
62      public static String getCurrentWebProjectId(HttpServletRequest request)
63      {
64          return (String) getCachedObject(request, "CurrentWebProject");
65      }
66  
67      public static void setCurrentWebProjectId(HttpServletRequest request,
68              String webProjectId)
69      {
70          setCachedObject(request, "CurrentWebProject", webProjectId);
71      }
72      
73      public static String getCurrentSandboxId()
74      {
75          return getCurrentStoreId();
76      }
77      
78      public static void setCurrentSandboxId(HttpServletRequest request, String sandboxId)
79      {
80          setCurrentStoreId(request, sandboxId);
81      }
82  
83      public static String getCurrentStoreId()
84      {
85          String storeId = null;
86          
87          if (ThreadLocalPreviewContext.getSandboxContext() != null)
88          {
89              storeId = ThreadLocalPreviewContext.getSandboxContext().getStoreId();
90          }
91          
92          if ("".equals(storeId))
93          {
94              storeId = null;
95          }
96          
97          return storeId;
98      }
99  
100     public static void setCurrentStoreId(HttpServletRequest request, String storeId)
101     {
102         request.getSession(true).setAttribute(WebFrameworkConstants.STORE_ID_SESSION_ATTRIBUTE_NAME, storeId);
103         
104         ThreadLocalPreviewContext sandboxContext = new ThreadLocalPreviewContext(storeId, getCurrentWebappId());
105     }
106 
107     public static String getCurrentWebappId()
108     {
109         String webappId = null;
110 
111         if (ThreadLocalPreviewContext.getSandboxContext() != null)
112         {
113             webappId = ThreadLocalPreviewContext.getSandboxContext().getWebappId();
114         }
115         
116         if ("".equals(webappId))
117         {
118             webappId = null;
119         }
120 
121         return webappId;
122     }
123 
124     public static void setCurrentWebappId(HttpServletRequest request, String webappId)
125     {
126         request.getSession(true).setAttribute(WebFrameworkConstants.WEBAPP_ID_SESSION_ATTRIBUTE_NAME, webappId);
127         
128         ThreadLocalPreviewContext sandboxContext = new ThreadLocalPreviewContext(getCurrentStoreId(), webappId);
129     }
130 
131     public static void setOverlayEnabled(HttpServletRequest request,
132             boolean enabled)
133     {
134         request.getSession().setAttribute("OverlayState",
135                 (enabled ? "enabled" : "disabled"));
136     }
137 
138     public static boolean isOverlayEnabled(HttpServletRequest request)
139     {
140         boolean enabled = true;
141 
142         String result = (String) request.getSession().getAttribute(
143                 "OverlayState");
144         if ("disabled".equals(result))
145         {
146             enabled = false;
147         }
148 
149         return enabled;
150     }
151 }