Skip to content

Instantly share code, notes, and snippets.

@jweisman
Last active February 9, 2017 10:29
Show Gist options
  • Save jweisman/96c9199932c3cf6cc6fc4e43ea7d6ec9 to your computer and use it in GitHub Desktop.
Save jweisman/96c9199932c3cf6cc6fc4e43ea7d6ec9 to your computer and use it in GitHub Desktop.
Alma SWORD Deposits
-----------------1605871705
Content-Type: application/atom+xml; charset="utf-8"
Content-Disposition: attachment; name=atom
MIME-Version: 1.0
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:dcterms="http://purl.org/dc/terms/">
<title>Deposit sample</title>
<dcterms:title>Deposit Sample</dcterms:title>
<dcterms:creator>Josh</dcterms:creator>
</entry>
-----------------1605871705
Content-Type: text/plain
Content-Disposition: attachment; name=payload; filename=test.txt
Packaging: http://purl.org/net/sword/package/Binary
MIME-Version: 1.0
This is a test file deposited via SWORD.
-----------------1605871705--
var request = require('request');
headers = {
"In-Progress": false,
"On-behalf-of": USER_NAME
}
var options = {
url: SWORD_URL,
method: 'POST',
auth: {
user: USER_NAME,
pass: PASSWORD
},
headers: headers,
multipart: [{
'content-type': 'application/atom+xml; charset="utf-8"',
'content-disposition': 'attachment; name=atom',
body: '<entry><dc:title>My Title</dc:title></entry>'
}, {
'content-type': 'application/zip',
'content-disposition': 'attachment; name=payload; filename=' + FILENAME)),
'Content-Transfer-Encoding': 'base64',
'Packaging': 'http://purl.org/net/sword/package/SimpleZip',
'Content-MD5': utils.checksum(FILE),
body: utils.base64_encode(FILE)
}]
};
require 'Sword2Ruby'
#Print out the version number
puts "Running Sword2Ruby version #{Sword2Ruby::VERSION}"
#Define the authentication credentials for the connection
sword_user = Sword2Ruby::User.new('user', 'password', 'on behalf of')
#Define the connection object using the username and password
connection = Sword2Ruby::Connection.new(sword_user)
#Get the Service Document
service = Atom::Service.new('http://sword.exldevnetwork.net/sd', connection)
#Print out some properties for the Service
puts "service.sword_version: #{service.sword_version}"
puts "service.sword_max_upload_size: #{service.sword_max_upload_size}"
puts "service.workspaces.count: #{service.workspaces.count}"
puts "service.collections.count: #{service.collections.count}"
#Get a collection
collection = service.collections.last
entry = Atom::Entry.new
entry.add_dublin_core_extension! "title", "Test from Ruby"
entry.add_dublin_core_extension! "creator", "Creator"
#Post a file to the collection
deposit_receipt = collection.post_multipart!(
:entry=>entry,
:filepath=>'test.txt', #Downloads.zip',
:content_type=>"test/plain", #application/zip",
:packaging=>"http://purl.org/net/sword/package/Binary", #SimpleZip"
)
#Print out the deposit receipt
puts "deposit_receipt.has_entry: #{deposit_receipt.has_entry}"
puts "deposit_receipt.entry.to_s: #{deposit_receipt.entry.to_s}"
<?php
// Test the V2 PHP client implementation using the Simple SWORD Server (SSS)
// The URL of the service document
$testurl = "http://localhost:8080/sd";
// The user (if required)
$testuser = "";
// The password of the user (if required)
$testpw = "";
// The on-behalf-of user (if required)
$testobo = "";
// The URL of the example deposit collection
$testdepositurl = "http://localhost:8080/col/1620699450000561";
// The test atom entry to deposit
$testatomentry = "test-files/atom_multipart/atom";
// The second test atom entry to deposit
$testatomentry2 = "test-files/atom_multipart/atom2";
// The test atom multipart file to deposit
$testmultipart = "test-files/atom_multipart_package";
// The second test file to deposit
$testmultipart2 = "test-files/atom_multipart_package2";
// The test content zip file to deposit
$testzipcontentfile = "test-files/atom_multipart_package2.zip";
// A plain content file
$testextrafile = "test-files/swordlogo.jpg";
// The file type of the extra file
$testextrafiletype = "image/jpg";
// The content type of the test file
$testcontenttype = "application/zip";
// The packaging format of the test file
$testpackaging = "http://purl.org/net/sword/package/SimpleZip";
require("../swordappclient.php");
$testsac = new SWORDAPPClient();
if (true) {
print "About to request servicedocument from " . $testurl . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testsdr = $testsac->servicedocument($testurl, $testuser, $testpw, $testobo);
print "Received HTTP status code: " . $testsdr->sac_status . " (" . $testsdr->sac_statusmessage . ")\n";
if ($testsdr->sac_status == 200) {
$testsdr->toString();
}
print "\n\n";
}
if (true) {
print "About to deposit multipart file (" . $testmultipart . ") to " . $testdepositurl . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testdr = $testsac->depositMultipart($testdepositurl, $testuser, $testpw, $testobo, $testmultipart, false);
print "Received HTTP status code: " . $testdr->sac_status . " (" . $testdr->sac_statusmessage . ")\n";
if (($testdr->sac_status >= 200) || ($testdr->sac_status < 300)) {
$testdr->toString();
}
print "\n\n";
$edit_iri = $testdr->sac_edit_iri;
$cont_iri = $testdr->sac_content_src;
$edit_media = $testdr->sac_edit_media_iri;
$statement_atom = $testdr->sac_state_iri_atom;
$statement_ore = $testdr->sac_state_iri_ore;
}
if (false) {
print "About to request Atom serialisation of the deposit statement from " . $statement_atom . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testatomstatement = $testsac->retrieveAtomStatement($statement_atom, $testuser, $testpw, $testobo);
if (($testatomstatement->sac_status >= 200) || ($testatomstatement->sac_status < 300)) {
$testatomstatement->toString();
}
print "\n\n";
}
if (false) {
print "About to request OAI-ORE serialisation of the deposit statement from " . $statement_ore . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testoaiore = $testsac->retrieveOAIOREStatement($statement_ore, $testuser, $testpw, $testobo);
echo $testoaiore;
print "\n\n";
}
if (false) {
print "About to retrieve content from " . $edit_media . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testresp = $testsac->retrieveContent($edit_media, $testuser, $testpw, $testobo, "http://purl.org/net/sword/package/SimpleZip");
// file_put_contents("temp-save.zip", $testresp);
print "\n\n";
}
if (false) {
print "About to replace content at " . $edit_media . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$status = $testsac->replaceFileContent($edit_media, $testuser, $testpw, $testobo, $testzipcontentfile, $testpackaging, $testcontenttype, false);
print "Received HTTP status code: " . $status . "\n";
if ($status == 204) {
echo "Content replaced\n";
}
print "\n\n";
}
if (false) {
print "About to replace atom entry (" . $testatomentry2 . ") to " . $edit_iri . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testdr = $testsac->replaceMetadata($edit_iri, $testuser, $testpw, $testobo, $testatomentry2, false);
print "Received HTTP status code: " . $testdr->sac_status .
" (" . $testdr->sac_statusmessage . ")\n";
if (($testdr->sac_status >= 200) || ($testdr->sac_status < 300)) {
$testdr->toString();
}
print "\n\n";
}
if (false) {
print "About to replace multipart atom entry and file (" . $testmultipart2 . ") to " . $edit_iri . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testdr = $testsac->replaceMetadataAndFile($edit_iri, $testuser, $testpw, $testobo, $testmultipart2, $testpackaging, false);
print "Received HTTP status code: " . $testdr->sac_status .
" (" . $testdr->sac_statusmessage . ")\n";
if (($testdr->sac_status >= 200) || ($testdr->sac_status < 300)) {
$testdr->toString();
}
print "\n\n";
}
if (false) {
print "About to add file (" . $testextrafile . ") to " . $edit_media . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testdr = $testsac->addExtraFileToMediaResource($edit_media, $testuser, $testpw, $testobo, $testextrafile, $testextrafiletype, false);
print "Received HTTP status code: " . $testdr->sac_status .
" (" . $testdr->sac_statusmessage . ")\n";
if (($testdr->sac_status >= 200) || ($testdr->sac_status < 300)) {
$testdr->toString();
}
print "\n\n";
}
if (false) {
print "About to add package (" . $testzipcontentfile . ") to " . $edit_iri . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testdr = $testsac->addExtraPackage($edit_iri, $testuser, $testpw, $testobo, $testzipcontentfile, $testpackaging, $testcontenttype, false);
print "Received HTTP status code: " . $testdr->sac_status .
" (" . $testdr->sac_statusmessage . ")\n";
if (($testdr->sac_status >= 200) || ($testdr->sac_status < 300)) {
$testdr->toString();
}
print "\n\n";
}
if (false) {
print "About to add atom entry (" . $testatomentry2 . ") to " . $edit_iri . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testdr = $testsac->addExtraAtomEntry($edit_iri, $testuser, $testpw, $testobo, $testatomentry2, false);
print "Received HTTP status code: " . $testdr->sac_status .
" (" . $testdr->sac_statusmessage . ")\n";
if (($testdr->sac_status >= 200) || ($testdr->sac_status < 300)) {
$testdr->toString();
}
print "\n\n";
}
if (false) {
print "About to add multipart atom entry and file (" . $testmultipart2 . ") to " . $edit_iri . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testdr = $testsac->addExtraMultipartPackage($edit_iri, $testuser, $testpw, $testobo, $testmultipart2, $testpackaging, false);
print "Received HTTP status code: " . $testdr->sac_status .
" (" . $testdr->sac_statusmessage . ")\n";
if (($testdr->sac_status >= 200) || ($testdr->sac_status < 300)) {
$testdr->toString();
}
print "\n\n";
}
/**
if (false) {
print "About to complete the deposit at " . $complete_url . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testdr = $testsac->completeIncompleteDeposit($testdepositurl, $testuser, $testpw, $testobo);
print "Received HTTP status code: " . $testdr->sac_status .
" (" . $testdr->sac_statusmessage . ")\n";
if (($testdr->sac_status >= 200) || ($testdr->sac_status < 300)) {
$testdr->toString();
}
print "\n\n";
}
*/
if (false) {
print "About to delete container at " . $edit_iri . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
try {
$deleteresponse = $testsac->deleteContainer($edit_iri, $testuser, $testpw, $testobo);
print " - Container successfully deleted, HTTP code 204\n";
} catch (Exception $e) {
echo $e->getMessage();
}
print "\n\n";
}
if (false) {
print "About to deposit atom entry (" . $testatomentry . ") to " . $testdepositurl . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testdr = $testsac->depositAtomEntry($testdepositurl, $testuser, $testpw, $testobo, $testatomentry, false);
print "Received HTTP status code: " . $testdr->sac_status .
" (" . $testdr->sac_statusmessage . ")\n";
if (($testdr->sac_status >= 200) || ($testdr->sac_status < 300)) {
$testdr->toString();
}
print "\n\n";
$edit_iri = $testdr->sac_edit_iri;
$cont_iri = $testdr->sac_content_src;
$edit_media = $testdr->sac_edit_media_iri;
$statement_atom = $testdr->sac_state_iri_atom;
$statement_ore = $testdr->sac_state_iri_ore;
}
if (false) {
print "About to retrieve deposit receipt from " . $edit_iri . "\n";
if (empty($testuser)) {
print "As: anonymous\n";
} else {
print "As: " . $testuser . "\n";
}
$testdr = $testsac->retrieveDepositReceipt($edit_iri, $testuser, $testpw, $testobo, "http://purl.org/net/sword/package/SimpleZip");
print "Received HTTP status code: " . $testsdr->sac_status . " (" . $testsdr->sac_statusmessage . ")\n";
if ($testdr->sac_status == 200) {
$testdr->toString();
}
print "\n\n";
}
?>
@jweisman
Copy link
Author

To run the curl example, use a command line such as:
curl -H "Content-Type: multipart/related; boundary=---------------1605871705; type=\"application/atom+xml\"" -H "MIME-Version: 1.0" -H "On-behalf-of: [username]" --data-binary @curl.txt --user [user]:[password] https://[alma-url]/sword/[INST]/col/[deposit_profile_id]

Be sure to replace all of the parameters marked by []

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment