Esempio n. 1
0
 @Override
 public void run() {
   /*
    * Create the client call
    */
   while (true) {
     try {
       client = new DefaultHttpClient();
       client.setCredentialsProvider(credentialProvider);
       HttpGet get = new HttpGet(STREAMING_API_URL + track);
       HttpResponse response;
       try {
         // Execute
         response = client.execute(get);
         StatusLine status = response.getStatusLine();
         if (status.getStatusCode() == 200) {
           InputStream inputStream = response.getEntity().getContent();
           BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
           String in;
           // Read line by line
           while ((in = reader.readLine()) != null) {
             // Parse and emit
             Tweet tweet = parseTweet(in);
             tweets.add(tweet);
           }
         }
       } catch (IOException e) {
         LOG.error("Error in communication with twitter api [" + get.getURI().toString() + "]");
         try {
           Thread.sleep(10000);
         } catch (InterruptedException e1) {
         }
       }
     } catch (Throwable e) {
       LOG.error(e);
     }
     LOG.error("An error ocurs, all runnable thread will be restarted");
     try {
       Thread.sleep(1000);
     } catch (Exception e) {
     }
   }
 }
Esempio n. 2
0
  /**
   * Prepares objects for HTTP communications with the Gatekeeper servlet.
   *
   * @return
   */
  private HttpClient initHttpConnection() {
    // Set client parameters.
    HttpParams params = RestUtils.createDefaultHttpParams();
    HttpProtocolParams.setUserAgent(
        params, ServiceUtils.getUserAgentDescription(userAgentDescription));

    // Set connection parameters.
    HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
    HttpConnectionParams.setSoTimeout(params, connectionTimeout);
    HttpConnectionParams.setStaleCheckingEnabled(params, false);

    DefaultHttpClient httpClient = new DefaultHttpClient(params);
    // Replace default error retry handler.
    httpClient.setHttpRequestRetryHandler(new RestUtils.JetS3tRetryHandler(maxRetryCount, null));

    // httpClient.getParams().setAuthenticationPreemptive(true);
    httpClient.setCredentialsProvider(credentialsProvider);

    return httpClient;
  }
  private DefaultHttpClient createConnection(String user, String password) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpParams params = httpClient.getParams();
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);

    UsernamePasswordCredentials bCred = new UsernamePasswordCredentials(user, password);
    BasicCredentialsProvider cProvider = new BasicCredentialsProvider();
    cProvider.setCredentials(AuthScope.ANY, bCred);

    params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
    httpClient.setParams(params);

    DefaultHttpClient nHttpClient = new DefaultHttpClient(cm, params);
    nHttpClient.setCredentialsProvider(cProvider);
    return nHttpClient;
  }