Java tutorial
/* * This file is part of Minus-Java. * * Minus-Java is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Minus-Java is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Minus-Java. If not, see <http://www.gnu.org/licenses/>. */ package net.paissad.minus.api; import static net.paissad.minus.utils.HttpClientUtils.ExpectedResponseType.STRING; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.json.simple.JSONObject; import net.paissad.minus.MinusConstants; import net.paissad.minus.api.listener.MinusListener; import net.paissad.minus.exception.MinusException; import net.paissad.minus.utils.HttpClientUtils; import net.paissad.minus.utils.JSONUtils; /** * @author Papa Issa DIAKHATE (paissad) * */ class TimelineUtils implements MinusConstants { private TimelineUtils() { } /** * @param user * @param gallery - The readerId of the gallery to use. * @param key - The key for which we want its related value. * @param listener * @return The value for the specified key. * @throws MinusException If the key is unknown, or if the specified gallery * is not found, or if unable to connect correctly to Minus API. * @see MinusConstants */ static Object getValueFromGallery(final MinusUser user, final MinusGallery gallery, final String key) throws MinusException { try { MinusHttpResponse minusResp = HttpClientUtils.doGet(TIMELINE_HISTORY_URL, null, user.getSessionId(), null, STRING); String httpResponse = (String) minusResp.getHttpResponse(); MinusUtils.validateHttpResponse(httpResponse); Map<String, Object> jsonResult = JSONUtils.getMapFromJSONString(httpResponse); MinusUtils.validateJsonResult(jsonResult); @SuppressWarnings("unchecked") List<JSONObject> galleriesAsObjects = (List<JSONObject>) jsonResult.get(RESPONSE_KEY_GALLERIES); for (JSONObject obj : galleriesAsObjects) { String currentReaderId = (String) obj.get(RESPONSE_KEY_READER_ID); if (currentReaderId.equals(gallery.getReaderId())) { // We got the correct gallery ! if (!obj.keySet().contains(key)) { throw new MinusException("The key (" + key + ") is unknown for the entity -> " + obj); } return obj.get(key); } } throw new MinusException("Unable to find the gallery " + gallery); } catch (Exception e) { throw new MinusException(e.getMessage(), e); } } /** * This method is an alias of TimelineUtils#getHistory(MinusUser, * MinusListener). * * @param user - The user for which we want to retrieve his galleries. * @param listener * @return The list of galleries. * @throws MinusException * @see #getHistory(MinusUser, MinusListener) */ static List<MinusGallery> getGalleries(final MinusUser user, final MinusListener<Map<String, Object>> listener) throws MinusException { return getHistory(user, listener); } /** * * @param user - The user for which we want to retrieve his history. * @param listener * @return The list of galleries. * @throws MinusException * @see #getGalleries(MinusUser, MinusListener) */ static List<MinusGallery> getHistory(final MinusUser user, final MinusListener<Map<String, Object>> listener) throws MinusException { try { MinusHttpResponse minusResp = HttpClientUtils.doGet(TIMELINE_HISTORY_URL, null, user.getSessionId(), null, STRING); String httpResponse = (String) minusResp.getHttpResponse(); MinusUtils.validateHttpResponse(httpResponse); Map<String, Object> jsonResult = JSONUtils.getMapFromJSONString(httpResponse); MinusUtils.validateJsonResult(jsonResult); MinusUtils.sendMessageToListenerIfNotNull(jsonResult, listener); return getGalleriesFromMap(user, jsonResult, false); } catch (Exception e) { throw new MinusException("Error while retrieving 'history' for user " + user + " : " + e.getMessage(), e); } } /** * Retrieves the galleries only for the specified user. * * @param user * @param listener * @return The list of galleries for the specified user. * @throws MinusException */ static List<MinusGallery> getMine(final MinusUser user, final MinusListener<Map<String, Object>> listener) throws MinusException { try { MinusHttpResponse minusResp = HttpClientUtils.doGet(TIMELINE_MINE_URL, null, user.getSessionId(), null, STRING); String httpResponse = (String) minusResp.getHttpResponse(); MinusUtils.validateHttpResponse(httpResponse); Map<String, Object> jsonResult = JSONUtils.getMapFromJSONString(httpResponse); MinusUtils.validateJsonResult(jsonResult); MinusUtils.sendMessageToListenerIfNotNull(jsonResult, listener); return getGalleriesFromMap(user, jsonResult, true); } catch (Exception e) { throw new MinusException( "Error while retrieving own galleries for user " + user + " : " + e.getMessage(), e); } } static List<MinusGallery> getFeed(final MinusUser user, final MinusListener<Map<String, Object>> listener) throws MinusException { try { MinusHttpResponse minusResp = HttpClientUtils.doGet(TIMELINE_FEED_URL, null, user.getSessionId(), null, STRING); String httpResponse = (String) minusResp.getHttpResponse(); MinusUtils.validateHttpResponse(httpResponse); Map<String, Object> jsonResult = JSONUtils.getMapFromJSONString(httpResponse); MinusUtils.validateJsonResult(jsonResult); MinusUtils.sendMessageToListenerIfNotNull(jsonResult, listener); return getGalleriesFromMap(user, jsonResult, false); } catch (Exception e) { throw new MinusException("Error while retrieving feed for the user " + user + " : " + e.getMessage(), e); } } // _________________________________________________________________________ /** * Read The specified <tt>Map</tt> and retrieve the gallery entities. * * @param user * @param jsonResult - The Map (this is a JSONObject in reality) * @param verifyEqualityOfUsernames - If <tt>true</tt> then verify that the * names of the creator of each retrieved gallery is always equal * to the name of the user (<code>user.getUsername()</code>). * <b>Note</b>: For example, in "/pane/feed.json", we can have * galleries of many different users, but in "/pane/mine.json", * we should only have the user's own gallery. That's why i think * it's a good practice to do checks. * @return The list of galleries, or an empty list if no JSONObject found. */ private static List<MinusGallery> getGalleriesFromMap(final MinusUser user, final Map<String, Object> jsonResult, final boolean verifyEqualityOfUsernames) { List<MinusGallery> galleries = new ArrayList<MinusGallery>(); @SuppressWarnings("unchecked") List<JSONObject> galleriesAsObjects = (List<JSONObject>) jsonResult.get(RESPONSE_KEY_GALLERIES); for (JSONObject obj : galleriesAsObjects) { String readerId = (String) obj.get(RESPONSE_KEY_READER_ID); String editorId = (String) obj.get(RESPONSE_KEY_EDITOR_ID); String creator = (String) obj.get(RESPONSE_KEY_CREATOR); if (creator == null || creator.trim().isEmpty()) { throw new IllegalStateException("A gallery should contains the name of its creator."); } if (verifyEqualityOfUsernames) { if (!creator.equalsIgnoreCase(user.getUsername())) { throw new IllegalStateException( "The name of the gallery's creator should not be different from the user's name"); } } MinusGallery oneGallery = new MinusGallery(user, editorId, readerId); galleries.add(oneGallery); } return galleries; } // _________________________________________________________________________ }