📄 pserveraccrinterface.java
字号:
/**
* Find out whether a particular group for a particular feature type has at
* least one server associated with it.
*/
public boolean hasServer(String featureType, String group)
throws IOException, SAXException, VPPNoSuchFileException
{
if (groupServers == null)
{
getFeatureInformation();
}
Boolean has = (Boolean) groupServers.get(featureType + group);
if (has != null)
{
return true;
}
return false;
}
/**
* Get the groups which are configured for the feature with the given name. <p>
* The data comes from the SystemConfiguration_ListofFeatureServers file
* on the pserver.
*/
public String[] getFeatureGroups(String featureName)
throws IOException, SAXException, VPPNoSuchFileException
{
if (featureGroups == null)
{
getFeatureInformation();
}
Vector groups = (Vector) featureGroups.get(featureName);
String[] groupNames = new String[groups.size()];
for (int i = 0; i < groups.size(); i++)
{
groupNames[i] = (String) groups.elementAt(i);
}
return groupNames;
}
/**
* Retrieves a list of the names of the features which are configured. <p>
* The data comes from the SystemConfiguration_ListofFeatureServers file
* on the pserver.
*/
public String[] getFeatureTypes()
throws IOException, SAXException, VPPNoSuchFileException
{
if (featureGroups == null)
{
getFeatureInformation();
}
Set keys = featureGroups.keySet();
String[] types = new String[keys.size()];
Iterator iterator = keys.iterator();
int i = 0;
while (iterator.hasNext())
{
types[i] = (String) iterator.next();
i++;
}
return types;
}
/**
* Reads the SystemConfiguration_ListOfFeatureServers file from the pserver
* to determine the feature types and the group names for each feature which
* are configured on the pserver.
*/
private void getFeatureInformation()
throws IOException, SAXException, VPPNoSuchFileException
{
featureGroups = new Hashtable();
groupServers = new Hashtable();
String filegroup = "SystemConfiguration";
String filename = "ListOfFeatureServers";
try
{
BufferedReader configInfo = connection.request("GET", filegroup,
filename);
Document doc = parser.parse(new InputSource(configInfo));
NodeList features = doc.getElementsByTagName("serverType");
for (int i = 0; i < features.getLength(); i++)
{
// get the feature name
String typeName =
features.item(i).getAttributes().getNamedItem("value").getNodeValue();
// get a list of groups associated with that feature
NodeList groups =
((Element) features.item(i)).getElementsByTagName("serverGroup");
Vector groupNames = new Vector();
for (int j = 0; j < groups.getLength(); j++)
{
Element group = (Element) groups.item(j);
String groupName =
group.getAttributes().getNamedItem("value").getNodeValue();
if (groupName != null)
{
groupNames.addElement(groupName);
// find out if this feature group has any servers associated with it
if (group.hasChildNodes())
{
NodeList servers =
group.getElementsByTagName("server"); // get all server nodes
if (servers.getLength() > 0)
{
String host =
((Element) servers.item(0)).getAttribute("host");
if (host != null)
{
groupServers.put(typeName + groupName,
new Boolean(true)); // key is featureType+groupName
} // end if host attribute is defined
} // end if there are server elements
} // end if group has child nodes
} // end if group has name
}
if (groupNames.size() > 0)
{
featureGroups.put(typeName, groupNames);
}
}
}
catch (VPPException ex)
{
if (VPPNoSuchFileException.class.isInstance(ex))
{
throw new VPPNoSuchFileException("Could not load file " + filegroup
+ " " + filename + "\n\nbecause:\n\n" + ex.getMessage());
}
else
{
connection.showVPPException(ex, "");
}
}
} // end method getFeatureGroups
/**
* Get the underlying VPPTransactionWrapper which is used by this class
* to communicate with the pserver.
*/
public VPPTransactionWrapper getConnection()
{
return connection;
} // end method getConnection
public static String getMasterUserNameFromXml(String xml)
{
// this should be done using an xml parser ...
int index = xml.indexOf("masterUser=\"") + "masterUser=\"".length();
String masterName = xml.substring(index, xml.indexOf("\"", index + 1));
return masterName;
}
/**
* Takes the given xml string and extracts the value of the password element.
* Returns null if the element is not present.
*/
public String getPasswordFromXml(String xml)
{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = null;
try
{
parser = spf.newSAXParser();
}
catch (ParserConfigurationException e1)
{
e1.printStackTrace();
return "";
}
catch (SAXException e2)
{
e2.printStackTrace();
return "";
}
PasswordHandler handler = new PasswordHandler();
try
{
parser.parse(new InputSource(new StringReader(xml)), handler);
return handler.getPassword();
}
catch (Exception e)
{
// quietly suppress this exception
e.printStackTrace();
}
return null;
}
/**
* A content handler for the sax parser used to retrieve the password element
* from the file. Note that the password is returned in its raw (hashed)
* form.
*/
class PasswordHandler extends DefaultHandler
{
private boolean atPasswordElement = false;
private String password = null;
public void startElement(String ignore1, String ignore2, String rawName,
Attributes attr)
{
if (rawName.equals("password"))
{
atPasswordElement = true;
}
}
public void characters(char[] ch, int start, int length)
{
if (atPasswordElement)
{
password = new String(ch, start, length);
}
}
public void endElement(java.lang.String uri, java.lang.String localName,
java.lang.String rawName)
{
if (rawName.equals("password"))
{
atPasswordElement = false;
}
}
public String getPassword()
{
return password;
}
} // end inner class PasswordHandler
/**
* Check whenter the supplied xml string allows access level of the supplied
* type (ie: admin or tech).
* This parses the file, finds the "accessLevel" elements (returns false if
* they do not exist), matches the "type" attribute to the supplied type and
* then returns the value of the "enabled" attribute.
*/
public boolean allowsAccessOfType(String xml,
String type) throws IOException, SAXException
{
Document doc = parser.parse(new InputSource(new StringReader(xml)));
NodeList accessLevels = doc.getElementsByTagName("accessLevel");
if (accessLevels == null)
{
return false;
}
if (accessLevels.getLength() < 1)
{
return false;
}
for (int i = 0; i < accessLevels.getLength(); i++)
{
Element accessLevel = (Element) accessLevels.item(i);
String typeFromFile = accessLevel.getAttribute("type");
if (typeFromFile.equals(type))
{
String enabled = accessLevel.getAttribute("enabled");
if (enabled.equals("true"))
{
return true;
}
else
{
return false;
}
}
}
return false;
}
/**
* @return true if the alias file was successfully created and saved, false
* otherwise.
*/
public boolean saveAlias(String aliasName, String masterName)
{
// want aliases to be unique, so check whether there is a user with the
// same name as the proposed alias
try
{
connection.doGet("Accounts", aliasName);
}
catch (VPPException exx)
{
if (VPPNoSuchFileException.class.isInstance(exx))
{
// this user name does not exist -> ok
// now check whether this alias already exists
String response = "";
try
{
response = connection.doGet("Aliases", aliasName);
}
catch (VPPException ex)
{
if (VPPNoSuchFileException.class.isInstance(ex))
{
// there is not a user with this name either
// so its ok to create the alias
String xml = "<userAlias masterUser=\"" + masterName
+ "\" alias=\"" + aliasName + "\"></userAlias>";
try
{
connection.doPut("Aliases", aliasName, xml);
}
catch (VPPException e)
{
connection.showVPPException(e, "");
}
return true;
} // end if no such user
else
{
connection.showVPPException(ex, "");
}
}
// if the alias exists, that is not necessarily and error: it may belong
// to this user
if (response.indexOf(masterName) != -1)
// if the user name is contained anywhere in the alias file, then assume
// that this alias belongs to this user. This should probably be done
// by actually parsing the file, but for now its good enough.
{
return true;
}
} // end if no such alias
else
{
connection.showVPPException(exx, "");
}
}
return false;
}
public void deleteAliasFile(String alias) throws VPPNoSuchFileException
{
try
{
connection.request("REMOVE", "Aliases", alias);
}
catch (VPPException ex)
{
if (VPPNoSuchFileException.class.isInstance(ex))
{
throw (VPPNoSuchFileException) ex;
}
else
{
connection.showVPPException(ex, "");
}
}
}
} // end class PServerInterface
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -