Creating user using JasperServer Rest API
First let us have a brief idea about REST(Representational State Transfer) web service is an alternative to RPC (Remote procedural calls) and webservices (SOAP, WSDL, et al.).REST relies on a stateless ,client-server cacheable communications protocols and in virtually in all cases HTTP is used.
What is REST WebService ?
REST is a light weighted alternative to WebServices like other web services it has features like:
- Platform Independent
- Language Independent
- Standards Based
- Easily be used with firewalls
Client calls services of server by sending request to the server in the form of URL and receiving response in return.Client bascially is a interface that passes parameters in the form of JSON,TEXT/PLAIN,XML etc.
For more info on rest services visit :
How to create user using Jasper-rest-api ?
To create a user account we need to put all details of user in the descriptor and pass it in
descriptor,and include it in a PUT request to the
rest_v2/users service, with the intended user ID (username) specified
in the URL
To create a user account the userId
must be unique if the userId is not unique then the already existing
user will be edited.
There are two urls First is for
commercial version and second is for community version.
Method : PUT
URL:
http://<host>:<port>/jasperserver[-pro]/rest_v2/organizations/orgID/users/userID
http://<host>:<port>/jasperserver[-pro]/rest_v2/users/userID
Content/type:application/json
or application/xml
The
descriptor that is passed needs to contain the following details:
{
“fullName”:”XYZ”,
“emailAddress”:”x@y.com”,
“enabled”:false,
“password”:”mypassword”,
"externallyDefined" :false,
“roles”:[{“name”:”ROLE_MANAGER”}]
}
Below are steps on how to create a rest client for creating users
on jasper server in java:
1.) Open
net beans under File option File->NewProject Select Java and
Java WebApplication Name the project (I have chosen project name
JasperClientTest).
2.) Add
jersey-client jar file,jersey-core jar file,jesey-server jar
file,jersey-servlet jar file,jersey-json jar file ,asm jar file by right clicking on libraries folder and selecting Add Jar option.
3.) Creating and Configuring a
Client Instance :
public static void main(String[]
args) {
ClientConfig config = new
DefaultClientConfig();
Client client =
Client.create(config);
}
4.) Creating a web resource instance:
public static void main(String[]
args) {
ClientConfig config = new
DefaultClientConfig();
Client client =
Client.create(config);
WebResource service =
client.resource(getBaseURI());
}
private static URI getBaseURI() {
UriBuilder.fromUri("http://localhost:8086/jasperserver").build();
}
5.)Passing
Authorization If the server holds login and password . Here I will
show an example of basic authorization
Authenticator.setDefault(new
Authenticator() {
@Override
protected
PasswordAuthentication getPasswordAuthentication() {
return
new PasswordAuthentication("jasperadmin","jasperadmin".toCharArray());
}
});
6.)Prepare
a json data having user details in it.
String
input =
"{\"fullName\":\"XYZ\",\"password\":\"jasperadmi\",\"emailAddress\":\"x@y.com\",\"tenantid\":\"Ag\",\"enabled\":true,\"externallyDefined\":false,\"roles\":[{\"name\":\"ROLE_USER\"},{\"name\":\"ROLE_VOUCHER\"}]}";
JSONObject jObject = new JSONObject(input);
7.) Create
a client response with put request passing url to call jasper User
Service and make its type "application/json"
ClientResponse
response =service.path("rest_v2").path("users").path("XYZ").type("application/json")
.put(ClientResponse.class,jObject);
URL that would be made is :localhost:8080/jasperserver/rest_v2/users/XYZ
(a)
In case the user is not created
if
(response.getStatus() != 201) {
throw
new RuntimeException("Failed : HTTP error code : "
+
response.getStatus());
}
(b)
In case the user is created
System.out.println("Output
from Server .... \n");
String
output = response.getEntity(String.class);
System.out.println(output);
9.) Jasper
Server will return a Status of 201 if the user is created,200 if the
user is found
(If user is already present in put request edits user details),400
status indicates there is some syntax error in json data that we are
passing,401 status indicates authorization
error like wise there are
other status which you could read from here)
10.) You can download the complete Netbean project of rest client from here .
Before running the project customize json and websource url according to your requirements
I would like your comments and feedback thanks :)
No comments:
Post a Comment