Package org.apache.commons.httpclient

Examples of org.apache.commons.httpclient.HttpClient


    // wait max 10 seconds to establish connection
    params.setConnectionTimeout(10000);
    // a read() call on the InputStream associated with this Socket
    // will block for only this amount
    params.setSoTimeout(10000);
    HttpClient c = new HttpClient(connectionManager);

    // use basic authentication if available
    if (user != null && user.length() > 0) {
      AuthScope authScope = new AuthScope(null, -1, null);
      Credentials credentials = new UsernamePasswordCredentials(user, password);
      c.getState().setCredentials(authScope, credentials);
    }
    return c;
  }
View Full Code Here


   * @param user can be NULL
   * @param password can be NULL
   * @return HttpClient
   */
  public static HttpClient getHttpClientInstance(String host, int port, String protocol, String user, String password) {
    HttpClient c = getHttpClientInstance(user, password);
    c.getHostConfiguration().setHost(host, port, protocol);
    return c;
  }
View Full Code Here

    if (firstChar.equals(firstChar.toUpperCase())) return "n";
    return "v";
  }

  private InputStream retreiveXMLReply(String partOfSpeech, String word) {
    HttpClient client = HttpClientFactory.getHttpClientInstance();
    HttpMethod method = new GetMethod(MORPHOLOGICAL_SERVICE_ADRESS);
    NameValuePair posValues = new NameValuePair(PART_OF_SPEECH_PARAM, partOfSpeech);
    NameValuePair wordValues = new NameValuePair(GLOSS_TERM_PARAM, word);
    if (log.isDebug()) {
      String url = MORPHOLOGICAL_SERVICE_ADRESS + "?" + PART_OF_SPEECH_PARAM + "=" + partOfSpeech + "&" + GLOSS_TERM_PARAM + "=" + word;
      log.debug("Send GET request to morph-service with URL: " + url);
    }
    method.setQueryString(new NameValuePair[] { posValues, wordValues });
    try {
      client.executeMethod(method);
      int status = method.getStatusCode();
      if (status == HttpStatus.SC_NOT_MODIFIED || status == HttpStatus.SC_OK) {
        if (log.isDebug()) {
          log.debug("got a valid reply!");
        }
View Full Code Here

    return url;
  }

  public InputStream getStream(final String baseUrl, final WmsAuthentication authentication) throws IOException {
    // Create a HTTP client object, which will initiate the connection:
    HttpClient client = new HttpClient();
    client.setConnectionTimeout(TIMEOUT);

    String url = addCredentialsToUrl(baseUrl, authentication);

    if (null != authentication && WmsAuthenticationMethod.BASIC.equals(authentication.getAuthenticationMethod())) {
      // Preemptive: In this mode HttpClient will send the basic authentication response even before the server
      // gives an unauthorized response in certain situations, thus reducing the overhead of making the
      // connection.
      client.getState().setAuthenticationPreemptive(true);

      // Set up the WMS credentials:
      Credentials credentials = new UsernamePasswordCredentials(authentication.getUser(),
          authentication.getPassword());
      client.getState().setCredentials(authentication.getRealm(), parseDomain(url), credentials);
    }

    // Create the GET method with the correct URL:
    GetMethod get = new GetMethod(url);
    get.setDoAuthentication(true);

    // Execute the GET:
    client.executeMethod(get);

    return new WmsHttpServiceStream(get);
  }
View Full Code Here

  public RasterLayerComponentImpl() {
    getConstraint().setAlignmentX(LayoutConstraint.JUSTIFIED);
    getConstraint().setAlignmentY(LayoutConstraint.JUSTIFIED);
    MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    manager.setMaxConnectionsPerHost(10);
    httpClient = new HttpClient(manager);
  }
View Full Code Here

    */
   public void testHeaderBasedAuthentication() throws Exception
   {
      String location = baseURLNoAuth + "header-auth/index.jsp";
      int responseCode = 0;
      HttpClient httpConn = new HttpClient();
      GetMethod indexGet = null;
      try
      {
         indexGet = new GetMethod(location);
         indexGet.setFollowRedirects(false);
         responseCode = httpConn.executeMethod(indexGet);
         assertEquals(HttpURLConnection.HTTP_FORBIDDEN, responseCode);
      }
      finally
      {
         indexGet.releaseConnection();
      }
      indexGet = null;
      try
      {
         indexGet = new GetMethod(location);
         indexGet.setFollowRedirects(false);
         // Add the request headers
         indexGet.addRequestHeader("JBOSS_TEST_USER_NAME", "jduke");
         indexGet.addRequestHeader("JBOSS_TEST_CREDENTIAL", "theduke");
         responseCode = httpConn.executeMethod(indexGet);
         assertEquals(HttpURLConnection.HTTP_OK, responseCode);
      }
      finally
      {
         indexGet.releaseConnection();
View Full Code Here

   {
      super(name);
//      this.testAppBaseURL = "http://" + super.getServerHost() + ":" + Integer.getInteger("web.port", 8080)
//            + "/generic-header-auth/";
      this.securedServletPath = "restricted/SecuredServlet";
      this.httpClient = new HttpClient();
   }
View Full Code Here

  
   public void testWeb() throws Exception
   {
      String baseURLNoAuth = "http://" + getServerHostForURL() +
                     ":" + Integer.getInteger("web.port", 8080) + "/";
      HttpClient httpConn = new HttpClient();
      GetMethod indexGet = new GetMethod(baseURLNoAuth + "sdtolerate/");
      int responseCode = httpConn.executeMethod(indexGet);
      String body = indexGet.getResponseBodyAsString();
      assertTrue("Get OK(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_OK);
      assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0);
      HttpState state = httpConn.getState();
      Cookie[] cookies = state.getCookies();
      String sessionID = null;
      for (int c = 0; c < cookies.length; c++)
      {
         Cookie k = cookies[c];
         if (k.getName().equalsIgnoreCase("JSESSIONID"))
            sessionID = k.getValue();
      }
      getLog().debug("Saw JSESSIONID=" + sessionID);
      // Submit the login form
      PostMethod formPost = new PostMethod(baseURLNoAuth + "sdtolerate/j_security_check");
      formPost.addRequestHeader("Referer", baseURLNoAuth + "sdtolerate/login.jsp");
      formPost.addParameter("j_username", this.username);
      formPost.addParameter("j_password", new String(password));
      responseCode = httpConn.executeMethod(formPost);
      String loginResult = formPost.getResponseBodyAsString();
      if( loginResult.indexOf("Encountered a login error") > 0 )
         fail("Login Failed");

      String response = formPost.getStatusText();
      log.debug("responseCode="+responseCode+", response="+response);
      assertTrue("Saw HTTP_MOVED_TEMP", responseCode == HttpURLConnection.HTTP_MOVED_TEMP);

      //  Follow the redirect to the index.jsp
      Header location = formPost.getResponseHeader("Location");
      String indexURI = location.getValue();
      GetMethod war1Index = new GetMethod(indexURI);
      responseCode = httpConn.executeMethod(war1Index);
      response = war1Index.getStatusText();
      log.debug("responseCode="+responseCode+", response="+response);
      assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
   }
View Full Code Here

   }
   public static HttpMethodBase accessURL(URL url, String realm,
      int expectedHttpCode, Header[] hdrs, int type)
      throws Exception
   {
      HttpClient httpConn = new HttpClient();
      HttpMethodBase request = createMethod(url, type);
      int hdrCount = hdrs != null ? hdrs.length : 0;
      for(int n = 0; n < hdrCount; n ++)
         request.addRequestHeader(hdrs[n]);
      try
      {
         log.debug("Connecting to: "+url);
         String userInfo = url.getUserInfo();
         if( userInfo != null )
         {
            UsernamePasswordCredentials auth = new UsernamePasswordCredentials(userInfo);
            httpConn.getState().setCredentials(realm, url.getHost(), auth);
         }
         log.debug("RequestURI: "+request.getURI());
         int responseCode = httpConn.executeMethod(request);
         String response = request.getStatusText();
         log.debug("responseCode="+responseCode+", response="+response);
         String content = request.getResponseBodyAsString();
         log.debug(content);
         // Validate that we are seeing the requested response code
View Full Code Here

   }

   @Test
   public void testNewInstanceCreatedForEveryRequest()
   {
      HttpClient client = new HttpClient();
      GetMethod get1 = new GetMethod(BASE_URI + getTestPrefix() + "toString");
      get1.addRequestHeader("Accept", "text/plain");
      GetMethod get2 = new GetMethod(BASE_URI + getTestPrefix() + "toString");
      get2.addRequestHeader("Accept", "text/plain");
      try
      {
         int status1 = client.executeMethod(get1);
         assertEquals(status1, 200);
         String response1 = get1.getResponseBodyAsString();
         get1.releaseConnection();
         int status2 = client.executeMethod(get2);
         assertEquals(status2, 200);
         String response2 = get2.getResponseBodyAsString();
         get2.releaseConnection();
         assertFalse(response1.equals(response2));
      }
View Full Code Here

TOP

Related Classes of org.apache.commons.httpclient.HttpClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.