How to simulate a user connection in java ?

Hi all,

I would like to simulate a user connection in my java class (for my jira service).
I mean, i want to set a local context to have administrator rights to do some actions :

I use the Power Report plugin, and i want to do an Http Get method to a specific servlet to this plugin. But to get back the stream from the servlet, I need to be connected, and it doesn't seem to work without being connected (the result stream I receive is an Http response like "403 forbidden : Access to the specified resouce has been forbidden).

Regards.

3 answers

1 accepted

Accepted Answer
0 votes

I found a solution to my problem.

I did not able to get my pdf file (the stream I told in the post) because I didn't send session cookies with my get request.

Here the code I wrote to work it well :
(temp.txt)

Hope it could be useful to someone in the future.

PS : Thank you Brice !

Hi Bastien,

Yes, it is right. To get the result stream you have to be logged into JIRA application, otherwise you won't be able to retrieve it (Forbidden access : 403 HTTP error code)

As your service is executed on server side, I'm not sure you can simulate a client side behaviour...The Http Get request has to be made from client side.

Regards

Here is my code to simulate the user login :

HttpClient client = new HttpClient();
			    
client.getParams().setParameter("http.useragent", "Jira Client");
		
PostMethod post = new PostMethod("http://localhost:8080/jira/rest/gadget/1.0/login");
NameValuePair[] data = {
       new NameValuePair("os_username", "admin"),
       new NameValuePair("os_password", "*********"),
       new NameValuePair("os_captcha", "")
};
post.setRequestBody(data);
		        		        
BufferedReader br = null;
		        
try{
	System.out.println("Connexion en post ...");
	int returnCode = client.executeMethod(post);

	if(returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
		System.err.println("The Post method is not implemented by this URI");
		post.getResponseBodyAsString();
	} else {
		        br = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));
			String readLine;
			 while(((readLine = br.readLine()) != null)) {
				 System.out.println(readLine);
	}
}catch(Exception e){}



My last syso prints :

{"allowCookies":true,"externalUserManagement":false,
"isPublicMode":false,"isElevatedSecurityCheckShown"
:false,
"loginSucceeded":true,"captchaFailure":false,"loginError":false,
"communicationError":false,"loginFailedByPermissions"
:false}

So I guess i'm connected.

But i think the problem comes from cookies ... I have to put the correct cookie to jira and/or birt during the connectin, right ?

I tried to add cookies to my get request, through this code :

Cookie[] cookies = client.getState().getCookies();
for (int i = 0; i < cookies.length; i++) {
      Cookie cookie = cookies[i];
      initialState.addCookie(cookie);
                       
 }
client.setState(initialState);  
...
client.executeMethod(method);


But i've got the same error "403 - forbidden".

I analysed the network stream (with firebug) when i generated my pdf file by Power Report gadget, and all is same as my java code.

I don't understand why Birt engine doesn't allow me to get the file from my service ...

It would be even easier use the Birt Reports for JIRA instead because it runs Birt right inside JIRA and BIRT reports are accessible as JIRA urls.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted 2 hours ago in Canada

TOPIC TUESDAY - Oct 16th - Fav Atlassian Logo

Hey folks, We are back with your Topic Tuesday Question for the week. This week we are asking what is your fav new Atlassian logo? My vote would be for Statuspage. Let us know your thoughts! &nbs...

12 views 2 0
View post

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you