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();
}
}
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);
}
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;
}
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;
}
Aggregations