com.contrastsecurity.ide.eclipse.ui.internal.model.VulnerabilityPage.java Source code

Java tutorial

Introduction

Here is the source code for com.contrastsecurity.ide.eclipse.ui.internal.model.VulnerabilityPage.java

Source

/*******************************************************************************
 * Copyright (c) 2017 Contrast Security.
 * All rights reserved. 
 * 
 * This program and the accompanying materials are made available under 
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License.
 * 
 * The terms of the GNU GPL version 3 which accompanies this distribution
 * and is available at https://www.gnu.org/licenses/gpl-3.0.en.html
 * 
 * Contributors:
 *     Contrast Security - initial API and implementation
 *******************************************************************************/
package com.contrastsecurity.ide.eclipse.ui.internal.model;

import java.util.LinkedHashSet;
import java.util.Set;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import com.contrastsecurity.ide.eclipse.core.Constants;
import com.contrastsecurity.ide.eclipse.core.ContrastCoreActivator;
import com.contrastsecurity.ide.eclipse.core.Util;
import com.contrastsecurity.ide.eclipse.ui.ContrastUIActivator;
import com.contrastsecurity.ide.eclipse.ui.internal.views.VulnerabilitiesView;
import com.contrastsecurity.models.Application;
import com.contrastsecurity.models.Applications;
import com.contrastsecurity.models.Server;
import com.contrastsecurity.models.Servers;
import com.contrastsecurity.sdk.ContrastSDK;

public class VulnerabilityPage extends AbstractPage {

    private ComboViewer serverCombo;
    private ComboViewer applicationCombo;
    private Label label;

    public VulnerabilityPage(Composite parent, int style, VulnerabilitiesView vulnerabilitiesView) {
        super(parent, style, vulnerabilitiesView);
        setLayout(new GridLayout());
        GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
        setLayoutData(gd);
        Composite comboComposite = new Composite(this, SWT.NONE);
        comboComposite.setLayout(new GridLayout(3, false));
        label = new Label(comboComposite, SWT.NONE);
        gd = new GridData(SWT.FILL, SWT.CENTER, false, false);
        label.setLayoutData(gd);
        String orgUuid = getOrgUuid();
        serverCombo = createServerCombo(comboComposite, orgUuid);
        applicationCombo = createApplicationCombo(comboComposite, orgUuid);
    }

    private String getOrgUuid() {
        String orgUuid = null;
        try {
            orgUuid = Util.getDefaultOrganizationUuid();
        } catch (Exception e) {
            ContrastUIActivator.log(e);
        }
        return orgUuid;
    }

    private ComboViewer createApplicationCombo(Composite composite, String orgUuid) {
        ComboViewer combo = new ComboViewer(composite, SWT.READ_ONLY);
        combo.getControl().setFont(composite.getFont());
        combo.setLabelProvider(new ContrastLabelProvider());
        combo.setContentProvider(new ArrayContentProvider());
        Set<ApplicationUIAdapter> contrastApplications = new LinkedHashSet<>();
        int count = 0;
        if (orgUuid != null) {
            Applications applications = null;
            try {
                applications = getSdk().getApplications(orgUuid);
            } catch (Exception e) {
                ContrastUIActivator.log(e);
            }
            if (applications != null && applications.getApplications() != null
                    && applications.getApplications().size() > 0) {
                for (Application application : applications.getApplications()) {
                    ApplicationUIAdapter app = new ApplicationUIAdapter(application, application.getName());
                    contrastApplications.add(app);
                    count++;
                }
            }
        }
        ApplicationUIAdapter allApplications = new ApplicationUIAdapter(null, "All Applications(" + count + ")");
        contrastApplications.add(allApplications);
        combo.setInput(contrastApplications);
        IEclipsePreferences prefs = ContrastCoreActivator.getPreferences();
        String appId = prefs.get(Constants.APPLICATION_ID, Constants.ALL_APPLICATIONS);
        ApplicationUIAdapter selected = allApplications;
        for (ApplicationUIAdapter adapter : contrastApplications) {
            if (appId.equals(adapter.getId())) {
                selected = adapter;
                break;
            }
        }
        combo.setInput(contrastApplications);
        combo.setSelection(new StructuredSelection(selected));
        return combo;
    }

    private ContrastSDK getSdk() {
        return getVulnerabilitiesView().getSdk();
    }

    private ComboViewer createServerCombo(Composite composite, String orgUuid) {
        ComboViewer combo = new ComboViewer(composite, SWT.READ_ONLY);
        combo.getControl().setFont(composite.getFont());
        combo.setLabelProvider(new ContrastLabelProvider());
        combo.setContentProvider(new ArrayContentProvider());
        Set<ServerUIAdapter> contrastServers = new LinkedHashSet<>();
        int count = 0;
        if (orgUuid != null) {
            Servers servers = null;
            try {
                servers = getSdk().getServers(orgUuid, null);
            } catch (Exception e) {
                ContrastUIActivator.log(e);
            }
            if (servers != null && servers.getServers() != null) {
                for (Server server : servers.getServers()) {
                    ServerUIAdapter contrastServer = new ServerUIAdapter(server, server.getName());
                    contrastServers.add(contrastServer);
                    count++;
                }
            }
        }
        ServerUIAdapter allServers = new ServerUIAdapter(null, "All Servers(" + count + ")");
        contrastServers.add(allServers);
        IEclipsePreferences prefs = ContrastCoreActivator.getPreferences();
        long serverId = prefs.getLong(Constants.SERVER_ID, Constants.ALL_SERVERS);
        ServerUIAdapter selected = allServers;
        for (ServerUIAdapter adapter : contrastServers) {
            if (serverId == adapter.getId()) {
                selected = adapter;
                break;
            }
        }
        combo.setInput(contrastServers);
        combo.setSelection(new StructuredSelection(selected));
        return combo;
    }

    public ComboViewer getServerCombo() {
        return serverCombo;
    }

    public ComboViewer getApplicationCombo() {
        return applicationCombo;
    }

    public Label getLabel() {
        return label;
    }

}