Attached is a quick and dirty fix for making HttpClient work in applets. I
simply commented out the getProperties() calls and replaced them with
setting the appropriate string to what was earlier in the default value
part of the call. With the patch applied this code works in a JApplet:

  String docbase = this.getDocumentBase().toString();    
  URL url = new URL(docbase);
  String host = url.getHost();
  String path = url.getPath();
  System.out.println(docbase + " = " + host + " + " + path);
  HttpClient c = new HttpClient();
  GetMethod g = new GetMethod(path);
  c.startSession(host,80);
  c.executeMethod(g);
  String response = g.getResponseBodyAsString();
  System.out.println(response);

In Mozilla's Java Console, the address and the contents of the page that 
started the applet are printed. Without the patch, an exception is thrown 
when trying to construct a new HttpClient object. 

This is obviously not a long-term viable fix, but it works for me, so I
guess that it at least shows that it wouldn't be impossible to eventually
build a good solution working for applets.

I'll continue tinkering with it and get back if bother to write a 
better solution. :-) 

  // Joel


On Wed, 4 Dec 2002, Paul Libbrecht wrote:

> I think your best attempt is to remove System.getProperties in the 
> source-code and try to patch things appropriately later (recompiling 
> cleanly and working on errors and follow-up).
> 
> The consideration about using jakarta-commons things for applet is, I 
> believe, important and I wonder wether some kind of flag as to wether a 
> library is or is not applet-compatible would make a lot of sense.
> (there used to be a time when this was in the faq and demos of most 
> projects).
> 
> Paul
diff -Nrc --exclude='*~' --exclude='*.class' 
commons-httpclient-20021202/src/org/apache/commons/httpclient/Authenticator.java 
commons-httpclient-modified/src/org/apache/commons/httpclient/Authenticator.java
*** commons-httpclient-20021202/src/org/apache/commons/httpclient/Authenticator.java   
 2002-12-02 11:15:41.000000000 +0100
--- commons-httpclient-modified/src/org/apache/commons/httpclient/Authenticator.java   
 2002-12-05 15:24:56.000000000 +0100
***************
*** 656,666 ****
  
          // check the preemptive policy
          // TODO: this needs to be a service from some configuration class
!         String preemptiveDefault =
              System.getProperties().getProperty(PREEMPTIVE_PROPERTY,
!                     PREEMPTIVE_DEFAULT);
!         preemptiveDefault = preemptiveDefault.trim().toLowerCase();
  
          if (!(preemptiveDefault.equals("true")
                      || preemptiveDefault.equals("false"))) { // property problem
              log.warn("Configuration property " + PREEMPTIVE_PROPERTY
--- 656,671 ----
  
          // check the preemptive policy
          // TODO: this needs to be a service from some configuration class
!       
!       // JP: getProperties is forbidden when in applets.
!         String preemptiveDefault = PREEMPTIVE_DEFAULT;
! 
!         /* String preemptiveDefault =
              System.getProperties().getProperty(PREEMPTIVE_PROPERTY,
!                     PREEMPTIVE_DEFAULT); */
  
+         preemptiveDefault = preemptiveDefault.trim().toLowerCase(); 
+       
          if (!(preemptiveDefault.equals("true")
                      || preemptiveDefault.equals("false"))) { // property problem
              log.warn("Configuration property " + PREEMPTIVE_PROPERTY
diff -Nrc --exclude='*~' --exclude='*.class' 
commons-httpclient-20021202/src/org/apache/commons/httpclient/HttpMethodBase.java 
commons-httpclient-modified/src/org/apache/commons/httpclient/HttpMethodBase.java
*** commons-httpclient-20021202/src/org/apache/commons/httpclient/HttpMethodBase.java  
 2002-12-02 11:15:41.000000000 +0100
--- commons-httpclient-modified/src/org/apache/commons/httpclient/HttpMethodBase.java  
 2002-12-05 14:54:51.000000000 +0100
***************
*** 174,184 ****
  
      /** The User-Agent header sent on every request. */
      protected static final Header USER_AGENT;
! 
      static {
!         String agent = System.getProperties()
!                              .getProperty("httpclient.useragent", 
!                                           "Jakarta Commons-HttpClient/2.0M1");
          USER_AGENT = new Header("User-Agent", agent);
      }
  
--- 174,184 ----
  
      /** The User-Agent header sent on every request. */
      protected static final Header USER_AGENT;
!     
      static {
!         // !!! getProperties forbidden in Applets !!!
!       // String agent = System.getProperties().getProperty("httpclient.useragent", 
"Jakarta Commons-HttpClient/2.0M1");       
!         String agent = "Jakarta Commons-HttpClient/2.0M1";
          USER_AGENT = new Header("User-Agent", agent);
      }
  
--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to