com.byteridge.bookcircle.utils.ResponseParser.java Source code

Java tutorial

Introduction

Here is the source code for com.byteridge.bookcircle.utils.ResponseParser.java

Source

//===============================================================================
// Copyright (c) 2010 Adam C Jones
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//===============================================================================

package com.byteridge.bookcircle.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.SAXException;

import android.net.Uri;
import android.sax.RootElement;
import android.util.Xml;

public class ResponseParser {

    private final static String _ConsumerKey = "4zhFdoCr6ZQaDJjobqFoiw";
    private final static String _ConsumerSecret = "j1I2821RvC7QFjj3FL5J5gCivzxnsiRPYYPXPybrc";
    private static CommonsHttpOAuthConsumer _Consumer = new CommonsHttpOAuthConsumer(_ConsumerKey, _ConsumerSecret);
    private static boolean _IsAuthenticated = false;

    public static Response parse(InputStream inputStream) throws IOException, SAXException {
        final Response response = new Response();
        RootElement root = new RootElement("GoodreadsResponse");
        response.set_Book(Book.appendSingletonListener(root, 0));
        response.set_Request(Request.appendSingletonListener(root));
        response.set_User(User.appendSingletonListener(root, 0));
        response.set_Shelves(Shelves.appendSingletonListener(root, 0));
        response.set_Reviews(Reviews.appendSingletonListener(root, 0));
        response.set_Review(Review.appendSingletonListener(root, 0));
        try {
            Xml.parse(inputStream, Xml.Encoding.UTF_8, root.getContentHandler());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    public static User GetAuthorizedUser() throws Exception {
        HttpGet getRequest = new HttpGet("http://www.goodreads.com/api/auth_user");
        _Consumer.sign(getRequest);
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(getRequest);

        Response responseData = ResponseParser.parse(response.getEntity().getContent());
        return responseData.get_User();
    }

    public static Reviews GetBooksOnShelf(String shelfName, String userId) throws Exception {
        return GetBooksOnShelf(shelfName, userId, 1);
    }

    public static Reviews GetBooksOnShelf(String shelfName, String userId, int page) throws Exception {
        Uri.Builder builder = new Uri.Builder();
        builder.scheme("http");
        builder.authority("www.goodreads.com");
        builder.path("review/list/" + userId + ".xml");
        builder.appendQueryParameter("key", _ConsumerKey);
        builder.appendQueryParameter("shelf", shelfName);
        builder.appendQueryParameter("v", "2");
        builder.appendQueryParameter("sort", "date_updated");
        builder.appendQueryParameter("order", "d");
        builder.appendQueryParameter("page", Integer.toString(page));
        HttpGet getBooksOnShelfRequest = new HttpGet(builder.build().toString());
        if (get_IsAuthenticated()) {
            _Consumer.sign(getBooksOnShelfRequest);
        }

        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response;

        response = httpClient.execute(getBooksOnShelfRequest);
        Response responseData = ResponseParser.parse(response.getEntity().getContent());
        return responseData.get_Reviews();

    }

    public static List<UserShelf> GetShelvesForUser(String userId) throws Exception {
        Uri.Builder builder = new Uri.Builder();
        builder.scheme("http");
        builder.authority("www.goodreads.com");
        builder.path("shelf/list");
        builder.appendQueryParameter("format", "xml");
        builder.appendQueryParameter("user_id", userId);
        builder.appendQueryParameter("key", _ConsumerKey);
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet getShelvesRequest = new HttpGet(builder.build().toString());
        if (get_IsAuthenticated()) {
            _Consumer.sign(getShelvesRequest);
        }
        HttpResponse shelvesResponse = httpClient.execute(getShelvesRequest);

        Response shelvesResponseData = ResponseParser.parse(shelvesResponse.getEntity().getContent());
        return shelvesResponseData.get_Shelves().get_UserShelves();
    }

    public static User GetUserDetails(String userId) throws Exception {
        Uri.Builder builder = new Uri.Builder();
        builder.scheme("http");
        builder.authority("www.goodreads.com");
        builder.path("user/show/" + userId + ".xml");
        builder.appendQueryParameter("key", _ConsumerKey);
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet getRequest = new HttpGet(builder.build().toString());
        if (get_IsAuthenticated()) {
            _Consumer.sign(getRequest);
        }
        HttpResponse response;
        response = httpClient.execute(getRequest);
        Response responseData = ResponseParser.parse(response.getEntity().getContent());
        return responseData.get_User();
    }

    private static void set_IsAuthenticated(boolean _IsAuthenticated) {
        ResponseParser._IsAuthenticated = _IsAuthenticated;
    }

    public static boolean get_IsAuthenticated() {
        return _IsAuthenticated;
    }

    public static void SetTokenWithSecret(String token, String tokenSecret) {
        _Consumer.setTokenWithSecret(token, tokenSecret);
        set_IsAuthenticated(true);
    }

    public static void ClearAuthentication() {
        set_IsAuthenticated(false);
    }
}