Search in sources :

Example 16 with NXCException

use of org.netxms.client.NXCException in project netxms by netxms.

the class ApplicationWorkbenchWindowAdvisor method doLogin.

/**
 * Show login dialog and perform login
 */
private void doLogin(final Display display) {
    settings = Activator.getDefault().getDialogSettings();
    boolean success = false;
    boolean autoConnect = false;
    boolean ignoreProtocolVersion = false;
    // $NON-NLS-1$
    String password = "";
    CertificateManager certMgr = CertificateManagerProvider.provideCertificateManager();
    certMgr.setKeyStoreRequestListener(this);
    certMgr.setPasswordRequestListener(this);
    for (String s : Platform.getCommandLineArgs()) {
        if (// $NON-NLS-1$
        s.startsWith("-server=")) {
            // $NON-NLS-1$
            settings.put("Connect.Server", s.substring(8));
        } else if (// $NON-NLS-1$
        s.startsWith("-login=")) {
            // $NON-NLS-1$
            settings.put("Connect.Login", s.substring(7));
        } else if (// $NON-NLS-1$
        s.startsWith("-password=")) {
            password = s.substring(10);
            // $NON-NLS-1$
            settings.put("Connect.AuthMethod", AuthenticationType.PASSWORD.getValue());
        } else if (// $NON-NLS-1$
        s.equals("-auto")) {
            autoConnect = true;
        } else if (// $NON-NLS-1$
        s.equals("-ignore-protocol-version")) {
            ignoreProtocolVersion = true;
        }
    }
    boolean encrypt = true;
    LoginDialog loginDialog = new LoginDialog(null, certMgr);
    while (!success) {
        if (!autoConnect) {
            if (loginDialog.open() != Window.OK)
                System.exit(0);
            password = loginDialog.getPassword();
        } else {
            // only do auto connect first time
            autoConnect = false;
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        ConsoleSharedData.setProperty("SlowLink", settings.getBoolean("Connect.SlowLink"));
        LoginJob job = new LoginJob(display, // $NON-NLS-1$
        settings.get("Connect.Server"), // $NON-NLS-1$
        settings.get("Connect.Login"), encrypt, ignoreProtocolVersion);
        AuthenticationType authMethod;
        try {
            // $NON-NLS-1$
            authMethod = AuthenticationType.getByValue(settings.getInt("Connect.AuthMethod"));
        } catch (NumberFormatException e) {
            authMethod = AuthenticationType.PASSWORD;
        }
        switch(authMethod) {
            case PASSWORD:
                job.setPassword(password);
                break;
            case CERTIFICATE:
                job.setCertificate(loginDialog.getCertificate(), getSignature(certMgr, loginDialog.getCertificate()));
                break;
            default:
                break;
        }
        try {
            ModalContext.run(job, true, SplashHandler.getInstance().getBundleProgressMonitor(), Display.getCurrent());
            success = true;
        } catch (InvocationTargetException e) {
            if ((e.getCause() instanceof NXCException) && ((((NXCException) e.getCause()).getErrorCode() == RCC.NO_ENCRYPTION_SUPPORT) || (((NXCException) e.getCause()).getErrorCode() == RCC.NO_CIPHERS)) && encrypt) {
                // $NON-NLS-1$ //$NON-NLS-2$
                boolean alwaysAllow = settings.getBoolean("Connect.AllowUnencrypted." + settings.get("Connect.Server"));
                int action = getAction(settings, alwaysAllow);
                if (action != SecurityWarningDialog.NO) {
                    autoConnect = true;
                    encrypt = false;
                    if (action == SecurityWarningDialog.ALWAYS) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        settings.put("Connect.AllowUnencrypted." + settings.get("Connect.Server"), true);
                    }
                }
            } else {
                e.getCause().printStackTrace();
                MessageDialog.openError(null, Messages.get().ApplicationWorkbenchWindowAdvisor_ConnectionError, e.getCause().getLocalizedMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            // $NON-NLS-1$
            MessageDialog.openError(null, Messages.get().ApplicationWorkbenchWindowAdvisor_Exception, e.toString());
        }
    }
    CertificateManagerProvider.dispose();
    // Suggest user to change password if it is expired
    final NXCSession session = (NXCSession) ConsoleSharedData.getSession();
    if ((session.getAuthenticationMethod() == AuthenticationType.PASSWORD) && session.isPasswordExpired()) {
        requestPasswordChange(loginDialog.getPassword(), session);
    }
}
Also used : NXCSession(org.netxms.client.NXCSession) CertificateManager(org.netxms.certificate.manager.CertificateManager) LoginDialog(org.netxms.ui.eclipse.console.dialogs.LoginDialog) LoginJob(org.netxms.ui.eclipse.jobs.LoginJob) InvocationTargetException(java.lang.reflect.InvocationTargetException) NXCException(org.netxms.client.NXCException) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NXCException(org.netxms.client.NXCException) AuthenticationType(org.netxms.client.constants.AuthenticationType)

Example 17 with NXCException

use of org.netxms.client.NXCException in project netxms by netxms.

the class InterfacesFragment method modifyExpectedState.

/**
 * Modifies expected state of the selected interface
 * @param newState	The new infterface state
 * @return true if the command can be issued properly
 */
private boolean modifyExpectedState(int newState) {
    if (selectedObject != null) {
        NXCSession session = service.getSession();
        if (session != null) {
            NXCObjectModificationData md = new NXCObjectModificationData(selectedObject.getObjectId());
            md.setExpectedState(newState);
            try {
                session.modifyObject(md);
            } catch (NXCException e) {
                Log.e(TAG, "NXCException in modifyState...", e);
                e.printStackTrace();
            } catch (IOException e) {
                Log.e(TAG, "IOException in modifyState...", e);
                e.printStackTrace();
            }
            return true;
        }
    }
    return false;
}
Also used : NXCSession(org.netxms.client.NXCSession) NXCObjectModificationData(org.netxms.client.NXCObjectModificationData) IOException(java.io.IOException) NXCException(org.netxms.client.NXCException)

Example 18 with NXCException

use of org.netxms.client.NXCException in project netxms by netxms.

the class CreateSnmpDci method createDci.

/**
 * @param session
 * @param value
 * @param description
 * @param pollingInterval
 * @param retentionTime
 * @param deltaCalculation
 * @param lockRequired
 * @throws Exception
 */
private static void createDci(NXCSession session, SnmpValue value, String description, int pollingInterval, int retentionTime, int deltaCalculation, Map<Long, Boolean> lockRequired) throws Exception {
    AbstractNode node = (AbstractNode) session.findObjectById(value.getNodeId(), AbstractNode.class);
    if (node == null)
        throw new NXCException(RCC.INTERNAL_ERROR);
    DataCollectionConfiguration dcc;
    if (lockRequired.get(node.getObjectId())) {
        dcc = session.openDataCollectionConfiguration(node.getObjectId());
    } else {
        dcc = new DataCollectionConfiguration(session, node.getObjectId());
    }
    final DataCollectionItem dci = (DataCollectionItem) dcc.findItem(dcc.createItem(null), DataCollectionItem.class);
    dci.setPollingInterval(pollingInterval);
    dci.setRetentionTime(retentionTime);
    dci.setOrigin(DataCollectionItem.SNMP);
    dci.setDataType(dciTypeFromAsnType(value.getType()));
    dci.setStatus(DataCollectionItem.ACTIVE);
    dci.setDescription(description);
    dci.setDeltaCalculation(deltaCalculation);
    dci.setName(value.getName());
    dcc.modifyObject(dci);
    if (lockRequired.get(node.getObjectId())) {
        dcc.close();
    }
}
Also used : AbstractNode(org.netxms.client.objects.AbstractNode) DataCollectionItem(org.netxms.client.datacollection.DataCollectionItem) NXCException(org.netxms.client.NXCException) DataCollectionConfiguration(org.netxms.client.datacollection.DataCollectionConfiguration)

Example 19 with NXCException

use of org.netxms.client.NXCException in project netxms by netxms.

the class NetxmsConnector method testConnection.

public boolean testConnection(String server, String login, String password, StringBuffer errorMessage) {
    ConnectionDetails connectionDetails = new ConnectionDetails(server).parse();
    NXCSession session = new NXCSession(connectionDetails.getAddress(), connectionDetails.getPort(), true);
    session.setIgnoreProtocolVersion(true);
    boolean ret = false;
    try {
        session.connect();
        session.login(login, password);
        ret = true;
    } catch (IOException e) {
        errorMessage.append(e.getMessage());
    } catch (NXCException e) {
        errorMessage.append(e.getMessage());
    }
    return ret;
}
Also used : NXCSession(org.netxms.client.NXCSession) IOException(java.io.IOException) NXCException(org.netxms.client.NXCException)

Example 20 with NXCException

use of org.netxms.client.NXCException in project netxms by netxms.

the class AgentFileManager method uploadFile.

/**
 * Upload local file to agent
 */
private void uploadFile(final boolean overvrite) {
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    if (selection.isEmpty())
        return;
    final Object[] objects = selection.toArray();
    final AgentFile upladFolder = ((AgentFile) objects[0]).isDirectory() ? ((AgentFile) objects[0]) : ((AgentFile) objects[0]).getParent();
    final StartClientToServerFileUploadDialog dlg = new StartClientToServerFileUploadDialog(getSite().getShell());
    if (dlg.open() == Window.OK) {
        final NXCSession session = (NXCSession) ConsoleSharedData.getSession();
        new ConsoleJob(Messages.get().AgentFileManager_UploadFileJobTitle, null, Activator.PLUGIN_ID, null) {

            @Override
            protected void runInternal(final IProgressMonitor monitor) throws Exception {
                List<File> fileList = dlg.getLocalFiles();
                for (int i = 0; i < fileList.size(); i++) {
                    final File localFile = fileList.get(i);
                    String remoteFile = fileList.get(i).getName();
                    if (fileList.size() == 1)
                        remoteFile = dlg.getRemoteFileName();
                    final String rFileName = remoteFile;
                    new NestedVerifyOverwrite(localFile.isDirectory() ? AgentFile.DIRECTORY : AgentFile.FILE, localFile.getName(), true, true, false) {

                        @Override
                        public void executeAction() throws NXCException, IOException {
                            session.uploadLocalFileToAgent(objectId, localFile, upladFolder.getFullName() + "/" + rFileName, overvrite, new // $NON-NLS-1$
                            ProgressListener() {

                                private long prevWorkDone = 0;

                                @Override
                                public void setTotalWorkAmount(long workTotal) {
                                    monitor.beginTask(Messages.get(getDisplay()).UploadFileToServer_TaskNamePrefix + localFile.getAbsolutePath(), (int) workTotal);
                                }

                                @Override
                                public void markProgress(long workDone) {
                                    monitor.worked((int) (workDone - prevWorkDone));
                                    prevWorkDone = workDone;
                                }
                            });
                            monitor.done();
                        }

                        @Override
                        public void executeSameFunctionWithOverwrite() throws IOException, NXCException {
                            session.uploadLocalFileToAgent(objectId, localFile, upladFolder.getFullName() + "/" + rFileName, true, new // $NON-NLS-1$
                            ProgressListener() {

                                private long prevWorkDone = 0;

                                @Override
                                public void setTotalWorkAmount(long workTotal) {
                                    monitor.beginTask(Messages.get(getDisplay()).UploadFileToServer_TaskNamePrefix + localFile.getAbsolutePath(), (int) workTotal);
                                }

                                @Override
                                public void markProgress(long workDone) {
                                    monitor.worked((int) (workDone - prevWorkDone));
                                    prevWorkDone = workDone;
                                }
                            });
                            monitor.done();
                        }
                    }.run(viewer.getControl().getDisplay());
                }
                upladFolder.setChildren(session.listAgentFiles(upladFolder, upladFolder.getFullName(), objectId));
                runInUIThread(new Runnable() {

                    @Override
                    public void run() {
                        viewer.refresh(upladFolder, true);
                    }
                });
            }

            @Override
            protected String getErrorMessage() {
                return "Cannot upload file to remote agent";
            }
        }.start();
    }
}
Also used : NXCSession(org.netxms.client.NXCSession) AgentFile(org.netxms.client.server.AgentFile) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) StartClientToServerFileUploadDialog(org.netxms.ui.eclipse.filemanager.dialogs.StartClientToServerFileUploadDialog) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) NXCException(org.netxms.client.NXCException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) List(java.util.List) ArrayList(java.util.ArrayList) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) AgentFile(org.netxms.client.server.AgentFile) File(java.io.File)

Aggregations

NXCException (org.netxms.client.NXCException)34 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)20 ConsoleJob (org.netxms.ui.eclipse.jobs.ConsoleJob)20 NXCSession (org.netxms.client.NXCSession)16 PartInitException (org.eclipse.ui.PartInitException)13 IOException (java.io.IOException)12 AbstractObject (org.netxms.client.objects.AbstractObject)8 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)6 List (java.util.List)4 ArrayList (java.util.ArrayList)3 UUID (java.util.UUID)3 Alarm (org.netxms.client.events.Alarm)3 AbstractNode (org.netxms.client.objects.AbstractNode)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 AccessListElement (org.netxms.client.AccessListElement)2 DataCollectionConfiguration (org.netxms.client.datacollection.DataCollectionConfiguration)2 DataCollectionItem (org.netxms.client.datacollection.DataCollectionItem)2 GraphSettings (org.netxms.client.datacollection.GraphSettings)2 AgentPolicy (org.netxms.client.objects.AgentPolicy)2 Node (org.netxms.client.objects.Node)2