You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Pankaj Arora <PA...@castiron.com> on 2007/05/28 06:20:48 UTC

HttpClient logging

Hi,
I am trying to programmatically tur on the log on wire for Http. I tried
using system properties as described in http logging guide.
http://jakarta.apache.org/commons/httpclient/logging.html.
But it seems as it doesn't take effect.
Below are 2 piece of code I wrote with/without logging on and both give
same output.


Code 1: Without logging:

________________________________________________________________________
________________________________________________
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.auth.*;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;

//import org.apache.commons.*;
import java.io.*;

public class HttpClientNew_log {
  
  private static String url = "http://tern:80/VAutomation/basic";

  public static void main(String[] args) {
    // Create an instance of HttpClient.


    HttpClient client1 = new HttpClient();
    HttpMethod _method1 = new GetMethod(url);
    HttpState _httpState1 = new HttpState();
    HostConfiguration hostConfig1 = new HostConfiguration();
    hostConfig1.setHost("tern",80);
    try {
      // Execute the method.
      int statusCode =
client1.executeMethod(hostConfig1,_method1,_httpState1);

      System.out.println("Status code :" + statusCode);
      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + _method1.getStatusLine()
+ "StatusCode:" + statusCode);
      }

      // Read the response body.
      byte[] responseBody = _method1.getResponseBody();

      // Deal with the response.
      // Use caution: ensure correct character encoding and is not
binary data
      //      System.out.println("Response Body is " + new
String(responseBody));

      Header[] responseHeaders = _method1.getResponseHeaders();
      //      Header header;
 
System.out.println("----------------------------------------------------
-----------------------------------");
      for( Header header : responseHeaders){
	  System.out.println("Headers is " + header.getName() + "and the
value is :" + header.getValue());
      }



    } catch (HttpException e) {
      System.err.println("Fatal protocol violation: " + e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    } finally {
      // Release the connection.
      _method1.releaseConnection();
      //      _method2.releaseConnection();
    }  
  }
} 
________________________________________________________________________
__________________________________________________
Output:
Status code :200
May 27, 2007 9:11:48 PM org.apache.commons.httpclient.HttpMethodBase
getResponseBody
WARNING: Going to buffer response body of large or unknown size. Using
getResponseBodyAsStream instead is recommended.
------------------------------------------------------------------------
---------------
Headers is Dateand the value is :Mon, 28 May 2007 04:38:32 GMT
Headers is Serverand the value is :Apache/2.0.40 (Red Hat Linux)
Headers is Connectionand the value is :close
Headers is Transfer-Encodingand the value is :chunked
Headers is Content-Typeand the value is :text/plain; charset=ISO-8859-1




Code 2 : Trying to switch logging programmatically:

________________________________________________________________________
_________________________________________
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.auth.*;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;

//import org.apache.commons.*;
import java.io.*;

public class HttpClientNew_log {
  
  private static String url = "http://tern:80/VAutomation/basic";

  public static void main(String[] args) {
    // Create an instance of HttpClient.


    HttpClient client1 = new HttpClient();
    HttpMethod _method1 = new GetMethod(url);
    HttpState _httpState1 = new HttpState();
    HostConfiguration hostConfig1 = new HostConfiguration();
    hostConfig1.setHost("tern",80);
System.setProperty("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime",
"true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.
wire", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.
commons.httpclient", "debug");
    try {
      // Execute the method.
      int statusCode =
client1.executeMethod(hostConfig1,_method1,_httpState1);

      System.out.println("Status code :" + statusCode);
      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + _method1.getStatusLine()
+ "StatusCode:" + statusCode);
      }

      // Read the response body.
      byte[] responseBody = _method1.getResponseBody();

      // Deal with the response.
      // Use caution: ensure correct character encoding and is not
binary data
      //      System.out.println("Response Body is " + new
String(responseBody));

      Header[] responseHeaders = _method1.getResponseHeaders();
      //      Header header;
 
System.out.println("----------------------------------------------------
-----------------------------------");
      for( Header header : responseHeaders){
	  System.out.println("Headers is " + header.getName() + "and the
value is :" + header.getValue());
      }



    } catch (HttpException e) {
      System.err.println("Fatal protocol violation: " + e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    } finally {
      // Release the connection.
      _method1.releaseConnection();
      //      _method2.releaseConnection();
    }  
  }
} 
________________________________________________________________________
______________________________________________

Output:

Status code :200
May 27, 2007 9:13:18 PM org.apache.commons.httpclient.HttpMethodBase
getResponseBody
WARNING: Going to buffer response body of large or unknown size. Using
getResponseBodyAsStream instead is recommended.
------------------------------------------------------------------------
---------------
Headers is Dateand the value is :Mon, 28 May 2007 04:40:02 GMT
Headers is Serverand the value is :Apache/2.0.40 (Red Hat Linux)
Headers is Connectionand the value is :close
Headers is Transfer-Encodingand the value is :chunked
Headers is Content-Typeand the value is :text/plain; charset=ISO-8859-1
________________________________________________________________________
____________________________________________


I am expecting some more log on wire etc. for debugging purposes.
Please do tell me what I missed here. I have constraint that I need to
enable the log programmatically only.


Thanks,
Pankaj Arora

---------------------------------------------------------------------
To unsubscribe, e-mail: httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpcomponents-dev-help@jakarta.apache.org


Re: HttpClient logging

Posted by Roland Weber <os...@dubioso.net>.
Hello Pankaj,

Pankaj Arora wrote:
> Thanks. That worked. But I want more control at run time for logging on
> wire by httpclient.
> So what are advanced logging implementation, that can be used.

Please refer to the commons-logging documentation for available
implementations:
http://jakarta.apache.org/commons/logging/

You may also want to check out SLF4J, it is binary compatible
with Jakarta Commons Logging:
http://www.slf4j.org/
It's license is not Apache, but compatible.

Both frameworks will allow you to implement your own logging
backend if none of the defaults suit your needs.

hope that helps,
  Roland


---------------------------------------------------------------------
To unsubscribe, e-mail: httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpcomponents-dev-help@jakarta.apache.org


RE: Kerberos (SPNEGO support)

Posted by Pankaj Arora <pa...@castiron.com>.
That won't work for GSS-API. That's only for JAAS. I have written a
sample code, wich now works for me. I will contribute that soon.

-----Original Message-----
From: sebb [mailto:sebbaz@gmail.com] 
Sent: Saturday, August 18, 2007 4:58 PM
To: httpcomponents-dev@jakarta.apache.org
Subject: Fwd: Kerberos (SPNEGO support)

[Tried to send this earler, but DNS problems caused it to fail]

The Sun documentation:

http://java.sun.com/j2se/1.4.2/docs/guide/security/jgss/tutorials/LoginS
ample.html

includes the words:

"the TextCallbackHandler class (from the
com.sun.security.auth.callback package) as the class to be used when
communicating with the user. This class can prompt the user for a user
name and password. "

HTH
On 14/08/07, Pankaj Arora <pa...@castiron.com> wrote:
> Thanks Roland.
> That code sample looks good.
> But I am facing a problem when I execute it. It prompts me for
username
> /password and provided correct credentials it does authenticate.
>
> Is it not possible programmatically to set username/password?
> Something similar to a callback Handler in LoginContext in the JAAS
> tutorial
> referred in the bug.
> https://issues.apache.org/jira/browse/HTTPCLIENT-523
>
>        LoginContext con = null;
>
>        try {
>            // Create a LoginContext with a callback handler
>            con = new LoginContext("com.sun.security.jgss.initiate",
> callbackHandler);
> }
>
>
> I don't see any API call in sun.security.jgss.GSSContextImpl to set
any
> callback function which may able me to set username/ password
> programmatically rather than from a prompt.
> My application can not take input from the user.
>
> Thanks,
> Pankaj Arora
>
>
> -----Original Message-----
> From: Roland Weber [mailto:ossfwot@dubioso.net]
> Sent: Friday, July 06, 2007 5:38 AM
> To: HttpComponents Project
> Subject: Re: Kerberos (SPNEGO support)
>
> Hello Pankaj,
>
> I believe it is possible:
> https://issues.apache.org/jira/browse/HTTPCLIENT-523
>
> The Subversion repository has recently moved, the code is now at:
>
http://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/trunk/sr
> c/contrib/org/apache/commons/httpclient/contrib/auth/
>
> hope that helps,
>  Roland
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> httpcomponents-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> httpcomponents-dev-help@jakarta.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
httpcomponents-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
httpcomponents-dev-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail:
httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail:
httpcomponents-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpcomponents-dev-help@jakarta.apache.org


Fwd: Kerberos (SPNEGO support)

Posted by sebb <se...@gmail.com>.
[Tried to send this earler, but DNS problems caused it to fail]

The Sun documentation:

http://java.sun.com/j2se/1.4.2/docs/guide/security/jgss/tutorials/LoginSample.html

includes the words:

"the TextCallbackHandler class (from the
com.sun.security.auth.callback package) as the class to be used when
communicating with the user. This class can prompt the user for a user
name and password. "

HTH
On 14/08/07, Pankaj Arora <pa...@castiron.com> wrote:
> Thanks Roland.
> That code sample looks good.
> But I am facing a problem when I execute it. It prompts me for username
> /password and provided correct credentials it does authenticate.
>
> Is it not possible programmatically to set username/password?
> Something similar to a callback Handler in LoginContext in the JAAS
> tutorial
> referred in the bug.
> https://issues.apache.org/jira/browse/HTTPCLIENT-523
>
>        LoginContext con = null;
>
>        try {
>            // Create a LoginContext with a callback handler
>            con = new LoginContext("com.sun.security.jgss.initiate",
> callbackHandler);
> }
>
>
> I don't see any API call in sun.security.jgss.GSSContextImpl to set any
> callback function which may able me to set username/ password
> programmatically rather than from a prompt.
> My application can not take input from the user.
>
> Thanks,
> Pankaj Arora
>
>
> -----Original Message-----
> From: Roland Weber [mailto:ossfwot@dubioso.net]
> Sent: Friday, July 06, 2007 5:38 AM
> To: HttpComponents Project
> Subject: Re: Kerberos (SPNEGO support)
>
> Hello Pankaj,
>
> I believe it is possible:
> https://issues.apache.org/jira/browse/HTTPCLIENT-523
>
> The Subversion repository has recently moved, the code is now at:
> http://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/trunk/sr
> c/contrib/org/apache/commons/httpclient/contrib/auth/
>
> hope that helps,
>  Roland
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> httpcomponents-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> httpcomponents-dev-help@jakarta.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpcomponents-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpcomponents-dev-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpcomponents-dev-help@jakarta.apache.org


RE: Kerberos (SPNEGO support)

Posted by Pankaj Arora <pa...@castiron.com>.
Thanks Roland.
That code sample looks good.
But I am facing a problem when I execute it. It prompts me for username
/password and provided correct credentials it does authenticate.

Is it not possible programmatically to set username/password?
Something similar to a callback Handler in LoginContext in the JAAS
tutorial 
referred in the bug.
https://issues.apache.org/jira/browse/HTTPCLIENT-523

	LoginContext con = null;

	try {
	    // Create a LoginContext with a callback handler
	    con = new LoginContext("com.sun.security.jgss.initiate",
callbackHandler);
} 
 

I don't see any API call in sun.security.jgss.GSSContextImpl to set any
callback function which may able me to set username/ password
programmatically rather than from a prompt.
My application can not take input from the user.

Thanks,
Pankaj Arora


-----Original Message-----
From: Roland Weber [mailto:ossfwot@dubioso.net] 
Sent: Friday, July 06, 2007 5:38 AM
To: HttpComponents Project
Subject: Re: Kerberos (SPNEGO support)

Hello Pankaj,

I believe it is possible:
https://issues.apache.org/jira/browse/HTTPCLIENT-523

The Subversion repository has recently moved, the code is now at:
http://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/trunk/sr
c/contrib/org/apache/commons/httpclient/contrib/auth/

hope that helps,
  Roland

---------------------------------------------------------------------
To unsubscribe, e-mail:
httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail:
httpcomponents-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpcomponents-dev-help@jakarta.apache.org


Re: Kerberos (SPNEGO support)

Posted by Roland Weber <os...@dubioso.net>.
Hello Pankaj,

I believe it is possible:
https://issues.apache.org/jira/browse/HTTPCLIENT-523

The Subversion repository has recently moved, the code is now at:
http://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/trunk/src/contrib/org/apache/commons/httpclient/contrib/auth/

hope that helps,
  Roland

---------------------------------------------------------------------
To unsubscribe, e-mail: httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpcomponents-dev-help@jakarta.apache.org


Kerberos (SPNEGO support)

Posted by Pankaj Arora <PA...@castiron.com>.
We need the ability to support Kerberos and NTLM Authentication in our
HTTP client so that we can post HTTP requests to Microsoft IIS based
applications.
For this purpose I need to add support for Simple and Protected GSSAPI
Negotiation Mechanism (SPNEGO) with support for Kerberos authentication
mechanisms.
Is it possible with Http Client? Please tell me if I can get some
examples on this.

Thanks,
Pankaj Arora


---------------------------------------------------------------------
To unsubscribe, e-mail: httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpcomponents-dev-help@jakarta.apache.org


Kerberos (SPNEGO support)

Posted by Pankaj Arora <PA...@castiron.com>.
We need the ability to support Kerberos and NTLM Authentication in our
HTTP client so that we can post HTTP requests to Microsoft IIS based
applications.
For this purpose I need to add support for Simple and Protected GSSAPI
Negotiation Mechanism (SPNEGO) with support for Kerberos authentication
mechanisms.
Is it possible with Http Client? Please tell me if I can get some
examples on this.

Thanks,
Pankaj Arora

-----Original Message-----
From: Pankaj Arora 
Sent: Monday, May 28, 2007 3:07 AM
To: 'HttpComponents Project'
Subject: RE: HttpClient logging

Thanks. That worked. But I want more control at run time for logging on
wire by httpclient.
So what are advanced logging implementation, that can be used.

Thanks,
Pankaj Arora 

-----Original Message-----
From: Roland Weber [mailto:ossfwot@dubioso.net] 
Sent: Monday, May 28, 2007 2:02 PM
To: HttpComponents Project
Subject: Re: HttpClient logging

Hello Pankaj,

try setting the system properties _before_ you call into any HttpCLient
classes, that is before even creating the HttpClient object. Once a
logger is created, SimpleLog will not pick up changes to the system
properties. The settings are cached and will remain constant for the
lifetime of the JVM. If you need to adjust log levels at runtime, you'll
have to use a more advanced logging implementation.

hope that helps,
  Roland

---------------------------------------------------------------------
To unsubscribe, e-mail:
httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail:
httpcomponents-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpcomponents-dev-help@jakarta.apache.org


RE: HttpClient logging

Posted by Pankaj Arora <PA...@castiron.com>.
Thanks. That worked. But I want more control at run time for logging on
wire by httpclient.
So what are advanced logging implementation, that can be used.

Thanks,
Pankaj Arora 

-----Original Message-----
From: Roland Weber [mailto:ossfwot@dubioso.net] 
Sent: Monday, May 28, 2007 2:02 PM
To: HttpComponents Project
Subject: Re: HttpClient logging

Hello Pankaj,

try setting the system properties _before_ you call into any HttpCLient
classes, that is before even creating the HttpClient object. Once a
logger is created, SimpleLog will not pick up changes to the system
properties. The settings are cached and will remain constant for the
lifetime of the JVM. If you need to adjust log levels at runtime, you'll
have to use a more advanced logging implementation.

hope that helps,
  Roland

---------------------------------------------------------------------
To unsubscribe, e-mail:
httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail:
httpcomponents-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpcomponents-dev-help@jakarta.apache.org


Re: HttpClient logging

Posted by Roland Weber <os...@dubioso.net>.
Hello Pankaj,

try setting the system properties _before_ you call
into any HttpCLient classes, that is before even
creating the HttpClient object. Once a logger is
created, SimpleLog will not pick up changes to the
system properties. The settings are cached and will
remain constant for the lifetime of the JVM. If you
need to adjust log levels at runtime, you'll have
to use a more advanced logging implementation.

hope that helps,
  Roland

---------------------------------------------------------------------
To unsubscribe, e-mail: httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpcomponents-dev-help@jakarta.apache.org