Some more httpclient fixes

This commit is contained in:
Mark H. Wood
2013-12-19 12:53:22 -05:00
parent 24d3e415c0
commit aa209d1508
12 changed files with 207 additions and 178 deletions

View File

@@ -7,7 +7,11 @@
*/
package org.dspace.discovery;
import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.SQLException;
@@ -31,8 +35,7 @@ import java.util.Vector;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
@@ -42,6 +45,7 @@ import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
@@ -1756,9 +1760,9 @@ public class SolrServiceImpl implements SearchService, IndexingService {
urlBuilder.append(solrQuery.toString());
try {
GetMethod get = new GetMethod(urlBuilder.toString());
new HttpClient().executeMethod(get);
return get.getResponseBodyAsStream();
HttpGet get = new HttpGet(urlBuilder.toString());
HttpResponse response = new DefaultHttpClient().execute(get);
return response.getEntity().getContent();
} catch (Exception e)
{

View File

@@ -13,13 +13,20 @@ import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import java.io.*;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
@@ -49,13 +56,6 @@ import org.dspace.statistics.util.LocationUtils;
import org.dspace.statistics.util.SpiderDetector;
import org.dspace.usage.UsageWorkflowEvent;
import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Static holder for a HttpSolrClient connection pool to issue
* usage logging events to Solr from DSpace libraries, and some static query
@@ -1322,9 +1322,9 @@ public class SolrLogger
String solrRequestUrl = solr.getBaseURL() + "/select";
solrRequestUrl = generateURL(solrRequestUrl, yearQueryParams);
GetMethod get = new GetMethod(solrRequestUrl);
new HttpClient().executeMethod(get);
InputStream csvInputstream = get.getResponseBodyAsStream();
HttpGet get = new HttpGet(solrRequestUrl);
HttpResponse response = new DefaultHttpClient().execute(get);
InputStream csvInputstream = response.getEntity().getContent();
//Write the csv ouput to a file !
File csvFile = new File(tempDirectory.getPath() + File.separatorChar + "temp." + dcStart.getYear() + "." + i + ".csv");
FileUtils.copyInputStreamToFile(csvInputstream, csvFile);
@@ -1398,10 +1398,10 @@ public class SolrLogger
String solrRequestUrl = solr.getBaseURL() + "/select";
solrRequestUrl = generateURL(solrRequestUrl, params);
GetMethod get = new GetMethod(solrRequestUrl);
new HttpClient().executeMethod(get);
HttpGet get = new HttpGet(solrRequestUrl);
HttpResponse response = new DefaultHttpClient().execute(get);
InputStream csvOutput = get.getResponseBodyAsStream();
InputStream csvOutput = response.getEntity().getContent();
Reader csvReader = new InputStreamReader(csvOutput);
List<String[]> rows = new CSVReader(csvReader).readAll();
String[][] csvParsed = rows.toArray(new String[rows.size()][]);

View File

@@ -15,8 +15,8 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpException;
import org.apache.commons.httpclient.HttpException;
import org.dspace.core.Context;
/**

View File

@@ -9,10 +9,8 @@ package org.dspace.submit.lookup;
import gr.ekt.bte.core.Record;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@@ -20,14 +18,18 @@ import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.dspace.app.util.XMLUtils;
import org.dspace.core.ConfigurationManager;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -41,6 +43,11 @@ public class ArXivService
{
private int timeout = 1000;
/**
* How long to wait for a connection to be established.
*
* @param timeout milliseconds
*/
public void setTimeout(int timeout)
{
this.timeout = timeout;
@@ -79,21 +86,28 @@ public class ArXivService
throws IOException, HttpException
{
List<Record> results = new ArrayList<Record>();
GetMethod method = null;
HttpGet method = null;
try
{
HttpClient client = new HttpClient();
client.setTimeout(timeout);
method = new GetMethod("http://export.arxiv.org/api/query");
NameValuePair id = new NameValuePair("id_list", arxivid);
NameValuePair queryParam = new NameValuePair("search_query",
query);
NameValuePair count = new NameValuePair("max_results",
String.valueOf(max_result));
method.setQueryString(new NameValuePair[] { id, queryParam,
count });
HttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
try {
URIBuilder uriBuilder = new URIBuilder("http://export.arxiv.org/api/query");
uriBuilder.addParameter("id_list", arxivid);
uriBuilder.addParameter("search_query", query);
uriBuilder.addParameter("max_results", String.valueOf(max_result));
method = new HttpGet(uriBuilder.build());
} catch (URISyntaxException ex)
{
throw new HttpException(ex.getMessage());
}
// Execute the method.
int statusCode = client.executeMethod(method);
HttpResponse response = client.execute(method);
StatusLine responseStatus = response.getStatusLine();
int statusCode = responseStatus.getStatusCode();
if (statusCode != HttpStatus.SC_OK)
{
@@ -101,7 +115,7 @@ public class ArXivService
throw new RuntimeException("arXiv query is not valid");
else
throw new RuntimeException("Http call failed: "
+ method.getStatusLine());
+ responseStatus);
}
try
@@ -113,7 +127,7 @@ public class ArXivService
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document inDoc = db.parse(method.getResponseBodyAsStream());
Document inDoc = db.parse(response.getEntity().getContent());
Element xmlRoot = inDoc.getDocumentElement();
List<Element> dataRoots = XMLUtils.getElementList(xmlRoot,

View File

@@ -15,8 +15,8 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpException;
import org.apache.commons.httpclient.HttpException;
import org.dspace.core.Context;
/**

View File

@@ -9,24 +9,22 @@ package org.dspace.submit.lookup;
import gr.ekt.bte.core.Record;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dspace.app.util.XMLUtils;
import org.w3c.dom.Document;
@@ -38,7 +36,7 @@ import org.w3c.dom.Element;
public class CiNiiService
{
/** log4j category */
private static Logger log = Logger.getLogger(CiNiiService.class);
private static final Logger log = Logger.getLogger(CiNiiService.class);
private int timeout = 1000;
@@ -82,14 +80,16 @@ public class CiNiiService
private Record search(String id, String appId)
throws IOException, HttpException
{
GetMethod method = null;
HttpGet method = null;
try
{
HttpClient client = new HttpClient();
client.setTimeout(timeout);
method = new GetMethod("http://ci.nii.ac.jp/naid/"+id+".rdf?appid="+appId);
HttpClient client = new DefaultHttpClient();
client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
method = new HttpGet("http://ci.nii.ac.jp/naid/"+id+".rdf?appid="+appId);
// Execute the method.
int statusCode = client.executeMethod(method);
HttpResponse response = client.execute(method);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != HttpStatus.SC_OK)
{
@@ -97,7 +97,7 @@ public class CiNiiService
throw new RuntimeException("CiNii RDF is not valid");
else
throw new RuntimeException("CiNii RDF Http call failed: "
+ method.getStatusLine());
+ statusLine);
}
try
@@ -109,7 +109,7 @@ public class CiNiiService
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document inDoc = db.parse(method.getResponseBodyAsStream());
Document inDoc = db.parse(response.getEntity().getContent());
Element xmlRoot = inDoc.getDocumentElement();
@@ -144,12 +144,12 @@ public class CiNiiService
return null;
}
GetMethod method = null;
HttpGet method = null;
List<String> ids = new ArrayList<String>();
try
{
HttpClient client = new HttpClient();
client.setTimeout(timeout);
HttpClient client = new DefaultHttpClient();
client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
StringBuilder query = new StringBuilder();
query.append("format=rss&appid=").append(appId)
.append("&count=").append(maxResults);
@@ -166,16 +166,18 @@ public class CiNiiService
query.append("&year_from=").append(String.valueOf(year));
query.append("&year_to=").append(String.valueOf(year));
}
method = new GetMethod("http://ci.nii.ac.jp/opensearch/search?"+query.toString());
method = new HttpGet("http://ci.nii.ac.jp/opensearch/search?"+query.toString());
// Execute the method.
int statusCode = client.executeMethod(method);
HttpResponse response = client.execute(method);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != HttpStatus.SC_OK)
{
if (statusCode == HttpStatus.SC_BAD_REQUEST)
throw new RuntimeException("CiNii OpenSearch query is not valid");
else
throw new RuntimeException("CiNii OpenSearch call failed: "
+ method.getStatusLine());
+ statusLine);
}
try
@@ -187,7 +189,7 @@ public class CiNiiService
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document inDoc = db.parse(method.getResponseBodyAsStream());
Document inDoc = db.parse(response.getEntity().getContent());
Element xmlRoot = inDoc.getDocumentElement();
List<Element> items = XMLUtils.getElementList(xmlRoot, "item");

View File

@@ -18,7 +18,7 @@ import java.util.Set;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.httpclient.HttpException;
import org.apache.http.HttpException;
import org.dspace.core.Context;
import org.jdom.JDOMException;
import org.xml.sax.SAXException;

View File

@@ -21,11 +21,6 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dspace.app.util.XMLUtils;
@@ -38,6 +33,18 @@ import org.xml.sax.SAXException;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
/**
* @author Andrea Bollini
@@ -68,27 +75,32 @@ public class CrossRefService
{
try
{
GetMethod method = null;
HttpGet method = null;
try
{
HttpClient client = new HttpClient();
client.setConnectionTimeout(timeout);
method = new GetMethod(
"http://www.crossref.org/openurl/");
HttpClient client = new DefaultHttpClient();
client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
try {
URIBuilder uriBuilder = new URIBuilder(
"http://www.crossref.org/openurl/");
uriBuilder.addParameter("pid", apiKey);
uriBuilder.addParameter("noredirect", "true");
uriBuilder.addParameter("id", record);
method = new HttpGet(uriBuilder.build());
} catch (URISyntaxException ex) {
throw new HttpException("Request not sent", ex);
}
NameValuePair pid = new NameValuePair("pid", apiKey);
NameValuePair noredirect = new NameValuePair(
"noredirect", "true");
NameValuePair id = new NameValuePair("id", record);
method.setQueryString(new NameValuePair[] { pid,
noredirect, id });
// Execute the method.
int statusCode = client.executeMethod(method);
HttpResponse response = client.execute(method);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != HttpStatus.SC_OK)
{
throw new RuntimeException("Http call failed: "
+ method.getStatusLine());
+ statusLine);
}
Record crossitem;
@@ -102,8 +114,7 @@ public class CrossRefService
DocumentBuilder db = factory
.newDocumentBuilder();
Document inDoc = db.parse(method
.getResponseBodyAsStream());
Document inDoc = db.parse(response.getEntity().getContent());
Element xmlRoot = inDoc.getDocumentElement();
Element queryResult = XMLUtils.getSingleElement(xmlRoot, "query_result");
@@ -142,57 +153,51 @@ public class CrossRefService
return results;
}
public NameValuePair[] buildQueryPart(String title, String author,
int year, int count)
public List<Record> search(Context context, String title, String authors,
int year, int count, String apiKey) throws IOException, HttpException
{
StringBuffer sb = new StringBuffer();
HttpGet method = null;
try
{
HttpClient client = new DefaultHttpClient();
client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
URIBuilder uriBuilder = new URIBuilder("http://search.labs.crossref.org/dois");
StringBuilder sb = new StringBuilder();
if (StringUtils.isNotBlank(title))
{
sb.append(title);
}
sb.append(" ");
if (StringUtils.isNotBlank(author))
if (StringUtils.isNotBlank(authors))
{
sb.append(author);
sb.append(authors);
}
String q = sb.toString().trim();
NameValuePair qParam = new NameValuePair("q", title);
NameValuePair yearParam = new NameValuePair("year",
year != -1 ? String.valueOf(year) : "");
NameValuePair countParam = new NameValuePair("rows",
count != -1 ? String.valueOf(count) : "");
uriBuilder.addParameter("q", q);
NameValuePair[] query = new NameValuePair[] { qParam, yearParam,
countParam };
return query;
}
uriBuilder.addParameter("year", year != -1 ? String.valueOf(year) : "");
uriBuilder.addParameter("rows", count != -1 ? String.valueOf(count) : "");
method = new HttpGet(uriBuilder.build());
public List<Record> search(Context context, String title, String authors,
int year, int count, String apiKey) throws IOException, HttpException
{
GetMethod method = null;
try
{
NameValuePair[] query = buildQueryPart(title, authors, year, count);
HttpClient client = new HttpClient();
client.setTimeout(timeout);
method = new GetMethod("http://search.labs.crossref.org/dois");
method.setQueryString(query);
// Execute the method.
int statusCode = client.executeMethod(method);
HttpResponse response = client.execute(method);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != HttpStatus.SC_OK)
{
throw new RuntimeException("Http call failed:: "
+ method.getStatusLine());
+ statusLine);
}
Gson gson = new Gson();
Type listType = new TypeToken<ArrayList<Map>>()
{
}.getType();
List<Map> json = gson.fromJson(method.getResponseBodyAsString(),
List<Map> json = gson.fromJson(
IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8),
listType);
Set<String> dois = new HashSet<String>();
for (Map r : json)

View File

@@ -10,25 +10,18 @@ package org.dspace.submit.lookup;
import gr.ekt.bte.core.DataLoadingSpec;
import gr.ekt.bte.core.Record;
import gr.ekt.bte.core.RecordSet;
import gr.ekt.bte.core.StringValue;
import gr.ekt.bte.core.Value;
import gr.ekt.bte.exceptions.MalformedSourceException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpException;
import org.dspace.core.Context;
import org.dspace.submit.util.SubmissionLookupPublication;
/**
* @author Andrea Bollini

View File

@@ -15,8 +15,8 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpException;
import org.apache.commons.httpclient.HttpException;
import org.apache.log4j.Logger;
import org.dspace.core.Context;
import org.dspace.core.LogManager;
@@ -31,7 +31,7 @@ public class PubmedOnlineDataLoader extends NetworkSubmissionLookupDataLoader
{
private boolean searchProvider = true;
private static Logger log = Logger.getLogger(PubmedOnlineDataLoader.class);
private static final Logger log = Logger.getLogger(PubmedOnlineDataLoader.class);
private PubmedService pubmedService = new PubmedService();

View File

@@ -13,6 +13,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
@@ -20,12 +21,16 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.log4j.Logger;
import org.dspace.app.util.XMLUtils;
import org.dspace.core.ConfigurationManager;
@@ -42,7 +47,7 @@ import org.xml.sax.SAXException;
public class PubmedService
{
private static Logger log = Logger.getLogger(PubmedService.class);
private static final Logger log = Logger.getLogger(PubmedService.class);
private int timeout = 1000;
@@ -96,28 +101,29 @@ public class PubmedService
List<Record> results = null;
if (!ConfigurationManager.getBooleanProperty(SubmissionLookupService.CFG_MODULE, "remoteservice.demo"))
{
GetMethod method = null;
HttpGet method = null;
try
{
HttpClient client = new HttpClient();
client.setTimeout(timeout);
method = new GetMethod(
"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi");
HttpClient client = new DefaultHttpClient();
client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
URIBuilder uriBuilder = new URIBuilder(
"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi");
uriBuilder.addParameter("db", "pubmed");
uriBuilder.addParameter("datetype", "edat");
uriBuilder.addParameter("retmax", "10");
uriBuilder.addParameter("term", query);
method = new HttpGet(uriBuilder.build());
NameValuePair db = new NameValuePair("db", "pubmed");
NameValuePair datetype = new NameValuePair("datetype", "edat");
NameValuePair retmax = new NameValuePair("retmax", "10");
NameValuePair queryParam = new NameValuePair("term",
query.toString());
method.setQueryString(new NameValuePair[] { db, queryParam,
retmax, datetype });
// Execute the method.
int statusCode = client.executeMethod(method);
HttpResponse response = client.execute(method);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != HttpStatus.SC_OK)
{
throw new RuntimeException("WS call failed: "
+ method.getStatusLine());
+ statusLine);
}
DocumentBuilderFactory factory = DocumentBuilderFactory
@@ -131,8 +137,7 @@ public class PubmedService
{
builder = factory.newDocumentBuilder();
Document inDoc = builder.parse(method
.getResponseBodyAsStream());
Document inDoc = builder.parse(response.getEntity().getContent());
Element xmlRoot = inDoc.getDocumentElement();
Element idList = XMLUtils.getSingleElement(xmlRoot,
@@ -213,28 +218,34 @@ public class PubmedService
SAXException
{
List<Record> results = new ArrayList<Record>();
GetMethod method = null;
HttpGet method = null;
try
{
HttpClient client = new HttpClient();
client.setTimeout(5 * timeout);
method = new GetMethod(
"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi");
HttpClient client = new DefaultHttpClient();
client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5 * timeout);
NameValuePair db = new NameValuePair("db", "pubmed");
NameValuePair retmode = new NameValuePair("retmode", "xml");
NameValuePair rettype = new NameValuePair("rettype", "full");
NameValuePair id = new NameValuePair("id", StringUtils.join(
try {
URIBuilder uriBuilder = new URIBuilder(
"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi");
uriBuilder.addParameter("db", "pubmed");
uriBuilder.addParameter("retmode", "xml");
uriBuilder.addParameter("rettype", "full");
uriBuilder.addParameter("id", StringUtils.join(
pubmedIDs.iterator(), ","));
method.setQueryString(new NameValuePair[] { db, retmode,
rettype, id });
method = new HttpGet(uriBuilder.build());
} catch (URISyntaxException ex)
{
throw new RuntimeException("Request not sent", ex);
}
// Execute the method.
int statusCode = client.executeMethod(method);
HttpResponse response = client.execute(method);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != HttpStatus.SC_OK)
{
throw new RuntimeException("WS call failed: "
+ method.getStatusLine());
throw new RuntimeException("WS call failed: " + statusLine);
}
DocumentBuilderFactory factory = DocumentBuilderFactory
@@ -245,7 +256,7 @@ public class PubmedService
DocumentBuilder builder = factory.newDocumentBuilder();
Document inDoc = builder
.parse(method.getResponseBodyAsStream());
.parse(response.getEntity().getContent());
Element xmlRoot = inDoc.getDocumentElement();
List<Element> pubArticles = XMLUtils.getElementList(xmlRoot,

View File

@@ -12,11 +12,11 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.httpclient.HttpException;
import org.dspace.core.Context;
import gr.ekt.bte.core.DataLoader;
import gr.ekt.bte.core.Record;
import org.apache.http.HttpException;
/**
* @author Andrea Bollini