Skip to content

Commit

Permalink
Merge pull request #411 from EKT/DS-1813
Browse files Browse the repository at this point in the history
[DS-1813] Unify configurations regarding SubmissionLookup step
  • Loading branch information
Andrea Bollini committed Dec 4, 2013
2 parents 60303dc + 5fd0ace commit 8c7bb7b
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 359 deletions.
187 changes: 69 additions & 118 deletions dspace-api/src/main/java/org/dspace/submit/lookup/ArXivService.java
Expand Up @@ -76,124 +76,75 @@ public List<Record> searchByTerm(String title, String author, int year)
}

private List<Record> search(String query, String arxivid, int max_result)
throws IOException, HttpException
{
List<Record> results = new ArrayList<Record>();
if (!ConfigurationManager.getBooleanProperty(SubmissionLookupService.CFG_MODULE, "remoteservice.demo"))
{
GetMethod 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 });
// Execute the method.
int statusCode = client.executeMethod(method);

if (statusCode != HttpStatus.SC_OK)
{
if (statusCode == HttpStatus.SC_BAD_REQUEST)
throw new RuntimeException("arXiv query is not valid");
else
throw new RuntimeException("Http call failed: "
+ method.getStatusLine());
}

try
{
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setValidating(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);

DocumentBuilder db = factory.newDocumentBuilder();
Document inDoc = db.parse(method.getResponseBodyAsStream());

Element xmlRoot = inDoc.getDocumentElement();
List<Element> dataRoots = XMLUtils.getElementList(xmlRoot,
"entry");

for (Element dataRoot : dataRoots)
{
Record crossitem = ArxivUtils
.convertArxixDomToRecord(dataRoot);
if (crossitem != null)
{
results.add(crossitem);
}
}
}
catch (Exception e)
{
throw new RuntimeException(
"ArXiv identifier is not valid or not exist");
}
}
finally
{
if (method != null)
{
method.releaseConnection();
}
}
}
else
{
InputStream stream = null;
try
{
File file = new File(
ConfigurationManager.getProperty("dspace.dir")
+ "/config/crosswalks/demo/arxiv.xml");
stream = new FileInputStream(file);
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setValidating(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document inDoc = db.parse(stream);

Element xmlRoot = inDoc.getDocumentElement();
List<Element> dataRoots = XMLUtils.getElementList(xmlRoot,
"entry");
for (Element dataRoot : dataRoots)
{
Record crossitem = ArxivUtils
.convertArxixDomToRecord(dataRoot);

if (crossitem != null)
{
results.add(crossitem);
}
}
}
catch (Exception e)
{
throw new RuntimeException(
"ArXiv identifier is not valid or not exist");
}
}
finally
{
if (stream != null)
{
stream.close();
}
}
}
return results;
}
throws IOException, HttpException
{
List<Record> results = new ArrayList<Record>();
GetMethod 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 });
// Execute the method.
int statusCode = client.executeMethod(method);

if (statusCode != HttpStatus.SC_OK)
{
if (statusCode == HttpStatus.SC_BAD_REQUEST)
throw new RuntimeException("arXiv query is not valid");
else
throw new RuntimeException("Http call failed: "
+ method.getStatusLine());
}

try
{
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setValidating(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);

DocumentBuilder db = factory.newDocumentBuilder();
Document inDoc = db.parse(method.getResponseBodyAsStream());

Element xmlRoot = inDoc.getDocumentElement();
List<Element> dataRoots = XMLUtils.getElementList(xmlRoot,
"entry");

for (Element dataRoot : dataRoots)
{
Record crossitem = ArxivUtils
.convertArxixDomToRecord(dataRoot);
if (crossitem != null)
{
results.add(crossitem);
}
}
}
catch (Exception e)
{
throw new RuntimeException(
"ArXiv identifier is not valid or not exist");
}
}
finally
{
if (method != null)
{
method.releaseConnection();
}
}

return results;
}

public Record getByArXivIDs(String raw) throws HttpException, IOException
{
Expand Down
Expand Up @@ -35,6 +35,9 @@ public class CrossRefOnlineDataLoader extends NetworkSubmissionLookupDataLoader

private boolean searchProvider = true;

private String apiKey = null;
private int maxResults = 10;

public void setSearchProvider(boolean searchProvider)
{
this.searchProvider = searchProvider;
Expand All @@ -60,9 +63,14 @@ public List<Record> getByIdentifier(Context context,
Set<String> dois = keys.get(DOI);
List<Record> items = null;
List<Record> results = new ArrayList<Record>();

if (getApiKey() == null){
throw new RuntimeException("No CrossRef API key is specified!");
}

try
{
items = crossrefService.search(context, dois);
items = crossrefService.search(context, dois, getApiKey());
}
catch (JDOMException e)
{
Expand All @@ -89,8 +97,12 @@ public List<Record> getByIdentifier(Context context,
public List<Record> search(Context context, String title, String author,
int year) throws HttpException, IOException
{
if (getApiKey() == null){
throw new RuntimeException("No CrossRef API key is specified!");
}

List<Record> items = crossrefService.search(context, title, author,
year, 10);
year, getMaxResults(), getApiKey());
return items;
}

Expand All @@ -99,4 +111,20 @@ public boolean isSearchProvider()
{
return searchProvider;
}

public String getApiKey() {
return apiKey;
}

public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}

public int getMaxResults() {
return maxResults;
}

public void setMaxResults(int maxResults) {
this.maxResults = maxResults;
}
}

0 comments on commit 8c7bb7b

Please sign in to comment.