Search in sources :

Example 16 with RPMProject

use of org.eclipse.linuxtools.rpm.core.RPMProject 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)

Aggregations

RPMProject (org.eclipse.linuxtools.rpm.core.RPMProject)16 Test (org.junit.Test)11 IFile (org.eclipse.core.resources.IFile)9 Path (org.eclipse.core.runtime.Path)9 IProject (org.eclipse.core.resources.IProject)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Ignore (org.junit.Ignore)5 URL (java.net.URL)4 IResource (org.eclipse.core.resources.IResource)4 File (java.io.File)3 IStatus (org.eclipse.core.runtime.IStatus)3 CoreException (org.eclipse.core.runtime.CoreException)2 Job (org.eclipse.core.runtime.jobs.Job)2 DownloadJob (org.eclipse.linuxtools.rpm.core.utils.DownloadJob)2 Specfile (org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile)2 SpecfileParser (org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser)2 Shell (org.eclipse.swt.widgets.Shell)2 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1