RESTful service for `ulif.openoffice`
*************************************

General design of REST service
==============================

The RESTful service provided here follows the following rules:

=============== ===================== =================================
  HTTP method       Path                    Semantics
=============== ===================== =================================
 GET            /docs/<DOC_ID>        Get the doc with the id <DOC_ID>
                                      (the source document)
--------------- --------------------- ---------------------------------
 GET            /docs/<DOC_ID>/<TYPE> Get the <TYPE> representation of
                                      doc with the id <DOC_ID>. Does
                                      not create a new converted
                                      version, if it is not already
                                      available.
--------------- --------------------- ---------------------------------
 PUT            /docs/<DOC_ID>/<TYPE> Update a converted document on
                                      the server. The conversion type
                                      is given by <TYPE>.
                                      If the resource already exists,
                                      it will be recreated. If it does
                                      not, it will be created.
                                      Returns the created or renewed
                                      resource.
--------------- --------------------- ---------------------------------
 POST           /docs/                Store a document on the server.
                                      The document is not
				      converted. On success the server
                                      returns the new <DOC_ID> for the
                                      newly created document.
--------------- --------------------- ---------------------------------
 POST           /docs/<DOC_ID>/<TYPE> Convert the already stored doc
                                      identified by <DOC_ID> into type
                                      <TYPE>. If the conversion was
                                      done before, it will be redone.
                                      Returns the converted doc.
--------------- --------------------- ---------------------------------
 HEAD           /docs/<DOC_ID>/       Get a list of conversions
                                      available for the respective doc.
--------------- --------------------- ---------------------------------
 HEAD           /docs/<DOC_ID>/<TYPE> Like GET, but only tells, whether
                                      requested doc is available.
=============== ===================== =================================

Currently supported types ('<TYPE>'):

* 'source'
    The source document out of which other representations are
    generated by conversion.

* 'pdf'
    A PDF representation of the respective source doc.

* 'html'
    An HTML representation of the respective source doc. HTML docs are
    normally delivered as .zip files, which all accompanying files
    like images, subdirs and stylesheets included.

DOC_IDs
-------

DOC IDs are determined by the server. A client should not make any
assumptions about the form, length and content of a DOC_ID, except
that it is a string with reasonable length (< 64 chars).

TODO
====

* methods/paths to get a list of available docs/conversions.

Examples:
=========

Preliminaries
-------------

We create a temporary cache dir where we can store conversion results,
etc.:

    >>> import tempfile
    >>> cache_dir = tempfile.mkdtemp()

We start up the framework. Note, that we use ``--mode=rest`` option
with `pyunoctl`:

    >>> import os
    >>> import ulif.openoffice
    >>> path = os.path.dirname(ulif.openoffice.__file__)
    >>> bin_path = os.path.abspath(os.path.join(
    ...                path, '..', '..', '..', 'bin'))
    >>> oooctl_path = os.path.join(bin_path, 'oooctl')
    >>> pyunoctl_path = os.path.join(bin_path, 'pyunoctl')

    >>> print system(oooctl_path + ' --stdout=/tmp/oooctl.log start')
    starting OpenOffice.org server, going into background...
    started with pid ...

    >>> print system(pyunoctl_path + ' --mode=rest' +
    ...              ' --stdout=/tmp/pyunoctl.log start' +
    ...              ' --cache-dir=%s' % cache_dir)
    startung RESTful HTTP server, going into background...
    started with pid ...

We create a new document to be stored on the server:

    >>> import random
    >>> import string
    >>> source_dir = tempfile.mkdtemp()
    >>> source_path = os.path.join(source_dir, 'samplefile.txt')
    >>> content = "".join(random.sample(string.letters+'\n', 32))
    >>> open(source_path, 'wb').write(content)

Give servers time to startup...

    >>> import time
    >>> time.sleep(2)

If we want to get some document, in the beginning we will get nothing,
as there is no doc stored yet:


    >>> import httplib
    >>> conn = httplib.HTTPConnection(
    ...          '127.0.0.1', 2009)
    >>> conn.request('GET', '/docs/foobar')
    >>> response = conn.getresponse()
    >>> response.getheaders()
	[('date', '...'), ('content-length', ': ...'), ('server', 'ulif.openoffice.RESTfulHTTPServer/<VERSION> Python/2.6.4')]



Clean up:

    >>> print system(pyunoctl_path + ' stop')
    stopping pid ... done.

    >>> print system(oooctl_path + ' stop')
    stopping pid ... done.

    >>> import shutil
    >>> shutil.rmtree(source_dir)
    >>> shutil.rmtree(cache_dir)
