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.json.JSONFactoryUtil;
020 import com.liferay.portal.kernel.json.JSONObject;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.util.Http;
024 import com.liferay.portal.kernel.util.HttpUtil;
025 import com.liferay.portal.kernel.util.PropsKeys;
026 import com.liferay.portal.kernel.util.Validator;
027 import com.liferay.portal.util.PortalUtil;
028 import com.liferay.portal.util.PrefsPropsUtil;
029 import com.liferay.portal.util.PropsValues;
030 import com.liferay.portal.util.WebKeys;
031
032 import javax.portlet.PortletRequest;
033
034 import javax.servlet.http.HttpServletRequest;
035 import javax.servlet.http.HttpSession;
036
037
040 public class FacebookConnectImpl implements FacebookConnect {
041
042 public String getAccessTokenURL(long companyId) throws SystemException {
043 return PrefsPropsUtil.getString(
044 companyId, PropsKeys.FACEBOOK_CONNECT_OAUTH_TOKEN_URL,
045 PropsValues.FACEBOOK_CONNECT_OAUTH_TOKEN_URL);
046 }
047
048 public String getAppId(long companyId) throws SystemException {
049 return PrefsPropsUtil.getString(
050 companyId, PropsKeys.FACEBOOK_CONNECT_APP_ID,
051 PropsValues.FACEBOOK_CONNECT_APP_ID);
052 }
053
054 public String getAppSecret(long companyId) throws SystemException {
055 return PrefsPropsUtil.getString(
056 companyId, PropsKeys.FACEBOOK_CONNECT_APP_SECRET,
057 PropsValues.FACEBOOK_CONNECT_APP_SECRET);
058 }
059
060 public String getAuthURL(long companyId) throws SystemException {
061 return PrefsPropsUtil.getString(
062 companyId, PropsKeys.FACEBOOK_CONNECT_OAUTH_AUTH_URL,
063 PropsValues.FACEBOOK_CONNECT_OAUTH_AUTH_URL);
064 }
065
066 public JSONObject getGraphResources(
067 long companyId, String path, String accessToken, String fields) {
068
069 try {
070 String url = HttpUtil.addParameter(
071 getGraphURL(companyId) + path, "access_token", accessToken);
072
073 if (Validator.isNotNull(fields)) {
074 url = HttpUtil.addParameter(url, "fields", fields);
075 }
076
077 Http.Options options = new Http.Options();
078
079 options.setLocation(url);
080
081 String json = HttpUtil.URLtoString(options);
082
083 return JSONFactoryUtil.createJSONObject(json);
084 }
085 catch (Exception e) {
086 if (_log.isWarnEnabled()) {
087 _log.warn(e, e);
088 }
089 }
090
091 return null;
092 }
093
094 public String getGraphURL(long companyId) throws SystemException {
095 return PrefsPropsUtil.getString(
096 companyId, PropsKeys.FACEBOOK_CONNECT_GRAPH_URL,
097 PropsValues.FACEBOOK_CONNECT_GRAPH_URL);
098 }
099
100 public String getProfileImageURL(PortletRequest portletRequest) {
101 HttpServletRequest request = PortalUtil.getHttpServletRequest(
102 portletRequest);
103
104 request = PortalUtil.getOriginalServletRequest(request);
105
106 HttpSession session = request.getSession();
107
108 String facebookId = (String)session.getAttribute(
109 WebKeys.FACEBOOK_USER_ID);
110
111 if (Validator.isNull(facebookId)) {
112 return null;
113 }
114
115 long companyId = PortalUtil.getCompanyId(request);
116
117 String token = (String)session.getAttribute(
118 WebKeys.FACEBOOK_ACCESS_TOKEN);
119
120 JSONObject jsonObject = getGraphResources(
121 companyId, "/me", token, "id,picture");
122
123 return jsonObject.getString("picture");
124 }
125
126 public String getRedirectURL(long companyId) throws SystemException {
127 return PrefsPropsUtil.getString(
128 companyId, PropsKeys.FACEBOOK_CONNECT_OAUTH_REDIRECT_URL,
129 PropsValues.FACEBOOK_CONNECT_OAUTH_REDIRECT_URL);
130 }
131
132 public boolean isEnabled(long companyId) throws SystemException {
133 return PrefsPropsUtil.getBoolean(
134 companyId, PropsKeys.FACEBOOK_CONNECT_AUTH_ENABLED,
135 PropsValues.FACEBOOK_CONNECT_AUTH_ENABLED);
136 }
137
138 private static Log _log = LogFactoryUtil.getLog(FacebookConnectImpl.class);
139
140 }