Search in sources :

Example 16 with URLDownload

use of org.jabref.logic.net.URLDownload in project jabref by JabRef.

the class DownloadExternalFile method download.

/**
     * Start a download.
     *
     * @param callback The object to which the filename should be reported when download
     *                 is complete.
     */
public void download(URL url, final DownloadCallback callback) throws IOException {
    String res = url.toString();
    String mimeType;
    // First of all, start the download itself in the background to a temporary file:
    final File tmp = File.createTempFile("jabref_download", "tmp");
    tmp.deleteOnExit();
    URLDownload udl = new URLDownload(url);
    try {
        // TODO: what if this takes long time?
        // TODO: stop editor dialog if this results in an error:
        // Read MIME type
        mimeType = udl.getMimeType();
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(frame, Localization.lang("Invalid URL") + ": " + ex.getMessage(), Localization.lang("Download file"), JOptionPane.ERROR_MESSAGE);
        LOGGER.info("Error while downloading " + "'" + res + "'", ex);
        return;
    }
    final URL urlF = url;
    final URLDownload udlF = udl;
    JabRefExecutorService.INSTANCE.execute(() -> {
        try {
            udlF.toFile(tmp.toPath());
        } catch (IOException e2) {
            dontShowDialog = true;
            if ((editor != null) && editor.isVisible()) {
                editor.setVisible(false, false);
            }
            JOptionPane.showMessageDialog(frame, Localization.lang("Invalid URL") + ": " + e2.getMessage(), Localization.lang("Download file"), JOptionPane.ERROR_MESSAGE);
            LOGGER.info("Error while downloading " + "'" + urlF + "'", e2);
            return;
        }
        // Download finished: call the method that stops the progress bar etc.:
        SwingUtilities.invokeLater(DownloadExternalFile.this::downloadFinished);
    });
    Optional<ExternalFileType> suggestedType = Optional.empty();
    if (mimeType != null) {
        LOGGER.debug("MIME Type suggested: " + mimeType);
        suggestedType = ExternalFileTypes.getInstance().getExternalFileTypeByMimeType(mimeType);
    }
    // Then, while the download is proceeding, let the user choose the details of the file:
    String suffix;
    if (suggestedType.isPresent()) {
        suffix = suggestedType.get().getExtension();
    } else {
        // If we did not find a file type from the MIME type, try based on extension:
        suffix = getSuffix(res);
        if (suffix == null) {
            suffix = "";
        }
        suggestedType = ExternalFileTypes.getInstance().getExternalFileTypeByExt(suffix);
    }
    String suggestedName = getSuggestedFileName(suffix);
    List<String> fDirectory = databaseContext.getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
    String directory;
    if (fDirectory.isEmpty()) {
        directory = null;
    } else {
        directory = fDirectory.get(0);
    }
    final String suggestDir = directory == null ? System.getProperty("user.home") : directory;
    File file = new File(new File(suggestDir), suggestedName);
    FileListEntry fileListEntry = new FileListEntry("", file.getCanonicalPath(), suggestedType);
    editor = new FileListEntryEditor(frame, fileListEntry, true, false, databaseContext, true);
    editor.getProgressBar().setIndeterminate(true);
    editor.setOkEnabled(false);
    editor.setExternalConfirm(closeEntry -> {
        File f = directory == null ? new File(closeEntry.getLink()) : expandFilename(directory, closeEntry.getLink());
        if (f.isDirectory()) {
            JOptionPane.showMessageDialog(frame, Localization.lang("Target file cannot be a directory."), Localization.lang("Download file"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if (f.exists()) {
            return JOptionPane.showConfirmDialog(frame, Localization.lang("'%0' exists. Overwrite file?", f.getName()), Localization.lang("Download file"), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION;
        } else {
            return true;
        }
    });
    if (dontShowDialog) {
        return;
    } else {
        editor.setVisible(true, false);
    }
    // Editor closed. Go on:
    if (editor.okPressed()) {
        File toFile = directory == null ? new File(fileListEntry.getLink()) : expandFilename(directory, fileListEntry.getLink());
        String dirPrefix;
        if (directory == null) {
            dirPrefix = null;
        } else {
            if (directory.endsWith(OS.FILE_SEPARATOR)) {
                dirPrefix = directory;
            } else {
                dirPrefix = directory + OS.FILE_SEPARATOR;
            }
        }
        boolean success = FileUtil.copyFile(Paths.get(tmp.toURI()), Paths.get(toFile.toURI()), true);
        if (!success) {
            // OOps, the file exists!
            LOGGER.error("File already exists! DownloadExternalFile.download()");
        }
        // path to relative:
        if ((dirPrefix != null) && fileListEntry.getLink().startsWith(directory) && (fileListEntry.getLink().length() > dirPrefix.length())) {
            fileListEntry = new FileListEntry(fileListEntry.getDescription(), fileListEntry.getLink().substring(dirPrefix.length()), fileListEntry.getType());
        }
        callback.downloadComplete(fileListEntry);
        if (!tmp.delete()) {
            LOGGER.info("Cannot delete temporary file");
        }
    } else {
        // Canceled. Just delete the temp file:
        if (downloadFinished && !tmp.delete()) {
            LOGGER.info("Cannot delete temporary file");
        }
    }
}
Also used : ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) FileListEntryEditor(org.jabref.gui.filelist.FileListEntryEditor) FileListEntry(org.jabref.gui.filelist.FileListEntry) IOException(java.io.IOException) URLDownload(org.jabref.logic.net.URLDownload) File(java.io.File) URL(java.net.URL)

Example 17 with URLDownload

use of org.jabref.logic.net.URLDownload in project jabref by JabRef.

the class FileDownloadTask method call.

@Override
protected Void call() throws Exception {
    URLDownload download = new URLDownload(source);
    try (ProgressInputStream inputStream = download.asInputStream()) {
        EasyBind.subscribe(inputStream.totalNumBytesReadProperty(), bytesRead -> updateProgress(bytesRead.longValue(), inputStream.getMaxNumBytes()));
        Files.copy(inputStream, destination, StandardCopyOption.REPLACE_EXISTING);
    }
    return null;
}
Also used : ProgressInputStream(org.jabref.logic.net.ProgressInputStream) URLDownload(org.jabref.logic.net.URLDownload)

Example 18 with URLDownload

use of org.jabref.logic.net.URLDownload in project jabref by JabRef.

the class LinkedFilesEditorViewModel method addFromURL.

private void addFromURL(URL url) {
    URLDownload urlDownload = new URLDownload(url);
    Optional<ExternalFileType> suggestedType = inferFileType(urlDownload);
    String suggestedTypeName = suggestedType.map(ExternalFileType::getName).orElse("");
    List<Path> fileDirectories = databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
    Path destination = constructSuggestedPath(suggestedType, fileDirectories);
    LinkedFileViewModel temporaryDownloadFile = new LinkedFileViewModel(new LinkedFile("", url, suggestedTypeName));
    files.add(temporaryDownloadFile);
    FileDownloadTask downloadTask = new FileDownloadTask(url, destination);
    temporaryDownloadFile.downloadProgressProperty().bind(downloadTask.progressProperty());
    downloadTask.setOnSucceeded(event -> {
        files.remove(temporaryDownloadFile);
        LinkedFile newLinkedFile = fromFile(destination, fileDirectories);
        files.add(new LinkedFileViewModel(newLinkedFile));
    });
    downloadTask.setOnFailed(event -> dialogService.showErrorDialogAndWait("", downloadTask.getException()));
    taskExecutor.execute(downloadTask);
}
Also used : UnknownExternalFileType(org.jabref.gui.externalfiletype.UnknownExternalFileType) ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) Path(java.nio.file.Path) LinkedFile(org.jabref.model.entry.LinkedFile) FileDownloadTask(org.jabref.gui.externalfiles.FileDownloadTask) URLDownload(org.jabref.logic.net.URLDownload)

Example 19 with URLDownload

use of org.jabref.logic.net.URLDownload in project jabref by JabRef.

the class IEEEXploreFetcher method processQuery.

@Override
public boolean processQuery(String query, ImportInspector dialog, OutputPrinter status) {
    //IEEE API seems to use .QT. as a marker for the quotes for exact phrase searching
    String terms = query.replaceAll("\"", "\\.QT\\.");
    shouldContinue = true;
    int parsed = 0;
    int pageNumber = 1;
    String postData = makeSearchPostRequestPayload(pageNumber, terms);
    try {
        //open the search URL
        URLDownload dl = new URLDownload(IEEEXploreFetcher.URL_SEARCH);
        //add request header
        dl.addHeader("Accept", "application/json");
        dl.addHeader("Content-Type", "application/json");
        dl.addHeader("Referer", "http://ieeexplore.ieee.org/search/searchresult.jsp");
        // set post data
        dl.setPostData(postData);
        //retrieve the search results
        String page = dl.asString();
        //the page can be blank if the search did not work (not sure the exact conditions that lead to this, but declaring it an invalid search for now)
        if (page.isEmpty()) {
            status.showMessage(Localization.lang("You have entered an invalid search '%0'.", query), DIALOG_TITLE, JOptionPane.INFORMATION_MESSAGE);
            return false;
        }
        //parses the JSON data returned by the query
        //TODO: a faster way would be to parse the JSON tokens one at a time just to extract the article number, but this seems to be fast enough...
        JSONObject searchResultsJson = new JSONObject(page);
        int hits = searchResultsJson.getInt("totalRecords");
        //if no search results were found
        if (hits == 0) {
            status.showMessage(Localization.lang("No entries found for the search string '%0'", query), DIALOG_TITLE, JOptionPane.INFORMATION_MESSAGE);
            return false;
        }
        //if max hits were exceeded, display the warning
        if (hits > IEEEXploreFetcher.MAX_FETCH) {
            status.showMessage(Localization.lang("%0 entries found. To reduce server load, only %1 will be downloaded.", String.valueOf(hits), String.valueOf(IEEEXploreFetcher.MAX_FETCH)), DIALOG_TITLE, JOptionPane.INFORMATION_MESSAGE);
        }
        //fetch the raw Bibtex results from IEEEXplore
        String bibtexPage = new URLDownload(createBibtexQueryURL(searchResultsJson)).asString(Globals.prefs.getDefaultEncoding());
        //preprocess the result (eg. convert HTML escaped characters to latex and do other formatting not performed by BibtexParser)
        bibtexPage = preprocessBibtexResultsPage(bibtexPage);
        //parse the page into Bibtex entries
        Collection<BibEntry> parsedBibtexCollection = new BibtexParser(Globals.prefs.getImportFormatPreferences()).parseEntries(bibtexPage);
        int nEntries = parsedBibtexCollection.size();
        Iterator<BibEntry> parsedBibtexCollectionIterator = parsedBibtexCollection.iterator();
        while (parsedBibtexCollectionIterator.hasNext() && shouldContinue) {
            dialog.addEntry(cleanup(parsedBibtexCollectionIterator.next()));
            dialog.setProgress(parsed, nEntries);
            parsed++;
        }
        return true;
    } catch (ParseException | IOException | JSONException e) {
        LOGGER.error("Error while fetching from " + getTitle(), e);
        ((ImportInspectionDialog) dialog).showErrorMessage(this.getTitle(), e.getLocalizedMessage());
    }
    return false;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) JSONObject(org.json.JSONObject) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) JSONException(org.json.JSONException) ParseException(org.jabref.logic.importer.ParseException) IOException(java.io.IOException) URLDownload(org.jabref.logic.net.URLDownload)

Aggregations

URLDownload (org.jabref.logic.net.URLDownload)19 IOException (java.io.IOException)11 URL (java.net.URL)7 BibEntry (org.jabref.model.entry.BibEntry)7 Matcher (java.util.regex.Matcher)5 ParseException (org.jabref.logic.importer.ParseException)4 FetcherException (org.jabref.logic.importer.FetcherException)3 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)3 DOI (org.jabref.model.entry.identifier.DOI)3 File (java.io.File)2 Path (java.nio.file.Path)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 JLabel (javax.swing.JLabel)2 ExternalFileType (org.jabref.gui.externalfiletype.ExternalFileType)2 OutputPrinter (org.jabref.logic.importer.OutputPrinter)2 ParserResult (org.jabref.logic.importer.ParserResult)2 Dimension (java.awt.Dimension)1 GridLayout (java.awt.GridLayout)1