All Downloads are FREE. Search and download functionalities are using the official Maven repository.

facebook4j.FacebookImpl Maven / Gradle / Ivy

There is a newer version: 2.4.13
Show newest version
/*
 * Copyright 2012 Ryuji Yamashita
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package facebook4j;

import facebook4j.Question.Option;
import facebook4j.api.AccountMethods;
import facebook4j.api.ActivityMethods;
import facebook4j.api.AlbumMethods;
import facebook4j.api.BatchRequestsMethods;
import facebook4j.api.CheckinMethods;
import facebook4j.api.CommentMethods;
import facebook4j.api.ConversationMethods;
import facebook4j.api.DomainMethods;
import facebook4j.api.EventMethods;
import facebook4j.api.FQLMethods;
import facebook4j.api.FamilyMethods;
import facebook4j.api.FavoriteMethods;
import facebook4j.api.FriendMethods;
import facebook4j.api.GameMethods;
import facebook4j.api.GroupMethods;
import facebook4j.api.InsightMethods;
import facebook4j.api.LikeMethods;
import facebook4j.api.LinkMethods;
import facebook4j.api.LocationMethods;
import facebook4j.api.MessageMethods;
import facebook4j.api.NoteMethods;
import facebook4j.api.NotificationMethods;
import facebook4j.api.PageMethods;
import facebook4j.api.PermissionMethods;
import facebook4j.api.PhotoMethods;
import facebook4j.api.PokeMethods;
import facebook4j.api.PostMethods;
import facebook4j.api.QuestionMethods;
import facebook4j.api.RawAPIMethods;
import facebook4j.api.SearchMethods;
import facebook4j.api.SubscribeMethods;
import facebook4j.api.TestUserMethods;
import facebook4j.api.UserMethods;
import facebook4j.api.VideoMethods;
import facebook4j.auth.Authorization;
import facebook4j.conf.Configuration;
import facebook4j.internal.http.HttpParameter;
import facebook4j.internal.http.HttpResponse;
import facebook4j.internal.org.json.JSONArray;
import facebook4j.internal.org.json.JSONException;
import facebook4j.internal.org.json.JSONObject;
import facebook4j.internal.util.z_F4JInternalStringUtil;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static facebook4j.internal.util.z_F4JInternalParseUtil.*;

/**
 * A java representation of the Facebook Graph API
* This class is thread safe and can be cached/re-used and used concurrently.
* Currently this class is not carefully designed to be extended. It is suggested to extend this class only for mock testing purpose.
* * @author Ryuji Yamashita - roundrop at gmail.com */ class FacebookImpl extends FacebookBaseImpl implements Facebook { private static final long serialVersionUID = 6277119018105563020L; /*package*/ FacebookImpl(Configuration conf, Authorization auth) { super(conf, auth); } private String buildEndpoint(String id) { return buildEndpoint(id, null, null); } private String buildEndpoint(String id, Reading reading) { return buildEndpoint(id, null, reading); } private String buildEndpoint(String id, String connection) { return buildEndpoint(id, connection, null); } private String buildEndpoint(String id, String connection, Reading reading) { StringBuilder url = new StringBuilder() .append(conf.getRestBaseURL() + id) .append(connection == null ? "" : "/" + connection) .append(reading == null ? "" : "?" + reading.getQuery()); return url.toString(); } private String buildEndpoint(String path, Map parameters) { StringBuilder url = new StringBuilder(); url.append(conf.getRestBaseURL() + path); if (parameters != null && parameters.size() > 0) { url.append("?"); int i = 0; for (final String k : parameters.keySet()) { if (i > 0) { url.append("&"); } try { url.append(URLEncoder.encode(k, "UTF-8")) .append("=") .append(URLEncoder.encode(parameters.get(k), "UTF-8")); } catch (UnsupportedEncodingException ignore) { } i++; } } return url.toString(); } private String buildVideoEndpoint(String id, String connection) { return buildVideoEndpoint(id, connection, null); } private String buildVideoEndpoint(String id, String connection, Reading reading) { StringBuilder url = new StringBuilder() .append(conf.getVideoBaseURL() + id) .append(connection == null ? "" : "/" + connection) .append(reading == null ? "" : "?" + reading.getQuery()); return url.toString(); } private String buildSearchEndpoint(String query, String objectType, Reading reading) { String q = null; if (query != null) { q = HttpParameter.encode(query); } StringBuilder url = new StringBuilder() .append(buildEndpoint("search")) .append(objectType == null ? "" : "?type=" + objectType) .append(q == null ? "" : objectType == null ? "?q=" + q : "&q=" + q) .append(reading == null ? "" : "&" + reading.getQuery()); return url.toString(); } /* User Methods */ public User getMe() throws FacebookException { return getMe(null); } public User getMe(Reading reading) throws FacebookException { ensureAuthorizationEnabled(); User user = factory.createUser(get(buildEndpoint("me", reading))); return user; } public User getUser(String userId) throws FacebookException { return getUser(userId, null); } public User getUser(String userId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); User user = factory.createUser(get(buildEndpoint(userId, reading))); return user; } public URL getPictureURL() throws FacebookException { ensureAuthorizationEnabled(); return getPictureURL("me"); } public URL getPictureURL(PictureSize size) throws FacebookException { ensureAuthorizationEnabled(); return getPictureURL("me", size); } public URL getPictureURL(int width, int height) throws FacebookException { ensureAuthorizationEnabled(); return getPictureURL("me", width, height); } public URL getPictureURL(String userId) throws FacebookException { return getPictureURL(userId, null); } public URL getPictureURL(String userId, PictureSize size) throws FacebookException { return _getPictureURL(userId, size); } public URL getPictureURL(String userId, int width, int height) throws FacebookException { return _getPictureURL(userId, width, height); } public URL getSSLPictureURL() throws FacebookException { ensureAuthorizationEnabled(); return getSSLPictureURL("me"); } public URL getSSLPictureURL(PictureSize size) throws FacebookException { ensureAuthorizationEnabled(); return getSSLPictureURL("me", size); } public URL getSSLPictureURL(String userId) throws FacebookException { return getSSLPictureURL(userId, null); } public URL getSSLPictureURL(String userId, PictureSize size) throws FacebookException { return _getSSLPictureURL(userId, size); } public List getUsers(String... ids) throws FacebookException { ensureAuthorizationEnabled(); return factory.createUserArray(get(conf.getRestBaseURL(), new HttpParameter[] { new HttpParameter("ids", z_F4JInternalStringUtil.join(ids))})); } /* Account Methods */ public ResponseList getAccounts() throws FacebookException { return getAccounts("me", null); } public ResponseList getAccounts(Reading reading) throws FacebookException { return getAccounts("me", reading); } public ResponseList getAccounts(String userId) throws FacebookException { return getAccounts(userId, null); } public ResponseList getAccounts(String userId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createAccountList(get(buildEndpoint(userId, "accounts", reading))); } /* Achievement Methods */ public ResponseList getAchievements() throws FacebookException { return getAchievements("me", null); } public ResponseList getAchievements(Reading reading) throws FacebookException { return getAchievements("me", reading); } public ResponseList getAchievements(String userId) throws FacebookException { return getAchievements(userId, null); } public ResponseList getAchievements(String userId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createAchievementList(get(buildEndpoint(userId, "achievements", reading))); } public String postAchievement(URL achievementURL) throws FacebookException { return postAchievement("me", achievementURL); } public String postAchievement(String userId, URL achievementURL) throws FacebookException { ensureAuthorizationEnabled(); JSONObject json = post(buildEndpoint(userId, "achievements"), new HttpParameter[] {new HttpParameter("achievement", achievementURL.toString())} ).asJSONObject(); try { return json.getString("id"); } catch (JSONException jsone) { throw new FacebookException(jsone.getMessage(), jsone); } } public boolean deleteAchievement(URL achievementURL) throws FacebookException { return deleteAchievement("me", achievementURL); } public boolean deleteAchievement(String userId, URL achievementURL) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = delete(buildEndpoint(userId, "achievements"), new HttpParameter[] {new HttpParameter("achievement", achievementURL.toString())}); return parseBoolean(res); } /* Activity Methods */ public ResponseList getActivities() throws FacebookException { return getActivities("me", null); } public ResponseList getActivities(Reading reading) throws FacebookException { return getActivities("me", reading); } public ResponseList getActivities(String userId) throws FacebookException { return getActivities(userId, null); } public ResponseList getActivities(String userId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createActivityList(get(buildEndpoint(userId, "activities", reading))); } /* Album Methods */ public ResponseList getAlbums() throws FacebookException { return getAlbums("me", null); } public ResponseList getAlbums(Reading reading) throws FacebookException { return getAlbums("me", reading); } public ResponseList getAlbums(String id) throws FacebookException { return getAlbums(id, null); } public ResponseList getAlbums(String id, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createAlbumList(get(buildEndpoint(id, "albums", reading))); } public Album getAlbum(String albumId) throws FacebookException { return getAlbum(albumId, null); } public Album getAlbum(String albumId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createAlbum(get(buildEndpoint(albumId, reading))); } public String createAlbum(AlbumUpdate albumUpdate) throws FacebookException { return createAlbum("me", albumUpdate); } public String createAlbum(String userId, AlbumUpdate albumUpdate) throws FacebookException { ensureAuthorizationEnabled(); JSONObject json = post(buildEndpoint(userId, "albums"), albumUpdate.asHttpParameterArray()) .asJSONObject(); try { return json.getString("id"); } catch (JSONException jsone) { throw new FacebookException(jsone.getMessage(), jsone); } } public ResponseList getAlbumPhotos(String albumId) throws FacebookException { return getAlbumPhotos(albumId, null); } public ResponseList getAlbumPhotos(String albumId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createPhotoList(get(buildEndpoint(albumId, "photos", reading))); } public String addAlbumPhoto(String albumId, Media source) throws FacebookException { return addAlbumPhoto(albumId, source, null); } public String addAlbumPhoto(String albumId, Media source, String message) throws FacebookException { ensureAuthorizationEnabled(); List httpParams = new ArrayList(); httpParams.add(source.asHttpParameter("source")); if (message != null) { httpParams.add(new HttpParameter("message", message)); } JSONObject json = post(buildEndpoint(albumId, "photos"), httpParams.toArray(new HttpParameter[httpParams.size()])) .asJSONObject(); try { return json.getString("id"); } catch (JSONException jsone) { throw new FacebookException(jsone.getMessage(), jsone); } } public ResponseList getAlbumComments(String albumId) throws FacebookException { return getAlbumComments(albumId, null); } public ResponseList getAlbumComments(String albumId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return _getComments(albumId, reading); } public String commentAlbum(String albumId, String message) throws FacebookException { ensureAuthorizationEnabled(); return _comment(albumId, message); } public String commentAlbum(String albumId, CommentUpdate commentUpdate) throws FacebookException { return _comment(albumId, commentUpdate); } public ResponseList getAlbumLikes(String albumId) throws FacebookException { return getAlbumLikes(albumId, null); } public ResponseList getAlbumLikes(String albumId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createLikeList(get(buildEndpoint(albumId, "likes", reading))); } public boolean likeAlbum(String albumId) throws FacebookException { ensureAuthorizationEnabled(); return _like(albumId); } public boolean unlikeAlbum(String albumId) throws FacebookException { ensureAuthorizationEnabled(); return _unlike(albumId); } public URL getAlbumCoverPhoto(String albumId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = get(buildEndpoint(albumId, "picture")); try { return new URL(res.getResponseHeader("Location")); } catch (MalformedURLException urle) { throw new FacebookException(urle.getMessage(), urle); } } /* Book Methods */ public ResponseList getBooks() throws FacebookException { return getBooks("me", null); } public ResponseList getBooks(Reading reading) throws FacebookException { return getBooks("me", reading); } public ResponseList getBooks(String userId) throws FacebookException { return getBooks(userId, null); } public ResponseList getBooks(String userId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createBookList(get(buildEndpoint(userId, "books", reading))); } /* Checkin Methods */ public ResponseList getCheckins() throws FacebookException { return getCheckins("me", null); } public ResponseList getCheckins(Reading reading) throws FacebookException { return getCheckins("me", reading); } public ResponseList getCheckins(String id) throws FacebookException { return getCheckins(id, null); } public ResponseList getCheckins(String id, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createCheckinList(get(buildEndpoint(id, "checkins", reading))); } public String checkin(CheckinUpdate checkinUpdate) throws FacebookException { return checkin("me", checkinUpdate); } public String checkin(String userId, CheckinUpdate checkinUpdate) throws FacebookException { ensureAuthorizationEnabled(); JSONObject json = post(buildEndpoint(userId, "checkins"), checkinUpdate.asHttpParameterArray()) .asJSONObject(); return getRawString("id", json); } public Checkin getCheckin(String checkinId) throws FacebookException { return getCheckin(checkinId, null); } public Checkin getCheckin(String checkinId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createCheckin(get(buildEndpoint(checkinId, reading))); } public ResponseList getCheckinComments(String checkinId) throws FacebookException { return getCheckinComments(checkinId, null); } public ResponseList getCheckinComments(String checkinId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return _getComments(checkinId, reading); } public String commentCheckin(String checkinId, String message) throws FacebookException { ensureAuthorizationEnabled(); return _comment(checkinId, message); } public ResponseList getCheckinLikes(String checkinId) throws FacebookException { return getCheckinLikes(checkinId, null); } public ResponseList getCheckinLikes(String checkinId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createLikeList(get(buildEndpoint(checkinId, "likes", reading))); } public boolean likeCheckin(String checkinId) throws FacebookException { ensureAuthorizationEnabled(); return _like(checkinId); } public boolean unlikeCheckin(String checkinId) throws FacebookException { ensureAuthorizationEnabled(); return _unlike(checkinId); } /* Domain Methods */ public Domain getDomain(String domainId) throws FacebookException { return factory.createDomain(get(buildEndpoint(domainId))); } public Domain getDomainByName(String domainName) throws FacebookException { return factory.createDomain(get(conf.getRestBaseURL(), new HttpParameter[]{new HttpParameter("domain", domainName)})); } public List getDomainsByName(String... domainName) throws FacebookException { String domainNames = z_F4JInternalStringUtil.join(domainName); return factory.createDomainArray(get(conf.getRestBaseURL(), new HttpParameter[]{new HttpParameter("domains", domainNames)})); } /* Event Methods */ public ResponseList getEvents() throws FacebookException { return getEvents("me", null); } public ResponseList getEvents(Reading reading) throws FacebookException { return getEvents("me", reading); } public ResponseList getEvents(String id) throws FacebookException { return getEvents(id, null); } public ResponseList getEvents(String id, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createEventList(get(buildEndpoint(id, "events", reading))); } public String createEvent(EventUpdate eventUpdate) throws FacebookException { return createEvent("me", eventUpdate); } public String createEvent(String id, EventUpdate eventUpdate) throws FacebookException { ensureAuthorizationEnabled(); JSONObject json = post(buildEndpoint(id, "events"), eventUpdate.asHttpParameterArray()) .asJSONObject(); return getRawString("id", json); } public Event getEvent(String eventId) throws FacebookException { return getEvent(eventId, null); } public Event getEvent(String eventId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = get(buildEndpoint(eventId, reading)); String resStr = res.asString().trim(); if (resStr.equals("false")) { return null; } return factory.createEvent(res); } public boolean editEvent(String eventId, EventUpdate eventUpdate) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = post(buildEndpoint(eventId), eventUpdate.asHttpParameterArray()); return parseBoolean(res); } public boolean deleteEvent(String eventId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = delete(buildEndpoint(eventId)); return parseBoolean(res); } public String postEventLink(String eventId, URL link) throws FacebookException { return postEventLink(eventId, link, null); } public String postEventLink(String eventId, URL link, String message) throws FacebookException { ensureAuthorizationEnabled(); return _postLink(eventId, link, message); } public String postEventStatusMessage(String eventId, String message) throws FacebookException { ensureAuthorizationEnabled(); return _postStatusMessage(eventId, message); } public ResponseList getRSVPStatusAsNoreply(String eventId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildEndpoint(eventId, "noreply"))); } public ResponseList getRSVPStatusAsNoreply(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildEndpoint(eventId, "noreply/" + userId))); } public ResponseList getRSVPStatusAsInvited(String eventId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildEndpoint(eventId, "invited"))); } public ResponseList getRSVPStatusAsInvited(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildEndpoint(eventId, "invited/" + userId))); } public boolean inviteToEvent(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = post(buildEndpoint(eventId, "invited/" + userId)); return parseBoolean(res); } public boolean inviteToEvent(String eventId, String[] userIds) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = post(buildEndpoint(eventId, "invited"), new HttpParameter[] { new HttpParameter("users", z_F4JInternalStringUtil.join(userIds))}); return parseBoolean(res); } public boolean uninviteFromEvent(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = delete(buildEndpoint(eventId, "invited/" + userId)); return parseBoolean(res); } public ResponseList getRSVPStatusInAttending(String eventId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildEndpoint(eventId, "attending"))); } public ResponseList getRSVPStatusInAttending(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildEndpoint(eventId, "attending/" + userId))); } public boolean rsvpEventAsAttending(String eventId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = post(buildEndpoint(eventId, "attending")); return parseBoolean(res); } public ResponseList getRSVPStatusInMaybe(String eventId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildEndpoint(eventId, "maybe"))); } public ResponseList getRSVPStatusInMaybe(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildEndpoint(eventId, "maybe/" + userId))); } public boolean rsvpEventAsMaybe(String eventId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = post(buildEndpoint(eventId, "maybe")); return parseBoolean(res); } public ResponseList getRSVPStatusInDeclined(String eventId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildEndpoint(eventId, "declined"))); } public ResponseList getRSVPStatusInDeclined(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildEndpoint(eventId, "declined/" + userId))); } public boolean rsvpEventAsDeclined(String eventId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = post(buildEndpoint(eventId, "declined")); return parseBoolean(res); } public URL getEventPictureURL(String eventId) throws FacebookException { return getEventPictureURL(eventId, null); } public URL getEventPictureURL(String eventId, PictureSize size) throws FacebookException { ensureAuthorizationEnabled(); return _getPictureURL(eventId, size); } public boolean updateEventPicture(String eventId, Media source) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = post(buildEndpoint(eventId, "picture"), new HttpParameter[] {source.asHttpParameter("source")}); return parseBoolean(res); } public boolean deleteEventPicture(String eventId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = delete(buildEndpoint(eventId, "picture")); return parseBoolean(res); } public ResponseList getEventPhotos(String eventId) throws FacebookException { return getEventPhotos(eventId, null); } public ResponseList getEventPhotos(String eventId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createPhotoList(get(buildEndpoint(eventId, "photos", reading))); } public String postEventPhoto(String eventId, Media source) throws FacebookException { return postEventPhoto(eventId, source, null); } public String postEventPhoto(String eventId, Media source, String message) throws FacebookException { ensureAuthorizationEnabled(); HttpParameter[] httpParameters = new HttpParameter[] {source.asHttpParameter("source")}; if (message != null) { httpParameters = HttpParameter.merge(httpParameters, new HttpParameter[]{new HttpParameter("message", message)}); } JSONObject json = post(buildEndpoint(eventId, "photos"), httpParameters).asJSONObject(); return getRawString("id", json); } public ResponseList




© 2015 - 2024 Weber Informatics LLC | Privacy Policy