From c327a3655e58a6cd7d9c077244a0a5bdf66d218a Mon Sep 17 00:00:00 2001 From: Christoph Date: Fri, 19 Aug 2016 21:02:22 +0200 Subject: [PATCH] Fix arxiv fetcher not working correctly (#1780) * Fix #1776, remove Cookie Handler implementation, created problems with arxiv UrlDl works without or own Cookie class, too * Fix emptyLine --- .../jabref/logic/importer/fetcher/ArXiv.java | 8 +- .../jabref/logic/net/CookieHandlerImpl.java | 122 ------------------ .../net/sf/jabref/logic/net/URLDownload.java | 13 -- 3 files changed, 5 insertions(+), 138 deletions(-) delete mode 100644 src/main/java/net/sf/jabref/logic/net/CookieHandlerImpl.java diff --git a/src/main/java/net/sf/jabref/logic/importer/fetcher/ArXiv.java b/src/main/java/net/sf/jabref/logic/importer/fetcher/ArXiv.java index 0f6f45bfc468..20f4780b512e 100644 --- a/src/main/java/net/sf/jabref/logic/importer/fetcher/ArXiv.java +++ b/src/main/java/net/sf/jabref/logic/importer/fetcher/ArXiv.java @@ -71,9 +71,9 @@ public class ArXiv implements FulltextFetcher, SearchBasedFetcher, IdBasedFetcher { private static final Log LOGGER = LogFactory.getLog(ArXiv.class); - private static final String API_URL = "http://export.arxiv.org/api/query"; + @Override public Optional findFullText(BibEntry entry) throws IOException { Objects.requireNonNull(entry); @@ -234,6 +234,7 @@ public Optional performSearchById(String identifier) throws FetcherExc return searchForEntryById(identifier).map(ArXivEntry::toBibEntry); } + private static class ArXivEntry { private final Optional title; @@ -247,6 +248,7 @@ private static class ArXivEntry { private final Optional journalReferenceText; private final Optional primaryCategory; + public ArXivEntry(Node item) { // see http://arxiv.org/help/api/user-manual#_details_of_atom_results_returned @@ -302,8 +304,8 @@ public ArXivEntry(Node item) { // Primary category // Ex: - primaryCategory = XMLUtil.getNode(item, "arxiv:primary_category").flatMap( - node -> XMLUtil.getAttributeContent(node, "term")); + primaryCategory = XMLUtil.getNode(item, "arxiv:primary_category") + .flatMap(node -> XMLUtil.getAttributeContent(node, "term")); } /** diff --git a/src/main/java/net/sf/jabref/logic/net/CookieHandlerImpl.java b/src/main/java/net/sf/jabref/logic/net/CookieHandlerImpl.java deleted file mode 100644 index 766d7583abf4..000000000000 --- a/src/main/java/net/sf/jabref/logic/net/CookieHandlerImpl.java +++ /dev/null @@ -1,122 +0,0 @@ -/* Copyright (C) 2003-2015 JabRef contributors. - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -*/ -package net.sf.jabref.logic.net; - -import java.io.IOException; -import java.net.CookieHandler; -import java.net.URI; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -/** - * - */ -public class CookieHandlerImpl extends CookieHandler { - - // "Long" term storage for cookies, not serialized so only - // for current JVM instance - private final List cache = new LinkedList<>(); - - - /** - * Saves all applicable cookies present in the response - * headers into cache. - * - * @param uri URI source of cookies - * @param responseHeaders Immutable map from field names to - * lists of field - * values representing the response header fields returned - */ - - @Override - public void put( - URI uri, - Map> responseHeaders) - throws IOException { - - List setCookieList = - responseHeaders.get("Set-Cookie"); - if (setCookieList != null) { - for (String item : setCookieList) { - Cookie cookie = new Cookie(uri, item); - // Remove cookie if it already exists - // New one will replace - for (Iterator i = cache.iterator(); i.hasNext(); ) { - Cookie existingCookie = i.next(); - if (cookie.equalNameAndDomain(existingCookie)) { - i.remove(); - break; - } - } - - cache.add(cookie); - } - } - } - - /** - * Gets all the applicable cookies from a cookie cache for - * the specified uri in the request header. - * - * @param uri URI to send cookies to in a request - * @param requestHeaders Map from request header field names - * to lists of field values representing the current request - * headers - * @return Immutable map, with field name "Cookie" to a list - * of cookies - */ - - @Override - public Map> get( - URI uri, - Map> requestHeaders) - throws IOException { - - // Retrieve all the cookies for matching URI - // Put in comma-separated list - StringBuilder cookies = new StringBuilder(); - for (Iterator i = cache.iterator(); i.hasNext(); ) { - //for (Cookie cookie : cache) { - Cookie cookie = i.next(); - // Remove cookies that have expired - if (cookie.hasExpired()) { - i.remove(); - } else if (cookie.matches(uri)) { - if (cookies.length() > 0) { - cookies.append(", "); - } - cookies.append(cookie); - } - } - - // Map to return - Map> cookieMap = - new HashMap<>(requestHeaders); - - // Convert StringBuilder to List, store in map - if (cookies.length() > 0) { - List list = - Collections.singletonList(cookies.toString()); - cookieMap.put("Cookie", list); - - } - return Collections.unmodifiableMap(cookieMap); - } -} diff --git a/src/main/java/net/sf/jabref/logic/net/URLDownload.java b/src/main/java/net/sf/jabref/logic/net/URLDownload.java index e942ebca67b8..3918452ab46b 100644 --- a/src/main/java/net/sf/jabref/logic/net/URLDownload.java +++ b/src/main/java/net/sf/jabref/logic/net/URLDownload.java @@ -28,7 +28,6 @@ import java.io.Reader; import java.io.StringWriter; import java.io.Writer; -import java.net.CookieHandler; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; @@ -79,24 +78,12 @@ public URLDownload(URL source) { addParameters("User-Agent", "JabRef"); - URLDownload.setCookieHandler(); } public URL getSource() { return source; } - private static void setCookieHandler() { - try { - // This should set up JabRef to receive cookies properly - if (CookieHandler.getDefault() == null) { - CookieHandler.setDefault(new CookieHandlerImpl()); - } - } catch (SecurityException ignored) { - // Setting or getting the system default cookie handler is forbidden - // In this case cookie handling is not possible. - } - } public String determineMimeType() throws IOException { // this does not cause a real performance issue as the underlying HTTP/TCP connection is reused