private void configureHttpClient() {
   MultiThreadedHttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager();
   HttpConnectionManagerParams connParams = connMgr.getParams();
   connParams.setMaxTotalConnections(maxConnections);
   connParams.setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, maxConnections);
   connMgr.setParams(connParams);
   hc = new HttpClient(connMgr);
   // NOTE: These didn't seem to help in my initial testing
   //			hc.getParams().setParameter("http.tcp.nodelay", true);
   //			hc.getParams().setParameter("http.connection.stalecheck", false);
   if (proxyHost != null) {
     HostConfiguration hostConfig = new HostConfiguration();
     hostConfig.setProxy(proxyHost, proxyPort);
     hc.setHostConfiguration(hostConfig);
     log.info("Proxy Host set to " + proxyHost + ":" + proxyPort);
     if (proxyUser != null && !proxyUser.trim().equals("")) {
       if (proxyDomain != null) {
         hc.getState()
             .setProxyCredentials(
                 new AuthScope(proxyHost, proxyPort),
                 new NTCredentials(proxyUser, proxyPassword, proxyHost, proxyDomain));
       } else {
         hc.getState()
             .setProxyCredentials(
                 new AuthScope(proxyHost, proxyPort),
                 new UsernamePasswordCredentials(proxyUser, proxyPassword));
       }
     }
   }
 }
Example #2
0
  @PostConstruct
  public void afterPropertiesSet() throws Exception {
    Protocol.registerProtocol("http", new Protocol("http", new OSProtocolSocketFactory(), 80));

    httpClient = new HttpClient();
    httpClient.setTimeout(30000);
    HostConfiguration conf = new HostConfiguration();
    conf.setHost(new URI("http://weibo.com"));
    httpClient.setHostConfiguration(conf);

    httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
    httpClient.getParams().setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);
    httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    //		 if(null == System.getProperty("env"))
    //			 if(true)
    //		 return;
    //		System.out.println(System.getProperty("env"));
    logger.info(System.getProperty("env"));
    intervalCheck();
    // ck =
    // "__utma=15428400.1695645915.1351319378.1351319378.1351319378.1;
    // SINAGLOBAL=1743311276659.3694.1359040897731; ssoln=18260402168%40139.com; myuid=2350795254;
    // [email protected]; wvr=5; SinaRot_wb_r_topic=84; UV5PAGE=usr511_196;
    // SUE=es%3D6f93acac95733bd5c77d2752300965da%26ev%3Dv1%26es2%3D1be20b8b8e69fb068e7a0ba6fc8ceae4%26rs0%3Dm9CUmlvoVqKlPMc7pNcK6xtfel75%252FhJveinSyZsCdQb1ruTbsb9Csi6Qn3BdmBVdshCUdX7K%252BMwYsznrL51YNDAYNBMcuMbDFtT7g4DO7rFh9C3OolCF%252FpY5eaUwHJj8X80HLgfZzpYq09wgOtACYCKIZu%252FlcRdvO0cFdL3TAQQ%253D%26rv%3D0; SUP=cv%3D1%26bt%3D1384174580%26et%3D1384260980%26d%3Dc909%26i%3Da6d4%26us%3D1%26vf%3D0%26vt%3D0%26ac%3D2%26st%3D0%26uid%3D3807150373%26name%3Djobpassion%2540gmail.com%26nick%3D%25E6%25B5%25B7%25E6%25B7%2598%25E8%25B5%2584%25E8%25AE%25AF2013%26fmp%3D%26lcp%3D2013-10-08%252021%253A20%253A24; SUS=SID-3807150373-1384174580-XD-vc5by-adf6a9ecb6d31faa79c1aeb9e72cc849; ALF=1386766580; SSOLoginState=1384174580; UUG=usrmdins41456; _s_tentry=login.sina.com.cn; UOR=www.juyouqu.com,widget.weibo.com,login.sina.com.cn; Apache=1184292640537.0234.1384174581147; ULV=1384174581208:100:8:2:1184292640537.0234.1384174581147:1384080610846; UV5=usrmdins312_148";
    // ck = null;
    // URL url = new URL("http://www.baidu.com");
    // URLConnection connection = url.openConnection();
    // connection.connect();
    // System.out.println(IOUtils.readLines(connection.getInputStream()));
  }
  private HttpClient getClient() {
    HttpClient httpClient = new HttpClient();
    httpClient.getParams().setParameter("http.protocol.content-charset", "gbk");
    httpClient.getParams().setSoTimeout(timeout);

    if (useSSL) {
      if (sslPort > 0) {
        Protocol myhttps =
            new Protocol(
                "https", (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(), sslPort);
        Protocol.registerProtocol("https", myhttps);
      }
    }

    if (useProxy) {
      HostConfiguration hc = new HostConfiguration();
      hc.setProxy(proxyUrl, proxyPort);
      httpClient.setHostConfiguration(hc);
      if (proxyUser != null) {
        httpClient
            .getState()
            .setProxyCredentials(
                AuthScope.ANY, new UsernamePasswordCredentials(proxyUser, proxyPassword));
      }
    }

    return httpClient;
  }
  private HostConfiguration getHostConfiguration(HttpClient client, Map<String, Object> props) {
    Object proxy = props.get(ApacheHttpClientConfig.PROPERTY_PROXY_URI);
    if (proxy != null) {
      URI proxyUri = getProxyUri(proxy);

      String proxyHost = proxyUri.getHost();
      if (proxyHost == null) {
        proxyHost = "localhost";
      }

      int proxyPort = proxyUri.getPort();
      if (proxyPort == -1) {
        proxyPort = 8080;
      }

      HostConfiguration hostConfig = new HostConfiguration(client.getHostConfiguration());
      String setHost = hostConfig.getProxyHost();
      int setPort = hostConfig.getProxyPort();

      if ((setHost == null)
          || (!setHost.equals(proxyHost))
          || (setPort == -1)
          || (setPort != proxyPort)) {
        hostConfig.setProxyHost(new ProxyHost(proxyHost, proxyPort));
      }
      return hostConfig;
    } else {
      return null;
    }
  }
 public static String executePost(final String host, final String path, final List<Part> parts)
     throws Exception {
   final HttpClient client = new HttpClient();
   client.getParams().setParameter("http.protocol.single-cookie-header", true);
   client
       .getParams()
       .setParameter(
           "http.useragent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)");
   final HttpState httpState = new HttpState();
   final HostConfiguration hostConfiguration = new HostConfiguration();
   // add the proxy
   HttpProxy.addProxy(hostConfiguration);
   hostConfiguration.setHost(host);
   final MultipartRequestEntity entity =
       new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), new HttpMethodParams());
   final PostMethod post = new PostMethod(getHostUrlPrefix(host) + path);
   post.setRequestEntity(entity);
   try {
     final int status = client.executeMethod(hostConfiguration, post, httpState);
     if (status != 200) {
       throw new Exception(
           "Post command to " + host + " failed, the server returned status: " + status);
     }
     return post.getResponseBodyAsString();
   } finally {
     post.releaseConnection();
   }
 }
 /**
  * @param sHostname, the host name
  * @param sProxyURL The proxy port, null if you don't use a proxy
  * @param iProxyPort, proxy port if you use one of -1 if not
  * @return An host configuration
  */
 private static HostConfiguration getHostConfiguration(
     String sHostname, String sProxyURL, int iProxyPort) {
   HostConfiguration host = new HostConfiguration();
   host.setHost(sHostname);
   if (sProxyURL != null && iProxyPort > 0) {
     host.setProxy(
         ConfigurationManager.getProperty(CONF_NETWORK_PROXY_HOSTNAME),
         ConfigurationManager.getInt(CONF_NETWORK_PROXY_PORT));
   }
   return host;
 }
 private HttpClient createHttpClient() {
   HttpClient client = new HttpClient();
   client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
   client.getParams().setConnectionManagerTimeout(timeout);
   if (proxy) {
     HostConfiguration hcf = new HostConfiguration();
     hcf.setProxy(this.proxyUrl, this.proxyPort);
     client.setHostConfiguration(hcf);
   }
   return client;
 }
Example #8
0
 protected HttpClient httpClient() {
   MultiThreadedHttpConnectionManager httpConnectionManager =
       new MultiThreadedHttpConnectionManager();
   HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
   connectionManagerParams.setDefaultMaxConnectionsPerHost(100);
   HostConfiguration localhost = new HostConfiguration();
   localhost.setHost("localhost");
   connectionManagerParams.setMaxConnectionsPerHost(localhost, 100);
   connectionManagerParams.setMaxTotalConnections(100);
   httpConnectionManager.setParams(connectionManagerParams);
   return new HttpClient(httpConnectionManager);
 }
Example #9
0
 public TrackerImpl(Peer localPeer, URL announceUrl, SHAHash infoHash) {
   this.announceUrl = announceUrl;
   this.infoHash = infoHash;
   this.localPeer = localPeer;
   try {
     httpClient = new HttpClient();
     HostConfiguration configuration = new HostConfiguration();
     configuration.setHost(new URI(announceUrl.toExternalForm()));
     httpClient.setHostConfiguration(configuration);
   } catch (URIException e) {
     throw new RuntimeException("Invalid URL", e);
   }
 }
Example #10
0
  /**
   * Obtem a timeline publica. Nao necessita de autenticacao, basicamente é um GET na URL
   * http://twitter.com/statuses/public_timeline.xml
   */
  public String getPublicTimeline() throws Exception {
    HttpClient client = new HttpClient();

    // get para obter a timeline publica
    GetMethod get = new GetMethod("/statuses/public_timeline.xml");

    // Criando o host, para onde sera enviada a requisicao
    HostConfiguration host = client.getHostConfiguration();
    host.setHost(new URI("http://twitter.com"));

    // executando o get na url de timeline publica do twitter
    client.executeMethod(host, get);

    // obtendo a resposta
    return get.getResponseBodyAsString();
  }
 public HttpClient getClient(HostConfiguration hostConfiguration) {
   if (isProxyHost(hostConfiguration.getHost())) {
     return _proxyHttpClient;
   } else {
     return _httpClient;
   }
 }
Example #12
0
  public void testLocationConnectSslClientCert() throws Exception {
    if (CommonTestUtil.isCertificateAuthBroken()) {
      return; // skip test
    }

    String url = "https://mylyn.org/secure/";
    AbstractWebLocation location = new WebLocation(url);
    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, null);

    if (!((PollingSslProtocolSocketFactory) hostConfiguration.getProtocol().getSocketFactory())
        .hasKeyManager()) {
      return; // skip test if keystore property is not set
    }

    GetMethod method = new GetMethod(WebUtil.getRequestPath(url));
    int statusCode = client.executeMethod(hostConfiguration, method);
    assertEquals(200, statusCode);
  }
  protected void initHttpClient(HttpClientConfig config) {
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(config.getHost(), config.getPort());

    connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(config.getPollingIntervalTime() * 4000);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(config.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, config.getMaxHostConnections());
    params.setMaxTotalConnections(config.getMaxTotalConnections());
    params.setConnectionTimeout(config.getTimeout());
    params.setSoTimeout(60 * 1000);

    connectionManager.setParams(params);
    httpclient = new HttpClient(connectionManager);
    httpclient.setHostConfiguration(hostConfiguration);
  }
  public void afterPropertiesSet() throws Exception {
    if (securePort != null && protocolSocketFactory != null) {
      Protocol.registerProtocol("https", new Protocol("https", protocolSocketFactory, securePort));
    } else {
      log.warning("secure port not set");
    }

    super.setHost(hostUrl.getHost(), hostUrl.getPort(), hostUrl.getProtocol());
  }
  public void proxifyState(HttpState httpState, HostConfiguration hostConfiguration) {

    Credentials proxyCredentials = _proxyCredentials;

    String host = hostConfiguration.getHost();

    if (isProxyHost(host) && (proxyCredentials != null)) {
      AuthScope scope = new AuthScope(_PROXY_HOST, _PROXY_PORT, null);

      httpState.setProxyCredentials(scope, proxyCredentials);
    }
  }
  @Test
  public void testTestConnection() {

    try {
      // test with the right collection is ok
      int actual = collection.testConnection(fixture.getHttpClient());

      assertEquals(CaldavStatus.SC_OK, actual);
    } catch (CalDAV4JException e) {
      e.printStackTrace();
      assertNull(e);
    }
    HostConfiguration hostConfig = collection.getHostConfiguration();
    hostConfig.setHost("UNEXISTENT");
    try {
      int actual = collection.testConnection(fixture.getHttpClient());
      assertFalse("Hey! We shouldn't be able to connect now", actual == CaldavStatus.SC_OK);
    } catch (CalDAV4JException e) {
      // do nothing, it should except
      assertNotNull("Server shouldn't connect now", e);
    }
  }
  protected void initHttpClient() {
    if (MockServer.isTestMode()) {
      return;
    }
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(
        diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
        diamondConfigure.getPort());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(diamondConfigure.getPollingIntervalTime() * 10);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, diamondConfigure.getMaxHostConnections());
    params.setMaxTotalConnections(diamondConfigure.getMaxTotalConnections());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    params.setSoTimeout(60 * 1000);

    connectionManager.setParams(params);
    httpClient = new HttpClient(connectionManager);
    httpClient.setHostConfiguration(hostConfiguration);
  }
  public HostConfiguration getHostConfiguration(String location) throws IOException {

    if (_log.isDebugEnabled()) {
      _log.debug("Location is " + location);
    }

    HostConfiguration hostConfiguration = new HostConfiguration();

    hostConfiguration.setHost(new URI(location, false));

    if (isProxyHost(hostConfiguration.getHost())) {
      hostConfiguration.setProxy(_PROXY_HOST, _PROXY_PORT);
    }

    HttpConnectionManager httpConnectionManager = _httpClient.getHttpConnectionManager();

    HttpConnectionManagerParams httpConnectionManagerParams = httpConnectionManager.getParams();

    int defaultMaxConnectionsPerHost =
        httpConnectionManagerParams.getMaxConnectionsPerHost(hostConfiguration);

    int maxConnectionsPerHost =
        GetterUtil.getInteger(
            PropsUtil.get(
                HttpImpl.class.getName() + ".max.connections.per.host",
                new Filter(hostConfiguration.getHost())));

    if ((maxConnectionsPerHost > 0) && (maxConnectionsPerHost != defaultMaxConnectionsPerHost)) {

      httpConnectionManagerParams.setMaxConnectionsPerHost(
          hostConfiguration, maxConnectionsPerHost);
    }

    int timeout =
        GetterUtil.getInteger(
            PropsUtil.get(
                HttpImpl.class.getName() + ".timeout", new Filter(hostConfiguration.getHost())));

    if (timeout > 0) {
      HostParams hostParams = hostConfiguration.getParams();

      hostParams.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, timeout);
      hostParams.setIntParameter(HttpConnectionParams.SO_TIMEOUT, timeout);
    }

    return hostConfiguration;
  }
 protected void applyProxy(HttpMethodBase method, String host) {
   HostConfiguration hc = new HostConfiguration();
   hc.setHost(host);
   hc.setProxy(proxyHost, proxyPort);
   method.setHostConfiguration(hc);
 }
Example #20
0
  /**
   * Constructor.
   *
   * @param helper The parent HTTP client helper.
   * @param method The method name.
   * @param requestUri The request URI.
   * @param hasEntity Indicates if the call will have an entity to send to the server.
   * @throws IOException
   */
  public HttpMethodCall(
      HttpClientHelper helper, final String method, String requestUri, boolean hasEntity)
      throws IOException {
    super(helper, method, requestUri);
    this.clientHelper = helper;

    if (requestUri.startsWith("http")) {
      if (method.equalsIgnoreCase(Method.GET.getName())) {
        this.httpMethod = new GetMethod(requestUri);
      } else if (method.equalsIgnoreCase(Method.POST.getName())) {
        this.httpMethod = new PostMethod(requestUri);
      } else if (method.equalsIgnoreCase(Method.PUT.getName())) {
        this.httpMethod = new PutMethod(requestUri);
      } else if (method.equalsIgnoreCase(Method.HEAD.getName())) {
        this.httpMethod = new HeadMethod(requestUri);
      } else if (method.equalsIgnoreCase(Method.DELETE.getName())) {
        this.httpMethod = new DeleteMethod(requestUri);
      } else if (method.equalsIgnoreCase(Method.CONNECT.getName())) {
        final HostConfiguration host = new HostConfiguration();
        host.setHost(new URI(requestUri, false));
        this.httpMethod = new ConnectMethod(host);
      } else if (method.equalsIgnoreCase(Method.OPTIONS.getName())) {
        this.httpMethod = new OptionsMethod(requestUri);
      } else if (method.equalsIgnoreCase(Method.TRACE.getName())) {
        this.httpMethod = new TraceMethod(requestUri);
      } else {
        this.httpMethod =
            new EntityEnclosingMethod(requestUri) {
              @Override
              public String getName() {
                return method;
              }
            };
      }

      this.httpMethod.setFollowRedirects(this.clientHelper.isFollowRedirects());
      this.httpMethod.setDoAuthentication(false);

      if (this.clientHelper.getRetryHandler() != null) {
        try {
          this.httpMethod
              .getParams()
              .setParameter(
                  HttpMethodParams.RETRY_HANDLER,
                  Engine.loadClass(this.clientHelper.getRetryHandler()).newInstance());
        } catch (Exception e) {
          this.clientHelper
              .getLogger()
              .log(
                  Level.WARNING,
                  "An error occurred during the instantiation of the retry handler.",
                  e);
        }
      }

      this.responseHeadersAdded = false;
      setConfidential(
          this.httpMethod.getURI().getScheme().equalsIgnoreCase(Protocol.HTTPS.getSchemeName()));
    } else {
      throw new IllegalArgumentException("Only HTTP or HTTPS resource URIs are allowed here");
    }
  }
Example #21
0
  public void initCookie() {

    // try {
    // System.setProperty("webdriver.chrome.driver", "./chromedriver.exe");
    // driver = new ChromeDriver();
    // driver.get("http://weibo.com/");
    // WebElement e = driver.findElement(By.name("username"));
    // e.sendKeys(account.getUsername());
    // e = driver.findElement(By.name("password"));
    // e.sendKeys(account.getPassword());
    // e = driver.findElement(By
    // .xpath("//span[@node-type='submitStates']"));
    // e.click();
    // Set<Cookie> cookies = driver.manage().getCookies();
    // // httpClient.startSession(new URL("http://weibo.com"));
    // HttpState state = new HttpState();
    // String ck = "";
    // for (Cookie c : cookies) {
    // ck += c.getName() + "=" + c.getValue() + "; ";
    // }
    // account.setCookie(ck);
    // driver.close();
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    try {
      HttpClient httpClient = new HttpClient();
      httpClient.setTimeout(30000);
      HostConfiguration conf = new HostConfiguration();
      conf.setHost(new URI("http://www.weibo.com"));
      httpClient.setHostConfiguration(conf);
      httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
      httpClient.getParams().setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);
      httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
      GetMethod method =
          new GetMethod(
              "http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&client=ssologin.js(v1.4.11)&_=1387711657507");
      httpClient.executeMethod(method);
      String s = method.getResponseBodyAsString();
      int idx;
      idx = s.indexOf("servertime\":") + "servertime\":".length();
      String servertime = s.substring(idx, s.indexOf(",", idx));
      idx = s.indexOf("nonce\":\"") + "nonce\":\"".length();
      String nonce = s.substring(idx, s.indexOf("\"", idx));
      idx = s.indexOf("pubkey\":\"") + "pubkey\":\"".length();
      String pubkey = s.substring(idx, s.indexOf("\"", idx));
      idx = s.indexOf("rsakv\":\"") + "rsakv\":\"".length();
      String rsakv = s.substring(idx, s.indexOf("\"", idx));
      PostMethod post =
          new PostMethod("http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.11)");

      final WebClient webClient = new WebClient();
      HtmlPage page = webClient.getPage("http://www.baidu.com/");
      InputStream is = Testweibo.class.getClassLoader().getResourceAsStream("weibo.js");
      String script = IOUtils.toString(is);
      is.close();
      ScriptResult sr = page.executeJavaScript(script);
      script =
          "sinaSSOController.rsaPubkey = '"
              + pubkey
              + "';sinaSSOController.servertime = '"
              + servertime
              + "';sinaSSOController.nonce = '"
              + nonce
              + "';sinaSSOController.rsakv = '"
              + rsakv
              + "';sinaSSOController.from = 'weibo';sinaSSOController.useTicket = 1;";
      sr = page.executeJavaScript(script);
      script =
          "sinaSSOController.login('"
              + account.getUsername()
              + "','"
              + account.getPassword()
              + "',7);";
      sr = page.executeJavaScript(script);
      NativeObject no = (NativeObject) sr.getJavaScriptResult();

      for (Object o : no.getAllIds()) {
        // System.out.println(o + "=>" + no.get(o));
        post.setParameter(o + "", no.get(o) + "");
      }
      webClient.closeAllWindows();
      post.setParameter("gateway", "1");
      post.setParameter("savestate", "7");
      post.setParameter("useticket", "1");
      post.setParameter(
          "pagerefer",
          "http://login.sina.com.cn/sso/logout.php?entry=miniblog&r=http%3A%2F%2Fweibo.com%2Flogout.php%3Fbackurl%3D%252F");
      post.setParameter("vsnf", "1");
      post.setParameter("encoding", "UTF-8");
      post.setParameter("prelt", "140");
      post.setParameter(
          "url",
          "http://www.weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack");
      post.setParameter("returntype", "META");
      httpClient.executeMethod(post);
      s = new String(post.getResponseBody(), "UTF-8");
      idx = s.indexOf("location.replace('") + "location.replace('".length();
      String url = s.substring(idx, s.indexOf("'", idx));
      //			System.out.println(url);
      logger.debug(url);
      method = new GetMethod(url);
      httpClient.executeMethod(method);
      ;
      //			System.out.println(method.getResponseBodyAsString());
      logger.debug(method.getResponseBodyAsString());
      //			 method = new GetMethod("http://weibo.com/");
      //			 httpClient.executeMethod(method);
      //			 System.out.println(method.getURI());
      //			 System.out.println(new String(method.getResponseBody(),"UTF-8"));

      //			String ck = "";
      //			for (Cookie c : httpClient.getState().getCookies()) {
      ////				if(c.getDomain().equals(".weibo.com")||c.getDomain().equals("weibo.com"))
      //				ck += c.getName() + "=" + c.getValue() + "; ";
      //			}
      //			account.setCookie(ck);
      clientMap.put(account.getUsername(), httpClient);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Example #22
0
  /**
   * @param since last modified time to use
   * @param req
   * @param url if null, ignored
   * @param redirCount number of redirs we've done
   */
  public static HttpData getDataOnce(
      HttpServletRequest req,
      HttpServletResponse res,
      long since,
      String surl,
      int redirCount,
      int timeout)
      throws IOException, HttpException, DataSourceException, MalformedURLException {

    HttpMethodBase request = null;
    HostConfiguration hcfg = new HostConfiguration();

    /*
      [todo hqm 2006-02-01] Anyone know why this code was here? It is setting
      the mime type to something which just confuses the DHTML parser.

      if (res != null) {
        res.setContentType("application/x-www-form-urlencoded;charset=UTF-8");
        }
    */

    try {

      // TODO: [2002-01-09 bloch] cope with cache-control
      // response headers (no-store, no-cache, must-revalidate,
      // proxy-revalidate).

      if (surl == null) {
        surl = getURL(req);
      }
      if (surl == null || surl.equals("")) {
        throw new MalformedURLException(
            /* (non-Javadoc)
             * @i18n.test
             * @org-mes="url is empty or null"
             */
            org.openlaszlo.i18n.LaszloMessages.getMessage(
                HTTPDataSource.class.getName(), "051018-312"));
      }

      String reqType = "";
      String headers = "";

      if (req != null) {
        reqType = req.getParameter("reqtype");
        headers = req.getParameter("headers");
      }

      boolean isPost = false;
      mLogger.debug("reqtype = " + reqType);

      if (reqType != null && reqType.equals("POST")) {
        request = new LZPostMethod();
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        isPost = true;
        mLogger.debug("setting POST req method");
      } else if (reqType != null && reqType.equals("PUT")) {
        request = new LZPutMethod();
        // todo [hqm 2007] treat PUT like POST?
        isPost = true;
        mLogger.debug("setting PUT req method");
      } else if (reqType != null && reqType.equals("DELETE")) {
        request = new LZDeleteMethod();
        mLogger.debug("setting DELETE req method");
      } else {
        mLogger.debug("setting GET (default) req method");
        request = new LZGetMethod();
      }

      request.setHttp11(mUseHttp11);

      // Proxy the request headers
      if (req != null) {
        LZHttpUtils.proxyRequestHeaders(req, request);
      }

      // Set headers from query string
      if (headers != null && headers.length() > 0) {
        StringTokenizer st = new StringTokenizer(headers, "\n");
        while (st.hasMoreTokens()) {
          String h = st.nextToken();
          int i = h.indexOf(":");
          if (i > -1) {
            String n = h.substring(0, i);
            String v = h.substring(i + 2, h.length());
            request.setRequestHeader(n, v);
            mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="setting header " + p[0] + "=" + p[1]
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(
                    HTTPDataSource.class.getName(), "051018-359", new Object[] {n, v}));
          }
        }
      }

      mLogger.debug("Parsing url");
      URI uri = LZHttpUtils.newURI(surl);
      try {
        hcfg.setHost(uri);
      } catch (Exception e) {
        throw new MalformedURLException(
            /* (non-Javadoc)
             * @i18n.test
             * @org-mes="can't form uri from " + p[0]
             */
            org.openlaszlo.i18n.LaszloMessages.getMessage(
                HTTPDataSource.class.getName(), "051018-376", new Object[] {surl}));
      }

      // This gets us the url-encoded (escaped) path and query string
      String path = uri.getEscapedPath();
      String query = uri.getEscapedQuery();
      mLogger.debug(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="encoded path:  " + p[0]
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(), "051018-389", new Object[] {path}));
      mLogger.debug(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="encoded query: " + p[0]
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(), "051018-397", new Object[] {query}));

      // This call takes a decoded (unescaped) path
      request.setPath(path);

      boolean hasQuery = (query != null && query.length() > 0);

      String rawcontent = null;
      // Newer rawpost protocol puts lzpostbody as a separate
      // top level query arg in the request.
      rawcontent = req.getParameter("lzpostbody");

      if (isPost) {
        // Older rawpost protocol put the "lzpostbody" arg
        // embedded in the "url" args's query args
        if (rawcontent == null && hasQuery) {
          rawcontent = findQueryArg("lzpostbody", query);
        }
        if (rawcontent != null) {
          // Get the unescaped query string
          ((EntityEnclosingMethod) request).setRequestBody(rawcontent);
        } else if (hasQuery) {
          StringTokenizer st = new StringTokenizer(query, "&");
          while (st.hasMoreTokens()) {
            String it = st.nextToken();
            int i = it.indexOf("=");
            if (i > 0) {
              String n = it.substring(0, i);
              String v = it.substring(i + 1, it.length());
              // POST encodes values during request
              ((PostMethod) request).addParameter(n, URLDecoder.decode(v, "UTF-8"));
            } else {
              mLogger.warn(
                  /* (non-Javadoc)
                   * @i18n.test
                   * @org-mes="ignoring bad token (missing '=' char) in query string: " + p[0]
                   */
                  org.openlaszlo.i18n.LaszloMessages.getMessage(
                      HTTPDataSource.class.getName(), "051018-429", new Object[] {it}));
            }
          }
        }
      } else {
        // This call takes an encoded (escaped) query string
        request.setQueryString(query);
      }

      // Put in the If-Modified-Since headers
      if (since != -1) {
        String lms = LZHttpUtils.getDateString(since);
        request.setRequestHeader(LZHttpUtils.IF_MODIFIED_SINCE, lms);
        mLogger.debug(
            /* (non-Javadoc)
             * @i18n.test
             * @org-mes="proxying lms: " + p[0]
             */
            org.openlaszlo.i18n.LaszloMessages.getMessage(
                HTTPDataSource.class.getName(), "051018-450", new Object[] {lms}));
      }

      mLogger.debug(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="setting up http client"
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(), "051018-460"));
      HttpClient htc = null;
      if (mConnectionMgr != null) {
        htc = new HttpClient(mConnectionMgr);
      } else {
        htc = new HttpClient();
      }

      htc.setHostConfiguration(hcfg);

      // This is the data timeout
      mLogger.debug(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="timeout set to " + p[0]
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(), "051018-478", new Object[] {new Integer(timeout)}));
      htc.setTimeout(timeout);

      // Set connection timeout the same
      htc.setConnectionTimeout(mConnectionTimeout);

      // Set timeout for getting a connection
      htc.setHttpConnectionFactoryTimeout(mConnectionPoolTimeout);

      // TODO: [2003-03-05 bloch] this should be more configurable (per app?)
      if (!isPost) {
        request.setFollowRedirects(mFollowRedirects > 0);
      }

      long t1 = System.currentTimeMillis();
      mLogger.debug("starting remote request");
      int rc = htc.executeMethod(hcfg, request);
      String status = HttpStatus.getStatusText(rc);
      if (status == null) {
        status = "" + rc;
      }
      mLogger.debug(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="remote response status: " + p[0]
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(), "051018-504", new Object[] {status}));

      HttpData data = null;
      if (isRedirect(rc) && mFollowRedirects > redirCount) {
        String loc = request.getResponseHeader("Location").toString();
        String hostURI = loc.substring(loc.indexOf(": ") + 2, loc.length());
        mLogger.info(
            /* (non-Javadoc)
             * @i18n.test
             * @org-mes="Following URL from redirect: " + p[0]
             */
            org.openlaszlo.i18n.LaszloMessages.getMessage(
                HTTPDataSource.class.getName(), "051018-517", new Object[] {hostURI}));
        long t2 = System.currentTimeMillis();
        if (timeout > 0) {
          timeout -= (t2 - t1);
          if (timeout < 0) {
            throw new InterruptedIOException(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes=p[0] + " timed out after redirecting to " + p[1]
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(
                    HTTPDataSource.class.getName(), "051018-529", new Object[] {surl, loc}));
          }
        }

        data = getDataOnce(req, res, since, hostURI, redirCount++, timeout);
      } else {
        data = new HttpData(request, rc);
      }

      if (req != null && res != null) {
        // proxy response headers
        LZHttpUtils.proxyResponseHeaders(request, res, req.isSecure());
      }

      return data;

    } catch (ConnectTimeoutException ce) {
      // Transduce to an InterrupedIOException, since lps takes these to be timeouts.
      if (request != null) {
        request.releaseConnection();
      }
      throw new InterruptedIOException(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="connecting to " + p[0] + ":" + p[1] + " timed out beyond " + p[2] + " msecs."
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(),
              "051018-557",
              new Object[] {
                hcfg.getHost(), new Integer(hcfg.getPort()), new Integer(mConnectionTimeout)
              }));
    } catch (HttpRecoverableException hre) {
      if (request != null) {
        request.releaseConnection();
      }
      throw hre;
    } catch (HttpException e) {
      if (request != null) {
        request.releaseConnection();
      }
      throw e;
    } catch (IOException ie) {
      if (request != null) {
        request.releaseConnection();
      }
      throw ie;
    } catch (RuntimeException e) {
      if (request != null) {
        request.releaseConnection();
      }
      throw e;
    }
  }
  public Response sendRequest(SubmitContext submitContext, Request request) throws Exception {
    AbstractHttpRequestInterface<?> httpRequest = (AbstractHttpRequestInterface<?>) request;

    HttpClient httpClient = HttpClientSupport.getHttpClient();
    ExtendedHttpMethod httpMethod = createHttpMethod(httpRequest);
    boolean createdState = false;

    HttpState httpState = (HttpState) submitContext.getProperty(SubmitContext.HTTP_STATE_PROPERTY);
    if (httpState == null) {
      httpState = new HttpState();
      submitContext.setProperty(SubmitContext.HTTP_STATE_PROPERTY, httpState);
      createdState = true;
    }

    HostConfiguration hostConfiguration = new HostConfiguration();

    String localAddress = System.getProperty("soapui.bind.address", httpRequest.getBindAddress());
    if (localAddress == null || localAddress.trim().length() == 0)
      localAddress = SoapUI.getSettings().getString(HttpSettings.BIND_ADDRESS, null);

    if (localAddress != null && localAddress.trim().length() > 0) {
      try {
        hostConfiguration.setLocalAddress(InetAddress.getByName(localAddress));
      } catch (Exception e) {
        SoapUI.logError(e);
      }
    }

    submitContext.removeProperty(RESPONSE);
    submitContext.setProperty(HTTP_METHOD, httpMethod);
    submitContext.setProperty(POST_METHOD, httpMethod);
    submitContext.setProperty(HTTP_CLIENT, httpClient);
    submitContext.setProperty(REQUEST_CONTENT, httpRequest.getRequestContent());
    submitContext.setProperty(HOST_CONFIGURATION, hostConfiguration);
    submitContext.setProperty(WSDL_REQUEST, httpRequest);
    submitContext.setProperty(RESPONSE_PROPERTIES, new StringToStringMap());

    for (RequestFilter filter : filters) {
      filter.filterRequest(submitContext, httpRequest);
    }

    try {
      Settings settings = httpRequest.getSettings();

      // custom http headers last so they can be overridden
      StringToStringMap headers = httpRequest.getRequestHeaders();
      for (String header : headers.keySet()) {
        String headerValue = headers.get(header);
        headerValue = PropertyExpander.expandProperties(submitContext, headerValue);
        httpMethod.setRequestHeader(header, headerValue);
      }

      // do request
      WsdlProject project = (WsdlProject) ModelSupport.getModelItemProject(httpRequest);
      WssCrypto crypto = null;
      if (project != null) {
        crypto =
            project
                .getWssContainer()
                .getCryptoByName(
                    PropertyExpander.expandProperties(submitContext, httpRequest.getSslKeystore()));
      }

      if (crypto != null && WssCrypto.STATUS_OK.equals(crypto.getStatus())) {
        hostConfiguration
            .getParams()
            .setParameter(
                SoapUIHostConfiguration.SOAPUI_SSL_CONFIG,
                crypto.getSource() + " " + crypto.getPassword());
      }

      // dump file?
      httpMethod.setDumpFile(
          PathUtils.expandPath(
              httpRequest.getDumpFile(), (AbstractWsdlModelItem<?>) httpRequest, submitContext));

      // include request time?
      if (settings.getBoolean(HttpSettings.INCLUDE_REQUEST_IN_TIME_TAKEN))
        httpMethod.initStartTime();

      // submit!
      httpClient.executeMethod(hostConfiguration, httpMethod, httpState);
      httpMethod.getTimeTaken();
    } catch (Throwable t) {
      httpMethod.setFailed(t);

      if (t instanceof Exception) throw (Exception) t;

      SoapUI.logError(t);
      throw new Exception(t);
    } finally {
      for (int c = filters.size() - 1; c >= 0; c--) {
        filters.get(c).afterRequest(submitContext, httpRequest);
      }

      if (!submitContext.hasProperty(RESPONSE)) {
        createDefaultResponse(submitContext, httpRequest, httpMethod);
      }

      Response response = (Response) submitContext.getProperty(BaseHttpRequestTransport.RESPONSE);
      StringToStringMap responseProperties =
          (StringToStringMap)
              submitContext.getProperty(BaseHttpRequestTransport.RESPONSE_PROPERTIES);

      for (String key : responseProperties.keySet()) {
        response.setProperty(key, responseProperties.get(key));
      }

      if (httpMethod != null) {
        httpMethod.releaseConnection();
      } else log.error("PostMethod is null");

      if (createdState) {
        submitContext.setProperty(SubmitContext.HTTP_STATE_PROPERTY, null);
      }
    }

    return (Response) submitContext.getProperty(BaseHttpRequestTransport.RESPONSE);
  }
  /**
   * Creates a new connection to the server.
   *
   * @param builder The HttpFileSystemConfigBuilder.
   * @param scheme The protocol.
   * @param hostname The hostname.
   * @param port The port number.
   * @param username The username.
   * @param password The password
   * @param fileSystemOptions The file system options.
   * @return a new HttpClient connection.
   * @throws FileSystemException if an error occurs.
   * @since 2.0
   */
  public static HttpClient createConnection(
      HttpFileSystemConfigBuilder builder,
      String scheme,
      String hostname,
      int port,
      String username,
      String password,
      FileSystemOptions fileSystemOptions)
      throws FileSystemException {
    HttpClient client;
    try {
      HttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
      HttpConnectionManagerParams connectionMgrParams = mgr.getParams();

      client = new HttpClient(mgr);

      final HostConfiguration config = new HostConfiguration();
      config.setHost(hostname, port, scheme);

      if (fileSystemOptions != null) {
        String proxyHost = builder.getProxyHost(fileSystemOptions);
        int proxyPort = builder.getProxyPort(fileSystemOptions);

        if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
          config.setProxy(proxyHost, proxyPort);
        }

        UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions);
        if (proxyAuth != null) {
          UserAuthenticationData authData =
              UserAuthenticatorUtils.authenticate(
                  proxyAuth,
                  new UserAuthenticationData.Type[] {
                    UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD
                  });

          if (authData != null) {
            final UsernamePasswordCredentials proxyCreds =
                new UsernamePasswordCredentials(
                    UserAuthenticatorUtils.toString(
                        UserAuthenticatorUtils.getData(
                            authData, UserAuthenticationData.USERNAME, null)),
                    UserAuthenticatorUtils.toString(
                        UserAuthenticatorUtils.getData(
                            authData, UserAuthenticationData.PASSWORD, null)));

            AuthScope scope = new AuthScope(proxyHost, AuthScope.ANY_PORT);
            client.getState().setProxyCredentials(scope, proxyCreds);
          }

          if (builder.isPreemptiveAuth(fileSystemOptions)) {
            HttpClientParams httpClientParams = new HttpClientParams();
            httpClientParams.setAuthenticationPreemptive(true);
            client.setParams(httpClientParams);
          }
        }

        Cookie[] cookies = builder.getCookies(fileSystemOptions);
        if (cookies != null) {
          client.getState().addCookies(cookies);
        }
      }
      /**
       * ConnectionManager set methodsmust be called after the host & port and proxy host & port are
       * set in the HostConfiguration. They are all used as part of the key when
       * HttpConnectionManagerParams tries to locate the host configuration.
       */
      connectionMgrParams.setMaxConnectionsPerHost(
          config, builder.getMaxConnectionsPerHost(fileSystemOptions));
      connectionMgrParams.setMaxTotalConnections(builder.getMaxTotalConnections(fileSystemOptions));

      client.setHostConfiguration(config);

      if (username != null) {
        final UsernamePasswordCredentials creds =
            new UsernamePasswordCredentials(username, password);
        AuthScope scope = new AuthScope(hostname, AuthScope.ANY_PORT);
        client.getState().setCredentials(scope, creds);
      }

      client.executeMethod(new HeadMethod());
    } catch (final Exception exc) {
      throw new FileSystemException(
          "vfs.provider.http/connect.error", new Object[] {hostname}, exc);
    }

    return client;
  }
Example #25
0
 public static void addProxy(final HostConfiguration config) {
   final String host = System.getProperty(HTTP_PROXYHOST);
   final String port = System.getProperty(HTTP_PROXYPORT, "-1");
   if (host != null && host.trim().length() > 0) config.setProxy(host, Integer.valueOf(port));
 }