KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > license > CreativeCommons


1 /*
2  * CreativeCommons.java
3  *
4  * Version: $Revision: 1.5 $
5  *
6  * Date: $Date: 2006/02/13 10:33:50 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.license;
41
42 import java.io.InputStream JavaDoc;
43 import java.io.ByteArrayInputStream JavaDoc;
44 import java.io.ByteArrayOutputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.net.URL JavaDoc;
47 import java.net.URLConnection JavaDoc;
48 import java.sql.SQLException JavaDoc;
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     /**
62      * The Bundle Name
63      */

64     public static final String JavaDoc CC_BUNDLE_NAME = "CC-LICENSE";
65
66     private static final String JavaDoc CC_BS_SOURCE = "org.dspace.license.CreativeCommons";
67
68     /**
69      * Some BitStream Names (BSN)
70      */

71     private static final String JavaDoc BSN_LICENSE_URL = "license_url";
72
73     private static final String JavaDoc BSN_LICENSE_TEXT = "license_text";
74
75     private static final String JavaDoc BSN_LICENSE_RDF = "license_rdf";
76
77     private static boolean enabled_p;
78
79     static
80     {
81         // we only check the property once
82
enabled_p = ConfigurationManager
83                 .getBooleanProperty("webui.submit.enable-cc");
84
85         if (enabled_p)
86         {
87             // if defined, set a proxy server for http requests to Creative
88
// Commons site
89
String JavaDoc proxyHost = ConfigurationManager
90                     .getProperty("http.proxy.host");
91             String JavaDoc 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     /**
103      * Simple accessor for enabling of CC
104      */

105     public static boolean isEnabled()
106     {
107         return enabled_p;
108     }
109
110         // create the CC bundle if it doesn't exist
111
// If it does, remove it and create a new one.
112
private static Bundle getCcBundle(Item item)
113         throws SQLException JavaDoc, AuthorizeException, IOException JavaDoc
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     /**
126      * This is a bit of the "do-the-right-thing" method for CC stuff in an item
127      */

128     public static void setLicense(Context context, Item item,
129             String JavaDoc cc_license_url) throws SQLException JavaDoc, IOException JavaDoc,
130             AuthorizeException
131     {
132         Bundle bundle = getCcBundle(item);
133
134         // get some more information
135
String JavaDoc license_text = fetchLicenseText(cc_license_url);
136         String JavaDoc license_rdf = fetchLicenseRDF(cc_license_url);
137
138         // here we need to transform the license_rdf into a document_rdf
139
// first we find the beginning of "<License"
140
int license_start = license_rdf.indexOf("<License");
141
142         // the 10 is the length of the license closing tag.
143
int license_end = license_rdf.indexOf("</License>") + 10;
144         String JavaDoc 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         // set the format
156
BitstreamFormat bs_format = BitstreamFormat.findByShortDescription(
157                 context, "License");
158
159         // set the URL bitstream
160
setBitstreamFromBytes(item, bundle, BSN_LICENSE_URL, bs_format,
161                 cc_license_url.getBytes());
162
163         // set the license text bitstream
164
setBitstreamFromBytes(item, bundle, BSN_LICENSE_TEXT, bs_format,
165                 license_text.getBytes());
166
167         // set the RDF bitstream
168
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 JavaDoc licenseStm, String JavaDoc mimeType)
174             throws SQLException JavaDoc, IOException JavaDoc, AuthorizeException
175     {
176         Bundle bundle = getCcBundle(item);
177
178         // generic "License" format -- change for CC?
179
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 JavaDoc, IOException JavaDoc, AuthorizeException
194     {
195         // remove CC license bundle if one exists
196
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 JavaDoc, IOException JavaDoc
206     {
207         // try to find CC license bundle
208
Bundle[] bundles = item.getBundles(CC_BUNDLE_NAME);
209
210         if (bundles.length == 0)
211         {
212             return false;
213         }
214
215         // verify it has correct contents
216
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 JavaDoc getLicenseURL(Item item) throws SQLException JavaDoc,
233             IOException JavaDoc, AuthorizeException
234     {
235         return getStringFromBitstream(item, BSN_LICENSE_URL);
236     }
237
238     public static String JavaDoc getLicenseText(Item item) throws SQLException JavaDoc,
239             IOException JavaDoc, AuthorizeException
240     {
241         return getStringFromBitstream(item, BSN_LICENSE_TEXT);
242     }
243
244     public static String JavaDoc getLicenseRDF(Item item) throws SQLException JavaDoc,
245             IOException JavaDoc, AuthorizeException
246     {
247         return getStringFromBitstream(item, BSN_LICENSE_RDF);
248     }
249
250     /**
251      * Get Creative Commons license RDF, returning Bitstream object.
252      * @return bitstream or null.
253      */

254     public static Bitstream getLicenseRdfBitstream(Item item) throws SQLException JavaDoc,
255             IOException JavaDoc, AuthorizeException
256     {
257         return getBitstream(item, BSN_LICENSE_RDF);
258     }
259
260     /**
261      * Get Creative Commons license Text, returning Bitstream object.
262      * @return bitstream or null.
263      */

264     public static Bitstream getLicenseTextBitstream(Item item) throws SQLException JavaDoc,
265             IOException JavaDoc, AuthorizeException
266     {
267         return getBitstream(item, BSN_LICENSE_TEXT);
268     }
269
270
271     /**
272      * Get a few license-specific properties. We expect these to be cached at
273      * least per server run.
274      */

275     public static String JavaDoc fetchLicenseText(String JavaDoc license_url)
276     {
277         String JavaDoc text_url = license_url;
278         byte[] urlBytes = fetchURL(text_url);
279
280         return (urlBytes != null) ? new String JavaDoc(urlBytes) : "";
281     }
282
283     public static String JavaDoc fetchLicenseRDF(String JavaDoc license_url)
284     {
285         String JavaDoc rdf_url = license_url + "rdf";
286         byte[] urlBytes = fetchURL(rdf_url);
287
288         return (urlBytes != null) ? new String JavaDoc(urlBytes) : "";
289     }
290
291     // The following two helper methods assume that the CC
292
// bitstreams are short and easily expressed as byte arrays in RAM
293

294     /**
295      * This helper method takes some bytes and stores them as a bitstream for an
296      * item, under the CC bundle, with the given bitstream name
297      */

298     private static void setBitstreamFromBytes(Item item, Bundle bundle,
299             String JavaDoc bitstream_name, BitstreamFormat format, byte[] bytes)
300             throws SQLException JavaDoc, IOException JavaDoc, AuthorizeException
301     {
302         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(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         // commit everything
311
bs.update();
312     }
313
314     /**
315      * This helper method wraps a String around a byte array returned from the
316      * bitstream method further down
317      */

318     private static String JavaDoc getStringFromBitstream(Item item,
319             String JavaDoc bitstream_name) throws SQLException JavaDoc, IOException JavaDoc,
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 JavaDoc(bytes);
330     }
331
332     /**
333      * This helper method retrieves the bytes of a bitstream for an item under
334      * the CC bundle, with the given bitstream name
335      */

336     private static Bitstream getBitstream(Item item, String JavaDoc bitstream_name)
337             throws SQLException JavaDoc, IOException JavaDoc, AuthorizeException
338     {
339         Bundle cc_bundle = null;
340
341         // look for the CC bundle
342
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 JavaDoc exc)
356         {
357             // this exception catching is a bit generic,
358
// but basically it happens if there is no CC bundle
359
return null;
360         }
361
362         return cc_bundle.getBitstreamByName(bitstream_name);
363     }
364
365     private static byte[] getBytesFromBitstream(Item item, String JavaDoc bitstream_name)
366             throws SQLException JavaDoc, IOException JavaDoc, AuthorizeException
367     {
368         Bitstream bs = getBitstream(item, bitstream_name);
369
370         // no such bitstream
371
if (bs == null)
372         {
373             return null;
374         }
375
376         // create a ByteArrayOutputStream
377
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
378         Utils.copy(bs.retrieve(), baos);
379
380         return baos.toByteArray();
381     }
382
383     /**
384      * Fetch the contents of a URL
385      */

386     private static byte[] fetchURL(String JavaDoc url_string)
387     {
388         try
389         {
390             URL JavaDoc url = new URL JavaDoc(url_string);
391             URLConnection JavaDoc connection = url.openConnection();
392             byte[] bytes = new byte[connection.getContentLength()];
393
394             // loop and read the data until it's done
395
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 JavaDoc exc)
413         {
414             return null;
415         }
416     }
417 }
418
Popular Tags