DS-2604 port from XMLUI to JSPUI the approach to reach the Creative Commons service (via REST API)

This commit is contained in:
Luigi Andrea Pascarelli
2016-09-22 15:28:15 +02:00
parent 4e1cad0109
commit 9086a1ecf2
16 changed files with 509 additions and 488 deletions

View File

@@ -22,7 +22,10 @@ import java.util.Map;
import java.util.NoSuchElementException;
import org.apache.log4j.Logger;
import org.dspace.license.factory.LicenseServiceFactory;
import org.dspace.license.service.CreativeCommonsService;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.jaxen.JaxenException;
import org.jaxen.jdom.JDOMXPath;
import org.jdom.Attribute;
@@ -30,10 +33,6 @@ import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
/**
@@ -58,7 +57,9 @@ public class CCLookup {
private SAXBuilder parser = new SAXBuilder();
private List<CCLicense> licenses = new ArrayList<CCLicense>();
private List<CCLicenseField> licenseFields = new ArrayList<CCLicenseField>();
protected CreativeCommonsService creativeCommonsService = LicenseServiceFactory.getInstance().getCreativeCommonsService();
/**
* Constructs a new instance with the default web services root.
*
@@ -406,26 +407,14 @@ public class CCLookup {
public String getRdf()
throws IOException {
String myString = null;
java.io.ByteArrayOutputStream outputstream = new java.io.ByteArrayOutputStream();
String result = "";
try {
outputstream.write("<result>\n".getBytes());
JDOMXPath xpathRdf = new JDOMXPath("//result/rdf");
JDOMXPath xpathLicenseRdf = new JDOMXPath("//result/licenserdf");
XMLOutputter xmloutputter = new XMLOutputter();
Element rdfParent = ((Element)xpathRdf.selectSingleNode(this.license_doc));
xmloutputter.output(rdfParent, outputstream);
Element licenseRdfParent = ((Element)xpathLicenseRdf.selectSingleNode(this.license_doc));
outputstream.write("\n".getBytes());
xmloutputter.output(licenseRdfParent, outputstream);
outputstream.write("\n</result>\n".getBytes());
result = creativeCommonsService.fetchLicenseRDF(license_doc);
} catch (Exception e) {
log.warn("An error occurred getting the rdf . . ." + e.getMessage() );
setSuccess(false);
} finally {
outputstream.close();
return outputstream.toString();
}
}
return result;
}
public boolean isSuccess() {

View File

@@ -7,9 +7,11 @@
*/
package org.dspace.license;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.sql.SQLException;
import java.util.List;
@@ -35,6 +37,10 @@ import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context;
import org.dspace.core.Utils;
import org.dspace.license.service.CreativeCommonsService;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.jdom.Document;
import org.jdom.transform.JDOMSource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
@@ -52,8 +58,10 @@ public class CreativeCommonsServiceImpl implements CreativeCommonsService, Initi
/**
* Some BitStream Names (BSN)
*/
@Deprecated
protected static final String BSN_LICENSE_URL = "license_url";
@Deprecated
protected static final String BSN_LICENSE_TEXT = "license_text";
protected static final String BSN_LICENSE_RDF = "license_rdf";
@@ -68,10 +76,12 @@ public class CreativeCommonsServiceImpl implements CreativeCommonsService, Initi
protected BundleService bundleService;
@Autowired(required = true)
protected ItemService itemService;
protected ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
protected CreativeCommonsServiceImpl()
{
}
@Override
@@ -79,8 +89,8 @@ public class CreativeCommonsServiceImpl implements CreativeCommonsService, Initi
{
// if defined, set a proxy server for http requests to Creative
// Commons site
String proxyHost = ConfigurationManager.getProperty("http.proxy.host");
String proxyPort = ConfigurationManager.getProperty("http.proxy.port");
String proxyHost = configurationService.getProperty("http.proxy.host");
String proxyPort = configurationService.getProperty("http.proxy.port");
if (StringUtils.isNotBlank(proxyHost) && StringUtils.isNotBlank(proxyPort))
{
@@ -134,37 +144,6 @@ public class CreativeCommonsServiceImpl implements CreativeCommonsService, Initi
setBitstreamFromBytes(context, item, bundle, BSN_LICENSE_RDF, bs_rdf_format, licenseRdf.getBytes());
}
@Override
public void setLicense(Context context, Item item,
String cc_license_url) throws SQLException, IOException,
AuthorizeException
{
Bundle bundle = getCcBundle(context, item);
// get some more information
String license_text = fetchLicenseText(cc_license_url);
String license_rdf = fetchLicenseRDF(cc_license_url);
// set the formats
BitstreamFormat bs_url_format = bitstreamFormatService.findByShortDescription(
context, "License");
BitstreamFormat bs_text_format = bitstreamFormatService.findByShortDescription(
context, "CC License");
BitstreamFormat bs_rdf_format = bitstreamFormatService.findByShortDescription(
context, "RDF XML");
// set the URL bitstream
setBitstreamFromBytes(context, item, bundle, BSN_LICENSE_URL, bs_url_format,
cc_license_url.getBytes());
// set the license text bitstream
setBitstreamFromBytes(context, item, bundle, BSN_LICENSE_TEXT, bs_text_format,
license_text.getBytes());
// set the RDF bitstream
setBitstreamFromBytes(context, item, bundle, BSN_LICENSE_RDF, bs_rdf_format,
license_rdf.getBytes());
}
@Override
public void setLicense(Context context, Item item,
@@ -223,8 +202,7 @@ public class CreativeCommonsServiceImpl implements CreativeCommonsService, Initi
// verify it has correct contents
try
{
if ((getLicenseURL(context, item) == null) || (getLicenseText(context, item) == null)
|| (getLicenseRDF(context, item) == null))
if ((getLicenseURL(context, item) == null))
{
return false;
}
@@ -237,20 +215,6 @@ public class CreativeCommonsServiceImpl implements CreativeCommonsService, Initi
return true;
}
@Override
public String getLicenseURL(Context context, Item item) throws SQLException,
IOException, AuthorizeException
{
return getStringFromBitstream(context, item, BSN_LICENSE_URL);
}
@Override
public String getLicenseText(Context context, Item item) throws SQLException,
IOException, AuthorizeException
{
return getStringFromBitstream(context, item, BSN_LICENSE_TEXT);
}
@Override
public String getLicenseRDF(Context context, Item item) throws SQLException,
IOException, AuthorizeException
@@ -265,6 +229,7 @@ public class CreativeCommonsServiceImpl implements CreativeCommonsService, Initi
return getBitstream(item, BSN_LICENSE_RDF);
}
@Deprecated
@Override
public Bitstream getLicenseTextBitstream(Item item) throws SQLException,
IOException, AuthorizeException
@@ -273,39 +238,25 @@ public class CreativeCommonsServiceImpl implements CreativeCommonsService, Initi
}
@Override
public String fetchLicenseRdf(String ccResult) {
StringWriter result = new StringWriter();
try {
InputStream inputstream = new ByteArrayInputStream(ccResult.getBytes("UTF-8"));
templates.newTransformer().transform(new StreamSource(inputstream), new StreamResult(result));
} catch (TransformerException te) {
throw new RuntimeException("Transformer exception " + te.getMessage(), te);
} catch (IOException ioe) {
throw new RuntimeException("IOexception " + ioe.getCause().toString(), ioe);
} finally {
return result.getBuffer().toString();
}
}
public String getLicenseURL(Context context, Item item) throws SQLException, IOException, AuthorizeException {
String licenseUri = getCCField("uri").ccItemValue(item);
if (StringUtils.isNotBlank(licenseUri)) {
return licenseUri;
}
//backward compatibility
return getStringFromBitstream(context, item, BSN_LICENSE_URL);
}
@Override
public String fetchLicenseText(String license_url)
{
String text_url = license_url;
byte[] urlBytes = fetchURL(text_url);
return (urlBytes != null) ? new String(urlBytes) : "";
}
@Override
public String fetchLicenseRDF(String license_url)
public String fetchLicenseRDF(Document license)
{
StringWriter result = new StringWriter();
try
{
templates.newTransformer().transform(
new StreamSource(license_url + "rdf"),
new JDOMSource(license),
new StreamResult(result)
);
}
@@ -408,40 +359,32 @@ public class CreativeCommonsServiceImpl implements CreativeCommonsService, Initi
return baos.toByteArray();
}
/**
* Fetch the contents of a URL
*/
protected byte[] fetchURL(String url_string)
{
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();
while ((line = reader.readLine()) != null)
{
sb.append(line);
}
return sb.toString().getBytes();
}
catch (Exception exc)
{
log.error(exc.getMessage());
return null;
}
}
/**
* Returns a metadata field handle for given field Id
*/
@Override
public LicenseMetadataValue getCCField(String fieldId)
{
return new LicenseMetadataValue(ConfigurationManager.getProperty("cc.license." + fieldId));
return new LicenseMetadataValue(configurationService.getProperty("cc.license." + fieldId));
}
@Override
public void removeLicense(Context context, LicenseMetadataValue uriField,
LicenseMetadataValue nameField, Item item) throws AuthorizeException, IOException, SQLException {
// only remove any previous licenses
String licenseUri = uriField.ccItemValue(item);
if (licenseUri != null) {
uriField.removeItemValue(context, item, licenseUri);
if (configurationService.getBooleanProperty("cc.submit.setname"))
{
String licenseName = nameField.keyedItemValue(item, licenseUri);
nameField.removeItemValue(context, item, licenseName);
}
if (configurationService.getBooleanProperty("cc.submit.addbitstream"))
{
removeLicense(context, item);
}
}
}
}

View File

@@ -12,6 +12,7 @@ import org.dspace.content.Bitstream;
import org.dspace.content.Item;
import org.dspace.core.Context;
import org.dspace.license.LicenseMetadataValue;
import org.jdom.Document;
import java.io.IOException;
import java.io.InputStream;
@@ -39,12 +40,6 @@ public interface CreativeCommonsService {
*/
public void setLicenseRDF(Context context, Item item, String licenseRdf) throws SQLException, IOException, AuthorizeException;
/**
* This is a bit of the "do-the-right-thing" method for CC stuff in an item
*/
public void setLicense(Context context, Item item,
String cc_license_url) throws SQLException, IOException,
AuthorizeException;
/**
* Used by DSpaceMetsIngester
@@ -72,9 +67,6 @@ public interface CreativeCommonsService {
public String getLicenseURL(Context context, Item item) throws SQLException,
IOException, AuthorizeException;
public String getLicenseText(Context context, Item item) throws SQLException,
IOException, AuthorizeException;
public String getLicenseRDF(Context context, Item item) throws SQLException,
IOException, AuthorizeException;
@@ -87,27 +79,20 @@ public interface CreativeCommonsService {
/**
* Get Creative Commons license Text, returning Bitstream object.
* @Deprecated
* @return bitstream or null.
*/
public Bitstream getLicenseTextBitstream(Item item) throws SQLException,
IOException, AuthorizeException;
public String fetchLicenseRdf(String ccResult);
/**
*
* The next two methods are old CC.
* Remains until prev. usages are eliminated.
* @Deprecated
*
*/
/**
* Get a few license-specific properties. We expect these to be cached at
* least per server run.
*/
public String fetchLicenseText(String license_url);
public String fetchLicenseRDF(String license_url);
public LicenseMetadataValue getCCField(String fieldId);
public String fetchLicenseRDF(Document license);
public void removeLicense(Context context, LicenseMetadataValue uriField,
LicenseMetadataValue nameField, Item item) throws AuthorizeException, IOException, SQLException;
}

View File

@@ -14,7 +14,6 @@ import org.apache.commons.lang3.*;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.dspace.app.util.SubmissionInfo;
import org.dspace.app.util.Util;
import org.dspace.authorize.AuthorizeException;
@@ -102,11 +101,6 @@ public class CCLicenseStep extends AbstractProcessingStep
session.setAttribute("inProgress", "TRUE");
// check what submit button was pressed in User Interface
String buttonPressed = Util.getSubmitButton(request, NEXT_BUTTON);
if ("submit_grant".equalsIgnoreCase(buttonPressed)
|| "submit_no_cc".equalsIgnoreCase(buttonPressed))
{
return processCC(context, request, response, subInfo);
}
String choiceButton = Util.getSubmitButton(request, SELECT_CHANGE);
Enumeration e = request.getParameterNames();
String isFieldRequired = "FALSE";
@@ -124,27 +118,10 @@ public class CCLicenseStep extends AbstractProcessingStep
{
Item item = subInfo.getSubmissionItem().getItem();
LicenseMetadataValue uriField = creativeCommonsService.getCCField("uri");
LicenseMetadataValue nameField = creativeCommonsService.getCCField("name");
String licenseUri = uriField.ccItemValue(item);
if (licenseUri != null)
//if (CreativeCommons.hasLicense(item, "dc", "rights", "uri", Item.ANY)
// && !CreativeCommons.getRightsURI(item, "dc", "rights", "uri", Item.ANY).equals(""))
{
//CreativeCommons.setItemMetadata(item, licenseURI, "dc", "rights", "uri", ConfigurationManager.getProperty("default.locale"));
uriField.removeItemValue(context, item, licenseUri);
if (configurationService.getBooleanProperty("cc.submit.setname"))
{
String licenseName = nameField.keyedItemValue(item, licenseUri);
nameField.removeItemValue(context, item, licenseName);
//CreativeCommons.setItemMetadata(item, CreativeCommons.getRightsName(item, "dc", "rights", null, Item.ANY), "dc", "rights", null, configurationService.getProperty("default.locale"));
}
if (configurationService.getBooleanProperty("cc.submit.addBitstream"))
{
creativeCommonsService.removeLicense(context, item);
}
removeRequiredAttributes(session);
itemService.update(context, item);
context.dispatchEvents();
}
return STATUS_COMPLETE;
}
@@ -154,7 +131,7 @@ public class CCLicenseStep extends AbstractProcessingStep
}
if (buttonPressed.equals(NEXT_BUTTON) || buttonPressed.equals(CANCEL_BUTTON) )
{
return processCCWS(context, request, response, subInfo);
return processCC(context, request, response, subInfo);
}
else
{
@@ -164,54 +141,6 @@ public class CCLicenseStep extends AbstractProcessingStep
}
}
/**
* Process the input from the CC license page
*
* @param context
* current DSpace context
* @param request
* current servlet request object
* @param response
* current servlet response object
* @param subInfo
* submission info object
*
* @return Status or error flag which will be processed by
* doPostProcessing() below! (if STATUS_COMPLETE or 0 is returned,
* no errors occurred!)
*/
protected int processCC(Context context, HttpServletRequest request,
HttpServletResponse response, SubmissionInfo subInfo)
throws ServletException, IOException, SQLException,
AuthorizeException
{
String buttonPressed = Util.getSubmitButton(request, NEXT_BUTTON);
// RLR hack - need to distinguish between progress bar real submission
// (if cc_license_url exists, then users has accepted the CC License)
String ccLicenseUrl = request.getParameter("cc_license_url");
if (buttonPressed.equals("submit_no_cc"))
{
// Skipping the CC license - remove any existing license selection
creativeCommonsService.removeLicense(context, subInfo.getSubmissionItem()
.getItem());
}
else if ((ccLicenseUrl != null) && (ccLicenseUrl.length() > 0))
{
Item item = subInfo.getSubmissionItem().getItem();
// save the CC license
creativeCommonsService.setLicense(context, item, ccLicenseUrl);
}
// commit changes
context.dispatchEvents();
// completed without errors
return STATUS_COMPLETE;
}
/**
* Process the input from the CC license page using CC Web service
@@ -230,7 +159,7 @@ public class CCLicenseStep extends AbstractProcessingStep
* doPostProcessing() below! (if STATUS_COMPLETE or 0 is returned,
* no errors occurred!)
*/
protected int processCCWS(Context context, HttpServletRequest request,
protected int processCC(Context context, HttpServletRequest request,
HttpServletResponse response, SubmissionInfo subInfo)
throws ServletException, IOException, SQLException,
AuthorizeException {
@@ -251,34 +180,25 @@ public class CCLicenseStep extends AbstractProcessingStep
LicenseMetadataValue nameField = creativeCommonsService.getCCField("name");
ccLookup.issue(licenseclass, map, configurationService.getProperty("cc.license.locale"));
Item item = subInfo.getSubmissionItem().getItem();
if (licenseclass.equals("xmlui.Submission.submit.CCLicenseStep.no_license"))
if (licenseclass.equals("webui.Submission.submit.CCLicenseStep.no_license"))
{
// only remove any previous licenses
String licenseUri = uriField.ccItemValue(item);
if (licenseUri != null) {
uriField.removeItemValue(context, item, licenseUri);
if (configurationService.getBooleanProperty("cc.submit.setname"))
{
String licenseName = nameField.keyedItemValue(item, licenseUri);
nameField.removeItemValue(context, item, licenseName);
}
if (configurationService.getBooleanProperty("cc.submit.addBitstream"))
{
creativeCommonsService.removeLicense(context, item);
}
itemService.update(context, item);
context.dispatchEvents();
removeRequiredAttributes(session);
}
creativeCommonsService.removeLicense(context, uriField, nameField, item);
itemService.update(context, item);
context.dispatchEvents();
removeRequiredAttributes(session);
return STATUS_COMPLETE;
}
else if (StringUtils.isBlank(licenseclass) || licenseclass.equals("xmlui.Submission.submit.CCLicenseStep.select_change"))
else if (StringUtils.isBlank(licenseclass) || licenseclass.equals("webui.Submission.submit.CCLicenseStep.select_change"))
{
removeRequiredAttributes(session);
return STATUS_COMPLETE;
}
else if (ccLookup.isSuccess())
{
creativeCommonsService.removeLicense(context, uriField, nameField, item);
uriField.addItemValue(context, item, ccLookup.getLicenseUrl());
if (configurationService.getBooleanProperty("cc.submit.addbitstream")) {
creativeCommonsService.setLicenseRDF(context, item, ccLookup.getRdf());
@@ -286,7 +206,8 @@ public class CCLicenseStep extends AbstractProcessingStep
if (configurationService.getBooleanProperty("cc.submit.setname")) {
nameField.addItemValue(context, item, ccLookup.getLicenseName());
}
itemService.update(context, item);
itemService.update(context, item);
context.dispatchEvents();
removeRequiredAttributes(session);
session.removeAttribute("inProgress");
@@ -303,7 +224,8 @@ public class CCLicenseStep extends AbstractProcessingStep
}
return STATUS_COMPLETE;
}
private void removeRequiredAttributes(HttpSession session) {
session.removeAttribute("ccError");
session.removeAttribute("isFieldRequired");