1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.googleapps;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.Http;
20  import com.liferay.portal.kernel.util.HttpUtil;
21  import com.liferay.portal.kernel.util.PropertiesUtil;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.Time;
25  import com.liferay.portal.model.Company;
26  import com.liferay.portal.service.CompanyLocalServiceUtil;
27  import com.liferay.portal.util.PrefsPropsUtil;
28  
29  import java.util.Properties;
30  
31  /**
32   * <a href="GAuthenticator.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class GAuthenticator {
37  
38      public GAuthenticator(long companyId) {
39          _companyId = companyId;
40  
41          init(true);
42      }
43  
44      public String getAuthenticationToken() {
45          init(false);
46  
47          return _authenticationToken;
48      }
49  
50      public long getCompanyId() {
51          return _companyId;
52      }
53  
54      public String getDomain() {
55          return _domain;
56      }
57  
58      public void init(boolean manual) {
59          if (manual || isStale()) {
60              try {
61                  doInit();
62              }
63              catch (Exception e) {
64                  _log.error(e, e);
65              }
66          }
67      }
68  
69      protected void doInit() throws Exception {
70          Company company = CompanyLocalServiceUtil.getCompany(_companyId);
71  
72          _domain = company.getMx();
73          _userName = PrefsPropsUtil.getString(
74              _companyId, PropsKeys.GOOGLE_APPS_USERNAME);
75          _password = PrefsPropsUtil.getString(
76              _companyId, PropsKeys.GOOGLE_APPS_PASSWORD);
77  
78          if (!_userName.contains(StringPool.AT)) {
79              _userName += StringPool.AT + _domain;
80          }
81  
82          Http.Options options = new Http.Options();
83  
84          options.addPart("Email", _userName);
85          options.addPart("Passwd", _password);
86          options.addPart("accountType", "HOSTED");
87          options.addPart("service", "apps");
88          options.setLocation("https://www.google.com/accounts/ClientLogin");
89          options.setPost(true);
90  
91          String content = HttpUtil.URLtoString(options);
92  
93          Properties properties = PropertiesUtil.load(content);
94  
95          _authenticationToken = properties.getProperty("Auth");
96  
97          _initTime = System.currentTimeMillis();
98      }
99  
100     private boolean isStale() {
101         if ((_initTime + (Time.HOUR * 23)) > System.currentTimeMillis()) {
102             return false;
103         }
104         else {
105             return true;
106         }
107     }
108 
109     private static Log _log = LogFactoryUtil.getLog(GAuthenticator.class);
110 
111     private String _authenticationToken;
112     private long _companyId;
113     private String _domain;
114     private long _initTime;
115     private String _password;
116     private String _userName;
117 
118 }