001
014
015 package com.liferay.portal.facebook;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018 import com.liferay.portal.kernel.facebook.FacebookConnect;
019 import com.liferay.portal.kernel.facebook.FacebookConnectUtil;
020 import com.liferay.portal.kernel.json.JSONFactoryUtil;
021 import com.liferay.portal.kernel.json.JSONObject;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.util.CharPool;
025 import com.liferay.portal.kernel.util.Http;
026 import com.liferay.portal.kernel.util.HttpUtil;
027 import com.liferay.portal.kernel.util.PropsKeys;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.util.PortalUtil;
030 import com.liferay.portal.util.PrefsPropsUtil;
031 import com.liferay.portal.util.PropsValues;
032 import com.liferay.portal.util.WebKeys;
033
034 import javax.portlet.PortletRequest;
035
036 import javax.servlet.http.HttpServletRequest;
037 import javax.servlet.http.HttpSession;
038
039
043 public class FacebookConnectImpl implements FacebookConnect {
044
045 public String getAccessToken(long companyId, String redirect, String code)
046 throws SystemException {
047
048 String url = HttpUtil.addParameter(
049 getAccessTokenURL(companyId), "client_id", getAppId(companyId));
050
051 url = HttpUtil.addParameter(
052 url, "redirect_uri", FacebookConnectUtil.getRedirectURL(companyId));
053
054 String facebookConnectRedirectURL = getRedirectURL(companyId);
055
056 facebookConnectRedirectURL = HttpUtil.addParameter(
057 facebookConnectRedirectURL, "redirect", redirect);
058
059 url = HttpUtil.addParameter(
060 url, "redirect_uri", facebookConnectRedirectURL);
061 url = HttpUtil.addParameter(
062 url, "client_secret", getAppSecret(companyId));
063 url = HttpUtil.addParameter(url, "code", code);
064
065 Http.Options options = new Http.Options();
066
067 options.setLocation(url);
068 options.setPost(true);
069
070 try {
071 String content = HttpUtil.URLtoString(options);
072
073 if (Validator.isNotNull(content)) {
074 int x = content.indexOf("access_token=");
075
076 if (x >= 0) {
077 int y = content.indexOf(CharPool.AMPERSAND, x);
078
079 if (y < x) {
080 y = content.length();
081 }
082
083 return content.substring(x + 13, y);
084 }
085 }
086 }
087 catch (Exception e) {
088 throw new SystemException(
089 "Unable to retrieve Facebook access token", e);
090 }
091
092 return null;
093 }
094
095 public String getAccessTokenURL(long companyId) throws SystemException {
096 return PrefsPropsUtil.getString(
097 companyId, PropsKeys.FACEBOOK_CONNECT_OAUTH_TOKEN_URL,
098 PropsValues.FACEBOOK_CONNECT_OAUTH_TOKEN_URL);
099 }
100
101 public String getAppId(long companyId) throws SystemException {
102 return PrefsPropsUtil.getString(
103 companyId, PropsKeys.FACEBOOK_CONNECT_APP_ID,
104 PropsValues.FACEBOOK_CONNECT_APP_ID);
105 }
106
107 public String getAppSecret(long companyId) throws SystemException {
108 return PrefsPropsUtil.getString(
109 companyId, PropsKeys.FACEBOOK_CONNECT_APP_SECRET,
110 PropsValues.FACEBOOK_CONNECT_APP_SECRET);
111 }
112
113 public String getAuthURL(long companyId) throws SystemException {
114 return PrefsPropsUtil.getString(
115 companyId, PropsKeys.FACEBOOK_CONNECT_OAUTH_AUTH_URL,
116 PropsValues.FACEBOOK_CONNECT_OAUTH_AUTH_URL);
117 }
118
119 public JSONObject getGraphResources(
120 long companyId, String path, String accessToken, String fields) {
121
122 try {
123 String url = HttpUtil.addParameter(
124 getGraphURL(companyId).concat(path), "access_token",
125 accessToken);
126
127 if (Validator.isNotNull(fields)) {
128 url = HttpUtil.addParameter(url, "fields", fields);
129 }
130
131 Http.Options options = new Http.Options();
132
133 options.setLocation(url);
134
135 String json = HttpUtil.URLtoString(options);
136
137 return JSONFactoryUtil.createJSONObject(json);
138 }
139 catch (Exception e) {
140 if (_log.isWarnEnabled()) {
141 _log.warn(e, e);
142 }
143 }
144
145 return null;
146 }
147
148 public String getGraphURL(long companyId) throws SystemException {
149 return PrefsPropsUtil.getString(
150 companyId, PropsKeys.FACEBOOK_CONNECT_GRAPH_URL,
151 PropsValues.FACEBOOK_CONNECT_GRAPH_URL);
152 }
153
154 public String getProfileImageURL(PortletRequest portletRequest) {
155 HttpServletRequest request = PortalUtil.getHttpServletRequest(
156 portletRequest);
157
158 request = PortalUtil.getOriginalServletRequest(request);
159
160 HttpSession session = request.getSession();
161
162 String facebookId = (String)session.getAttribute(
163 WebKeys.FACEBOOK_USER_ID);
164
165 if (Validator.isNull(facebookId)) {
166 return null;
167 }
168
169 long companyId = PortalUtil.getCompanyId(request);
170
171 String token = (String)session.getAttribute(
172 WebKeys.FACEBOOK_ACCESS_TOKEN);
173
174 JSONObject jsonObject = getGraphResources(
175 companyId, "/me", token, "id,picture");
176
177 return jsonObject.getString("picture");
178 }
179
180 public String getRedirectURL(long companyId) throws SystemException {
181 return PrefsPropsUtil.getString(
182 companyId, PropsKeys.FACEBOOK_CONNECT_OAUTH_REDIRECT_URL,
183 PropsValues.FACEBOOK_CONNECT_OAUTH_REDIRECT_URL);
184 }
185
186 public boolean isEnabled(long companyId) throws SystemException {
187 return PrefsPropsUtil.getBoolean(
188 companyId, PropsKeys.FACEBOOK_CONNECT_AUTH_ENABLED,
189 PropsValues.FACEBOOK_CONNECT_AUTH_ENABLED);
190 }
191
192 public boolean isVerifiedAccountRequired(long companyId)
193 throws SystemException {
194
195 return PrefsPropsUtil.getBoolean(
196 companyId, PropsKeys.FACEBOOK_CONNECT_VERIFIED_ACCOUNT_REQUIRED,
197 PropsValues.FACEBOOK_CONNECT_VERIFIED_ACCOUNT_REQUIRED);
198 }
199
200 private static Log _log = LogFactoryUtil.getLog(FacebookConnectImpl.class);
201
202 }