GetAllEnabledUsers: Java example

 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
 
import javax.xml.namespace.QName;
 
import org.apache.http.HttpEntity;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
 
 
public class webservices {
 
static String  companyName = "CompanyKey";
static String userName = "Loginname";
static String password = "Password";
 
public static void main(String[] args) throws Exception{
Global();
 
}
//pass the parameters for global call
public static HttpEntity getRequestEntity(){
JSONArray jsonArr = new JSONArray();
HttpEntity entity = null;
JSONObject jsonElement = new JSONObject();
JSONObject jsonObj = new JSONObject();
JSONObject jsonObj1 = new JSONObject();
jsonElement.put("tenant",jsonObj);
jsonObj.put("companyKey",companyName);
 
String json = jsonElement.toJSONString();
try {
entity = new StringEntity(json);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return entity;
 
 
 
 
}
 
 
public static void Global() throws ClientProtocolException, IOException, ParseException{
 
String url = "https://global.replicon.com/DiscoveryService1.svc/GetTenantEndpointDetails";
// Create an http client to perform the http communication
CloseableHttpClient httpClient = HttpClients.createDefault();
// Make the actual http request
HttpPost httpRequest =  new HttpPost(url);
        
HttpEntity requestEntity = getRequestEntity();
if(requestEntity != null) {
    httpRequest.setHeader("Content-type", "application/json");
    httpRequest.setEntity(requestEntity);
}
 
CloseableHttpResponse response = httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity);
GetEnabledUsers(responseContent);
 
 
 
}
 
private static void GetEnabledUsers(String responseContent) throws ParseException, ClientProtocolException, IOException {
 
{
JSONParser parser = new JSONParser();
JSONObject jsonObj = (JSONObject) parser.parse(responseContent);
JSONObject jsonObj1 = (JSONObject) jsonObj.get("d");
String  jsonObj2 =(String) jsonObj1.get("applicationRootUrl").toString();
 
 
String url = jsonObj2+companyName+"/services/UserService1.svc/GetEnabledUsers";
 
// Create an http client to perform the http communication
CloseableHttpClient httpClient = HttpClients.createDefault();
 
// Http request to specific web service. Request will be given to client
HttpPost request =  new HttpPost(url);
 
// Create credentials for the authentication
CredentialsProvider provider =  new BasicCredentialsProvider();
 
UsernamePasswordCredentials credential = new UsernamePasswordCredentials(companyName+'\\'+userName, password);
provider.setCredentials(AuthScope.ANY, credential);
HttpClientContext localContext = HttpClientContext.create();
localContext.setCredentialsProvider(provider);
       
// Make the actual http request
HttpPost httpRequest =  new HttpPost(url);
       
HttpEntity requestEntity = getRequestEntity();
if(requestEntity != null) {
httpRequest.setHeader("Content-type", "application/json");
httpRequest.setEntity(requestEntity);
}
 
CloseableHttpResponse response = httpClient.execute(httpRequest, localContext);
HttpEntity entity = response.getEntity();
String responseContent1 = EntityUtils.toString(entity);
System.out.println(responseContent1);
 
}
 
 
}
 
 
 
 
 
 
 
 
}