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);
  }
Example #2
0
  public static void main(String[] args) throws Exception {

    Abdera abdera = new Abdera();
    AbderaClient abderaClient = new AbderaClient(abdera);
    Factory factory = abdera.getFactory();

    // Perform introspection. This is an optional step. If you already
    // know the URI of the APP collection to POST to, you can skip it.
    Document<Service> introspection =
        abderaClient.get("http://localhost:9080/EmployeeService/").getDocument();
    Service service = introspection.getRoot();
    Collection collection =
        service.getCollection("Employee Directory Workspace", "itc Employee Database");
    report("The Collection Element", collection.toString());

    // Create the entry to post to the collection
    Entry entry = factory.newEntry();
    entry.setId("tag:example.org,2006:foo");
    entry.setTitle("This is the title");
    entry.setUpdated(new AtomDate().getValue());
    entry.setPublished(new AtomDate().getValue());
    entry.addLink("/employee");
    entry.addAuthor("James");
    entry.setContent("This is the content");
    report("The Entry to Post", entry.toString());

    // Post the entry. Be sure to grab the resolved HREF of the collection
    Document<Entry> doc =
        abderaClient.post(collection.getResolvedHref().toString(), entry).getDocument();

    // In some implementations (such as Google's GData API, the entry URI is
    // distinct from it's edit URI. To be safe, we should assume it may be
    // different
    IRI entryUri = doc.getBaseUri();
    report("The Created Entry", doc.getRoot().toString());

    // Grab the Edit URI from the entry. The entry MAY have more than one
    // edit link. We need to make sure we grab the right one.
    IRI editUri = getEditUri(doc.getRoot());

    // If there is an Edit Link, we can edit the entry
    if (editUri != null) {
      // Before we can edit, we need to grab an "editable" representation
      doc = abderaClient.get(editUri.toString()).getDocument();

      // Change whatever you want in the retrieved entry
      // doc.getRoot().getTitleElement().setValue("This is the changed title");

      // Put it back to the server
      abderaClient.put(editUri.toString(), doc.getRoot());

      // This is just to show that the entry has been modified
      doc = abderaClient.get(entryUri.toString()).getDocument();
      report("The Modified Entry", doc.getRoot().toString());
    } else {
      // Otherwise, the entry cannot be modified (no suitable edit link was found)
      report("The Entry cannot be modified", null);
    }

    // Delete the entry. Again, we need to make sure that we have the current
    // edit link for the entry
    doc = abderaClient.get(entryUri.toString()).getDocument();
    editUri = getEditUri(doc.getRoot());
    if (editUri != null) {
      abderaClient.delete(editUri.toString());
      report("The Enry has been deleted", null);
    } else {
      report("The Entry cannot be deleted", null);
    }
  }