API:EPrints/Apache/CRUD

From EPrints Documentation
Jump to: navigation, search

EPrints 3 Reference: Directory Structure - Metadata Fields - Repository Configuration - XML Config Files - XML Export Format - EPrints data structure - Core API - Data Objects


API: Core API

Latest Source Code (3.4, 3.3) | Revision Log | Before editing this page please read Pod2Wiki


Warning This feature requires EPrints version 3.3.0 or later

Contents

NAME

EPrints::Apache::CRUD - Create, read, update and delete via HTTP

User Comments


SYNOPSIS

 $crud = EPrints::Apache::CRUD->new(
     repository => $repo,
     request => $r,
     datasetid => "eprint",
     dataobjid => "23",
   );
 

User Comments


DESCRIPTION

The CRUD (Create/Read/Update/Delete) module provides the Web API for manipulating content on the server. The API is an AtomPub implementation that exposes Import and Export plugins via simple URLs and HTTP content type negotiation.

You should use the <link> entries in the repository's home page to locate the CRUD endpoint, as they may change in the future:

 <link rel="Sword" href="https://myrepo/sword-app/servicedocument" />
 <link rel="SwordDeposit" href="https://myrepo/id/contents" />
 

User Comments


Examples

Create a new eprint based on a single file:

 curl -X POST \
   -i \
   -u user:password \
   -d 'Hello, World!' \
   -H 'Content-Type: text/plain' \
   https://myrepo/id/contents
 
 HTTP/1.1 201 Created
 Content-Type: application/atom+xml;charset=utf-8
 ...
 

Add a file to an existing eprint:

 curl -X POST \
   -i \
   -u user:password \
   -d 'Hello, World!' \
   -H 'Content-Disposition: attachment; filename=hello.txt' \
   -H 'Content-Type: text/plain' \
   https://myrepo/id/eprint/23/contents
 
 HTTP/1.1 201 Created
 Content-Type: application/atom+xml;charset=utf-8
 ...
 

Get an eprint's metadata in Atom XML:

 curl -X GET \
   -i \
   -u user:password \
   -H 'Accept: application/atom+xml' \
   https://myrepo/id/eprint/23
 
 HTTP/1.1 200 OK
 Content-Type: application/atom+xml;charset=utf-8
 ...
 

Get the list of contents (documents) of an eprint in Atom XML:

 curl -X GET \
   -i \
   -u user:password \
   -H 'Accept: application/atom+xml' \
   https://myrepo/id/eprint/23/contents
 
 HTTP/1.1 200 OK
 Content-Type: application/atom+xml;charset=utf-8
 ...
 

You can find more examples in the tests/84_sword.t unit test.

User Comments


URI Layout

These URIs are relative to your EPrints HTTP/HTTPs root.

User Comments


/id/contents GET,HEAD,OPTIONS,POST

Requires authentication.

GET a list of the eprints owned by the user. POST to create a new EPrint object.

User Comments


/id/[datasetid]/[dataobjid] DELETE,GET,HEAD,OPTIONS,PUT

Requires authentication depending on user's privileges and object visibility.

GET an object's metadata or, for File objects, the file content. PUT to replace the metadata and/or contents (see Updating complex objects using PUT). If the object does not exist will attempt to create it with the given dataobjid (requires upsert privilege).

User Comments


/id/[datasetid]/[dataobjid]/contents DELETE,GET,HEAD,OPTIONS,POST,PUT

Requires authentication depending on user's privileges and object visibility.

GET the logical contents of the object: documents for eprints or files for documents. PUT to replace the existing contents or POST to add to the existing contents.

User Comments


HTTP Content Negotiation

GET/HEAD requests are processed using Export plugins. POST/PUT requests are processed using Import plugins.

The plugin used depends on the request's Accept (GET/HEAD) or Content-Type (POST/PUT) header and the type of object being acted on. For example, the following request:

 GET /id/eprint/23 HTTP/1.1
 Accept: application/vnd.eprints.data+xml
 

Will search for an Export plugin that accepts objects of type dataobj/eprint and can produce output in the MIME type application/vnd.eprints.data+xml. This will most likely be the EP3 XML plugin.

In addition to the general plugin negotiation behaviour some special cases are supported to improve compatibility with Atom Pub/Web Browser clients:

User Comments


/id/eprint/...

Requesting EPrint objects as text/html will result in a 303 Redirect to the eprint object's abstract page or, if the eprint is not public, its View page.

User Comments


/id/document/.../contents

Requesting the /contents of a Document object will return the content of the document's main file.

User Comments


/id/file/...

Requesting a File object with no Accept header (or */*) will return the file's content.

User Comments


POST /id/.../contents

When creating new records via POST, content negotiation is performed against the Import plugins.

If no Import plugin supports the Content-Type header the content will be treated as application/octet-stream and stored in a new object. The resulting Atom entry will describe the new object (e.g. the eprint object in which the new document and file objects were created).

Otherwise, the result will depend on the Import plugin's output. Import plugins may produce a single object, multiple objects or an object plus content file(s).

User Comments


Content-Type header

If no Content-Type header is given the MIME type defaults to application/octet-stream for POSTs and PUTs.

User Comments


Content-Disposition header

If the Content-Disposition header is missing or does not contain a filename parameter the filename defaults to main.bin for POSTs and PUTs.

User Comments


Updating complex objects using PUT

Eprint objects contain zero or more documents, which each contain zero or more files. When you update (PUT) an eprint object the contained documents will only be replaced if the Import plugin defines new documents e.g. the Atom Import plugin will never define new documents so PUTing Atom content will only update the eprint's metadata. PUTing EP3 XML will replace the documents if you include a <documents> XML element.

PUTing to /contents will always replace all contents - PUTing to /eprint/23/contents is equivalent to DELETE /eprint/23/contents then POST /eprint/23/contents.

User Comments


PUT/DELETE from Javascript

User Comments


Upserting objects with PUT

User Comments


CONSTANTS

User Comments


CRUD_SCOPE_USER_CONTENTS

Restrict the scope of the CRUD request to user submitting content (i.e. /id/content).

User Comments


CRUD_SCOPE_DATASET

Restrict the scope of the CRUD request to a dataset (i.e. /id/FOO...).

User Comments


CRUD_SCOPE_DATAOBJ

Restrict the scope of the CRUD request to a data object (i.e. /id/FOO/BAR).

User Comments


CRUD_SCOPE_FIELD

Restrict the scope of the CRUD request to a field of data object (i.e. /id/FOO/BAR/BAZ).

User Comments


CRUD_SCOPE_CONTENTS

Restrict the scope of the CRUD request to the contents of data object (i.e. /id/FOO/BAR/contents).

User Comments


CRUD_SCOPE_SERVICEDOCUMENT

Restrict the scope of the CRUD request to the service document.

User Comments


METHODS

User Comments


Constructor Methods

User Comments


new

$crud = EPrints::Apache::CRUD->new( %params )

Return a new CRUD request using parameters given in %params.

User Comments


Class Methods

User Comments


generate_error_document

$error_document = EPrints::Apache::CRUD::generate_error_document( $repo, %opts )

Return error document for EPrints::Repository $repo using options from %opts.

User Comments


is_true

$boolean = EPrints::Apache::CRUD::is_true( $header )

Tests if $header attribute is true.

User Comments


is_false

$boolean = EPrints::Apache::CRUD::is_false( $header )

Tests if $header attribute is false.

User Comments


Object Methods

User Comments


repository

$repo = $crud->repository()

Returns the current repository.

User Comments


request

$r = $crud->request()

Returns the current Apache2::RequestUtil.

User Comments


method

$method = $crud->method()

Returns the HTTP method.

User Comments


scope

$scope = $crud->scope()

Returns the scope of the action being performed.

User Comments


dataset

$dataset = $crud->dataset()

Returns the current dataset (if any).

User Comments


dataobj

$dataobj = $crud->dataobj()

Returns the current dataobj (if any).

User Comments


field

$field = $crud->field()

Returns the current field (if available);

User Comments


headers

$headers = $crud->headers()

Get the processed headers.

User Comments


options

@verbs = $crud->options()

Returns the available HTTP verbs for the current request.

User Comments


plugin

$plugin = $crud->plugin()

Returns the current plugin (if available).

User Comments


is_write

$bool = $crud->is_write()

Returns true if the request is not a read-only method.

User Comments


accept_type

$accept_type = $crud->accept_type()

Returns the EPrints type for the current request.

User Comments


check_packaging

$rc = $crud->check_packaging()

Check the Packaging header is ok, if given.

User Comments


resolve_relations

$dataobj = $crud->resolve_relations( $dataobj [, @relations ] )

Resolve the relation path from $dataobj and return the resulting data object.

Returns c<undef> if there is no such related object.

User Comments


authen

$rc = $crud->authen

Returns HTTP code based of whether CRUD request can be authenticated.

User Comments


authz

$rc = $crud->authz

Returns HTTP code based of whether CRUD request can be authorized.

User Comments


parse_input

$list = $crud->parse_input( $plugin, $f [, %params ] )

Parse the content submitted by the user using the given $plugin. $f is called by c<epdata_to_dataobj> to convert EPrints data markup to a data object. %params are passed to the plugin's input_fh method.

Returns undef on error. Otherwise returns an EPrints::List of data objects.

User Comments


create_dataobj

$dataobj = $crud->create_dataobj( $owner, $epdata )

Creates data object as user $owner with metadata from $epdata

Returns an EPrints::DataObj.

User Comments


import_plugins

@plugins = $crud->import_plugins( [ %params ] )

Returns all matching import plugins against %params ordered by descending q score.

User Comments


export_plugins

@plugins = $crud->export_plugins( [ %params ] )

Returns all matching export plugins against %params ordered by descending q score.

User Comments


content_negotiate_best_plugin

$plugin = $crud->content_negotiate_best_plugin()

Work out the best plugin to export/update an object based on the client headers.

User Comments


crud

EPrints::Apache::CRUD( $media_range )

Takes the $media_range string and returns array of acceptable MIME types. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1 for more detail.

User Comments


handler

$rc = $crud->handler

Handler of CRUD requests.

Returns HTTP response code.

User Comments


delete

$rc = $crud->DELETE()

Handle DELETE requests.

User Comments


http_method_not_allowed

HTTP_METHOD_NOT_ALLOWED

Can't perform DELETE on /id/contents.

User Comments


http_not_found

HTTP_NOT_FOUND

No such object.

User Comments


http_conflict

HTTP_CONFLICT

Lock conflict with another user.

User Comments


http_no_content

HTTP_NO_CONTENT

Successfully removed the object.

User Comments


get

$rc = $crud->GET( [ $owner ] )

Handle GET requests.

User Comments


http_no_content

HTTP_NO_CONTENT

No sub-objects in /id/.../contents.

User Comments


http_not_acceptable

HTTP_NOT_ACCEPTABLE

More than one sub-object in /id/.../contents.

User Comments


http_unsupported_media_type

HTTP_UNSUPPORTED_MEDIA_TYPE

No Export plugin matches the Accept header/object type.

User Comments


http_see_other

HTTP_SEE_OTHER

Redirect to a non-CRUD EPrints page.

User Comments


http_not_found

HTTP_NOT_FOUND

Object not found.

User Comments


http_ok

HTTP_OK

Object outputted successfully.

User Comments


post

$rc = $crud->POST( [ $owner ] )

Handle POST requests.

User Comments


http_method_not_allowed

HTTP_METHOD_NOT_ALLOWED

Can only POST to /id/.../contents.

User Comments


http_bad_request

HTTP_BAD_REQUEST

No plugin for the SWORD Packaging header.

User Comments


http_created

HTTP_CREATED

Object(s) successfully created.

User Comments


put

$rc = $crud->PUT( [ $owner ] )

Handle PUT requests.

User Comments


http_unsupported_media_type

HTTP_UNSUPPORTED_MEDIA_TYPE

No Import plugin matched the Content-Type header/object type.

User Comments


http_range_not_satisfiable

HTTP_RANGE_NOT_SATISFIABLE

Range header is invalid or unsupported for the object type.

User Comments


http_forbidden

HTTP_FORBIDDEN

User does not have permission to create/update the object.

User Comments


http_created

HTTP_CREATED

Object was successfully created.

User Comments


http_no_content

HTTP_NO_CONTENT

Object was successfully updated.

User Comments


put_contents

$rc = $crud->PUT_contents( [ $owner ] )

Equivalent to DELETE /id/.../contents then POST /id/.../contents.

See DELETE and POST.

User Comments


metadata_relevant

$crud->metadata_relevant( $file )

Test and if suitable use $file as metadata source to update the associated EPrints::DataObj::EPrint.

User Comments


servicedocument

$crud->servicedocument

Generate response containing CRUD service document.

User Comments


on_behalf_of

$rc = $crud->on_behalf_of( $user )

Submit CRUD request on behalf of another user.

Returns HTTPS response code based on whether request on behalf of is permitted.

User Comments


is_file_ok

$boolean = $crud->is_file_ok( $epdata )

Tests if files with $epdata were uploaded with corruption.

User Comments


process_headers

$boolean = $crud->process_headers

Process headers of CRUD request

User Comments


sword_error

$boolean = $crud->sword_error( %opts )

Generate SWORD error documents based on provided %opts.

User Comments


plugin_error

$boolean = $crud->plugin_error( $plugin, $messages )

Generate error message for import $plugin used with $messages provided.

User Comments


send_response

$rc = $crud->send_response( $status, $content_type, $content )

Output response to CRUD request with HTTP status code $status, content type $content_type.

Returns HTTP response code OK.

User Comments


SEE ALSO

http://en.wikipedia.org/wiki/Create,_read,_update_and_delete

http://en.wikipedia.org/wiki/Content_negotiation

User Comments


COPYRIGHT

© Copyright 2023 University of Southampton.

EPrints 3.4 is supplied by EPrints Services.

http://www.eprints.org/eprints-3.4/

LICENSE

This file is part of EPrints 3.4 http://www.eprints.org/.

EPrints 3.4 and this file are released under the terms of the GNU Lesser General Public License version 3 as published by the Free Software Foundation unless otherwise stated.

EPrints 3.4 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with EPrints 3.4. If not, see http://www.gnu.org/licenses/.

User Comments