How to Create a user in Jasper Server using Jasper REST API in Java

Posted on by By Mounika, in Jaspersoft | 0

This blog explains the creation of a user in jasper server using java. Java code uses REST API to create a user in the Jasper server.

Make data easy with Helical Insight.
Helical Insight is the world’s best open source business intelligence tool.

Get your 30 Days Trail Version

The RESTful interface of Jasper Reports server responds to HTTP requests from client applications in the following methods

  1. GET – used to get the information of server resources
  2. POST – used to create resources in the server
  3. PUT – used to update the resources in the server
  4. DELETE – used to delete the resources from the server

Algorithm:

  1. Log in to the server using REST API and store session ID
  2. Check whether the user to be created already exists using REST API GET request and return the status of existence
  3. If a user does not exist, create user using REST APi PUT request

Below is the code used to create a user in jasper server

package usercreate;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

public class createuser {
	public static void main(String args[]) {
		String jsessionid=createuser.LoginRequest("jasperadmin", "jasperadmin","localhost/jasperserver/");
		String checkstatus=createuser.check("mounika","mounika",jsessionid,"localhost/jasperserver/");
		if(checkstatus=="false")
		{
		createuser.create("mounika","mounika","true",jsessionid,"localhost/jasperserver/");
		}
	}

	public static String LoginRequest(String username, String password, String URL) {
		URL url = null;
		String jsessionId = "";
		HttpURLConnection con = null;
		String urlLink = URL+"j_spring_security_check?j_username=" + username
				+ "&j_password=" + password;
		try {
			url = new URL(urlLink);
		    CookieManager cookieManager = new CookieManager();
		    CookieHandler.setDefault(cookieManager);
			con = (HttpURLConnection) url.openConnection();
			con.setRequestMethod("GET");
			con.connect();
			System.out.println("Login response code :" + con.getResponseCode());
				List cookies = cookieManager.getCookieStore().getCookies();
				for (HttpCookie cookie : cookies) {
					if (cookie.getName().toString().equals("JSESSIONID")) {	
						jsessionId= cookie.getValue();
					} 
				}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jsessionId;	
	}
	
	
		public static String check(String user, String pwd, String session_id, String URL) {
	    String checkstatus="false";
		URL url = null;
		HttpURLConnection con = null;
		int responseCode = 0;
		String urlLink = URL+"rest_v2/users/"+user;
		try {
			url = new URL(urlLink);
			con = (HttpURLConnection) url.openConnection();
			con.setRequestProperty("Cookie", "JSESSIONID=" + session_id);
			con.setRequestProperty("Content-Type", "application/json");
			con.setRequestProperty("Accept", "application/json");
			con.setRequestMethod("GET");
			con.setDoOutput(true);
			con.setDoInput(true);
			con.connect();
			responseCode = con.getResponseCode();
	}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("creation Response Code : " + responseCode);
		if (responseCode == 404) {
			System.out.println("User is not present");
		} else if (responseCode == 200) {
			System.out.println("User is present");
			checkstatus="true";
		}
		return checkstatus;
	}
	
	public static void create(String user, String pwd, String enabled, String session_id, String URL) {
		URL url = null;
		HttpURLConnection con = null;
		int responseCode = 0;
		String urlLink = URL+"rest_v2/users/"+user;
		try {
			url = new URL(urlLink);
			con = (HttpURLConnection) url.openConnection();
			con.setRequestProperty("Cookie", "JSESSIONID=" + session_id);
			con.setRequestProperty("Content-Type", "application/json");
			con.setRequestProperty("Accept", "application/json");
			con.setRequestMethod("PUT");
			con.setDoOutput(true);
			con.setDoInput(true);
			JSONObject jsonBodyInfo = new JSONObject();
			try {
				jsonBodyInfo.put("fullName", user);
				jsonBodyInfo.put("password", pwd);
				jsonBodyInfo.put("enabled", enabled);

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
			wr.write(jsonBodyInfo.toString());
			wr.flush();

			con.connect();
			responseCode = con.getResponseCode();
	}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("creation Response Code : " + responseCode);
		if (responseCode == 404) {
			System.out.println("Server down");

		} else if (responseCode == 401) {
			System.out.println("Un-Authorised Access");
		}
		if (responseCode == 201) {
			System.out.println("User Created");
		}
		if (responseCode == 200) {
			System.out.println("User Overrided since same user is present");
		}

	}
}

Result:

Make data easy with Helical Insight.
Helical Insight is the world’s best open source business intelligence tool.

Grab The 30 Days Free Trail

In case if you have any queries please get us at support@helicaltech.com

Thank You
Mounika Pulimamidi
Helical IT Solutions Pvt Ltd

logo

Best Open Source Business Intelligence Software Helical Insight Here

logo

A Business Intelligence Framework


0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments