1 40 package org.dspace.license; 41 42 import java.io.InputStream ; 43 import java.io.ByteArrayInputStream ; 44 import java.io.ByteArrayOutputStream ; 45 import java.io.IOException ; 46 import java.net.URL ; 47 import java.net.URLConnection ; 48 import java.sql.SQLException ; 49 50 import org.dspace.authorize.AuthorizeException; 51 import org.dspace.content.Bitstream; 52 import org.dspace.content.BitstreamFormat; 53 import org.dspace.content.Bundle; 54 import org.dspace.content.Item; 55 import org.dspace.core.ConfigurationManager; 56 import org.dspace.core.Context; 57 import org.dspace.core.Utils; 58 59 public class CreativeCommons 60 { 61 64 public static final String CC_BUNDLE_NAME = "CC-LICENSE"; 65 66 private static final String CC_BS_SOURCE = "org.dspace.license.CreativeCommons"; 67 68 71 private static final String BSN_LICENSE_URL = "license_url"; 72 73 private static final String BSN_LICENSE_TEXT = "license_text"; 74 75 private static final String BSN_LICENSE_RDF = "license_rdf"; 76 77 private static boolean enabled_p; 78 79 static 80 { 81 enabled_p = ConfigurationManager 83 .getBooleanProperty("webui.submit.enable-cc"); 84 85 if (enabled_p) 86 { 87 String proxyHost = ConfigurationManager 90 .getProperty("http.proxy.host"); 91 String proxyPort = ConfigurationManager 92 .getProperty("http.proxy.port"); 93 94 if ((proxyHost != null) && (proxyPort != null)) 95 { 96 System.setProperty("http.proxyHost", proxyHost); 97 System.setProperty("http.proxyPort", proxyPort); 98 } 99 } 100 } 101 102 105 public static boolean isEnabled() 106 { 107 return enabled_p; 108 } 109 110 private static Bundle getCcBundle(Item item) 113 throws SQLException , AuthorizeException, IOException 114 { 115 Bundle[] bundles = item.getBundles(CC_BUNDLE_NAME); 116 117 if ((bundles.length > 0) && (bundles[0] != null)) 118 { 119 item.removeBundle(bundles[0]); 120 } 121 return item.createBundle(CC_BUNDLE_NAME); 122 } 123 124 125 128 public static void setLicense(Context context, Item item, 129 String cc_license_url) throws SQLException , IOException , 130 AuthorizeException 131 { 132 Bundle bundle = getCcBundle(item); 133 134 String license_text = fetchLicenseText(cc_license_url); 136 String license_rdf = fetchLicenseRDF(cc_license_url); 137 138 int license_start = license_rdf.indexOf("<License"); 141 142 int license_end = license_rdf.indexOf("</License>") + 10; 144 String document_rdf = "<rdf:RDF xmlns=\"http://web.resource.org/cc/\"\n" 145 + " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" 146 + " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" 147 + "<Work rdf:about=\"\">\n" 148 + "<license rdf:resource=\"" 149 + cc_license_url 150 + "\">\n" 151 + "</Work>\n\n" 152 + license_rdf.substring(license_start, license_end) 153 + "\n\n</rdf:RDF>"; 154 155 BitstreamFormat bs_format = BitstreamFormat.findByShortDescription( 157 context, "License"); 158 159 setBitstreamFromBytes(item, bundle, BSN_LICENSE_URL, bs_format, 161 cc_license_url.getBytes()); 162 163 setBitstreamFromBytes(item, bundle, BSN_LICENSE_TEXT, bs_format, 165 license_text.getBytes()); 166 167 setBitstreamFromBytes(item, bundle, BSN_LICENSE_RDF, bs_format, 169 document_rdf.getBytes()); 170 } 171 172 public static void setLicense(Context context, Item item, 173 InputStream licenseStm, String mimeType) 174 throws SQLException , IOException , AuthorizeException 175 { 176 Bundle bundle = getCcBundle(item); 177 178 BitstreamFormat bs_format = BitstreamFormat.findByShortDescription( 180 context, "License"); 181 182 Bitstream bs = bundle.createBitstream(licenseStm); 183 bs.setSource(CC_BS_SOURCE); 184 bs.setName((mimeType != null && 185 (mimeType.equalsIgnoreCase("text/xml") || 186 mimeType.equalsIgnoreCase("text/rdf"))) ? 187 BSN_LICENSE_RDF : BSN_LICENSE_TEXT); 188 bs.setFormat(bs_format); 189 bs.update(); 190 } 191 192 public static void removeLicense(Context context, Item item) 193 throws SQLException , IOException , AuthorizeException 194 { 195 Bundle[] bundles = item.getBundles(CC_BUNDLE_NAME); 197 198 if ((bundles.length > 0) && (bundles[0] != null)) 199 { 200 item.removeBundle(bundles[0]); 201 } 202 } 203 204 public static boolean hasLicense(Context context, Item item) 205 throws SQLException , IOException 206 { 207 Bundle[] bundles = item.getBundles(CC_BUNDLE_NAME); 209 210 if (bundles.length == 0) 211 { 212 return false; 213 } 214 215 try 217 { 218 if ((getLicenseURL(item) == null) || (getLicenseText(item) == null) 219 || (getLicenseRDF(item) == null)) 220 { 221 return false; 222 } 223 } 224 catch (AuthorizeException ae) 225 { 226 return false; 227 } 228 229 return true; 230 } 231 232 public static String getLicenseURL(Item item) throws SQLException , 233 IOException , AuthorizeException 234 { 235 return getStringFromBitstream(item, BSN_LICENSE_URL); 236 } 237 238 public static String getLicenseText(Item item) throws SQLException , 239 IOException , AuthorizeException 240 { 241 return getStringFromBitstream(item, BSN_LICENSE_TEXT); 242 } 243 244 public static String getLicenseRDF(Item item) throws SQLException , 245 IOException , AuthorizeException 246 { 247 return getStringFromBitstream(item, BSN_LICENSE_RDF); 248 } 249 250 254 public static Bitstream getLicenseRdfBitstream(Item item) throws SQLException , 255 IOException , AuthorizeException 256 { 257 return getBitstream(item, BSN_LICENSE_RDF); 258 } 259 260 264 public static Bitstream getLicenseTextBitstream(Item item) throws SQLException , 265 IOException , AuthorizeException 266 { 267 return getBitstream(item, BSN_LICENSE_TEXT); 268 } 269 270 271 275 public static String fetchLicenseText(String license_url) 276 { 277 String text_url = license_url; 278 byte[] urlBytes = fetchURL(text_url); 279 280 return (urlBytes != null) ? new String (urlBytes) : ""; 281 } 282 283 public static String fetchLicenseRDF(String license_url) 284 { 285 String rdf_url = license_url + "rdf"; 286 byte[] urlBytes = fetchURL(rdf_url); 287 288 return (urlBytes != null) ? new String (urlBytes) : ""; 289 } 290 291 294 298 private static void setBitstreamFromBytes(Item item, Bundle bundle, 299 String bitstream_name, BitstreamFormat format, byte[] bytes) 300 throws SQLException , IOException , AuthorizeException 301 { 302 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 303 Bitstream bs = bundle.createBitstream(bais); 304 305 bs.setName(bitstream_name); 306 bs.setSource(CC_BS_SOURCE); 307 308 bs.setFormat(format); 309 310 bs.update(); 312 } 313 314 318 private static String getStringFromBitstream(Item item, 319 String bitstream_name) throws SQLException , IOException , 320 AuthorizeException 321 { 322 byte[] bytes = getBytesFromBitstream(item, bitstream_name); 323 324 if (bytes == null) 325 { 326 return null; 327 } 328 329 return new String (bytes); 330 } 331 332 336 private static Bitstream getBitstream(Item item, String bitstream_name) 337 throws SQLException , IOException , AuthorizeException 338 { 339 Bundle cc_bundle = null; 340 341 try 343 { 344 Bundle[] bundles = item.getBundles(CC_BUNDLE_NAME); 345 346 if ((bundles != null) && (bundles.length > 0)) 347 { 348 cc_bundle = bundles[0]; 349 } 350 else 351 { 352 return null; 353 } 354 } 355 catch (Exception exc) 356 { 357 return null; 360 } 361 362 return cc_bundle.getBitstreamByName(bitstream_name); 363 } 364 365 private static byte[] getBytesFromBitstream(Item item, String bitstream_name) 366 throws SQLException , IOException , AuthorizeException 367 { 368 Bitstream bs = getBitstream(item, bitstream_name); 369 370 if (bs == null) 372 { 373 return null; 374 } 375 376 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 378 Utils.copy(bs.retrieve(), baos); 379 380 return baos.toByteArray(); 381 } 382 383 386 private static byte[] fetchURL(String url_string) 387 { 388 try 389 { 390 URL url = new URL (url_string); 391 URLConnection connection = url.openConnection(); 392 byte[] bytes = new byte[connection.getContentLength()]; 393 394 int offset = 0; 396 397 while (true) 398 { 399 int len = connection.getInputStream().read(bytes, offset, 400 bytes.length - offset); 401 402 if (len == -1) 403 { 404 break; 405 } 406 407 offset += len; 408 } 409 410 return bytes; 411 } 412 catch (Exception exc) 413 { 414 return null; 415 } 416 } 417 } 418 | Popular Tags |