Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to simulate a user connection in java ?

Bastien
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 27, 2012

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

0 votes
Answer accepted
Bastien
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2012

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 !

0 votes
Pablo Beltran
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 16, 2015

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.

0 votes
Brice Gestas _Elements_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 27, 2012

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

Bastien
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 27, 2012

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 ?

Bastien
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 27, 2012

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events