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).


3 answers

1 accepted

This widget could not be displayed.

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 :

Hope it could be useful to someone in the future.

PS : Thank you Brice !

This widget could not be displayed.

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.


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", "")
BufferedReader br = null;
	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");
	} else {
		        br = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));
			String readLine;
			 while(((readLine = br.readLine()) != null)) {
}catch(Exception e){}

My last syso prints :


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];

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 ...

This widget could not be displayed.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in Teamwork

What teamwork quotes inspire you?

Hey everyone! My name is Natalie and I'm an editor of the Atlassian Blog and I've got a question for you: What's your favorite quote about teamwork?  We've compiled a list here, along with...

141 views 16 7
Join discussion

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