Search in sources :

Example 1 with DownloadJob

use of org.eclipse.linuxtools.rpm.core.utils.DownloadJob in project linuxtools by eclipse.

the class SourcesFileDownloadHyperlink method open.

/**
 * Tries to open the given file name looking for it in the current directory
 * and in ../SOURCES.
 *
 * @see org.eclipse.jface.text.hyperlink.IHyperlink#open()
 */
@Override
public void open() {
    IContainer container = original.getParent();
    IResource saveFolder = container.getParent().findMember(IRPMConstants.SOURCES_FOLDER);
    if (saveFolder == null) {
        saveFolder = container.findMember(original.getFullPath().removeLastSegments(1));
    }
    if (saveFolder == null) {
        // $NON-NLS-1$
        saveFolder = container.findMember("/");
    }
    try {
        URL url = new URL(fileName);
        URLConnection connection = url.openConnection();
        String savedFileName = fileName.substring(fileName.lastIndexOf('/') + 1);
        IFile savedFile = original.getProject().getFile(saveFolder.getProjectRelativePath().append(savedFileName));
        if (savedFile.exists()) {
            MessageBox mb = new MessageBox(Display.getCurrent().getActiveShell(), SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
            mb.setText(Messages.SourcesFileDownloadHyperlink_0);
            mb.setMessage(NLS.bind(Messages.SourcesFileDownloadHyperlink_1, savedFileName));
            int rc = mb.open();
            if (rc == SWT.OK) {
                new DownloadJob(savedFile, connection).schedule();
            }
        } else {
            new DownloadJob(savedFile, connection).schedule();
        }
    } catch (IOException e) {
        MessageBox mb = new MessageBox(Display.getCurrent().getActiveShell(), SWT.ICON_WARNING | SWT.OK);
        mb.setMessage(Messages.SourcesFileDownloadHyperlink_2);
        mb.setText(Messages.SourcesFileDownloadHyperlink_3);
        mb.open();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IOException(java.io.IOException) IContainer(org.eclipse.core.resources.IContainer) DownloadJob(org.eclipse.linuxtools.rpm.core.utils.DownloadJob) IResource(org.eclipse.core.resources.IResource) URL(java.net.URL) URLConnection(java.net.URLConnection) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 2 with DownloadJob

use of org.eclipse.linuxtools.rpm.core.utils.DownloadJob in project linuxtools by eclipse.

the class DownloadPrepareSourcesTest method downloadFile.

/**
 * Download and also test if the file was downloaded correctly
 *
 * @param project The RPM project
 * @throws IOException
 * @throws InterruptedException
 */
public void downloadFile(RPMProject project) throws IOException, InterruptedException {
    // connect to the URL
    URL url = new URL("http://ftp.gnu.org/gnu/hello/hello-2.8.tar.gz");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    assertEquals(connection.getResponseCode(), HttpURLConnection.HTTP_OK);
    // download the file
    String filename = "hello-2.8.tar.gz";
    IFile file = project.getConfiguration().getSourcesFolder().getFile(new Path(filename));
    Job downloadJob = new DownloadJob(file, connection);
    downloadJob.setUser(true);
    downloadJob.schedule();
    downloadJob.join();
    assertEquals(downloadJob.getResult(), Status.OK_STATUS);
}
Also used : Path(org.eclipse.core.runtime.Path) HttpURLConnection(java.net.HttpURLConnection) IFile(org.eclipse.core.resources.IFile) DownloadJob(org.eclipse.linuxtools.rpm.core.utils.DownloadJob) Job(org.eclipse.core.runtime.jobs.Job) DownloadJob(org.eclipse.linuxtools.rpm.core.utils.DownloadJob) URL(java.net.URL)

Example 3 with DownloadJob

use of org.eclipse.linuxtools.rpm.core.utils.DownloadJob in project linuxtools by eclipse.

the class SpecfileEditorDownloadSourcesActionDelegate method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final Shell shell = HandlerUtil.getActiveShellChecked(event);
    final SpecfileParser specparser = new SpecfileParser();
    final IResource resource = RPMHandlerUtils.getResource(event);
    final RPMProject rpj = RPMHandlerUtils.getRPMProject(resource);
    final IFile workFile = (IFile) rpj.getSpecFile();
    final Specfile specfile = workFile != null ? specparser.parse(workFile) : null;
    // retrieve source(s) from specfile
    final List<SpecfileSource> sourceURLList = specfile != null ? (List<SpecfileSource>) specfile.getSources() : null;
    // currently stops immediately once an invalid source URL is encountered
    for (final SpecfileSource sourceurls : sourceURLList) {
        try {
            String rawURL = sourceurls.getFileName();
            String resolvedURL = UiUtils.resolveDefines(specfile, rawURL);
            URL url = null;
            try {
                url = new URL(resolvedURL);
            } catch (MalformedURLException e) {
                SpecfileLog.logError(NLS.bind(Messages.DownloadSources_malformedURL, resolvedURL), e);
                // $NON-NLS-1$
                RPMUtils.showErrorDialog(// $NON-NLS-1$
                shell, // $NON-NLS-1$
                "Error", NLS.bind(Messages.DownloadSources_malformedURL, resolvedURL));
                return null;
            }
            URLConnection connection = url.openConnection();
            if (!(connection instanceof HttpURLConnection) || ((HttpURLConnection) connection).getResponseCode() != HttpURLConnection.HTTP_NOT_FOUND) {
                connection.connect();
                // grab the name of the file from the URL
                // $NON-NLS-1$
                int offset = url.toString().lastIndexOf("/");
                String filename = url.toString().substring(offset + 1);
                // create the path to the "to be downloaded" file
                IFile file = rpj.getConfiguration().getSourcesFolder().getFile(new Path(filename));
                Job downloadJob = new DownloadJob(file, connection);
                downloadJob.setUser(true);
                downloadJob.schedule();
            }
        } catch (IOException e) {
            SpecfileLog.logError(Messages.DownloadSources_cannotConnectToURL, e);
            // $NON-NLS-1$
            RPMUtils.showErrorDialog(// $NON-NLS-1$
            shell, // $NON-NLS-1$
            "Error", Messages.DownloadSources_cannotConnectToURL);
            return null;
        }
    }
    return null;
}
Also used : Specfile(org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile) Path(org.eclipse.core.runtime.Path) MalformedURLException(java.net.MalformedURLException) IFile(org.eclipse.core.resources.IFile) RPMProject(org.eclipse.linuxtools.rpm.core.RPMProject) IOException(java.io.IOException) SpecfileParser(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser) DownloadJob(org.eclipse.linuxtools.rpm.core.utils.DownloadJob) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) Shell(org.eclipse.swt.widgets.Shell) SpecfileSource(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource) HttpURLConnection(java.net.HttpURLConnection) DownloadJob(org.eclipse.linuxtools.rpm.core.utils.DownloadJob) Job(org.eclipse.core.runtime.jobs.Job) IResource(org.eclipse.core.resources.IResource)

Example 4 with DownloadJob

use of org.eclipse.linuxtools.rpm.core.utils.DownloadJob in project linuxtools by eclipse.

the class SpecfileEditorPrepareSourcesActionDelegate method downloadFile.

public boolean downloadFile(Shell shell, RPMProject rpj, Specfile specfile) {
    // retrieve source(s) from specfile
    final List<SpecfileSource> sourceURLList = specfile != null ? specfile.getSources() : null;
    for (final SpecfileSource sourceurls : sourceURLList) {
        try {
            String resolvedURL = UiUtils.resolveDefines(specfile, sourceurls.getFileName());
            URL url = null;
            try {
                url = new URL(resolvedURL);
            } catch (MalformedURLException e) {
                SpecfileLog.logError(NLS.bind(Messages.PrepareSources_downloadSourcesMalformedURL, resolvedURL), e);
                RPMUtils.showErrorDialog(shell, Messages.PrepareSources_error, NLS.bind(Messages.PrepareSources_downloadSourcesMalformedURL, resolvedURL));
                return false;
            }
            URLConnection connection = url.openConnection();
            if (!(connection instanceof HttpURLConnection) || ((HttpURLConnection) connection).getResponseCode() != HttpURLConnection.HTTP_NOT_FOUND) {
                connection.connect();
                // grab the name of the file from the URL
                // $NON-NLS-1$
                int offset = url.toString().lastIndexOf("/");
                String filename = url.toString().substring(offset + 1);
                // create the path to the "to be downloaded" file
                IFile file = rpj.getConfiguration().getSourcesFolder().getFile(new Path(filename));
                Job downloadJob = new DownloadJob(file, connection);
                downloadJob.setUser(true);
                downloadJob.schedule();
                try {
                    downloadJob.join();
                } catch (InterruptedException e1) {
                    return false;
                }
                if (!downloadJob.getResult().isOK()) {
                    return false;
                }
            }
        } catch (OperationCanceledException e) {
            SpecfileLog.logError(Messages.PrepareSources_downloadCancelled, e);
            RPMUtils.showErrorDialog(shell, Messages.PrepareSources_error, Messages.PrepareSources_downloadCancelled);
            return false;
        } catch (IOException e) {
            SpecfileLog.logError(Messages.PrepareSources_downloadConnectionFail, e);
            RPMUtils.showErrorDialog(shell, Messages.PrepareSources_error, Messages.PrepareSources_downloadConnectionFail);
            return false;
        }
    }
    return true;
}
Also used : Path(org.eclipse.core.runtime.Path) MalformedURLException(java.net.MalformedURLException) IFile(org.eclipse.core.resources.IFile) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IOException(java.io.IOException) DownloadJob(org.eclipse.linuxtools.rpm.core.utils.DownloadJob) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) SpecfileSource(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource) HttpURLConnection(java.net.HttpURLConnection) DownloadJob(org.eclipse.linuxtools.rpm.core.utils.DownloadJob) Job(org.eclipse.core.runtime.jobs.Job)

Aggregations

URL (java.net.URL)4 IFile (org.eclipse.core.resources.IFile)4 DownloadJob (org.eclipse.linuxtools.rpm.core.utils.DownloadJob)4 IOException (java.io.IOException)3 HttpURLConnection (java.net.HttpURLConnection)3 URLConnection (java.net.URLConnection)3 Path (org.eclipse.core.runtime.Path)3 Job (org.eclipse.core.runtime.jobs.Job)3 MalformedURLException (java.net.MalformedURLException)2 IResource (org.eclipse.core.resources.IResource)2 SpecfileSource (org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource)2 IContainer (org.eclipse.core.resources.IContainer)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 RPMProject (org.eclipse.linuxtools.rpm.core.RPMProject)1 Specfile (org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile)1 SpecfileParser (org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser)1 MessageBox (org.eclipse.swt.widgets.MessageBox)1 Shell (org.eclipse.swt.widgets.Shell)1