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