SnmpAuthentication auth = null;
SnmpPrivacy privacy = null;
String oid = oidField.getText();
SnmpManager manager;
SnmpRequest request;
SnmpObjectType objectType;
String value;
String message;
// Validate port number
try {
port = Integer.parseInt(portField.getText());
} catch (NumberFormatException ignore) {
port = 0;
}
if (port <= 0 || port >= 65536) {
portField.requestFocus();
message = "Provide valid (numeric) port number in the " +
"range [1..65535].";
JOptionPane.showMessageDialog(frame,
message,
"Port Number Error",
JOptionPane.ERROR_MESSAGE);
return null;
}
// Get SNMP manager parameters
if (version < 3) {
if (read) {
community = new String(readCommunityField.getPassword());
} else {
community = new String(writeCommunityField.getPassword());
}
} else {
contextName = contextNameField.getText();
contextEngine = contextEngineField.getText();
userName = userNameField.getText();
if (authTypeCombo.getSelectedIndex() > 0) {
type = authTypeCombo.getSelectedItem().toString();
password = new String(authPasswordField.getPassword());
auth = new SnmpAuthentication(type, password);
}
if (privacyTypeCombo.getSelectedIndex() > 0) {
type = privacyTypeCombo.getSelectedItem().toString();
password = new String(privacyPasswordField.getPassword());
privacy = new SnmpPrivacy(type, password);
}
}
// Create SNMP manager
try {
if (version == 1) {
manager = SnmpManager.createSNMPv1(host, port, community);
} else if (version == 2) {
manager = SnmpManager.createSNMPv2c(host, port, community);
} else {
manager = SnmpManager.createSNMPv3(host,
port,
contextName,
contextEngine,
userName,
auth,
privacy);
}
} catch (SnmpException e) {
JOptionPane.showMessageDialog(frame,
e.getMessage(),
"SNMP Error",
JOptionPane.ERROR_MESSAGE);
return null;
}
// Create request
if (read) {
request = new SnmpRequest(oid);
} else {
objectType = frame.getSelectedNode().getSnmpObjectType();
value = valueField.getText();
request = new SnmpRequest(oid, objectType.getSyntax(), value);
}
return new Operation(manager, request, feedback);
}