com.vaadin.ui.Upload

Here are the examples of the java api com.vaadin.ui.Upload taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

4 Examples 7

17 Source : SslCertificateUploader.java
with Apache License 2.0
from opensecuritycontroller

public clreplaced SslCertificateUploader extends CustomComponent implements Receiver, FailedListener, SucceededListener {

    private static final Logger log = LoggerFactory.getLogger(SslCertificateUploader.clreplaced);

    private static final long serialVersionUID = 1L;

    private UploadNotifier uploadNotifier;

    protected File file;

    protected Upload upload;

    protected final VerticalLayout verLayout = new VerticalLayout();

    protected X509TrustManagerApi x509TrustManager;

    public SslCertificateUploader(X509TrustManagerApi x509TrustManager) {
        this.x509TrustManager = x509TrustManager;
        Panel panel = new Panel();
        layout(panel);
        setCompositionRoot(panel);
    }

    protected void layout(Panel panel) {
        createUpload();
        this.verLayout.setSpacing(true);
        panel.setWidth("100%");
        panel.setContent(this.verLayout);
        this.verLayout.addComponent(this.upload);
        this.verLayout.addStyleName(StyleConstants.COMPONENT_SPACING);
    }

    public void setUploadNotifier(UploadNotifier uploadNotifier) {
        this.uploadNotifier = uploadNotifier;
    }

    @Override
    public OutputStream receiveUpload(String filename, String mimeType) {
        if (filename != null && !filename.isEmpty()) {
            log.info("Start uploading certificate: " + filename);
            try {
                this.file = File.createTempFile("tmp", filename);
                return new FileOutputStream(this.file);
            } catch (final java.io.IOException e) {
                log.error("Error opening certificate: " + filename, e);
                ViewUtil.iscNotification(getString(UPLOAD_COMMON_ERROR) + filename, Notification.Type.ERROR_MESSAGE);
            }
        }
        return null;
    }

    protected void createUpload() {
        this.upload = new Upload();
        this.upload.setButtonCaption(getString(MAINTENANCE_SSLCONFIGURATION_UPLOAD));
        this.upload.setReceiver(this);
        this.upload.addFailedListener(this);
        this.upload.addSucceededListener(this);
        this.upload.setImmediate(false);
        final UploadInfoWindow uploadInfoWindow = new UploadInfoWindow(this.upload);
        this.upload.addStartedListener((StartedListener) event -> {
            if (uploadInfoWindow.getParent() == null) {
                ViewUtil.addWindow(uploadInfoWindow);
            }
        });
    }

    private void repaintUpload() {
        boolean enabled = this.upload.isEnabled();
        this.verLayout.removeComponent(this.upload);
        createUpload();
        this.upload.setEnabled(enabled);
        this.verLayout.addComponent(this.upload);
    }

    @Override
    public void uploadSucceeded(SucceededEvent event) {
        boolean succeeded = true;
        try {
            processCertificateFile();
            log.info("=============== Upload certificate succeeded");
            repaintUpload();
        } catch (Exception ex) {
            succeeded = false;
            log.error("=============== Failed to upload certificate", ex);
            ViewUtil.iscNotification("SSL certificate upload failed. " + ex.getMessage() + " Please use a valid certificate file", Notification.Type.ERROR_MESSAGE);
            repaintUpload();
        }
        if (this.uploadNotifier != null) {
            this.uploadNotifier.finishedUpload(succeeded);
        }
    }

    protected void processCertificateFile() throws Exception {
        log.info("================ SSL certificate upload completed");
        log.info("================ Adding new entry to truststore...");
        ViewUtil.iscNotification(getString(MAINTENANCE_SSLCONFIGURATION_SUCCESSFUL, new Date()), null, Notification.Type.TRAY_NOTIFICATION);
        this.x509TrustManager.addEntry(this.file);
        removeUploadedFile();
    }

    @Override
    public void uploadFailed(FailedEvent event) {
        log.error(new Label(new Date() + ": SSL certificate upload failed.").getValue());
        if (event.getFilename() == null || event.getFilename().isEmpty()) {
            log.warn("No upload certificate file specified");
            ViewUtil.iscNotification(getString(MAINTENANCE_SSLCONFIGURATION_NOFILE), Notification.Type.ERROR_MESSAGE);
            repaintUpload();
        } else if (event.getReason() instanceof UploadInterruptedException) {
            log.warn("SSL certificate upload is cancelled by the user");
        } else {
            log.warn("SSL certificate upload failed");
            ViewUtil.iscNotification(getString(MAINTENANCE_SSLCONFIGURATION_FAILED), Notification.Type.ERROR_MESSAGE);
            repaintUpload();
        }
        removeUploadedFile();
    }

    protected void removeUploadedFile() {
        if (this.file != null && this.file.exists()) {
            try {
                FileUtils.forceDelete(this.file);
            } catch (IOException e) {
                log.error("Deleting ssl certificate file: " + this.file + " failed when upload was cancelled by user.", e);
            }
        }
    }

    public interface UploadNotifier {

        void finishedUpload(boolean uploadStatus);
    }
}

17 Source : ApplianceUploader.java
with Apache License 2.0
from opensecuritycontroller

@SuppressWarnings("serial")
public clreplaced ApplianceUploader extends CustomComponent implements Receiver, FailedListener {

    static final String OVF_UPLOAD_PATH = "data" + File.separator + "ovf" + File.separator;

    private static final Logger log = LoggerFactory.getLogger(ApplianceUploader.clreplaced);

    private static int TEMP_FOLDER_COUNTER = 0;

    private final Upload upload;

    private File file;

    private final Panel panel = new Panel();

    private final VerticalLayout verLayout = new VerticalLayout();

    private String uploadPath;

    public ApplianceUploader() {
        this.upload = new Upload();
        this.upload.setButtonCaption(null);
        this.upload.setReceiver(this);
        this.upload.addFailedListener(this);
        this.upload.setImmediate(false);
        final UploadInfoWindow uploadInfoWindow = new UploadInfoWindow(this.upload);
        this.upload.addStartedListener(new StartedListener() {

            @Override
            public void uploadStarted(final StartedEvent event) {
                if (uploadInfoWindow.getParent() == null) {
                    ViewUtil.addWindow(uploadInfoWindow);
                }
            }
        });
        this.verLayout.setSpacing(true);
        this.verLayout.addStyleName(StyleConstants.COMPONENT_SPACING);
        this.verLayout.addComponent(this.upload);
        this.panel.setWidth("100%");
        this.panel.setContent(this.verLayout);
        setCompositionRoot(this.panel);
    }

    public Upload getUpload() {
        return this.upload;
    }

    @Override
    public OutputStream receiveUpload(String filename, String mimeType) {
        if (filename == null || filename.isEmpty()) {
            return null;
        }
        log.info("Start uploading file: " + filename);
        try {
            if (validateZipFile(filename)) {
                this.uploadPath = getUploadPath(true);
                File uploadDirectory = new File(this.uploadPath);
                if (!uploadDirectory.exists()) {
                    FileUtils.forceMkdir(uploadDirectory);
                }
                this.file = new File(this.uploadPath + filename);
                return new FileOutputStream(this.file);
            }
        } catch (Exception e) {
            log.error("Error opening file: " + filename, e);
            ViewUtil.iscNotification(VmidcMessages.getString(VmidcMessages_.UPLOAD_COMMON_ERROR) + e.getMessage(), Notification.Type.ERROR_MESSAGE);
        }
        return null;
    }

    public static String getImageFolderPath() {
        return getUploadPath(false);
    }

    public String getUploadPath() {
        return this.uploadPath;
    }

    /**
     * Gets the upload path for the file. If isTmpFolder is set to true, returns a unique temporary upload file path.
     * If it is set to false it returns the actual image upload folder.
     *
     * @param isTmpFolder
     *            the temporary path the file is uploaded before being moved to the actual image path
     *
     * @return the temporary path when isTmpFolder is set to true
     *         the image path when isTmpFolder is set to false
     */
    public static String getUploadPath(boolean isTmpFolder) {
        String uploadPath = "";
        uploadPath += OVF_UPLOAD_PATH;
        if (isTmpFolder) {
            uploadPath += "tmp" + TEMP_FOLDER_COUNTER++ + File.separator;
        }
        return uploadPath;
    }

    @Override
    public void uploadFailed(FailedEvent event) {
        if (event.getFilename() == null || event.getFilename().isEmpty()) {
            ViewUtil.iscNotification(VmidcMessages.getString(VmidcMessages_.UPLOAD_APPLIANCE_NOFILE), Notification.Type.ERROR_MESSAGE);
        } else if (event.getReason() instanceof UploadInterruptedException) {
            log.warn("Appliance Image upload is cancelled by the user");
        } else {
            ViewUtil.iscNotification(VmidcMessages.getString(VmidcMessages_.UPLOAD_APPLIANCE_FAILED), Notification.Type.ERROR_MESSAGE);
        }
        if (this.uploadPath != null) {
            File uploadDirectory = new File(this.uploadPath);
            if (uploadDirectory.exists()) {
                try {
                    FileUtils.deleteDirectory(uploadDirectory);
                } catch (IOException e) {
                    log.error("Deleting upload directory: " + this.uploadPath + " failed when upload was cancelled by user.", e);
                }
            }
        }
    }

    private boolean validateZipFile(String fileName) {
        return FilenameUtils.getExtension(fileName).equals("zip");
    }
}

16 Source : PluginUploader.java
with Apache License 2.0
from opensecuritycontroller

@SuppressWarnings("serial")
public clreplaced PluginUploader extends CustomComponent implements Receiver, FailedListener, SucceededListener {

    private static final Logger log = LoggerFactory.getLogger(PluginUploader.clreplaced);

    private static int TEMP_FOLDER_COUNTER = 0;

    private final Upload upload;

    private File file;

    private final Panel panel = new Panel();

    private final VerticalLayout verLayout = new VerticalLayout();

    private String uploadPath;

    private final ServerApi server;

    public interface UploadSucceededListener {

        void uploadComplete(String uploadPath);
    }

    private UploadSucceededListener uploadSucceededListener;

    public PluginUploader(ServerApi server) {
        this.server = server;
        this.upload = new Upload();
        this.upload.setButtonCaption("Upload");
        this.upload.setReceiver(this);
        this.upload.addFailedListener(this);
        this.upload.addSucceededListener(this);
        this.upload.setImmediate(false);
        final UploadInfoWindow uploadInfoWindow = new UploadInfoWindow(this.upload);
        this.upload.addStartedListener(new StartedListener() {

            @Override
            public void uploadStarted(final StartedEvent event) {
                if (uploadInfoWindow.getParent() == null) {
                    ViewUtil.addWindow(uploadInfoWindow);
                }
            }
        });
        this.verLayout.setSpacing(true);
        this.verLayout.addStyleName(StyleConstants.COMPONENT_SPACING);
        this.verLayout.addComponent(this.upload);
        this.panel.setWidth("100%");
        this.panel.setContent(this.verLayout);
        setCompositionRoot(this.panel);
    }

    @Override
    public OutputStream receiveUpload(String filename, String mimeType) {
        if (filename == null || filename.isEmpty()) {
            return null;
        }
        log.info("Start uploading file: " + filename);
        try {
            if (validateFileExtension(filename)) {
                this.uploadPath = getTemporaryUploadPath();
                File uploadDirectory = new File(this.uploadPath);
                if (!uploadDirectory.exists()) {
                    FileUtils.forceMkdir(uploadDirectory);
                }
                this.file = new File(this.uploadPath + filename);
                return new FileOutputStream(this.file);
            }
        } catch (Exception e) {
            log.error("Error opening file: " + filename, e);
            ViewUtil.iscNotification(VmidcMessages.getString(VmidcMessages_.UPLOAD_COMMON_ERROR) + e.getMessage(), Notification.Type.ERROR_MESSAGE);
        }
        return null;
    }

    /**
     * Gets the upload path for the file. If isTmpFolder is set to true, returns a unique temporary upload file path.
     * If it is set to false it returns the actual image upload folder.
     *
     * @param isTmpFolder
     *            the temporary path the file is uploaded before being moved to the actual image path
     *
     * @return the temporary path when isTmpFolder is set to true
     *         the image path when isTmpFolder is set to false
     */
    private static String getTemporaryUploadPath() {
        String uploadPath = "tmp" + TEMP_FOLDER_COUNTER++ + File.separator;
        return uploadPath;
    }

    @Override
    public void uploadSucceeded(SucceededEvent event) {
        log.info("Upload Successful! replacedyzing Uploaded Image.....");
        try {
            // Do the unzip only if there is enough disc space
            if (!this.server.isEnoughSpace()) {
                String message = VmidcMessages.getString(VmidcMessages_.UPLOAD_PLUGIN_NOSPACE);
                throw new VmidcException(message);
            }
            if (!validateFileExtension(this.file.getName())) {
                String message = VmidcMessages.getString(VmidcMessages_.UPLOAD_PLUGIN_FAILED);
                throw new VmidcException(message);
            }
            if (this.uploadSucceededListener != null) {
                this.uploadSucceededListener.uploadComplete(this.uploadPath);
            }
        } catch (Exception e) {
            log.error("Failed to unzip uploaded zip file", e);
            try {
                FileUtils.deleteDirectory(new File(this.uploadPath));
            } catch (IOException ex) {
                log.error("Failed to cleanup the tmp directory", ex);
            }
            this.uploadPath = null;
            ViewUtil.iscNotification(e.getMessage(), Notification.Type.ERROR_MESSAGE);
        }
    }

    @Override
    public void uploadFailed(FailedEvent event) {
        if (event.getFilename() == null || event.getFilename().isEmpty()) {
            String message = VmidcMessages.getString(VmidcMessages_.UPLOAD_PLUGIN_NOFILE);
            ViewUtil.iscNotification(message, Notification.Type.ERROR_MESSAGE);
        } else if (event.getReason() instanceof UploadInterruptedException) {
            log.warn(event.getFilename().toString() + " Plugin upload is cancelled by the user");
        } else {
            String message = VmidcMessages.getString(VmidcMessages_.UPLOAD_PLUGIN_FAILED);
            ViewUtil.iscNotification(message, Notification.Type.WARNING_MESSAGE);
        }
        if (this.uploadPath != null) {
            File uploadDirectory = new File(this.uploadPath);
            if (uploadDirectory.exists()) {
                try {
                    FileUtils.deleteDirectory(uploadDirectory);
                } catch (IOException e) {
                    log.error("Deleting upload directory: " + this.uploadPath + " failed when upload was cancelled by user.", e);
                }
            }
        }
    }

    private boolean validateFileExtension(String fileName) {
        String extension = FilenameUtils.getExtension(fileName);
        return extension.equals("bar");
    }

    public void addSucceededListener(UploadSucceededListener uploadSucceededListener) {
        this.uploadSucceededListener = uploadSucceededListener;
    }

    public String getUploadPath() {
        return this.uploadPath;
    }
}

16 Source : DbRestorer.java
with Apache License 2.0
from opensecuritycontroller

@SuppressWarnings("serial")
public clreplaced DbRestorer extends CustomComponent implements Receiver, FailedListener, SucceededListener {

    private static final Logger log = LoggerFactory.getLogger(DbRestorer.clreplaced);

    private final Upload upload;

    private File file;

    private final Panel panel = new Panel();

    private final VerticalLayout verLayout = new VerticalLayout();

    private final RestoreServiceApi restoreService;

    private final ServerApi server;

    private final ValidationApi validator;

    public DbRestorer(RestoreServiceApi restoreService, ServerApi server, ValidationApi validator) {
        this.restoreService = restoreService;
        this.server = server;
        this.validator = validator;
        this.upload = new Upload();
        this.upload.setButtonCaption(VmidcMessages.getString(VmidcMessages_.UPLOAD_RESTORE));
        this.upload.setReceiver(this);
        this.upload.addFailedListener(this);
        this.upload.addSucceededListener(this);
        this.upload.setImmediate(false);
        final UploadInfoWindow uploadInfoWindow = new UploadInfoWindow(this.upload);
        this.upload.addStartedListener(new StartedListener() {

            @Override
            public void uploadStarted(final StartedEvent event) {
                if (uploadInfoWindow.getParent() == null) {
                    ViewUtil.addWindow(uploadInfoWindow);
                }
            }
        });
        this.verLayout.setSpacing(true);
        this.panel.setWidth("100%");
        this.panel.setContent(this.verLayout);
        this.verLayout.addComponent(this.upload);
        this.verLayout.addStyleName(StyleConstants.COMPONENT_SPACING);
        setCompositionRoot(this.panel);
    }

    @Override
    public OutputStream receiveUpload(String filename, String mimeType) {
        if (filename == null || filename.isEmpty()) {
            return null;
        }
        // validate uploaded file is a zip file or encrypted backup file
        if (!this.restoreService.isValidBackupFilename(filename)) {
            ViewUtil.iscNotification(VmidcMessages.getString(VmidcMessages_.UPLOAD_RESTORE_INVALID_BACKUP, this.server.getProductName()), Notification.Type.WARNING_MESSAGE);
            return null;
        }
        log.info("Start uploading file: " + filename);
        try {
            this.file = new File(filename);
            return new FileOutputStream(this.file);
        } catch (final java.io.FileNotFoundException e) {
            log.error("Error opening file: " + filename, e);
            ViewUtil.iscNotification(VmidcMessages.getString(VmidcMessages_.UPLOAD_COMMON_ERROR) + e.getMessage(), Notification.Type.ERROR_MESSAGE);
        }
        return null;
    }

    @Override
    public void uploadSucceeded(SucceededEvent event) {
        log.info("Upload Successful! Restoring Database ......");
        try {
            PreplacedwordWindow.SubmitFormListener restoreAction = preplacedword -> {
                RestoreRequest req = new RestoreRequest();
                req.setBkpFile(this.file);
                req.setPreplacedword(preplacedword);
                try {
                    this.restoreService.dispatch(req);
                    ViewUtil.iscNotification("Upload", VmidcMessages.getString(VmidcMessages_.UPLOAD_RESTORE_UPLOAD_STARTED, this.server.getProductName()), Notification.Type.WARNING_MESSAGE);
                } catch (Exception e) {
                    log.error("Restore Service Failed.", e);
                    ViewUtil.iscNotification(e.getMessage(), Notification.Type.ERROR_MESSAGE);
                } finally {
                    // We can delete the uploaded file now since it has been extracted.
                    this.file.delete();
                }
            };
            if (this.restoreService.isValidEncryptedBackupFilename(event.getFilename())) {
                // ask user for decryption preplacedword first
                PreplacedwordWindow preplacedwordWindow = new PreplacedwordWindow(this.validator);
                preplacedwordWindow.setSubmitFormListener(restoreAction);
                ViewUtil.addWindow(preplacedwordWindow);
            } else if (this.restoreService.isValidZipBackupFilename(event.getFilename())) {
                // perform restore without decryption
                restoreAction.submit(null);
            }
        } catch (Exception e) {
            log.error("Restore Service Failed.", e);
            // ensure that uploaded file is deleted
            this.file.delete();
            ViewUtil.iscNotification(e.getMessage(), Notification.Type.ERROR_MESSAGE);
        }
    }

    @Override
    public void uploadFailed(FailedEvent event) {
        if (event.getFilename() == null || event.getFilename().isEmpty()) {
            ViewUtil.iscNotification(VmidcMessages.getString("upload.restore.nofile"), Notification.Type.ERROR_MESSAGE);
        } else if (event.getReason() instanceof UploadInterruptedException) {
            log.warn("Database backup Upload is cancelled by the user");
        } else {
            ViewUtil.iscNotification(VmidcMessages.getString("upload.restore.invalid.backup"), Notification.Type.WARNING_MESSAGE);
        }
    }
}