Polarion ALM login with Java HttpClient

Description
mberhorst
Posts: 15
Joined: Wed Mar 03, 2010 9:46 am

Polarion ALM login with Java HttpClient

Postby mberhorst » Fri May 14, 2010 11:18 am

Hello everybody,

my aim is, to call a servlet of a custom Polarion plugin from a Java class.
The servlet is only callable after the Polarion authentication.
I try to implement the Polarion login action in Java with the Apache Components HttpClient.
http://hc.apache.org/httpclient-3.x/

My Java code for the Java HttpClient:

Code: Select all

HttpClient client = new HttpClient();     
client.getHostConfiguration().setHost("HOST/polarion/", 80, "http");     
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);   
//GetMethod
GetMethod getMethod = new GetMethod(client.getHostConfiguration().getHost());
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());   
int statusCode;     
try {
   //execute GetMethod to check if the URL is reachable
    statusCode = client.executeMethod(getMethod);
   //if statusCode == 200 URL is reachable
    if (statusCode == 200) {
        System.out.println(getMethod.getStatusLine());             
        //get Cookie information
        HttpState state = client.getState();
        Cookie[] cookies = state.getCookies();
        for (Cookie cookie: cookies) {
           System.out.println("Cookie [" + cookie.getDomain() + " - " + cookie.getName() + " - " + cookie.getValue() + "]");
        }           
        //PostMethod for Polarion login
        PostMethod postMethod = new PostMethod(client.getHostConfiguration().getHost());
        postMethod.setParameter("org.apache.jetspeed.login.username", "user");
        postMethod.setParameter("org.apache.jetspeed.login.password", "pass");
        client.executeMethod(postMethod); 
        System.out.println("\n"+postMethod.getStatusLine()+" "+postMethod.getURI());
      System.out.println(postMethod.getResponseBodyAsString());
    }
} catch (HttpException e) {
    System.out.println("HtttpException: "+e.getMessage());
} catch (IOException e) {
    System.out.println("IOExcekption: "+e.getMessage());
}


When I output the responded body, it shows the code of the login page.
Is there an error in my java code?
Or is there an other way to authenticate to Polarion from a Java class?

Thank you in advance!

regards
Michael

ChernikovaEkaterina
Posts: 15
Joined: Mon Dec 07, 2009 9:08 pm

Re: Polarion ALM login with Java HttpClient

Postby ChernikovaEkaterina » Mon May 17, 2010 3:34 pm

Hi Michael,

For Polarion older than 2010 and 2010 in single repository mode this
should work (it is commented out and ugly, but maybe of some value):

Code: Select all

//            String sessionCookie = "";
//            URL urlConn;
//            urlConn = new URL(url);
//            HttpURLConnection conn = (HttpURLConnection)urlConn.openConnection();
//            conn.setDoInput(true);
//            conn.setDoOutput(true);
//            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//
//            for (Iterator iter = conn.getHeaderFields().entrySet().iterator(); iter.hasNext();) {
//                Entry entry = (Entry) iter.next();
//                if("Set-Cookie".equals(entry.getKey())) {
//                    Collection value = (Collection) entry.getValue();
//                    for (Iterator it = value.iterator(); it.hasNext();) {
//                        String cookie = (String) it.next();
//                        cookie = cookie.split(";")[0];
//                        sessionCookie = sessionCookie + cookie + ";";
//                    }
//                    break;
//                }
//            }
//
//            urlConn = new URL(prop.getProperty("server", "http://localhost:81") + "/polarion/j_security_check");
//            conn = (HttpURLConnection)urlConn.openConnection();
//            conn.setDoInput(true);
//            conn.setDoOutput(true);
//            conn.setRequestMethod("POST");
//            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//            conn.setRequestProperty("Cookie", sessionCookie);
//            DataOutputStream printout = new DataOutputStream (conn.getOutputStream ());
//            String content = "j_username="+prop.getProperty("user", "admin")+"&j_password="+prop.getProperty("passwd", "admin");
//            printout.writeBytes (content);
//            printout.flush ();
//            printout.close ();
//            conn.getInputStream(); //flush


For Polarion 2010 in multirepository mode this should work:

Code: Select all

        String user = prop.getProperty("user", "admin");
        String passwd = prop.getProperty("passwd", "admin");
        System.out.println("Authenticate: URL="+url+", user="+user+", password="+passwd);

        HttpURLConnection.setFollowRedirects(false);

        URL urlConn = new URL(prop.getProperty("serverLogin", HOST) + "/polarion/master-login");
        HttpURLConnection conn = (HttpURLConnection)urlConn.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        DataOutputStream printout = new DataOutputStream (conn.getOutputStream ());
        String content = "j_username="+user+"&j_password="+passwd+"&slave.id=cns";
        printout.writeBytes (content);
        printout.flush ();
        printout.close ();
        conn.getInputStream(); //flush

        URL locationURL = new URL(conn.getHeaderField("Location"));
        conn = (HttpURLConnection)locationURL.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

        String sessionCookie = "";
        for (Iterator iter = conn.getHeaderFields().entrySet().iterator(); iter.hasNext();) {
            Entry entry = (Entry) iter.next();
            if("Set-Cookie".equals(entry.getKey())) {
                Collection value = (Collection) entry.getValue();
                for (Iterator it = value.iterator(); it.hasNext();) {
                    String cookie = (String) it.next();
                    cookie = cookie.split(";")[0];
                    sessionCookie = sessionCookie + cookie + ";";
                }
                break;
            }
        }

Both versions will end up filling sessionCookie with proper cookie value.

Best Regards,
Ekaterina Chernikova
Support Polarion Software

mberhorst
Posts: 15
Joined: Wed Mar 03, 2010 9:46 am

Re: Polarion ALM login with Java HttpClient

Postby mberhorst » Wed Jul 28, 2010 2:26 pm

Hello everybody,

our Polarion Build is 3.4.2.20100503-0145 and we use single repository mode.
I try the authentication for the version 2010 in single repository mode.
With the InputStreamReader I receive the souce code of the polarion login page.
Should I not receive the souce code of the Polarion home page?
Maybe I make a mistake or I get no valid cookie.
My aim is to call a polarion servlet after the authentication.
Untill now the servlet is not working correct, because of the missing Polarion session.

My Java code:

Code: Select all

String sessionCookie = "";
URL urlConn;
urlConn = new URL(polarionHost+"/polarion");
System.out.println("URL to get cookie: "+polarionHost+"/polarion");
// URL to get cookie: http://host/polarion

HttpURLConnection conn = (HttpURLConnection)urlConn.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

for (Iterator iter = conn.getHeaderFields().entrySet().iterator(); iter.hasNext();) {
   Map.Entry entry = (Map.Entry) iter.next();
   if("Set-Cookie".equals(entry.getKey())) {
      Collection value = (Collection) entry.getValue();
      for (Iterator it = value.iterator(); it.hasNext();) {
         String cookie = (String) it.next();
         cookie = cookie.split(";")[0];
         sessionCookie = sessionCookie + cookie + ";";
      }
      break;
   }
}
System.out.println("Session-Coockie: "+sessionCookie);
// Session-Coockie: JSESSIONID=33D753B0434640EE240B6D9EB0611D12;

urlConn = new URL(polarionHost + "/polarion/j_security_check");   
System.out.println("URL for login: "+polarionHost+ "/polarion/j_security_check"); 
// URL for login: http://host/polarion/j_security_check

conn = (HttpURLConnection)urlConn.openConnection();                               
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST"); 
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");             
conn.setRequestProperty("Cookie", sessionCookie);

DataOutputStream printout = new DataOutputStream(conn.getOutputStream());
String content = "j_username="+user+"&j_password="+pass;
printout.writeBytes(content);
printout.flush();
printout.close();

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
   System.out.println(inputLine);
   // Output the source code of the Polarion login page
}
in.close();


Thank you in advance!

regards
Michael

JiriWalek
Posts: 49
Joined: Fri May 05, 2006 3:05 pm

Re: Polarion ALM login with Java HttpClient

Postby JiriWalek » Mon Aug 02, 2010 2:51 pm

Hi,

and do you need your servlet to be really controled by Polarion realm and by Form authentication?

In WEB-INF/web.xml file of your web application you can ommit the security section, or change from FORM to BASIC auth method, so you can later invoke the servlet easily.

If you change it from FORM to BASIC you can keep the DoAsServletFilter, if you remove it, you should call Subject.doAs ...

Best, Jiri
Jiri Walek
jiri.walek@polarion.com
Polarion Software

mberhorst
Posts: 15
Joined: Wed Mar 03, 2010 9:46 am

Re: Polarion ALM login with Java HttpClient

Postby mberhorst » Tue Aug 10, 2010 6:18 am

Thank you,

your answer brought me to the solution of my problem.
The problem was, that the DoAsFilter.java class used the polarion.extensions user for authentication.
My servlet call a Polarion Job, so the polarion.extensions user have got too less rights to run the job.
I changed the user and now it works.

Best regards
Michael

SimonMayer
Posts: 1
Joined: Fri Dec 02, 2011 11:54 am

Re: Polarion ALM login with Java HttpClient

Postby SimonMayer » Thu Dec 15, 2011 10:51 am

Hi,

is there any solution to perform an automatic Login to Polarion 2011?

Best regards

fr4nc3sc0
Posts: 4
Joined: Fri Mar 23, 2012 4:33 pm

Re: Polarion ALM login with Java HttpClient

Postby fr4nc3sc0 » Fri Jul 06, 2012 8:26 am

You can login via curl or lynx?

fr4nc3sc0
Posts: 4
Joined: Fri Mar 23, 2012 4:33 pm

Re: Polarion ALM login with Java HttpClient

Postby fr4nc3sc0 » Fri Jul 06, 2012 8:28 am

You can login via curl or lynx?


Return to “Polarion Application Lifecycle Management (ALM)”

Who is online

Users browsing this forum: No registered users and 7 guests