Creating/Removing Users and Groups via REST API

This entry guides you through on how to create and remove users and groups via REST API using Java in Ambience v4.x

Contents:
A/ Creating User
B/ Removing User
C/ Creating Group
D/ Removing Group

Additional Contents:
E/ Adding and Removing User Accounts from a Group


A/ Creating User

  1. Create a Java Project using your desired IDE.

  2. Ensure the following jar files are in your java project /lib build path:

  • httpclient-<version>.jar
  • httpcore-<version>.jar
  1. Create a new Java class.
    (i.e. Create_User.java)

  2. Add in the following line of codes:

import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

@SuppressWarnings("deprecation")
public class Create_User extends DefaultHttpClient {

	public static void main(String[] args) {

		  try (DefaultHttpClient httpClient = new DefaultHttpClient() ){
			
			//change hostname (localhost) and domain name (eno) as appropriate
			HttpPost postRequest = new HttpPost(
				"http://localhost:8080/elx/do/eno/ad/usergroupdb/user");
			
			//attach an Authorization header to the request to authenticate the request as being from an administrator
			//send the user:pass as a Base64 encoded string, so admin:sa becomes YWRtaW46c2E= 
			postRequest.addHeader("Authorization", "Basic YWRtaW46c2E=");

			StringEntity input = new StringEntity("{\"name\":\"user2\",\"admin\":false,\"enabled\": true,"
					+ "\"changePassword\":false,\"email\":\"test2@example.com\",\"groups\": [\"group1\"],\"password\":\"abc123\"}");
			input.setContentType("application/json");
			postRequest.setEntity(input);
			HttpResponse response = httpClient.execute(postRequest);
			
			if (response.getStatusLine().getStatusCode() != 201) {
				throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getReasonPhrase());
			}

			httpClient.getConnectionManager().shutdown();

		  } catch (MalformedURLException e) {

			e.printStackTrace();

		  } catch (IOException e) {

			e.printStackTrace();

		  }

		}
	  
	}

*Do edit the codes above as needed.

  1. Save the file and run the code.

  2. A new user (i.e. user2) should be created in your Ambience server following the properties defined in the codes above.
    For example,
    image


B/ Removing User

  1. Create a Java Project using your desired IDE.

  2. Ensure the following jar files are in your java project /lib build path:

  • httpclient-<version>.jar
  • httpcore-<version>.jar
  1. Create a new Java class.
    (i.e. Remove_User.java)

  2. Add in the following line of codes:

import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.DefaultHttpClient;

@SuppressWarnings("deprecation")
public class Remove_User extends DefaultHttpClient {

	public static void main(String[] args) {

		  try (DefaultHttpClient httpClient = new DefaultHttpClient() ){
			
			//change hostname (localhost), domain name (eno), username (user2) as appropriate
			HttpDelete delRequest = new HttpDelete(
				"http://localhost:8080/elx/do/eno/ad/usergroupdb/user/user2");
			
			//attach an Authorization header to the request to authenticate the request as being from an administrator
			//send the user:pass as a Base64 encoded string, so admin:sa becomes YWRtaW46c2E= 
			delRequest.addHeader("Authorization", "Basic YWRtaW46c2E=");
            
            HttpResponse response = httpClient.execute(delRequest);
			

			if (response.getStatusLine().getStatusCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getReasonPhrase());
			}

			httpClient.getConnectionManager().shutdown();

		  } catch (MalformedURLException e) {

			e.printStackTrace();

		  } catch (IOException e) {

			e.printStackTrace();

		  }

		}
	  
	}

*Do edit the codes above as needed.

  1. Save the file and run the code.

  2. The user, (i.e. user2) will be removed from the Ambience server following the username defined in the codes above.


C/ Creating Group

  1. Create a Java Project using your desired IDE.

  2. Ensure the following jar files are in your java project /lib build path:

  • httpclient-<version>.jar
  • httpcore-<version>.jar
  1. Create a new Java class.
    (i.e. Create_Group.java)

  2. Add in the following line of codes:

import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

@SuppressWarnings("deprecation")
public class Create_Group extends DefaultHttpClient {

	public static void main(String[] args) {

		  try (DefaultHttpClient httpClient = new DefaultHttpClient() ){

			//DefaultHttpClient httpClient = new DefaultHttpClient();
			
			//change hostname (localhost) and domain name (eno) as appropriate
			HttpPost postRequest = new HttpPost(
				"http://localhost:8080/elx/do/eno/ad/usergroupdb/group");
			
			//attach an Authorization header to the request to authenticate the request as being from an administrator
			//send the user:pass as a Base64 encoded string, so admin:sa becomes YWRtaW46c2E= 
			postRequest.addHeader("Authorization", "Basic YWRtaW46c2E=");

			StringEntity input = new StringEntity("{\"name\":\"group8\",\"users\": [\"test\"]}");
			input.setContentType("application/json");
			postRequest.setEntity(input);
			HttpResponse response = httpClient.execute(postRequest);
			
			if (response.getStatusLine().getStatusCode() != 201) {
				throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getReasonPhrase());
			}

			httpClient.getConnectionManager().shutdown();

		  } catch (MalformedURLException e) {

			e.printStackTrace();

		  } catch (IOException e) {

			e.printStackTrace();

		  }

		}
	  
	}

*Do edit the codes above as needed.

  1. Save the file and run the code.

  2. A new group (i.e. group8) should be created in your Ambience server following the properties defined in the codes above.
    For example,
    image


D/ Removing Group

  1. Create a Java Project using your desired IDE.

  2. Ensure the following jar files are in your java project /lib build path:

  • httpclient-<version>.jar
  • httpcore-<version>.jar
  1. Create a new Java class.
    (i.e. Remove_Group.java)

  2. Add in the following line of codes:

import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.DefaultHttpClient;

@SuppressWarnings("deprecation")
public class Remove_Group extends DefaultHttpClient {

	public static void main(String[] args) {

		  try (DefaultHttpClient httpClient = new DefaultHttpClient() ){
			
			//change hostname (localhost), domain name (eno), group name (group8) as appropriate
			HttpDelete delRequest = new HttpDelete(
				"http://localhost:8080/elx/do/eno/ad/usergroupdb/group/group8");
			
			//attach an Authorization header to the request to authenticate the request as being from an administrator
			//send the user:pass as a Base64 encoded string, so admin:sa becomes YWRtaW46c2E= 
			delRequest.addHeader("Authorization", "Basic YWRtaW46c2E=");
            
            HttpResponse response = httpClient.execute(delRequest);
			

			if (response.getStatusLine().getStatusCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getReasonPhrase());
			}

			httpClient.getConnectionManager().shutdown();

		  } catch (MalformedURLException e) {

			e.printStackTrace();

		  } catch (IOException e) {

			e.printStackTrace();

		  }

		}
	  
	}

*Do edit the codes above as needed.

  1. Save the file and run the code.

  2. The group, (i.e. group8) will be removed from the Ambience server following the group name defined in the codes above.


Additional Contents:

E/ Adding and Removing User Accounts from a Group

  1. Create a Java Project using your desired IDE.

  2. Ensure the following jar files are in your java project /lib build path:

  • httpclient-<version>.jar
  • httpcore-<version>.jar
  1. Create a new Java class.
    (i.e. edit-useracct-group.java)

  2. Add in the following line of codes:

import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

@SuppressWarnings("deprecation")
public class edit-useracct-group extends DefaultHttpClient {

	public static void main(String[] args) {

		  try (DefaultHttpClient httpClient = new DefaultHttpClient() ){
			
			//change hostname (localhost), domain name (eno) and group name (group8) as appropriate
			HttpPut putRequest = new HttpPut(
				"http://localhost:8080/elx/do/eno/ad/usergroupdb/group/group8");
			
			//attach an Authorization header to the request to authenticate the request as being from an administrator
			//send the user:pass as a Base64 encoded string, so admin:sa becomes YWRtaW46c2E= 
			putRequest.addHeader("Authorization", "Basic YWRtaW46c2E=");

            //edit the list of users ([\"test\", \"admin\"]) as appropriate. In this case, test and admin user accounts are added into group8.
            /*
            for example, to remove the user account "test", StringEntity input will be as follows: 
                StringEntity input = new StringEntity("{\"name\":\"group8\",\"users\": [\"admin\"]}");
            
            to add the user account, "test", StringEntity input will be as follows:
                StringEntity input = new StringEntity("{\"name\":\"group8\",\"users\": [\"admin\", \"test\"]}");
            */
			StringEntity input = new StringEntity("{\"name\":\"group8\",\"users\": [\"test\", \"admin\"]}");
			input.setContentType("application/json");
			putRequest.setEntity(input);
			HttpResponse response = httpClient.execute(putRequest);
			
			if (response.getStatusLine().getStatusCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getReasonPhrase());
			}

			httpClient.getConnectionManager().shutdown();

		  } catch (MalformedURLException e) {

			e.printStackTrace();

		  } catch (IOException e) {

			e.printStackTrace();

		  }

		}
	  
	}

*Do edit the codes above as needed.

  1. Save the file and run the code.