예제 #1
0
  public RequestExecutor execute(Request r) throws ClientProtocolException, IOException {
    clear();
    r.customizeIfNeeded();
    request = r.getRequest();

    // Optionally setup for basic authentication
    if (r.getUsername() != null) {
      httpClient
          .getCredentialsProvider()
          .setCredentials(
              AuthScope.ANY, new UsernamePasswordCredentials(r.getUsername(), r.getPassword()));

      // And add request interceptor to have preemptive authentication
      httpClient.addRequestInterceptor(new PreemptiveAuthInterceptor(), 0);
    } else {
      // Make sure existing credentials are not reused - but looks like we
      // cannot set null as credentials
      httpClient
          .getCredentialsProvider()
          .setCredentials(
              AuthScope.ANY,
              new UsernamePasswordCredentials(getClass().getName(), getClass().getSimpleName()));
      httpClient.removeRequestInterceptorByClass(PreemptiveAuthInterceptor.class);
    }

    // Setup redirects
    httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, r.getRedirects());

    // Execute request
    response = httpClient.execute(request);
    entity = response.getEntity();
    if (entity != null) {
      consumeEntity();
    }
    return this;
  }
예제 #2
0
  /**
   * Creates a new fetcher for fetching HTTP objects. Not really suitable for production use. Use of
   * an HTTP proxy for security is also necessary for production deployment.
   *
   * @param maxObjSize Maximum size, in bytes, of the object we will fetch, 0 if no limit..
   * @param connectionTimeoutMs timeout, in milliseconds, for connecting to hosts.
   * @param readTimeoutMs timeout, in millseconds, for unresponsive connections
   * @param basicHttpFetcherProxy The http proxy to use.
   */
  public BasicHttpFetcher(
      int maxObjSize, int connectionTimeoutMs, int readTimeoutMs, String basicHttpFetcherProxy) {
    // Create and initialize HTTP parameters
    setMaxObjectSizeBytes(maxObjSize);
    setSlowResponseWarning(DEFAULT_SLOW_RESPONSE_WARNING);

    HttpParams params = new BasicHttpParams();

    ConnManagerParams.setTimeout(params, connectionTimeoutMs);

    // These are probably overkill for most sites.
    ConnManagerParams.setMaxTotalConnections(params, 1152);
    ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(256));

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(params, "Apache Shindig");
    HttpProtocolParams.setContentCharset(params, "UTF-8");

    HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMs);
    HttpConnectionParams.setSoTimeout(params, readTimeoutMs);
    HttpConnectionParams.setStaleCheckingEnabled(params, true);

    HttpClientParams.setRedirecting(params, true);
    HttpClientParams.setAuthenticating(params, false);

    // Create and initialize scheme registry
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    // UCSF. Change to allow self signed registered certs
    /**
     * try { SSLContext sslContext = SSLContext.getInstance("SSL"); // set up a TrustManager that
     * trusts everything sslContext.init(null, new TrustManager[] { new X509TrustManager() { public
     * X509Certificate[] getAcceptedIssuers() { System.out.println("getAcceptedIssuers
     * ============="); return null; }
     *
     * <p>public void checkClientTrusted(X509Certificate[] certs, String authType) {
     * System.out.println("checkClientTrusted ============="); }
     *
     * <p>public void checkServerTrusted(X509Certificate[] certs, String authType) {
     * System.out.println("checkServerTrusted ============="); } } }, new SecureRandom());
     * SSLSocketFactory sf = new SSLSocketFactory(sslContext);
     * sf.setHostnameVerifier((X509HostnameVerifier)org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
     * schemeRegistry.register(new Scheme("https", sf, 443)); } catch (Exception e) { throw new
     * RuntimeException(e); }
     */
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    // end UCSF

    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    DefaultHttpClient client = new DefaultHttpClient(cm, params);

    // Set proxy if set via guice.
    if (!StringUtils.isEmpty(basicHttpFetcherProxy)) {
      String[] splits = basicHttpFetcherProxy.split(":");
      ConnRouteParams.setDefaultProxy(
          client.getParams(), new HttpHost(splits[0], Integer.parseInt(splits[1]), "http"));
    }

    // try resending the request once
    client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(1, true));

    // Add hooks for gzip/deflate
    client.addRequestInterceptor(
        new HttpRequestInterceptor() {
          public void process(final org.apache.http.HttpRequest request, final HttpContext context)
              throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
              request.addHeader("Accept-Encoding", "gzip, deflate");
            }
          }
        });
    client.addResponseInterceptor(
        new HttpResponseInterceptor() {
          public void process(
              final org.apache.http.HttpResponse response, final HttpContext context)
              throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
              Header ceheader = entity.getContentEncoding();
              if (ceheader != null) {
                for (HeaderElement codec : ceheader.getElements()) {
                  String codecname = codec.getName();
                  if ("gzip".equalsIgnoreCase(codecname)) {
                    response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                    return;
                  } else if ("deflate".equals(codecname)) {
                    response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
                    return;
                  }
                }
              }
            }
          }
        });
    client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler());

    // Disable automatic storage and sending of cookies (see SHINDIG-1382)
    client.removeRequestInterceptorByClass(RequestAddCookies.class);
    client.removeResponseInterceptorByClass(ResponseProcessCookies.class);

    // Use Java's built-in proxy logic in case no proxy set via guice.
    if (StringUtils.isEmpty(basicHttpFetcherProxy)) {
      ProxySelectorRoutePlanner routePlanner =
          new ProxySelectorRoutePlanner(
              client.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
      client.setRoutePlanner(routePlanner);
    }

    FETCHER = client;
  }
예제 #3
0
  @Override
  protected Document doInBackground(RouteParams... params) {

    if (params.length == 0) return null;

    RouteParams par = params[0];

    String cached = null, xmlstring = null;

    if (!par.force_download) cached = getCached(par.train_number);

    if (cached != null
        && checkTable(cached, par.departure, par.deptime)
        && checkTable(cached, par.arrival, par.arrtime)) xmlstring = cached;
    else {
      if (!CommonUtils.onlineCheckSilent()) return null;

      isCached = false;
      publishProgress();

      String data =
          "start=yes&REQTrain_name="
              + par.train_number
              + "&date="
              + par.date
              + "&time="
              + par.deptime
              + "&sTI=1&dirInput="
              + par.arrival
              + "&L=vs_java3&input="
              + par.departure
              + "&boardType="
              + par.type;
      String url = "http://rozklad.sitkol.pl/bin/stboard.exe/pn";

      try {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost(url);
        client.removeRequestInterceptorByClass(
            org.apache.http.protocol.RequestExpectContinue.class);
        client.removeRequestInterceptorByClass(org.apache.http.protocol.RequestUserAgent.class);
        request.addHeader("Content-Type", "text/plain");
        request.setEntity(new StringEntity(data));

        InputStream inputStream = client.execute(request).getEntity().getContent();

        ByteArrayOutputStream content = new ByteArrayOutputStream();

        int readBytes = 0;
        while ((readBytes = inputStream.read(sBuffer)) != -1) {
          content.write(sBuffer, 0, readBytes);
        }

        xmlstring = new String(content.toByteArray());
        xmlstring = xmlstring.replace("< ", "<");

        saveInCache(par.train_number, xmlstring);
      } catch (Exception e) {
        return null;
      }
    }
    try {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = factory.newDocumentBuilder();
      InputSource inStream = new InputSource();
      inStream.setCharacterStream(new StringReader("<a>" + xmlstring + "</a>"));
      return db.parse(inStream);
    } catch (Exception e) {
      return null;
    }
  }