Skip to Content
author's profile photo Former Member
Former Member

Basic Authentication to OWA

Hi guys, I've a problem, I'm trying to access to OWA, I'm using org.apache.commons.httpclient.HttpClient to get a cookie to authenticate the user to OWA and get the content of the browser in a console mode, when I get the cookie and the HTML content I'm only set the cookie and the HTML content on the object response like this:

public class OWAMail extends HttpServlet {

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

try {
   HttpClient client = new HttpClient();
   ArrayList authPrefs = new ArrayList(1);
   authPrefs.add(AuthPolicy.BASIC);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY,authPrefs);
   client.getState().setCredentials(new    AuthScope("10.210.64.230",80,AuthScope.ANY_REALM), new    UsernamePasswordCredentials("mxm\" + userid , mailpassword ) );
   GetMethod get = new    GetMethod("http://server/exchange");
   get.setDoAuthentication(true);

   int status = client.executeMethod(get);
   respuesta = get.getResponseBodyAsString();
   Cookie[] cookies = client.getState().getCookies();
   response.addCokie(cookies[1]);
   PrintWriter out = response.getWriter();
   out.println(respuesta);
   out.flush();
   out.close();
} catch(Exception e){
   e.printStackTrace();
} finally {
   get.releaseConnection();
}

.....

}

The problem appears when I print the response on the browser because a basic authentication asking for the user credentials appears again, I think the cookie is ignored.....

Any ideas would be greatly appreciated

Regards

Lalo.....

Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • Posted on Oct 25, 2006 at 08:41 AM

    Hi Lalo

    are you sure the correct cookie is set on the response?

    Regards

    Nikolai

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 25, 2006 at 11:22 AM

    Hi Lalo!

    Let me paraphrase what you are trying with this code-snippet:

    1. Use HttpClient to establish a connection to OWA of a given Exchange server.

    2. Get the cookie from the Exchange server's response.

    3. Pass that cookie to the response that is sent back to the client (browser).

    There are two problems that I see with this approach:

    1. The cookie you get out of the HttpClient with client.getState().getCookies(); are typed as org.apache.commons.httpclient.cookie.Cookie. The addCookie method of the HttpServletResponse is expecting an object of type javax.servlet.http.Cookie. In my eyes this should not even compile.

    2. I don't think that it is possible to add a cookie of server A to the response of server B and pretend being the cookie of server B.

    If you want to pass the contents of the Exchange server's cookie to the HTTP-repsonse of your AppServer I would consider to somehow copy the values of the org.apache.commons.httpclient.cookie.Cookie object to a new object of javax.servlet.http.Cookie and then add it to the response-object.

    By the way.. Is this code part of an existing application or are you just playing around with it?

    I hope I could help you out in some way.. 😊

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Thanks Nicolai and thanks robin...

      Well as you said Robin "The addCookie method of the HttpServletResponse is expecting an object of type javax.servlet.http.Cookie. In my eyes this should not even compile", you're right I get the cookie in this way:

         javax.servlet.http.Cookie c = new javax.servlet.http.Cookie
                                  (cookies[1].getName(), cookies[1].getValue());
                          c.setPath(cookies[1].getCookiePath());
                          response.addCookie(c);
      

      But I omitted this in the code-snippet (Sorry).

      I think the problem is as you said trying to pass the cookie from Server A to server B, and I don't know how to solved it. This is the problem...

      Thanks....

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.