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 static facebook4j.internal.util.z_F4JInternalParseUtil.*;

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.List;
import java.util.Locale;
import java.util.Map;

import facebook4j.Question.Option;
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;

/**
 * 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 buildURL(String id) { return buildURL(id, null, null); } private String buildURL(String id, Reading reading) { return buildURL(id, null, reading); } private String buildURL(String id, String connection) { return buildURL(id, connection, null); } private String buildURL(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 buildVideoURL(String id, String connection) { return buildVideoURL(id, connection, null); } private String buildVideoURL(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 buildSearchURL(String query, String objectType, Reading reading) { String q = null; if (query != null) { q = HttpParameter.encode(query); } StringBuilder url = new StringBuilder() .append(buildURL("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(buildURL("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(buildURL(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(String userId) throws FacebookException { return getPictureURL(userId, null); } public URL getPictureURL(String userId, PictureSize size) throws FacebookException { String url = buildURL(userId, "picture"); HttpResponse res; if (size != null) { res = get(url, new HttpParameter[]{new HttpParameter("type", size.toString())}); } else { res = get(url); } try { return new URL(res.getResponseHeader("Location")); } catch (MalformedURLException urle) { throw new FacebookException(urle.getMessage(), urle); } } 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(buildURL(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(buildURL(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(buildURL(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(buildURL(userId, "achievements"), new HttpParameter[] {new HttpParameter("achievement", achievementURL.toString())}); return Boolean.valueOf(res.asString().trim()); } /* 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(buildURL(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 userId) throws FacebookException { return getAlbums(userId, null); } public ResponseList getAlbums(String userId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createAlbumList(get(buildURL(userId, "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(buildURL(albumId, reading))); } public String createAlbum(AlbumCreate albumCreate) throws FacebookException { return createAlbum("me", albumCreate); } public String createAlbum(String userId, AlbumCreate albumCreate) throws FacebookException { ensureAuthorizationEnabled(); JSONObject json = post(buildURL(userId, "albums"), albumCreate.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(buildURL(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(buildURL(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 ResponseList getAlbumLikes(String albumId) throws FacebookException { return getAlbumLikes(albumId, null); } public ResponseList getAlbumLikes(String albumId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createLikeList(get(buildURL(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(buildURL(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(buildURL(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 userId) throws FacebookException { return getCheckins(userId, null); } public ResponseList getCheckins(String userId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createCheckinList(get(buildURL(userId, "checkins", reading))); } public String checkin(CheckinCreate checkinCreate) throws FacebookException { return checkin("me", checkinCreate); } public String checkin(String userId, CheckinCreate checkinCreate) throws FacebookException { ensureAuthorizationEnabled(); JSONObject json = post(buildURL(userId, "checkins"), checkinCreate.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(buildURL(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(buildURL(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(buildURL(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 userId) throws FacebookException { return getEvents(userId, null); } public ResponseList getEvents(String userId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createEventList(get(buildURL(userId, "events", reading))); } public String createEvent(EventUpdate eventUpdate) throws FacebookException { return createEvent("me", eventUpdate); } public String createEvent(String userId, EventUpdate eventUpdate) throws FacebookException { ensureAuthorizationEnabled(); JSONObject json = post(buildURL(userId, "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(buildURL(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(buildURL(eventId), eventUpdate.asHttpParameterArray()); return Boolean.valueOf(res.asString().trim()); } public boolean deleteEvent(String eventId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = delete(buildURL(eventId)); return Boolean.valueOf(res.asString().trim()); } public ResponseList getEventFeed(String eventId) throws FacebookException { return getEventFeed(eventId, null); } public ResponseList getEventFeed(String eventId, Reading reading) throws FacebookException { ensureAuthorizationEnabled(); return factory.createPostList(get(buildURL(eventId, reading))); } public String postEventFeed(String eventId, PostUpdate postUpdate) throws FacebookException { ensureAuthorizationEnabled(); return _postFeed(eventId, postUpdate); } 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(buildURL(eventId, "noreply"))); } public ResponseList getRSVPStatusAsNoreply(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildURL(eventId, "noreply/" + userId))); } public ResponseList getRSVPStatusAsInvited(String eventId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildURL(eventId, "invited"))); } public ResponseList getRSVPStatusAsInvited(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildURL(eventId, "invited/" + userId))); } public boolean inviteToEvent(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = post(buildURL(eventId, "invited/" + userId)); return Boolean.valueOf(res.asString().trim()); } public boolean inviteToEvent(String eventId, String[] userIds) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = post(buildURL(eventId, "invited"), new HttpParameter[] { new HttpParameter("users", z_F4JInternalStringUtil.join(userIds))}); return Boolean.valueOf(res.asString().trim()); } public boolean uninviteFromEvent(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = delete(buildURL(eventId, "invited/" + userId)); return Boolean.valueOf(res.asString().trim()); } public ResponseList getRSVPStatusInAttending(String eventId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildURL(eventId, "attnding"))); } public ResponseList getRSVPStatusInAttending(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildURL(eventId, "attending/" + userId))); } public boolean rsvpEventAsAttending(String eventId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = post(buildURL(eventId, "attending")); return Boolean.valueOf(res.asString().trim()); } public ResponseList getRSVPStatusInMaybe(String eventId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildURL(eventId, "maybe"))); } public ResponseList getRSVPStatusInMaybe(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildURL(eventId, "maybe/" + userId))); } public boolean rsvpEventAsMaybe(String eventId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = post(buildURL(eventId, "maybe")); return Boolean.valueOf(res.asString().trim()); } public ResponseList getRSVPStatusInDeclined(String eventId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildURL(eventId, "declined"))); } public ResponseList getRSVPStatusInDeclined(String eventId, String userId) throws FacebookException { ensureAuthorizationEnabled(); return factory.createRSVPStatusList(get(buildURL(eventId, "declined/" + userId))); } public boolean rsvpEventAsDeclined(String eventId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = post(buildURL(eventId, "declined")); return Boolean.valueOf(res.asString().trim()); } public URL getEventPictureURL(String eventId) throws FacebookException { return getEventPictureURL(eventId, null); } public URL getEventPictureURL(String eventId, PictureSize size) throws FacebookException { ensureAuthorizationEnabled(); return _getPictureURL(eventId, "picture", size); } public boolean updateEventPicture(String eventId, Media source) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = post(buildURL(eventId, "picture"), new HttpParameter[] {source.asHttpParameter("source")}); return Boolean.valueOf(res.asString().trim()); } public boolean deleteEventPicture(String eventId) throws FacebookException { ensureAuthorizationEnabled(); HttpResponse res = delete(buildURL(eventId, "picture")); return Boolean.valueOf(res.asString().trim()); } 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(buildURL(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(buildURL(eventId, "photos"), httpParameters).asJSONObject(); return getRawString("id", json); } public ResponseList




© 2015 - 2024 Weber Informatics LLC | Privacy Policy