hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Keith Campbell <keithcampb...@mac.com>
Subject Re: multiple instances of httpclient
Date Fri, 19 Aug 2005 02:36:17 GMT
Yes -- that worked. One last question if somebody could answer:
Because of implementing it this way -- I think I need to create a  
synchronized block
where I clear and set cookies from the HttpClient. But I'm concerned  
that this might
be too big of a performance hit if I have many concurrent users.
Could somebody quickly take a look at my code and let me know if  
there's some way to possibly speed this up or is this the best I can  
hope for?

package com.paperloop.bus;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class DUIHtmlProvider implements HTMLProvider {

     private String xml;
     private static String BASE_LOCKWOOD_POST_URL = "http:// 
xxx.xxx.xxx.xxx:8083/pl_dui/";

     public DUIHtmlProvider(HttpServletRequest request, HttpClient  
client) {

         String duiUrl = request.getParameter("url"), queryString;

         // has this user logged in to DUI yet?
         if (duiUrl == null) {
             // no url within DUI yet -- must be new user
             duiUrl = "index.php";
             // set GET query string to login query string
             queryString = "pcd=NS&uid="+request.getParameter("uid");
         } else {
             // user has a url -- already logged in
             queryString = request.getQueryString();
         }

         // Create a GET method instance.
         GetMethod method = new GetMethod(BASE_LOCKWOOD_POST_URL +  
duiUrl);

         // set the query string passed in from CC
         method.setQueryString(queryString);

         // Provide custom retry handler is necessary
         method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                 new DefaultHttpMethodRetryHandler(3, false));

         // get cookies from this user's session
         Cookie[] cookie = (Cookie[]) request.getSession().getAttribute(
                 "cookie");

         // we don't want users' to execute a GET or POST
         // while somebody else's cookies are set in the HTTP
         // client, so this block is synchronized (Performance
         // hit should be negligible but maybe should be tested??)
         synchronized (client) {

             // get the client HTTP state
             HttpState state = client.getState();

             // clear cookies from the HTTP client
             state.clearCookies();

             // restore this user's cookies to the HTTP client state
             state.addCookies(cookie);

             // add the HttpState back to the client
             client.setState(state);

             // execute the GET method
             try {
                 int statusCode = client.executeMethod(method);

                 if (statusCode != HttpStatus.SC_OK) {
                     System.out.print("Method failed: " +  
method.getStatusLine());
                 } else {
                     // get the response as a String
                     xml = method.getResponseBodyAsString();
                 }

             } catch (HttpException he) {
                 System.out.print("Fatal protocol violation: " +  
he.getMessage());
                 he.printStackTrace();
             } catch (IOException ioe) {
                 System.out.print("Fatal transport error: " +  
ioe.getMessage());
                 ioe.printStackTrace();
             } finally {
                 method.releaseConnection(); // Release the connection.
             }

             // get the Cookies from the HTTP client
             // and set them in the user's Session
             Cookie[] cookieAfter = client.getState().getCookies();
             request.getSession().setAttribute("cookie",cookieAfter);
         }
     }

     public String getHtml() {
         return xml;
     }

}







On Aug 18, 2005, at 7:52 PM, Gustavo Hexsel wrote:

>   I believe you're right.  Things might get tricky if you need the
> credentials present in the HttpState too, but it doesn't appear to  
> be your
> case.
>
>   I'm guessing that, while the user's session is active, it's  
> easier to keep
> the whole HttpState in it, to avoid creating and setting cookies  
> all the
> time.  But I see that HttpState isn't serializable so you might be  
> out of
> luck there.
>
>   []s Gus
>
>
>
> -----Original Message-----
> From: Keith Campbell [mailto:keithcampbell@mac.com]
> Sent: August 18, 2005 5:48 PM
> To: HttpClient User Discussion
> Subject: Re: multiple instances of httpclient
>
>
> Hi again,
>
> I think I may have answered my question but please tell me if I'm on
> the right track or not ....
>
> I  think I can maintain multiple user sessions using one instance of
> HttpClient if I do something like the following:
>
> Maintain one instance of HttpClient for all users.
>
> Support multiple users by getting the HttpState and storing Cookies
> to each user's session.
>
> So ... for each session, first thing to do is get the cookies from
> the session if there are cookies there, clear any existing cookies
> from the HttpState, set the cookies from the session to the
> HttpState, execute the get or post, retrieve the cookies from the
> HttpState and set them again in the user session.
>
> Is this the correct way to go about doing what I want to do?
>
> Keith
>
> On Aug 18, 2005, at 6:59 PM, Keith Campbell wrote:
>
>
>> Hi all,
>>
>> A quick question for the list please. I've been struggling with
>> this for a couple of days now w/o much success.
>>
>> I have a servlet which is used to send requests (using HttpClient)
>> to another web application where:
>>
>> 1) each user must login -- each user logs in using GET by passing a
>> user id as a parameter
>> 2) each user's session must be persistent
>> 3) multiple users will be using the application and each must
>> maintain a unique session
>>
>> To do this, I've been trying to instantiate HttpClient, in my
>> servlet, once per user session. And I'd like to be able to store/
>> retrieve this instance in the user session. But HttpClient is not
>> serializable ....
>>
>> How do I accomplish this functionality?
>>
>> Thanks in advance,
>> Keith Campbell
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: httpclient-user- 
>> unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: httpclient-user-
>> help@jakarta.apache.org
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user- 
> help@jakarta.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user- 
> help@jakarta.apache.org
>
>


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


Mime
View raw message