/** * Attempt to filter a bitstream * * <p>An exception will be thrown if the media filter class cannot be instantiated, exceptions * from filtering will be logged to STDOUT and swallowed. * * @return true if bitstream processed, false if no applicable filter or already processed */ public static boolean filterBitstream(Context c, Item myItem, Bitstream myBitstream) throws Exception { boolean filtered = false; // iterate through filter classes. A single format may be actioned // by more than one filter for (int i = 0; i < filterClasses.length; i++) { List fmts = (List) filterFormats.get(filterClasses[i].getClass().getName()); if (fmts.contains(myBitstream.getFormat().getShortDescription())) { try { // only update item if bitstream not skipped if (filterClasses[i].processBitstream(c, myItem, myBitstream)) { myItem.update(); // Make sure new bitstream has a sequence // number filtered = true; } } catch (Exception e) { System.out.println( "ERROR filtering, skipping bitstream #" + myBitstream.getID() + " " + e); e.printStackTrace(); } } } return filtered; }
/** * For a bitstream that's a thumbnail or extracted text, find the corresponding bitstream it was * derived from, in the ORIGINAL bundle. * * @param item the item we're dealing with * @param derived the derived bitstream * @return the corresponding original bitstream (or null) */ protected static Bitstream findOriginalBitstream(Item item, Bitstream derived) throws SQLException { Bundle[] bundles = item.getBundles(); // Filename of original will be filename of the derived bitstream // minus the extension (last 4 chars - .jpg or .txt) String originalFilename = derived.getName().substring(0, derived.getName().length() - 4); // First find "original" bundle for (int i = 0; i < bundles.length; i++) { if ((bundles[i].getName() != null) && bundles[i].getName().equals("ORIGINAL")) { // Now find the corresponding bitstream Bitstream[] bitstreams = bundles[i].getBitstreams(); for (int bsnum = 0; bsnum < bitstreams.length; bsnum++) { if (bitstreams[bsnum].getName().equals(originalFilename)) { return bitstreams[bsnum]; } } } } // Didn't find it return null; }
/** * Remove a file from an item * * @param context current DSpace context * @param item Item where file should be removed from * @param bitstreamID The id of bitstream representing the file to remove * @return Status or error flag which will be processed by UI-related code! (if STATUS_COMPLETE or * 0 is returned, no errors occurred!) */ protected int processRemoveFile(Context context, Item item, int bitstreamID) throws IOException, SQLException, AuthorizeException { Bitstream bitstream; // Try to find bitstream try { bitstream = Bitstream.find(context, bitstreamID); } catch (NumberFormatException nfe) { bitstream = null; } if (bitstream == null) { // Invalid or mangled bitstream ID // throw an error and return immediately return STATUS_INTEGRITY_ERROR; } // remove bitstream from bundle.. // delete bundle if it's now empty Bundle[] bundles = bitstream.getBundles(); bundles[0].removeBitstream(bitstream); Bitstream[] bitstreams = bundles[0].getBitstreams(); // remove bundle if it's now empty if (bitstreams.length < 1) { item.removeBundle(bundles[0]); item.update(); } // no errors occurred return STATUS_COMPLETE; }
/** * Return this resource's children. Item's children are its bitstreams. * * @return the DAV resource[] * @throws SQLException the SQL exception */ @Override protected DAVResource[] children() throws SQLException { // Check for overall read permission on Item if (!AuthorizeManager.authorizeActionBoolean(this.context, this.item, Constants.READ)) { return new DAVResource[0]; } Vector result = new Vector(); Bundle[] bundles = this.item.getBundles(); for (Bundle element : bundles) { // check read permission on this Bundle if (!AuthorizeManager.authorizeActionBoolean(this.context, element, Constants.READ)) { continue; } Bitstream[] bitstreams = element.getBitstreams(); for (Bitstream element0 : bitstreams) { String ext[] = element0.getFormat().getExtensions(); result.add( new DAVBitstream( this.context, this.request, this.response, makeChildPath( DAVBitstream.getPathElt( element0.getSequenceID(), ext.length < 1 ? null : ext[0])), this.item, element0)); } } return (DAVResource[]) result.toArray(new DAVResource[result.size()]); }
private void populateEntry(Context context, Entry entry, Bitstream bitstream) throws DSpaceSwordException { BitstreamFormat format = bitstream.getFormat(); String contentType = null; if (format != null) { contentType = format.getMIMEType(); } SwordUrlManager urlManager = new SwordUrlManager(new SwordConfigurationDSpace(), context); String bsUrl = urlManager.getBitstreamUrl(bitstream); entry.setId(bsUrl); entry.setTitle(bitstream.getName()); String desc = bitstream.getDescription(); if ("".equals(desc) || desc == null) { desc = bitstream.getName(); } entry.setSummary(desc); entry.setUpdated(new Date()); // required, though content is spurious // add an edit-media link for the bitstream ... Abdera abdera = new Abdera(); Link link = abdera.getFactory().newLink(); link.setHref(urlManager.getActionableBitstreamUrl(bitstream)); link.setMimeType(contentType); link.setRel("edit-media"); entry.addLink(link); // set the content of the bitstream entry.setContent(new IRI(bsUrl), contentType); }
/** * Make the contents of an external resource mentioned in an <code>mdRef</code> element * available as an <code>InputStream</code>. See the <code>METSManifest.MdRef</code> interface * for details. * * @param mdref the METS mdRef element to locate the input for. * @return the input stream of its content. */ public InputStream getInputStream(Element mdref) throws MetadataValidationException, IOException, SQLException, AuthorizeException { Bitstream mdbs = getBitstreamForMdRef(mdref); if (mdbs == null) throw new MetadataValidationException( "Failed dereferencing bitstream for mdRef element=" + mdref.toString()); return mdbs.retrieve(); }
// returns absolute URL to download content of bitstream (which might not belong to any Item) protected String urlOfBitstream(HttpServletRequest request, Bitstream logo) { String name = logo.getName(); return resolveURL(request, null) + (uiType.equalsIgnoreCase(UITYPE_XMLUI) ? "/bitstream/id/" : "/retrieve/") + logo.getID() + "/" + (name == null ? "" : name); }
/** * each entry represents a bitstream.... * * @param c * @param i * @param path * @param fileName * @param bundleName * @throws SQLException * @throws IOException * @throws AuthorizeException */ private void processContentFileEntry( Context c, Item i, String path, String fileName, String bundleName, boolean primary) throws SQLException, IOException, AuthorizeException { String fullpath = path + File.separatorChar + fileName; // get an input stream BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fullpath)); Bitstream bs = null; String newBundleName = bundleName; if (bundleName == null) { // is it license.txt? if ("license.txt".equals(fileName)) { newBundleName = "LICENSE"; } else { // call it ORIGINAL newBundleName = "ORIGINAL"; } } if (!isTest) { // find the bundle Bundle[] bundles = i.getBundles(newBundleName); Bundle targetBundle = null; if (bundles.length < 1) { // not found, create a new one targetBundle = i.createBundle(newBundleName); } else { // put bitstreams into first bundle targetBundle = bundles[0]; } // now add the bitstream bs = targetBundle.createBitstream(bis); bs.setName(fileName); // Identify the format // FIXME - guessing format guesses license.txt incorrectly as a text // file format! BitstreamFormat bf = FormatIdentifier.guessFormat(c, bs); bs.setFormat(bf); // Is this a the primary bitstream? if (primary) { targetBundle.setPrimaryBitstreamID(bs.getID()); targetBundle.update(); } bs.update(); } bis.close(); }
public static Document getRssDocument(Bitstream rssUrlBitstream) { try { return getRssDocument( rssUrlBitstream.getbContext(), rssUrlBitstream.retrieve(), rssUrlBitstream.getBundles()[0]); } catch (Exception e) { ExceptionLogger.logException(log, e); } return null; }
/** * Get the license for this Item as String. License is the first bitstream named "license.txt" in * a LICENSE bundle, apparently? * * <p>FIXME: is this correct? there's no counterexample.. * * @return license string, or null if none found. * @throws SQLException the SQL exception * @throws AuthorizeException the authorize exception * @throws IOException Signals that an I/O exception has occurred. */ private String getLicenseAsString() throws SQLException, AuthorizeException, IOException { Bundle lb[] = this.item.getBundles(Constants.LICENSE_BUNDLE_NAME); for (Bundle element : lb) { Bitstream lbs = element.getBitstreamByName("license.txt"); if (lbs != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream((int) lbs.getSize()); Utils.copy(lbs.retrieve(), baos); return baos.toString(); } } return null; }
@Override public int perform(DSpaceObject dso) throws IOException { status = Curator.CURATE_SKIP; logDebugMessage("The target dso is " + dso.getName()); if (dso instanceof Item) { status = Curator.CURATE_SUCCESS; Item item = (Item) dso; try { openSession(); } catch (IOException ioE) { // no point going further - set result and error out closeSession(); setResult(CONNECT_FAIL_MESSAGE); return Curator.CURATE_ERROR; } try { Bundle bundle = item.getBundles("ORIGINAL")[0]; results = new ArrayList<String>(); for (Bitstream bitstream : bundle.getBitstreams()) { InputStream inputstream = bitstream.retrieve(); logDebugMessage("Scanning " + bitstream.getName() + " . . . "); int bstatus = scan(bitstream, inputstream, getItemHandle(item)); inputstream.close(); if (bstatus == Curator.CURATE_ERROR) { // no point going further - set result and error out setResult(SCAN_FAIL_MESSAGE); status = bstatus; break; } if (failfast && bstatus == Curator.CURATE_FAIL) { status = bstatus; break; } else if (bstatus == Curator.CURATE_FAIL && status == Curator.CURATE_SUCCESS) { status = bstatus; } } } catch (AuthorizeException authE) { throw new IOException(authE.getMessage(), authE); } catch (SQLException sqlE) { throw new IOException(sqlE.getMessage(), sqlE); } finally { closeSession(); } if (status != Curator.CURATE_ERROR) { formatResults(item); } } return status; }
/** * Delete bitstream from item * * @param context * @param ItemArchive * @param isTest * @param suppressUndo * @throws IllegalArgumentException * @throws ParseException * @throws IOException * @throws AuthorizeException * @throws SQLException */ public void execute(Context context, ItemArchive itarch, boolean isTest, boolean suppressUndo) throws IllegalArgumentException, IOException, SQLException, AuthorizeException, ParseException { File f = new File(itarch.getDirectory(), ItemUpdate.DELETE_CONTENTS_FILE); if (!f.exists()) { ItemUpdate.pr( "Warning: Delete_contents file for item " + itarch.getDirectoryName() + " not found."); } else { List<Integer> list = MetadataUtilities.readDeleteContentsFile(f); if (list.isEmpty()) { ItemUpdate.pr("Warning: empty delete_contents file for item " + itarch.getDirectoryName()); } else { for (int id : list) { try { Bitstream bs = Bitstream.find(context, id); if (bs == null) { ItemUpdate.pr("Bitstream not found by id: " + id); } else { Bundle[] bundles = bs.getBundles(); for (Bundle b : bundles) { if (isTest) { ItemUpdate.pr("Delete bitstream with id = " + id); } else { b.removeBitstream(bs); ItemUpdate.pr("Deleted bitstream with id = " + id); } } if (alterProvenance) { DtoMetadata dtom = DtoMetadata.create("dc.description.provenance", "en", ""); String append = "Bitstream " + bs.getName() + " deleted on " + DCDate.getCurrent() + "; "; Item item = bundles[0].getItems()[0]; ItemUpdate.pr("Append provenance with: " + append); if (!isTest) { MetadataUtilities.appendMetadata(item, dtom, false, append); } } } } catch (SQLException e) { ItemUpdate.pr("Error finding bitstream from id: " + id + " : " + e.toString()); } } } } }
@GET @Path("/{bitstream_id}") @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public Bitstream getBitstream( @PathParam("bitstream_id") Integer bitstream_id, @QueryParam("expand") String expand) { org.dspace.core.Context context = null; try { context = new org.dspace.core.Context(); org.dspace.content.Bitstream bitstream = org.dspace.content.Bitstream.find(context, bitstream_id); if (AuthorizeManager.authorizeActionBoolean( context, bitstream, org.dspace.core.Constants.READ)) { return new org.dspace.rest.common.Bitstream(bitstream, expand); } else { throw new WebApplicationException(Response.Status.UNAUTHORIZED); } } catch (SQLException e) { log.error(e.getMessage()); throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); } finally { if (context != null) { try { context.complete(); } catch (SQLException e) { log.error(e.getMessage() + " occurred while trying to close"); } } } }
/** * Fast stream copy routine * * @param b * @return * @throws IOException * @throws SQLException * @throws AuthorizeException */ public static byte[] copy(Bitstream b) throws IOException, SQLException, AuthorizeException { InputStream in = null; ByteArrayOutputStream out = null; try { in = b.retrieve(); out = new ByteArrayOutputStream(); while (true) { synchronized (buffer) { int amountRead = in.read(buffer); if (amountRead == -1) { break; } out.write(buffer, 0, amountRead); } } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } return out.toByteArray(); }
private void populateTableRowFromCollection(Collection collection, TableRow row) { int id = collection.getID(); Bitstream logo = collection.getLogo(); Item templateItem = collection.getTemplateItem(); Group admins = collection.getAdministrators(); Group[] workflowGroups = collection.getWorkflowGroups(); if (logo == null) { row.setColumnNull("logo_bitstream_id"); } else { row.setColumn("logo_bitstream_id", logo.getID()); } if (templateItem == null) { row.setColumnNull("template_item_id"); } else { row.setColumn("template_item_id", templateItem.getID()); } if (admins == null) { row.setColumnNull("admin"); } else { row.setColumn("admin", admins.getID()); } for (int i = 1; i <= workflowGroups.length; i++) { Group g = workflowGroups[i - 1]; if (g == null) { row.setColumnNull("workflow_step_" + i); } else { row.setColumn("workflow_step_" + i, g.getID()); } } // Now loop over all allowed metadata fields and set the value into the // TableRow. for (CollectionMetadataField field : CollectionMetadataField.values()) { String value = collection.getMetadata(field.toString()); if (value == null) { row.setColumnNull(field.toString()); } else { row.setColumn(field.toString(), value); } } row.setColumn("uuid", collection.getIdentifier().getUUID().toString()); }
/** * Register the bitstream file into DSpace * * @param c * @param i * @param assetstore * @param bitstreamPath the full filepath expressed in the contents file * @param bundleName * @throws SQLException * @throws IOException * @throws AuthorizeException */ private void registerBitstream( Context c, Item i, int assetstore, String bitstreamPath, String bundleName) throws SQLException, IOException, AuthorizeException { // TODO validate assetstore number // TODO make sure the bitstream is there Bitstream bs = null; String newBundleName = bundleName; if (bundleName == null) { // is it license.txt? if (bitstreamPath.endsWith("license.txt")) { newBundleName = "LICENSE"; } else { // call it ORIGINAL newBundleName = "ORIGINAL"; } } if (!isTest) { // find the bundle Bundle[] bundles = i.getBundles(newBundleName); Bundle targetBundle = null; if (bundles.length < 1) { // not found, create a new one targetBundle = i.createBundle(newBundleName); } else { // put bitstreams into first bundle targetBundle = bundles[0]; } // now add the bitstream bs = targetBundle.registerBitstream(assetstore, bitstreamPath); // set the name to just the filename int iLastSlash = bitstreamPath.lastIndexOf('/'); bs.setName(bitstreamPath.substring(iLastSlash + 1)); // Identify the format // FIXME - guessing format guesses license.txt incorrectly as a text file format! BitstreamFormat bf = FormatIdentifier.guessFormat(c, bs); bs.setFormat(bf); bs.update(); } }
@GET @Path("/{bitstream_id}/retrieve") public javax.ws.rs.core.Response getFile( @PathParam("bitstream_id") final Integer bitstream_id, @QueryParam("userIP") String user_ip, @QueryParam("userAgent") String user_agent, @QueryParam("xforwarderfor") String xforwarderfor, @Context HttpHeaders headers, @Context HttpServletRequest request) { org.dspace.core.Context context = null; try { context = new org.dspace.core.Context(); org.dspace.content.Bitstream bitstream = org.dspace.content.Bitstream.find(context, bitstream_id); if (AuthorizeManager.authorizeActionBoolean( context, bitstream, org.dspace.core.Constants.READ)) { if (writeStatistics) { writeStats(context, bitstream_id, user_ip, user_agent, xforwarderfor, headers, request); } return Response.ok(bitstream.retrieve()).type(bitstream.getFormat().getMIMEType()).build(); } else { throw new WebApplicationException(Response.Status.UNAUTHORIZED); } } catch (IOException e) { log.error(e.getMessage()); throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); } catch (SQLException e) { log.error(e.getMessage()); throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); } catch (AuthorizeException e) { log.error(e.getMessage()); throw new WebApplicationException(Response.Status.UNAUTHORIZED); } finally { if (context != null) { try { context.complete(); } catch (SQLException e) { log.error(e.getMessage() + " occurred while trying to close"); } } } }
/** * Returns canonical link to a bitstream in the item. * * @param item The DSpace Item that the bitstream is part of * @param bitstream The bitstream to link to * @returns a String link to the bitstream */ private String makeBitstreamLink(Item item, Bitstream bitstream) { String name = bitstream.getName(); StringBuilder result = new StringBuilder(contextPath); result.append("/bitstream/item/").append(String.valueOf(item.getID())); // append name although it isn't strictly necessary try { if (name != null) { result.append("/").append(Util.encodeBitstreamName(name, "UTF-8")); } } catch (UnsupportedEncodingException uee) { // just ignore it, we don't have to have a pretty // name on the end of the url because the sequence id will // locate it. However it means that links in this file might // not work.... } result.append("?sequence=").append(String.valueOf(bitstream.getSequenceID())); return result.toString(); }
// utility to grovel bitstream formats.. private static void setFormatToMIMEType(Context context, Bitstream bs, String mimeType) throws SQLException { BitstreamFormat bf[] = BitstreamFormat.findNonInternal(context); for (int i = 0; i < bf.length; ++i) { if (bf[i].getMIMEType().equalsIgnoreCase(mimeType)) { bs.setFormat(bf[i]); break; } } }
/* (non-Javadoc) * @see org.dspace.app.dav.DAVResource#proppatchInternal(int, org.jdom.Element) */ @Override protected int proppatchInternal(int action, Element prop) throws SQLException, AuthorizeException, IOException, DAVStatusException { String newValue = (action == DAV.PROPPATCH_REMOVE) ? null : prop.getText(); // these are "metadata" values.. if (elementsEqualIsh(prop, short_descriptionProperty) || elementsEqualIsh(prop, introductory_textProperty) || elementsEqualIsh(prop, side_bar_textProperty) || elementsEqualIsh(prop, copyright_textProperty) || elementsEqualIsh(prop, provenance_descriptionProperty)) { this.collection.setMetadata(prop.getName(), newValue); } else if (elementsEqualIsh(prop, displaynameProperty)) { this.collection.setMetadata("name", newValue); } else if (elementsEqualIsh(prop, default_licenseProperty)) { this.collection.setLicense(newValue); } else if (elementsEqualIsh(prop, logoProperty)) { if (action == DAV.PROPPATCH_REMOVE) { this.collection.setLogo(null); } else { Element bs = prop.getChild("bitstream", DAV.NS_DSPACE); if (bs != null) { InputStream bis = DAVBitstream.getXmlBitstreamContent(this.context, bs); BitstreamFormat bsf = DAVBitstream.getXmlBitstreamFormat(this.context, bs); if (bis == null || bsf == null) { throw new DAVStatusException(DAV.SC_CONFLICT, "Unacceptable value for logo property."); } Bitstream nbs = this.collection.setLogo(bis); nbs.setFormat(bsf); nbs.update(); } else { throw new DAVStatusException( DAV.SC_CONFLICT, "No <bitstream> element value found for logo property."); } } } else { throw new DAVStatusException( DAV.SC_CONFLICT, "The " + prop.getName() + " property cannot be changed."); } this.collection.update(); return HttpServletResponse.SC_OK; }
/** * VERY crude dissemination: just look for the first bitstream with the PDF package type, and toss * it out. Works on packages importer with this packager, and maybe some others. */ public void disseminate( Context context, DSpaceObject dso, PackageParameters params, OutputStream out) throws PackageValidationException, CrosswalkException, AuthorizeException, SQLException, IOException { if (dso.getType() != Constants.ITEM) throw new PackageValidationException( "This disseminator can only handle objects of type ITEM."); Item item = (Item) dso; try { BitstreamFormat pdff = BitstreamFormat.findByShortDescription(context, BITSTREAM_FORMAT_NAME); if (pdff == null) throw new PackageValidationException( "Cannot find BitstreamFormat \"" + BITSTREAM_FORMAT_NAME + "\""); Bitstream pkgBs = PackageUtils.getBitstreamByFormat(item, pdff, Constants.DEFAULT_BUNDLE_NAME); if (pkgBs == null) throw new PackageValidationException( "Cannot find Bitstream with format \"" + BITSTREAM_FORMAT_NAME + "\""); Utils.copy(pkgBs.retrieve(), out); } finally { } }
/** * Each submission step must define its own information to be reviewed during the final * Review/Verify Step in the submission process. * * <p>The information to review should be tacked onto the passed in List object. * * <p>NOTE: To remain consistent across all Steps, you should first add a sub-List object (with * this step's name as the heading), by using a call to reviewList.addList(). This sublist is the * list you return from this method! * * @param reviewList The List to which all reviewable information should be added * @return The new sub-List object created by this step, which contains all the reviewable * information. If this step has nothing to review, then return null! */ public List addReviewSection(List reviewList) throws SAXException, WingException, UIException, SQLException, IOException, AuthorizeException { // Create a new list section for this step (and set its heading) List uploadSection = reviewList.addList("submit-review-" + this.stepAndPage, List.TYPE_FORM); uploadSection.setHead(T_head); // Review all uploaded files Item item = submission.getItem(); Bundle[] bundles = item.getBundles("ORIGINAL"); Bitstream[] bitstreams = new Bitstream[0]; if (bundles.length > 0) { bitstreams = bundles[0].getBitstreams(); } for (Bitstream bitstream : bitstreams) { BitstreamFormat bitstreamFormat = bitstream.getFormat(); String name = bitstream.getName(); String url = makeBitstreamLink(item, bitstream); String format = bitstreamFormat.getShortDescription(); Message support = ReviewStep.T_unknown; if (bitstreamFormat.getSupportLevel() == BitstreamFormat.KNOWN) { support = T_known; } else if (bitstreamFormat.getSupportLevel() == BitstreamFormat.SUPPORTED) { support = T_supported; } org.dspace.app.xmlui.wing.element.Item file = uploadSection.addItem(); file.addXref(url, name); file.addContent(" - " + format + " "); file.addContent(support); } // return this new "upload" section return uploadSection; }
/** * Tests bitstream for membership in specified bundles (ORIGINAL, TEXT, THUMBNAIL) * * @param bitstream * @throws BitstreamFilterException * @returns true if bitstream is in specified bundles */ public boolean accept(Bitstream bitstream) throws BitstreamFilterException { try { Bundle[] bundles = bitstream.getBundles(); for (Bundle b : bundles) { for (String bn : bundlesToEmpty) { if (b.getName().equals(bn)) { return true; } } } } catch (SQLException e) { throw new BitstreamFilterException(e); } return false; }
/* If we created a new Bitstream but now realised there is a problem then remove it. */ private void backoutBitstream(SubmissionInfo subInfo, Bitstream b, Item item) throws SQLException, AuthorizeException, IOException { // remove bitstream from bundle.. // delete bundle if it's now empty Bundle[] bnd = b.getBundles(); bnd[0].removeBitstream(b); Bitstream[] bitstreams = bnd[0].getBitstreams(); // remove bundle if it's now empty if (bitstreams.length < 1) { item.removeBundle(bnd[0]); item.update(); } subInfo.setBitstream(null); }
/** * Perform a deposit, using the supplied SWORD Deposit object. * * @param deposit * @throws SWORDErrorException * @throws DSpaceSWORDException */ public DepositResult doDeposit(Deposit deposit) throws SWORDErrorException, DSpaceSWORDException { // get the things out of the service that we need Context context = swordService.getContext(); SWORDConfiguration swordConfig = swordService.getSwordConfig(); SWORDUrlManager urlManager = swordService.getUrlManager(); // FIXME: the spec is unclear what to do in this situation. I'm going // the throw a 415 (ERROR_CONTENT) until further notice // // determine if this is an acceptable file format if (!swordConfig.isAcceptableContentType(context, deposit.getContentType(), collection)) { log.error( "Unacceptable content type detected: " + deposit.getContentType() + " for collection " + collection.getID()); throw new SWORDErrorException( ErrorCodes.ERROR_CONTENT, "Unacceptable content type in deposit request: " + deposit.getContentType()); } // determine if this is an acceptable packaging type for the deposit // if not, we throw a 415 HTTP error (Unsupported Media Type, ERROR_CONTENT) if (!swordConfig.isSupportedMediaType(deposit.getPackaging(), this.collection)) { log.error( "Unacceptable packaging type detected: " + deposit.getPackaging() + "for collection" + collection.getID()); throw new SWORDErrorException( ErrorCodes.ERROR_CONTENT, "Unacceptable packaging type in deposit request: " + deposit.getPackaging()); } // Obtain the relevant ingester from the factory SWORDIngester si = SWORDIngesterFactory.getInstance(context, deposit, collection); swordService.message("Loaded ingester: " + si.getClass().getName()); // do the deposit DepositResult result = si.ingest(swordService, deposit, collection); swordService.message("Archive ingest completed successfully"); // if there's an item availalble, and we want to keep the original // then do that try { if (swordConfig.isKeepOriginal()) { swordService.message( "DSpace will store an original copy of the deposit, " + "as well as ingesting the item into the archive"); // in order to be allowed to add the file back to the item, we need to ignore authorisations // for a moment boolean ignoreAuth = context.ignoreAuthorization(); context.setIgnoreAuthorization(true); String bundleName = ConfigurationManager.getProperty("sword-server", "bundle.name"); if (bundleName == null || "".equals(bundleName)) { bundleName = "SWORD"; } Item item = result.getItem(); Bundle[] bundles = item.getBundles(bundleName); Bundle swordBundle = null; if (bundles.length > 0) { swordBundle = bundles[0]; } if (swordBundle == null) { swordBundle = item.createBundle(bundleName); } String fn = swordService.getFilename(context, deposit, true); Bitstream bitstream; FileInputStream fis = null; try { fis = new FileInputStream(deposit.getFile()); bitstream = swordBundle.createBitstream(fis); } finally { if (fis != null) { fis.close(); } } bitstream.setName(fn); bitstream.setDescription("SWORD deposit package"); BitstreamFormat bf = BitstreamFormat.findByMIMEType(context, deposit.getContentType()); if (bf != null) { bitstream.setFormat(bf); } bitstream.update(); swordBundle.update(); item.update(); swordService.message( "Original package stored as " + fn + ", in item bundle " + swordBundle); // now reset the context ignore authorisation context.setIgnoreAuthorization(ignoreAuth); // set the media link for the created item result.setMediaLink(urlManager.getMediaLink(bitstream)); } else { // set the vanilla media link, which doesn't resolve to anything result.setMediaLink(urlManager.getBaseMediaLinkUrl()); } } catch (SQLException e) { log.error("caught exception: ", e); throw new DSpaceSWORDException(e); } catch (AuthorizeException e) { log.error("caught exception: ", e); throw new DSpaceSWORDException(e); } catch (FileNotFoundException e) { log.error("caught exception: ", e); throw new DSpaceSWORDException(e); } catch (IOException e) { log.error("caught exception: ", e); throw new DSpaceSWORDException(e); } return result; }
public void processDSpaceObject( Context context, HttpServletRequest request, HttpServletResponse response, DSpaceObject dso, String extraPathInfo) throws ServletException, IOException, SQLException, AuthorizeException { // OK, we have a valid URI. What is it? if (dso.getType() == Constants.BITSTREAM) { // FIXME: Check for if-modified-since header Bitstream bitstream = (Bitstream) dso; log.info( LogManager.getHeader(context, "view_bitstream", "bitstream_id=" + bitstream.getID())); // Pipe the bits // InputStream is = bitstream.retrieve(); InputStream is = BitstreamStorageManager.retrieve(context, bitstream); // Set the response MIME type response.setContentType(bitstream.getFormat().getMIMEType()); response.setHeader("Content-Length", String.valueOf(bitstream.getSize())); response.setHeader("Content-disposition", "attachment; filename=" + bitstream.getName()); Utils.bufferedCopy(is, response.getOutputStream()); is.close(); response.getOutputStream().flush(); } else if (dso.getType() == Constants.ITEM) { Item item = (Item) dso; response.setDateHeader("Last-Modified", item.getLastModified().getTime()); // Check for if-modified-since header long modSince = request.getDateHeader("If-Modified-Since"); if (modSince != -1 && item.getLastModified().getTime() < modSince) { // Item has not been modified since requested date, // hence bitstream has not; return 304 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } else { // Display the item page displayItem(context, request, response, item); } } else if (dso.getType() == Constants.COLLECTION) { Collection c = (Collection) dso; // Store collection location in request request.setAttribute("dspace.collection", c); /* * Find the "parent" community the collection, mainly for * "breadcrumbs" FIXME: At the moment, just grab the first community * the collection is in. This should probably be more context * sensitive when we have multiple inclusion. */ Community[] parents = (Community[]) c.getCommunities().toArray(); request.setAttribute("dspace.community", parents[0]); /* * Find all the "parent" communities for the collection for * "breadcrumbs" */ request.setAttribute("dspace.communities", getParents(parents[0], true)); // home page, or forward to another page? if ((extraPathInfo == null) || (extraPathInfo.equals("/"))) { collectionHome(context, request, response, parents[0], c); } else { // Forward to another servlet request.getRequestDispatcher(extraPathInfo).forward(request, response); } } else if (dso.getType() == Constants.COMMUNITY) { Community c = (Community) dso; // Store collection location in request request.setAttribute("dspace.community", c); /* * Find all the "parent" communities for the community */ request.setAttribute("dspace.communities", getParents(c, false)); // home page, or forward to another page? if ((extraPathInfo == null) || (extraPathInfo.equals("/"))) { communityHome(context, request, response, c); } else { // Forward to another servlet request.getRequestDispatcher(extraPathInfo).forward(request, response); } } else { // Shouldn't happen. Log and treat as invalid ID log.info( LogManager.getHeader( context, "URI not an item, collection or community", "identifier=" + dso.getIdentifier().toString())); JSPManager.showInvalidIDError(request, response, request.getPathInfo(), -1); return; } }
protected void doDSGet(Context context, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, AuthorizeException { ExternalIdentifierDAO identifierDAO = ExternalIdentifierDAOFactory.getInstance(context); Item item = null; Bitstream bitstream = null; String idString = request.getPathInfo(); String filenameNoPath = null; String fullpath = null; String uri = null; // Parse URL if (idString != null) { // Remove leading slash if (idString.startsWith("/")) { idString = idString.substring(1); } // Get uri and full file path int slashIndex = idString.indexOf('/'); if (slashIndex != -1) { slashIndex = idString.indexOf('/', slashIndex + 1); if (slashIndex != -1) { uri = idString.substring(0, slashIndex); fullpath = URLDecoder.decode(idString.substring(slashIndex + 1), Constants.DEFAULT_ENCODING); // Get filename with no path slashIndex = fullpath.indexOf('/'); if (slashIndex != -1) { String[] pathComponents = fullpath.split("/"); if (pathComponents.length <= maxDepthGuess + 1) { filenameNoPath = pathComponents[pathComponents.length - 1]; } } } } } if (uri != null && fullpath != null) { // Find the item try { /* * If the original item doesn't have a persistent identifier * yet (because it's in the workflow) what we actually have is * a URL of the form: db-id/1234 where 1234 is the database ID * of the item. * * FIXME: This first part could be totally omitted now that we * have the dsi:x/y format of identification. */ if (uri.startsWith("db-id")) { String dbIDString = uri.substring(uri.indexOf('/') + 1); int dbID = Integer.parseInt(dbIDString); item = ItemDAOFactory.getInstance(context).retrieve(dbID); } else { ExternalIdentifier identifier = identifierDAO.retrieve(uri); ObjectIdentifier oi = identifier.getObjectIdentifier(); item = (Item) oi.getObject(context); } } catch (NumberFormatException nfe) { // Invalid ID - this will be dealt with below } } if (item != null) { // Try to find bitstream with exactly matching name + path bitstream = getItemBitstreamByName(item, fullpath); if (bitstream == null && filenameNoPath != null) { // No match with the full path, but we can try again with // only the filename bitstream = getItemBitstreamByName(item, filenameNoPath); } } // Did we get a bitstream? if (bitstream != null) { log.info( LogManager.getHeader( context, "view_html", "uri=" + uri + ",bitstream_id=" + bitstream.getID())); // Set the response MIME type response.setContentType(bitstream.getFormat().getMIMEType()); // Response length response.setHeader("Content-Length", String.valueOf(bitstream.getSize())); // Pipe the bits InputStream is = bitstream.retrieve(); Utils.bufferedCopy(is, response.getOutputStream()); is.close(); response.getOutputStream().flush(); } else { // No bitstream - we got an invalid ID log.info(LogManager.getHeader(context, "view_html", "invalid_bitstream_id=" + idString)); JSPManager.showInvalidIDError(request, response, idString, Constants.BITSTREAM); } }
public void setup(org.dspace.content.Bitstream bitstream, String expand) throws SQLException { List<String> expandFields = new ArrayList<String>(); if (expand != null) { expandFields = Arrays.asList(expand.split(",")); } // A logo bitstream might not have a bundle... if (bitstream.getBundles() != null & bitstream.getBundles().length >= 0) { if (bitstream.getParentObject().getType() == Constants.ITEM) { bundleName = bitstream.getBundles()[0].getName(); } } description = bitstream.getDescription(); format = bitstream.getFormatDescription(); sizeBytes = bitstream.getSize(); retrieveLink = "/bitstreams/" + bitstream.getID() + "/retrieve"; mimeType = bitstream.getFormat().getMIMEType(); sequenceId = bitstream.getSequenceID(); CheckSum checkSum = new CheckSum(); checkSum.setCheckSumAlgorith(bitstream.getChecksumAlgorithm()); checkSum.setValue(bitstream.getChecksum()); this.setCheckSum(checkSum); if (expandFields.contains("parent") || expandFields.contains("all")) { parentObject = new DSpaceObject(bitstream.getParentObject()); } else { this.addExpand("parent"); } if (!expandFields.contains("all")) { this.addExpand("all"); } }
/** * perform the ingest using the given deposit object onto the specified target dspace object, * using the sword service implementation * * @param service * @param deposit * @param target * @return * @throws DSpaceSWORDException * @throws SWORDErrorException */ public DepositResult ingest(SWORDService service, Deposit deposit, DSpaceObject target) throws DSpaceSWORDException, SWORDErrorException { try { if (!(target instanceof Item)) { throw new DSpaceSWORDException( "SimpleFileIngester can only be loaded for deposit onto DSpace Items"); } Item item = (Item) target; // now set the sword service SWORDService swordService = service; // get the things out of the service that we need Context context = swordService.getContext(); SWORDUrlManager urlManager = swordService.getUrlManager(); Bundle[] bundles = item.getBundles("ORIGINAL"); Bundle original; if (bundles.length > 0) { original = bundles[0]; } else { original = item.createBundle("ORIGINAL"); } Bitstream bs; FileInputStream fis = null; try { fis = new FileInputStream(deposit.getFile()); bs = original.createBitstream(fis); } finally { if (fis != null) { fis.close(); } } String fn = swordService.getFilename(context, deposit, false); bs.setName(fn); swordService.message("File created in item with filename " + fn); BitstreamFormat bf = BitstreamFormat.findByMIMEType(context, deposit.getContentType()); if (bf != null) { bs.setFormat(bf); } // to do the updates, we need to ignore authorisation in the context boolean ignoreAuth = context.ignoreAuthorization(); context.setIgnoreAuthorization(true); bs.update(); original.update(); item.update(); // reset the ignore authorisation context.setIgnoreAuthorization(ignoreAuth); DepositResult result = new DepositResult(); result.setHandle(urlManager.getBitstreamUrl(bs)); result.setTreatment(this.getTreatment()); result.setBitstream(bs); return result; } catch (SQLException e) { throw new DSpaceSWORDException(e); } catch (AuthorizeException e) { throw new DSpaceSWORDException(e); } catch (IOException e) { throw new DSpaceSWORDException(e); } }
/** * Process Item, correcting CC-License if encountered. * * @param item * @throws SQLException * @throws AuthorizeException * @throws IOException */ protected static void handleItem(Item item) throws SQLException, AuthorizeException, IOException { Bundle[] bundles = item.getBundles("CC-LICENSE"); if (bundles == null || bundles.length == 0) return; Bundle bundle = bundles[0]; Bitstream bitstream = bundle.getBitstreamByName("license_rdf"); String license_rdf = new String(copy(bitstream)); /* quickly fix xml by ripping out offensive parts */ license_rdf = license_rdf.replaceFirst("<license", ""); license_rdf = license_rdf.replaceFirst("</license>", ""); StringWriter result = new StringWriter(); try { templates .newTransformer() .transform( new StreamSource(new ByteArrayInputStream(license_rdf.getBytes())), new StreamResult(result)); } catch (TransformerException e) { throw new RuntimeException(e.getMessage(), e); } StringBuffer buffer = result.getBuffer(); Bitstream newBitstream = bundle.createBitstream(new ByteArrayInputStream(buffer.toString().getBytes())); newBitstream.setName(bitstream.getName()); newBitstream.setDescription(bitstream.getDescription()); newBitstream.setFormat(bitstream.getFormat()); newBitstream.setSource(bitstream.getSource()); newBitstream.setUserFormatDescription(bitstream.getUserFormatDescription()); newBitstream.update(); bundle.removeBitstream(bitstream); bundle.update(); }