com.Nextspace.Infrastructure.ClientAccount Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of NextspaceClient Show documentation
Show all versions of NextspaceClient Show documentation
This is the Java/Maven library to use as a part of Nextspace API client applications. It provides function-call level of abstraction for most of the Nextspace API requests and associated concepts.
The newest version!
package com.Nextspace.Infrastructure;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.Nextspace.Utility.JSON;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/**
* Represents Nextspace Client Account and contains registration details for it.
* @author alex.l
*/
public class ClientAccount
{
// Static instance of the Logger for this class.
private static final Logger LOG = LogManager.getLogger(ClientAccount.class);
// ID of the Environment for which deails of the Account were loaded.
private NextspaceHostingEnvironment EnvironmentID;
// ID of the Client Account for which details were loaded.
private String AccountID;
// Subdomain associated with this Client Account.
private String Subdomain;
// Displayed Name set for the Client Account.
private String DisplayName;
// ID of the Client which owns this Client Account.
private long ClientID;
// ID of the Client Account Type.
private String AccountTypeID;
// ID of the hosting location for the client account data storage.
private String HostingLocationKey;
// ID of the User who owns this Client Account.
private String OwnerUserID;
// ID of the Database Server that hosts the database for this Client Account.
private long DatabaseServerID;
// Contains effective Limits applied to this Client Account.
private JSON Limits;
/**
* Gets ID of the Client Account.
* @return ID of the Client Account.
*/
public String getID()
{
return this.AccountID;
}
/**
* Gets the Name set for the Nextspace Client Account.
* @return Title name of the Client Account.
*/
public String getDisplayName()
{
return this.DisplayName;
}
/**
* Gets Subdomain associated with this Client Account.
* @return Subdomain associated with the Client Account. This is what goes into {subdomain}.api.nextspace.host.
*/
public String getSubdomain()
{
return this.Subdomain;
}
/**
* Initializes new instance and loads details of the client Account.
* @param environment Keyword that identifies the environment in which to operate - PROD, UAT, STG, DEV.
* @param nextspaceAccountID ID of the Client Account.
* @throws Exception Errors are reported in the form of Exceptions thrown.
*/
public ClientAccount(NextspaceHostingEnvironment environment, String nextspaceAccountID) throws Exception
{
this.EnvironmentID = environment;
this.AccountID = nextspaceAccountID;
String cam = environment.GuardianURL();
JSON json = null;
try
{
URL url = new URL(cam + "/account/" + this.AccountID + "?WithLimits=true");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
con.setInstanceFollowRedirects(false);
int status = con.getResponseCode();
if (status == 200)
{
StringBuilder content;
try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())))
{
String inputLine;
content = new StringBuilder();
while ((inputLine = in.readLine()) != null)
{
content.append(inputLine);
}
String str = content.toString();
json = new JSON(str);
}
}
else
{
throw new Exception("Nextspace CAMIDM returned : " + status + " for URL:" + cam );
}
con.disconnect();
}
catch (Exception up)
{
LOG.error("Failed to load details of the Netxspace Client Account ID=" + nextspaceAccountID + " (Environment = " + environment + ").", up);
throw up;
}
// === Loading Client Account Details.
if (json != null)
{
this.Subdomain = json.getString("Subdomain");
this.DisplayName = json.getString("Name");
this.ClientID = json.getLong("Client.ID");
this.AccountTypeID = json.getString("AccountType.ID");
this.HostingLocationKey = json.getString("HostingLocation.Key");
this.OwnerUserID = json.getString("OwnerUser.ID");
this.DatabaseServerID = json.getLong("DatabaseServer.ID");
this.Limits = json.getJSON("Limits");
}
}
}