Jasper Server Authentication, Checking and Creating Directory using Java REST API

Posted on by By admin, in Jaspersoft | 0

In this blog the following topics will be covered:

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

Get your 30 Days Trail Version

1) Jasper server Authentication

2) Checking and creating a directoryusing java REST API.

Prerequisites: Eclipse Neon, JDK 1.8

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
  1. Jasper server Authentication:

REST service:  j_spring_security_check

Method:  GET

URL: http://localhost:port/jasperserver-pro/j_spring_security_check?j_username=” + username+ “&j_password=” + password

If the credentials are correct, then it returns session id.

Java source code:

package task.login;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class ServerAuthentication {
public static void main(String args[]) {

System.out.println("Session ID : " + ServerAuthentication.

LoginRequest("superuser", "superuser"));

}

public static String LoginRequest(String username, String password) {
URL url = null;
HttpURLConnection con = null;
String jsessionId = "";
String urlLink = "http://localhost:port/jasperserver-  pro/j_spring_security_check?"+ "j_username=" + username+ "&j_password=" + password;
try {

url = new URL(urlLink);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.connect();
System.out.println("Login response code :" + con.getResponseCode());
String session_id = con.toString() + "";
int index1 = session_id.indexOf("=");
int index2 = session_id.indexOf("?");
jsessionId = session_id.substring(index1 + 1, index2);
} catch (IOException e) {
e.printStackTrace();
}
return jsessionId;
}
}

2 . Directory checking in Jasper server : 

REST service:  rest_v2

Method:  GET

URL: http://localhost:port/jasperserver-pro/rest_v2/resources/public/my_Test/” + dirName

If the directory already exists in the server returns true , not exist then returns false.

Java source code:

package task.login;

import java.io.IOException;

import java.net.HttpURLConnection;

import java.net.URL; 

class CheckingDirectory {

public static void main(String args[]) {

System.out.println(CheckingDirectory.checkDirectory("myName"));

}

public static boolean checkDirectory(String dirName) {

URL url = null;

HttpURLConnection con = null;

int responseCode = 0;

boolean existStaus = false;

String username = "superuser";

String password ="superuser";

String session_id = ServerAuthentication.LoginRequest(username, password);

String urlLink = "http://localhost:port/jasperserver-pro/rest_v2/resources/public/my_Test/" + dirName;
try {

url = new URL(urlLink);

con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Cookie", "JSESSIONID=" + session_id);
con.setDoOutput(true);
con.setRequestMethod("GET");
con.connect();
responseCode = con.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("Checking Response Code : " + responseCode);

if (responseCode == 404) {
existStaus = false;
} else if (responseCode == 200) {
existStaus = true;
}
return existStaus;
}
}
  1. Directory Creating in Jasper server :

REST service:  rest_v2

Method:  POST

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

Claim Your 30 Days Free Trail

URL: http://localhost/jasperserver-pro/rest_v2/resources/public/my_Test

If the directory is created returns true, if not return false.

Java source code:

package task.login;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.net.HttpURLConnection;

import java.net.URL;

import org.json.JSONException;

import org.json.JSONObject;

class CreatingDirectory {

public static void main(String args[]) {

System.out.println(CreatingDirectory.createDirectory("MyName"));

}

public static boolean createDirectory(String dirName) {

boolean creationStatus = false;

URL url = null;

HttpURLConnection con = null;

int responseCode = 0;

String username="superuser";

String password="superuser";

String session_id = ServerAuthentication.LoginRequest(username,password);

String urlLink = "http://localhost:port/jasperserver-pro/rest_v2/resources/public/my_Test";

try {
url = new URL(urlLink);
con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Cookie", "JSESSIONID=" + session_id);
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/repository.folder+json");
con.setRequestProperty("Accept", "application/repository.folder+json");
con.setRequestMethod("POST");
JSONObject jsonBodyInfo = new JSONObject();
try {
jsonBodyInfo.put("label", dirName);
jsonBodyInfo.put("description", "Sample Test");
} 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) {
creationStatus = false;
} else if (responseCode == 401) {
System.out.println("Un-Authorised Access");
}
if (responseCode == 201) {
creationStatus = true;
}
return creationStatus;
}
}

User Demo : we can check functionality of these individual programs  at a time  using this source code

package task.login;
import java.util.Scanner;
public class UserDemo {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String dirName;

String username, password;

System.out.println("Enter Login Details of Jasper Server");

System.out.println("Enter username :");

username = sc.next();

System.out.println("Enter password :");

password = sc.next();

String sessionId = ServerAuthentication.LoginRequest(username, password);

if (sessionId != null) {

System.out.println("Login successful with Session ID : " + sessionId);

}

System.out.println("Enter directory name to check whether exists or not in server :");

dirName = sc.next();

boolean existStatus = CheckingDirectory.checkDirectory(dirName);

 if (existStatus == false) {

boolean creationStatus = CreatingDirectory.createDirectory(dirName);

if (creationStatus == true) {

System.out.println("Directory Successfully Created with name :" + dirName);

} else {

System.out.println("Failed to created Directory");

}

} else {

System.out.println("Directory Already exists with name :" + dirName);

}

}

}
logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

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

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

Grab The 30 Days Free Trail

Thank You
Anjaneyulu
Helical IT Solutions Pvt Ltd

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