コード例 #1
0
ファイル: DAVItem.java プロジェクト: edina-jorum/Jorum-DSpace
  /**
   * GET implementation returns the contents of the Item as a package. The query arg "package" must
   * be specified.
   *
   * @throws SQLException the SQL exception
   * @throws AuthorizeException the authorize exception
   * @throws ServletException the servlet exception
   * @throws IOException Signals that an I/O exception has occurred.
   * @throws DAVStatusException the DAV status exception
   */
  @Override
  protected void get() throws SQLException, AuthorizeException, IOException, DAVStatusException {
    // Check for overall read permission on Item, because nothing else will
    AuthorizeManager.authorizeAction(this.context, this.item, Constants.READ);

    String packageType = this.request.getParameter("package");
    Bundle[] original = this.item.getBundles("ORIGINAL");
    int bsid;

    if (packageType == null) {
      packageType = "default";
    }
    PackageDisseminator dip =
        (PackageDisseminator) PluginManager.getNamedPlugin(PackageDisseminator.class, packageType);
    if (dip == null) {
      throw new DAVStatusException(
          HttpServletResponse.SC_BAD_REQUEST,
          "Cannot find a disseminate plugin for package=" + packageType);
    } else {
      try {
        PackageParameters pparams = PackageParameters.create(this.request);
        this.response.setContentType(dip.getMIMEType(pparams));
        dip.disseminate(this.context, this.item, pparams, this.response.getOutputStream());
      } catch (CrosswalkException pe) {
        throw new DAVStatusException(
            HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
            "Failed in crosswalk of metadata: " + pe.toString());
      } catch (PackageException pe) {
        pe.log(log);
        throw new DAVStatusException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, pe.toString());
      }
    }
  }
コード例 #2
0
  public Element disseminateElement(DSpaceObject dso)
      throws CrosswalkException, IOException, SQLException, AuthorizeException {
    if (dso.getType() != Constants.ITEM) {
      throw new CrosswalkObjectNotSupported(
          "METSDisseminationCrosswalk can only crosswalk an Item.");
    }
    Item item = (Item) dso;

    PackageDisseminator dip =
        (PackageDisseminator)
            PluginManager.getNamedPlugin(PackageDisseminator.class, METS_PACKAGER_PLUGIN);
    if (dip == null) {
      throw new CrosswalkInternalException(
          "Cannot find a disseminate plugin for package=" + METS_PACKAGER_PLUGIN);
    }

    try {
      // Set the manifestOnly=true param so we just get METS document
      PackageParameters pparams = new PackageParameters();
      pparams.put("manifestOnly", "true");

      // Create a temporary file to disseminate into
      String tempDirectory = ConfigurationManager.getProperty("upload.temp.dir");
      File tempFile =
          File.createTempFile("METSDissemination" + item.hashCode(), null, new File(tempDirectory));
      tempFile.deleteOnExit();

      // Disseminate METS to temp file
      Context context = new Context();
      dip.disseminate(context, item, pparams, tempFile);

      try {
        SAXBuilder builder = new SAXBuilder();
        Document metsDocument = builder.build(tempFile);
        return metsDocument.getRootElement();
      } catch (JDOMException je) {
        throw new MetadataValidationException(
            "Error parsing METS (see wrapped error message for more details) ", je);
      }
    } catch (PackageException pe) {
      throw new CrosswalkInternalException(
          "Failed making METS manifest in packager (see wrapped error message for more details) ",
          pe);
    }
  }
コード例 #3
0
  public static void main(String[] argv) throws Exception {
    Options options = new Options();
    options.addOption("c", "collection", true, "destination collection(s) Handle (repeatable)");
    options.addOption("e", "eperson", true, "email address of eperson doing importing");
    options.addOption(
        "w",
        "install",
        false,
        "disable workflow; install immediately without going through collection's workflow");
    options.addOption("t", "type", true, "package type or MIMEtype");
    options.addOption(
        "o", "option", true, "Packager option to pass to plugin, \"name=value\" (repeatable)");
    options.addOption(
        "d", "disseminate", false, "Disseminate package (output); default is to submit.");
    options.addOption("i", "item", true, "Handle of item to disseminate.");
    options.addOption("h", "help", false, "help");

    CommandLineParser parser = new PosixParser();
    CommandLine line = parser.parse(options, argv);

    String sourceFile = null;
    String eperson = null;
    String[] collections = null;
    boolean useWorkflow = true;
    String packageType = null;
    boolean submit = true;
    String itemHandle = null;
    PackageParameters pkgParams = new PackageParameters();

    if (line.hasOption('h')) {
      HelpFormatter myhelp = new HelpFormatter();
      myhelp.printHelp("Packager  [options]  package-file|-\n", options);
      System.out.println("\nAvailable Submission Package (SIP) types:");
      String pn[] = PluginManager.getAllPluginNames(PackageIngester.class);
      for (int i = 0; i < pn.length; ++i) System.out.println("  " + pn[i]);
      System.out.println("\nAvailable Dissemination Package (DIP) types:");
      pn = PluginManager.getAllPluginNames(PackageDisseminator.class);
      for (int i = 0; i < pn.length; ++i) System.out.println("  " + pn[i]);
      System.exit(0);
    }
    if (line.hasOption('w')) useWorkflow = false;
    if (line.hasOption('e')) eperson = line.getOptionValue('e');
    if (line.hasOption('c')) collections = line.getOptionValues('c');
    if (line.hasOption('t')) packageType = line.getOptionValue('t');
    if (line.hasOption('i')) itemHandle = line.getOptionValue('i');
    String files[] = line.getArgs();
    if (files.length > 0) sourceFile = files[0];
    if (line.hasOption('d')) submit = false;
    if (line.hasOption('o')) {
      String popt[] = line.getOptionValues('o');
      for (int i = 0; i < popt.length; ++i) {
        String pair[] = popt[i].split("\\=", 2);
        if (pair.length == 2) pkgParams.addProperty(pair[0].trim(), pair[1].trim());
        else if (pair.length == 1) pkgParams.addProperty(pair[0].trim(), "");
        else System.err.println("Warning: Illegal package option format: \"" + popt[i] + "\"");
      }
    }

    // Sanity checks on arg list: required args
    if (sourceFile == null
        || eperson == null
        || packageType == null
        || (submit && collections == null)) {
      System.err.println("Error - missing a REQUIRED argument or option.\n");
      HelpFormatter myhelp = new HelpFormatter();
      myhelp.printHelp("PackageManager  [options]  package-file|-\n", options);
      System.exit(0);
    }

    // find the EPerson, assign to context
    Context context = new Context();
    EPerson myEPerson = null;
    myEPerson = EPerson.findByEmail(context, eperson);
    if (myEPerson == null) usageError("Error, eperson cannot be found: " + eperson);
    context.setCurrentUser(myEPerson);

    if (submit) {
      // make sure we have an input file

      // GWaller 11/1/10 Disable piping of input in - we need to archive the package so it is
      // simpler to assume a file stream
      //                 rather than save the System.in bytes and re-read.

      if (sourceFile.equals("-")) {
        usageError(
            "Error, input piping not allowed. Specify a file name of a physical file to read");
      }

      InputStream source = new FileInputStream(sourceFile);

      PackageIngester sip =
          (PackageIngester) PluginManager.getNamedPlugin(PackageIngester.class, packageType);
      if (sip == null) usageError("Error, Unknown package type: " + packageType);

      // find collections
      Collection[] mycollections = null;

      System.out.println("Destination collections:");

      // validate each collection arg to see if it's a real collection
      mycollections = new Collection[collections.length];
      for (int i = 0; i < collections.length; i++) {
        // sanity check: did handle resolve, and to a collection?
        DSpaceObject dso = HandleManager.resolveToObject(context, collections[i]);
        if (dso == null)
          throw new IllegalArgumentException(
              "Bad collection list -- "
                  + "Cannot resolve collection handle \""
                  + collections[i]
                  + "\"");
        else if (dso.getType() != Constants.COLLECTION)
          throw new IllegalArgumentException(
              "Bad collection list -- "
                  + "Object at handle \""
                  + collections[i]
                  + "\" is not a collection!");
        mycollections[i] = (Collection) dso;
        System.out.println(
            (i == 0 ? "  Owning " : "  ") + " Collection: " + mycollections[i].getMetadata("name"));
      }

      try {
        // GWaller 26/08/09 Support array of collections
        WorkspaceItem wi = sip.ingest(context, mycollections, source, pkgParams, null);

        // GWaller 11/1/10 IssueID #157 Archive the package
        InputStream sourceCopy = new FileInputStream(sourceFile);
        Bundle archivedBundle =
            BundleUtils.getBundleByName(wi.getItem(), Constants.ARCHIVED_CONTENT_PACKAGE_BUNDLE);
        Bitstream bs = archivedBundle.createBitstream(sourceCopy);
        bs.setName(new File(sourceFile).getName());
        bs.update();
        archivedBundle.update();

        if (useWorkflow) {
          String handle = null;

          // Check if workflow completes immediately, and
          // return Handle if so.
          WorkflowItem wfi = WorkflowManager.startWithoutNotify(context, wi);

          if (wfi.getState() == WorkflowManager.WFSTATE_ARCHIVE) {
            Item ni = wfi.getItem();
            handle = HandleManager.findHandle(context, ni);
          }
          if (handle == null)
            System.out.println("Created Workflow item, ID=" + String.valueOf(wfi.getID()));
          else System.out.println("Created and installed item, handle=" + handle);
        } else {
          InstallItem.installItem(context, wi);
          System.out.println(
              "Created and installed item, handle="
                  + HandleManager.findHandle(context, wi.getItem()));
        }
        context.complete();
        System.exit(0);
      } catch (Exception e) {
        // abort all operations
        context.abort();
        e.printStackTrace();
        System.out.println(e);
        System.exit(1);
      }
    } else {
      OutputStream dest =
          (sourceFile.equals("-"))
              ? (OutputStream) System.out
              : (OutputStream) (new FileOutputStream(sourceFile));

      PackageDisseminator dip =
          (PackageDisseminator)
              PluginManager.getNamedPlugin(PackageDisseminator.class, packageType);
      if (dip == null) usageError("Error, Unknown package type: " + packageType);

      DSpaceObject dso = HandleManager.resolveToObject(context, itemHandle);
      if (dso == null)
        throw new IllegalArgumentException(
            "Bad Item handle -- " + "Cannot resolve handle \"" + itemHandle);
      dip.disseminate(context, dso, pkgParams, dest);
    }
  }