Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DS-2604] Fix JSPUI Creative Commons license retrieval #1506

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 69 additions & 26 deletions dspace-api/src/main/java/org/dspace/license/CreativeCommons.java
Expand Up @@ -8,10 +8,11 @@
package org.dspace.license;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Objects;

import javax.xml.transform.Templates;
import javax.xml.transform.TransformerConfigurationException;
Expand All @@ -22,6 +23,7 @@

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dspace.app.util.Util;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bitstream;
import org.dspace.content.BitstreamFormat;
Expand All @@ -34,6 +36,10 @@

public class CreativeCommons
{
// HTTP RFC says do not automatically follow more than 5 redirects, it indicates a probable loop
// http://www.freesoft.org/CIE/RFC/1945/46.htm
private static final int MAX_REDIRECTS = 5;

/** log4j category */
private static Logger log = Logger.getLogger(CreativeCommons.class);

Expand Down Expand Up @@ -305,10 +311,9 @@ public static String fetchLicenseRdf(String ccResult) {
*/
public static String fetchLicenseText(String license_url)
{
String text_url = license_url;
byte[] urlBytes = fetchURL(text_url);
String text = fetchURL(license_url, 0);

return (urlBytes != null) ? new String(urlBytes) : "";
return Objects.toString(text, "");
}

public static String fetchLicenseRDF(String license_url)
Expand Down Expand Up @@ -421,33 +426,74 @@ private static byte[] getBytesFromBitstream(Item item, String bitstream_name)
return baos.toByteArray();
}

/**
* Fetch the contents of a URL
*/
private static byte[] fetchURL(String url_string)
private static String fetchURL(String url_string, int numRedirects)
{
if (numRedirects > MAX_REDIRECTS)
{
log.error("Could not retrieve Creative Commons license, maximum number of redirects reached (" + numRedirects + ")");
return null;
}

try
{
String line = "";
URL url = new URL(url_string);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

while ((line = reader.readLine()) != null)
String mailAdmin = ConfigurationManager.getProperty("mail.admin");
String userAgent = "DSpace/" + Util.getSourceVersion() + "(+" + ConfigurationManager.getProperty("dspace.url");
if (StringUtils.isNotBlank(mailAdmin)) {
userAgent = userAgent + "; " + mailAdmin;
}
userAgent += ")";
connection.addRequestProperty("User-Agent", userAgent);
connection.setConnectTimeout(2000);
connection.setReadTimeout(2000);
connection.setInstanceFollowRedirects(false); // we're handling redirects ourselves

int code = connection.getResponseCode();
if (code == HttpURLConnection.HTTP_MOVED_PERM || code == HttpURLConnection.HTTP_MOVED_TEMP)
{
// recursively follow redirects
String locationHeader = connection.getHeaderField("Location");
log.debug("Following redirect to " + locationHeader);
InputStream inputStream = connection.getInputStream();
if (inputStream != null)
{
inputStream.close();
}
return fetchURL(locationHeader, numRedirects + 1);
}
else if (code == HttpURLConnection.HTTP_OK)
{
sb.append(line);
InputStream inputStream = connection.getInputStream();
if (inputStream != null)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();

String line;
while ((line = reader.readLine()) != null)
{
sb.append(line);
}
inputStream.close();
return sb.toString();
}
}
else
{
log.error("Could not retrieve Creative Commons license from URL " + url.toString()
+ "; response was " + connection.getResponseCode() + ": " + connection.getResponseMessage());
}

return sb.toString().getBytes();
}
catch (Exception exc)
{
log.error(exc.getMessage());
return null;
log.error("Caught exception while trying to obtain Creative Commons license: " + exc.getMessage(), exc);
}
log.warn("Could not retrieve license");
return null;
}

/**
* Returns a metadata field handle for given field Id
*/
Expand All @@ -472,10 +518,7 @@ public MdField(String fieldName)
if (fieldName != null && fieldName.length() > 0)
{
String[] fParams = fieldName.split("\\.");
for (int i = 0; i < fParams.length; i++)
{
params[i] = fParams[i];
}
System.arraycopy(fParams, 0, params, 0, fParams.length);
params[3] = Item.ANY;
}
}
Expand All @@ -493,7 +536,7 @@ public String ccItemValue(Item item)
Metadatum[] dcvalues = item.getMetadata(params[0], params[1], params[2], params[3]);
for (Metadatum dcvalue : dcvalues)
{
if ((dcvalue.value).indexOf(ccShib) != -1)
if ((dcvalue.value).contains(ccShib))
{
// return first value that matches the shib
return dcvalue.value;
Expand Down Expand Up @@ -539,15 +582,15 @@ public void removeItemValue(Item item, String value)
if (value != null)
{
Metadatum[] dcvalues = item.getMetadata(params[0], params[1], params[2], params[3]);
ArrayList<String> arrayList = new ArrayList<String>();
ArrayList<String> arrayList = new ArrayList<>();
for (Metadatum dcvalue : dcvalues)
{
if (! dcvalue.value.equals(value))
{
arrayList.add(dcvalue.value);
}
}
String[] values = (String[])arrayList.toArray(new String[arrayList.size()]);
String[] values = arrayList.toArray(new String[0]);
item.clearMetadata(params[0], params[1], params[2], params[3]);
item.addMetadata(params[0], params[1], params[2], params[3], values);
}
Expand Down