You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Joel Palmius <jo...@mh.se> on 2002/12/05 15:50:21 UTC

Re: [HttpClient] Using httpclient in applets (includes dirty fix)

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

Re: [HttpClient] Using httpclient in applets (includes dirty fix)

Posted by Chris Smith <ch...@mindiq.com>.
> This raises a funny question: is there a way for a library
> being called from an applet to test wether it's in an applet ?
> I would do it precisely with System.getProperties() catching
> the exception but there should be a cleaner way !

The problem you want to solve is not determining whether the code is running
in an applet, but whether you can get the system properties that you want.
The best way to solve that problem is very similar to what you said: try to
do so, and catch the SecurityException if it doesn't work.  This isn't just
about applets... it's about ANY code that's running under a security
manager, with a wide range of possible security policies.  That could
include applets, any of a bunch of other framework based environments, and
even some servlet containers.

The one change I'd suggest would be to make sure we always call getProperty
for the specific property we're interested in, rather than
System.getProperties... some security policies may allow access to some
properties, but not all.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Re: [HttpClient] Using httpclient in applets (includes dirty fix)

Posted by Paul Libbrecht <pa...@activemath.org>.
On Jeudi, décembre 5, 2002, at 03:50 , Joel Palmius wrote:

> 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();


This raises a funny question: is there a way for a library being called 
from an applet to test wether it's in an applet ? I would do it 
precisely with System.getProperties() catching the exception but there 
should be a cleaner way !

Paul