View Javadoc

1   /*
2   
3   This software is OSI Certified Open Source Software.
4   OSI Certified is a certification mark of the Open Source Initiative.
5   
6   The license (Mozilla version 1.0) can be read at the MMBase site.
7   See http://www.MMBase.org/license
8   
9   */
10  package net.sf.mmapps.commons.bridge;
11  
12  import java.util.HashMap;
13  import java.util.Map;
14  
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpSession;
17  
18  import net.sf.mmapps.commons.util.StringUtil;
19  
20  import org.mmbase.bridge.*;
21  import org.mmbase.util.logging.Logger;
22  import org.mmbase.util.logging.Logging;
23  
24  
25  
26  public class CloudUtil {
27  
28      public static final String DEFAULT_SESSIONNAME = "cloud_mmbase";
29      public static final String DEFAULT_CLOUD_NAME = "mmbase";
30      public static final String DEFAULT_AUTHENTICATION = "name/password";
31      
32      /*** MMbase logging system */
33      private static Logger log = Logging.getLoggerInstance(CloudUtil.class.getName());
34      
35      private CloudUtil() {
36          //Utility
37      }
38      
39      public static Cloud createCloud(HttpServletRequest req) {
40          return createCloud(req, DEFAULT_SESSIONNAME);
41      }
42  
43      public static Cloud createCloud(HttpServletRequest req, String sessionname) {
44          String username = req.getParameter("username");
45          String password = req.getParameter("password");
46  
47          Cloud cloud = null;
48          if (!StringUtil.isEmptyOrWhitespace(username) && !StringUtil.isEmptyOrWhitespace(password)) {
49              String cloudName = req.getParameter("cloud");
50              String authenticate = req.getParameter("authenticate");
51  
52              if (StringUtil.isEmptyOrWhitespace(cloudName)) {
53                 cloudName = DEFAULT_CLOUD_NAME;
54              }
55              if (StringUtil.isEmptyOrWhitespace(authenticate)) {
56                 authenticate = DEFAULT_AUTHENTICATION;
57              }
58  
59             final CloudContext context = ContextProvider.getCloudContext("local");
60             final Map loginInfo = getUserCredentials(username, password);
61             cloud = context.getCloud(cloudName, authenticate, loginInfo);
62             
63             HttpSession session = req.getSession();
64             if (session != null) {
65                session.setAttribute(sessionname, cloud);
66             }
67             else {
68                log.warn("Could not get or create a session to put the cloud on.");
69             }
70          }
71  
72          return cloud;
73      }
74      
75      public static Map getUserCredentials(String username, String password) {
76          Map<String,String> result = new HashMap<String,String>(3, 0.7f);
77          result.put("username", username);
78          result.put("password", password);
79          return result;
80       }
81  
82      public static Cloud getCloudFromSession(HttpServletRequest request) {
83          return getCloudFromSession(request, DEFAULT_SESSIONNAME);
84      }
85      
86      public static Cloud getCloudFromSession(HttpServletRequest request, String sessionname) {
87          HttpSession session = request.getSession(false);
88          if (session != null) {
89              Cloud cloud = (Cloud) session.getAttribute(sessionname);
90              if (cloud != null) {
91                  log.debug("cloud for user " + cloud.getUser().getIdentifier());
92                  return cloud;
93              }
94          }
95          return null;
96      }
97  
98      /***
99       * Checks if a cloud is on the session with given the default sessionname.
100      * @param request HttpServletRequest to search for the cloud.
101      * @return true if a cloud is found, false otherwise.
102      */
103     public static boolean hasCloud(HttpServletRequest request) {
104        return hasCloud(request, DEFAULT_SESSIONNAME);
105     }
106 
107 
108     /***
109      * Checks if a cloud is on the session with given sessionname.
110      * @param request HttpServletRequest to search for the cloud.
111      * @param sessionname The name of the cloud on the session.
112      * @return true if a cloud is found, false otherwise.
113      */
114     public static boolean hasCloud(HttpServletRequest request, String sessionname) {
115        HttpSession session = request.getSession(false);
116        if (session != null) {
117           Cloud cloud = (Cloud) session.getAttribute(sessionname);
118           if (cloud != null) {
119              log.debug("cloud for user " + cloud.getUser().getIdentifier());
120              return true;
121           }
122        }
123        return false;
124     }
125     
126     public static void addCloudToThread(HttpServletRequest request) {
127         if (hasCloud(request)) {
128             Cloud cloud = getCloudFromSession(request);
129             CloudThreadLocal.bind(cloud);
130         }
131     }
132 
133     public static void removeCloudFromThread() {
134         CloudThreadLocal.unbind();
135     }
136     
137     public static Cloud getCloudFromThread() {
138         return CloudThreadLocal.currentCloud();
139     }
140 }