<commit_before>/*
 * Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.

 * Author: <NAME>
 * Created: 20.11.2009 16:02:58
 *
 * $Id$
 */
package com.haulmont.cuba.web.filestorage;

import com.haulmont.cuba.core.app.FileStorageService;
import com.haulmont.cuba.core.entity.FileDescriptor;
import com.haulmont.cuba.core.global.FileStorageException;
import com.haulmont.cuba.core.global.FileTypesHelper;
import com.haulmont.cuba.gui.ServiceLocator;
import com.vaadin.terminal.DownloadStream;

import java.io.*;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.commons.io.FileUtils;

public class FileDownloadWindow extends FileWindow {

    public FileDownloadWindow(String windowName, FileDescriptor fd) {
        super(windowName, fd);
    }

    public FileDownloadWindow(String windowName, File f) {
        super(windowName, f);
    }

    @Override
    public DownloadStream handleURI(URL context, String relativeUri) {
        DownloadStream downloadStream = new FileDownloadStream(new ByteArrayInputStream(getFileData()),
                FileTypesHelper.getMIMEType("." + getExtension()), getFileName());

        return downloadStream;
    }

    protected String getFileName() {
        if (fd != null) {
            try {
                return URLEncoder.encode(fd.getName(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        } else if (f != null) {
            return f.getName();
        } else {
            throw new RuntimeException("file descriptor and file are null");
        }
    }

    protected byte[] getFileData() {
        if (fd != null) {
            FileStorageService fss = ServiceLocator.lookup(FileStorageService.JNDI_NAME);
            try {
                return fss.loadFile(fd);
            } catch (FileStorageException e) {
                log.error("Unable to download file", e);
                throw new RuntimeException(e);
            }
        } else if (f != null){
            try {
                return FileUtils.readFileToByteArray(f);
            } catch (IOException e) {
                log.error("Unable to download file", e);
                throw new RuntimeException(e);
            }
        } else {
            throw new RuntimeException("file descriptor and file are null");
        }
    }

    protected String getExtension() {
        if (fd != null) {
            return fd.getExtension();
        } else if (f != null){
            int i = f.getName().lastIndexOf(".");
            return i > 0 ? f.getName().substring(i, f.getName().length()) : "txt";
        } else {
            throw new RuntimeException("file descriptor and file are null");
        }
    }

    public class FileDownloadStream extends DownloadStream implements Closeable {
        public FileDownloadStream(InputStream stream, String contentType, String fileName) {
            super(stream, contentType, fileName);
        }

        public void close() throws IOException {
        }
    }
}<commit_msg>Fix (Исправлено окно загрузки файлов с сервера)<commit_after>/*
 * Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.

 * Author: <NAME>
 * Created: 20.11.2009 16:02:58
 *
 * $Id$
 */
package com.haulmont.cuba.web.filestorage;

import com.haulmont.cuba.core.entity.FileDescriptor;
import com.haulmont.cuba.web.App;
import com.haulmont.cuba.web.app.FileDownloadHelper;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.FileResource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.VerticalLayout;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

public class FileDownloadWindow extends FileWindow {

    public FileDownloadWindow(String windowName, FileDescriptor fd) {
        super(windowName, fd);
        initUI();
    }

    public FileDownloadWindow(String windowName, File f) {
        super(windowName, f);
        initUI();
    }

    @Override
    protected void close() {
        //Hack the situation when a user are refreshing the window
    }

    private void initUI() {
        final VerticalLayout mainLayout = new VerticalLayout();
        mainLayout.setSizeFull();
        mainLayout.setMargin(false);
        mainLayout.setSpacing(false);

        final Embedded embedded = new Embedded();
        embedded.setSizeFull();
        if (fd != null) {
            embedded.setSource(new ExternalResource(createURL()));
        } else if (f != null) {
            embedded.setSource(new FileResource(f, App.getInstance()));
        } else {
            throw new RuntimeException("there is no resourse for display");
        }
        embedded.setType(Embedded.TYPE_BROWSER);
        mainLayout.addComponent(embedded);

        setContent(mainLayout);
    }

    protected URL createURL() {
        try {
            App app = App.getInstance();
            return new URL(app.getURL() + FileDownloadHelper.makeUrl(fd, true));
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }
}
{'fraction_non_alphanumeric': 0.10291040517405364, 'fraction_numerical': 0.007228457295035191, 'mean_word_length': 2.4960106382978724, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6274167', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1419, 'n_words': 367}