Search in sources :

Example 6 with NewsRobHttpClient

use of com.newsrob.download.NewsRobHttpClient in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method updateSubscriptionList.

void updateSubscriptionList(final EntryManager entryManager, final Job job) throws IOException, ParserConfigurationException, SAXException, GRTokenExpiredException {
    if (job.isCancelled())
        return;
    if (entryManager.getLastSyncedSubscriptions() != -1l && System.currentTimeMillis() < entryManager.getLastSyncedSubscriptions() + ONE_DAY_IN_MS) {
        PL.log("Not updating subscription list this time.", context);
        return;
    }
    PL.log("Updating subscription list.", context);
    Timing t = new Timing("UpdateSubscriptionList", context);
    final NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
    try {
        final String url = getGoogleHost() + "/reader/api/0/subscription/list";
        HttpRequestBase req = createGRRequest(httpClient, url);
        HttpResponse response;
        try {
            response = executeGRRequest(httpClient, req, true);
        } catch (GRAnsweredBadRequestException e) {
            throw new IOException("GR: Bad Request.");
        }
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser parser = saxParserFactory.newSAXParser();
        final Map<String, String> remoteTitlesAndIds = new HashMap<String, String>(107);
        DefaultHandler handler = new SimpleStringExtractorHandler() {

            private String currentFeedAtomId;

            private String currentString;

            @Override
            public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
                if (job.isCancelled())
                    throw new UpdateSubscriptionsCancelledException();
                super.startElement(uri, localName, name, attributes);
                String fqn = getFullyQualifiedPathName();
                if ("/object/list/object".equals(fqn)) {
                    currentFeedAtomId = null;
                } else if ("/object/list/object/string".equals(fqn)) {
                    currentString = attributes.getValue("name");
                }
            }

            @Override
            public void receivedString(String localName, String fqn, String s) {
                if (!"/object/list/object/string".equals(fqn))
                    return;
                if ("id".equals(currentString))
                    currentFeedAtomId = TAG_GR + s;
                else if ("title".equals(currentString)) {
                    if (currentFeedAtomId != null)
                        remoteTitlesAndIds.put(currentFeedAtomId, s);
                // entryManager.updateFeedName(currentFeedAtomId, s);
                }
            }
        };
        parser.parse(NewsRobHttpClient.getUngzippedContent(response.getEntity(), context), handler);
        if (NewsRob.isDebuggingEnabled(context))
            PL.log("Got subscription list with " + remoteTitlesAndIds.size() + " feeds.", context);
        if (job.isCancelled())
            return;
        entryManager.updateFeedNames(remoteTitlesAndIds);
    } finally {
        httpClient.close();
        t.stop();
    }
    entryManager.updateLastSyncedSubscriptions(System.currentTimeMillis());
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HashMap(java.util.HashMap) SimpleStringExtractorHandler(com.newsrob.util.SimpleStringExtractorHandler) Attributes(org.xml.sax.Attributes) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) SAXParser(javax.xml.parsers.SAXParser) Timing(com.newsrob.util.Timing) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 7 with NewsRobHttpClient

use of com.newsrob.download.NewsRobHttpClient in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method unsubscribeFeed.

public void unsubscribeFeed(String feedAtomId) throws IOException, NeedsSessionException, ReaderAPIException {
    NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
    try {
        Timing t = new Timing("EntriesRetriever.unsubcribeFeed()", context);
        HttpPost editApiRequest = new HttpPost(getGoogleHost() + "/reader/api/0/subscription/edit?client=" + CLIENT_NAME);
        LinkedList<NameValuePair> nameValuePairs = new LinkedList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("s", feedAtomId));
        nameValuePairs.add(new BasicNameValuePair("ac", "unsubscribe"));
        HttpResponse result;
        try {
            result = submitPostRequest(httpClient, editApiRequest, nameValuePairs, false);
        } catch (GRAnsweredBadRequestException e) {
            try {
                result = submitPostRequest(httpClient, editApiRequest, nameValuePairs, false);
            } catch (GRAnsweredBadRequestException e1) {
                throw new ReaderAPIException("GR believes it received a bad request.");
            }
        }
        if (HttpStatus.SC_OK == result.getStatusLine().getStatusCode())
            // LATER
            entryManager.removeFeedFromFeeds2Unsubscribe("tag:google.com,2005:reader/" + feedAtomId);
        // Clean
        // up
        // the
        // tag:google
        // business
        t.stop();
    } finally {
        httpClient.close();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) HttpResponse(org.apache.http.HttpResponse) Timing(com.newsrob.util.Timing) LinkedList(java.util.LinkedList)

Example 8 with NewsRobHttpClient

use of com.newsrob.download.NewsRobHttpClient in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method authenticate.

boolean authenticate(Context context, String email, String password, String captchaToken, String captchaAnswer) throws ClientProtocolException, IOException, AuthenticationFailedException {
    getEntryManager().getNewsRobNotificationManager().cancelSyncProblemNotification();
    NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
    try {
        // ?client="
        HttpPost authenticateRequest = new HttpPost("https://www.google.com/accounts/ClientLogin");
        // +
        // CLIENT_NAME);
        List<NameValuePair> keyValuePairs = new ArrayList<NameValuePair>();
        // HOSTED_OR_GOOGLE
        keyValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
        keyValuePairs.add(new BasicNameValuePair("Email", email));
        keyValuePairs.add(new BasicNameValuePair("Passwd", password));
        keyValuePairs.add(new BasicNameValuePair("source", CLIENT_NAME));
        keyValuePairs.add(new BasicNameValuePair("service", "reader"));
        if (captchaToken != null) {
            keyValuePairs.add(new BasicNameValuePair("logintoken", captchaToken));
            keyValuePairs.add(new BasicNameValuePair("logincaptcha", captchaAnswer));
        }
        authenticateRequest.setEntity(new UrlEncodedFormEntity(keyValuePairs, HTTP.UTF_8));
        HttpResponse response = httpClient.execute(authenticateRequest);
        int statusCode = response.getStatusLine().getStatusCode();
        String content = EntityUtils.toString(response.getEntity());
        switch(statusCode) {
            case HttpStatus.SC_UNAUTHORIZED:
            case HttpStatus.SC_FORBIDDEN:
                Map<String, String> keyValues = U.parseKeyValuePairsFromString(content);
                String error = keyValues.get("Error");
                String requiredCaptchaToken = keyValues.get("CaptchaToken");
                String captchaUrl = keyValues.get("CaptchaUrl");
                if (captchaUrl != null) {
                    captchaUrl = "https://www.google.com/accounts/" + captchaUrl;
                    String msg = "Error=" + error + " CaptchaToken=" + captchaToken + " CaptchaUrl=" + captchaUrl;
                    Log.d(TAG, msg);
                    PL.log(msg, context);
                    throw new LoginWithCaptchaRequiredException(requiredCaptchaToken, captchaUrl);
                }
                if ("BadAuthentication".equals(error))
                    error = "Username / Password wrong?";
                else
                    error += "\n" + response.getStatusLine();
                throw new AuthenticationFailedException("Authentication failed:\n" + error);
            case HttpStatus.SC_OK:
                break;
            default:
                String msg = "Oh, status code was " + statusCode + " but unexpected.";
                PL.log(msg, context);
                Log.w(TAG, msg);
                throw new AuthenticationFailedException("Autentication failed:\n" + EntityUtils.toString(response.getEntity()));
        }
        getEntryManager().saveAuthToken(new AuthToken(AuthToken.AuthType.AUTH_STANDALONE, U.parseKeyValuePairsFromString(content).get("Auth")));
        response.getEntity().consumeContent();
        getEntryManager().getNewsRobNotificationManager().cancelSyncProblemNotification();
        return true;
    } finally {
        httpClient.close();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient)

Example 9 with NewsRobHttpClient

use of com.newsrob.download.NewsRobHttpClient in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method fetchNewArticlesByAtomIds.

private void fetchNewArticlesByAtomIds(Job job, FetchContext fetchCtx, List<String> atomIds) throws SAXException, IOException, ParserConfigurationException, NeedsSessionException, ReaderAPIException {
    NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
    try {
        String url = getGoogleHost() + "/reader/api/0/stream/items/contents?output=atom&xt=" + GOOGLE_STATE_READ + "&client=" + CLIENT_NAME;
        HttpPost getNewArticlesRequest = new HttpPost(url);
        List<NameValuePair> keyValuePairs = new ArrayList<NameValuePair>();
        for (String unreadId : atomIds) {
            keyValuePairs.add(new BasicNameValuePair("i", unreadId));
        }
        keyValuePairs.add(new BasicNameValuePair("client", CLIENT_NAME));
        HttpResponse response;
        try {
            response = submitPostRequest(httpClient, getNewArticlesRequest, keyValuePairs, true);
        } catch (GRAnsweredBadRequestException e) {
            try {
                response = submitPostRequest(httpClient, getNewArticlesRequest, keyValuePairs, true);
            } catch (GRAnsweredBadRequestException e1) {
                throw new ReaderAPIException("GR believes to have received a bad request!");
            }
        }
        processReadingList(job, fetchCtx, response);
    } finally {
        httpClient.close();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse)

Example 10 with NewsRobHttpClient

use of com.newsrob.download.NewsRobHttpClient in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method discoverFeeds.

public List<DiscoveredFeed> discoverFeeds(final String query) throws ReaderAPIException, IOException, GRTokenExpiredException, ParserConfigurationException, SAXException, GRAnsweredBadRequestException {
    Timing t = new Timing("discoverFeeds()", context);
    final List<DiscoveredFeed> results = new ArrayList<DiscoveredFeed>();
    if (query == null || query.length() == 0)
        return results;
    NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
    try {
        final String queryPath = "/reader/api/0/feed-finder?q=";
        HttpRequestBase req = createGRRequest(httpClient, getGoogleHost() + queryPath + query);
        HttpResponse response = executeGRRequest(httpClient, req, true);
        throwExceptionWhenNotStatusOK(response);
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser parser = saxParserFactory.newSAXParser();
        DefaultHandler handler = new SimpleStringExtractorHandler() {

            private DiscoveredFeed discoveredFeed;

            @Override
            public void endElement(String uri, String localName, String name) throws SAXException {
                super.endElement(uri, localName, name);
                if (discoveredFeed != null && "entry".equals(localName)) {
                    // System.out.println("Added discovered feed " +
                    // discoveredFeed);
                    results.add(discoveredFeed);
                    discoveredFeed = null;
                }
            }

            @Override
            public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
                super.startElement(uri, localName, name, attributes);
                // System.out.println("startElement=" + localName);
                if ("entry".equals(localName)) {
                    discoveredFeed = new DiscoveredFeed();
                    // System.out.println("Created new Discovered Feed");
                    return;
                }
                if ("link".equals(localName)) {
                    String rel = attributes.getValue("rel");
                    String href = attributes.getValue("href");
                    if ("self".equals(rel))
                        return;
                    if (discoveredFeed != null) {
                        if (rel != null) {
                            if ("alternate".equals(rel))
                                discoveredFeed.alternateUrl = href;
                            else if ("http://www.google.com/reader/atom/relation/feed".equals(rel))
                                discoveredFeed.feedUrl = href;
                        }
                    } else {
                        DiscoveredFeed df = new DiscoveredFeed();
                        df.title = query;
                        df.feedUrl = href;
                        results.add(df);
                    }
                }
            // System.out.println("startElement2=" + localName);
            }

            @Override
            public void receivedString(String localName, String fqn, String s) {
                if (discoveredFeed == null)
                    return;
                if ("title".equals(localName)) {
                    discoveredFeed.title = s;
                } else if ("content".equals(localName)) {
                    discoveredFeed.snippet = s;
                }
            }
        };
        parser.parse(NewsRobHttpClient.getUngzippedContent(response.getEntity(), context), handler);
        return results;
    } finally {
        httpClient.close();
        t.stop();
    }
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) SimpleStringExtractorHandler(com.newsrob.util.SimpleStringExtractorHandler) ArrayList(java.util.ArrayList) Attributes(org.xml.sax.Attributes) HttpResponse(org.apache.http.HttpResponse) DefaultHandler(org.xml.sax.helpers.DefaultHandler) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) SAXParser(javax.xml.parsers.SAXParser) Timing(com.newsrob.util.Timing) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

NewsRobHttpClient (com.newsrob.download.NewsRobHttpClient)12 Timing (com.newsrob.util.Timing)9 ArrayList (java.util.ArrayList)8 HttpResponse (org.apache.http.HttpResponse)7 IOException (java.io.IOException)6 HttpPost (org.apache.http.client.methods.HttpPost)6 NameValuePair (org.apache.http.NameValuePair)5 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)5 SAXException (org.xml.sax.SAXException)4 SimpleStringExtractorHandler (com.newsrob.util.SimpleStringExtractorHandler)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 MalformedURLException (java.net.MalformedURLException)3 ParseException (java.text.ParseException)3 ExecutionException (java.util.concurrent.ExecutionException)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 SAXParser (javax.xml.parsers.SAXParser)3 SAXParserFactory (javax.xml.parsers.SAXParserFactory)3 ClientProtocolException (org.apache.http.client.ClientProtocolException)3 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)3 DefaultHandler (org.xml.sax.helpers.DefaultHandler)3