Statistics
| Branch: | Tag: | Revision:

root / pithos / public / functions.py @ 83b4c5fa

History | View | Annotate | Download (5.2 kB)

1 f7b1d2af Antony Chazapis
# Copyright 2011 GRNET S.A. All rights reserved.
2 f7b1d2af Antony Chazapis
# 
3 f7b1d2af Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 f7b1d2af Antony Chazapis
# without modification, are permitted provided that the following
5 f7b1d2af Antony Chazapis
# conditions are met:
6 f7b1d2af Antony Chazapis
# 
7 f7b1d2af Antony Chazapis
#   1. Redistributions of source code must retain the above
8 f7b1d2af Antony Chazapis
#      copyright notice, this list of conditions and the following
9 f7b1d2af Antony Chazapis
#      disclaimer.
10 f7b1d2af Antony Chazapis
# 
11 f7b1d2af Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 f7b1d2af Antony Chazapis
#      copyright notice, this list of conditions and the following
13 f7b1d2af Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 f7b1d2af Antony Chazapis
#      provided with the distribution.
15 f7b1d2af Antony Chazapis
# 
16 f7b1d2af Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 f7b1d2af Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 f7b1d2af Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 f7b1d2af Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 f7b1d2af Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 f7b1d2af Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 f7b1d2af Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 f7b1d2af Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 f7b1d2af Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 f7b1d2af Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 f7b1d2af Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 f7b1d2af Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 f7b1d2af Antony Chazapis
# 
29 f7b1d2af Antony Chazapis
# The views and conclusions contained in the software and
30 f7b1d2af Antony Chazapis
# documentation are those of the authors and should not be
31 f7b1d2af Antony Chazapis
# interpreted as representing official policies, either expressed
32 f7b1d2af Antony Chazapis
# or implied, of GRNET S.A.
33 f7b1d2af Antony Chazapis
34 f7b1d2af Antony Chazapis
import logging
35 f7b1d2af Antony Chazapis
36 f7b1d2af Antony Chazapis
from django.http import HttpResponse
37 f7b1d2af Antony Chazapis
38 7bef5750 Antony Chazapis
from pithos.api.faults import (Fault, BadRequest, ItemNotFound)
39 83b4c5fa Antony Chazapis
from pithos.api.util import (put_object_headers, update_manifest_meta,
40 8cb45c13 Antony Chazapis
    validate_modification_preconditions, validate_matching_preconditions,
41 8cb45c13 Antony Chazapis
    object_data_response, api_method)
42 f7b1d2af Antony Chazapis
from pithos.backends import backend
43 f7b1d2af Antony Chazapis
44 f7b1d2af Antony Chazapis
45 f7b1d2af Antony Chazapis
logger = logging.getLogger(__name__)
46 f7b1d2af Antony Chazapis
47 f7b1d2af Antony Chazapis
48 f7b1d2af Antony Chazapis
def object_demux(request, v_account, v_container, v_object):
49 f7b1d2af Antony Chazapis
    if request.method == 'HEAD':
50 f7b1d2af Antony Chazapis
        return object_meta(request, v_account, v_container, v_object)
51 f7b1d2af Antony Chazapis
    elif request.method == 'GET':
52 f7b1d2af Antony Chazapis
        return object_read(request, v_account, v_container, v_object)
53 f7b1d2af Antony Chazapis
    else:
54 f7b1d2af Antony Chazapis
        return method_not_allowed(request)
55 f7b1d2af Antony Chazapis
56 f7b1d2af Antony Chazapis
# TODO: Use a version of api_method that does not check for a token.
57 f7b1d2af Antony Chazapis
58 f7b1d2af Antony Chazapis
@api_method('HEAD')
59 f7b1d2af Antony Chazapis
def object_meta(request, v_account, v_container, v_object):
60 f7b1d2af Antony Chazapis
    # Normal Response Codes: 204
61 f7b1d2af Antony Chazapis
    # Error Response Codes: serviceUnavailable (503),
62 f7b1d2af Antony Chazapis
    #                       itemNotFound (404),
63 f7b1d2af Antony Chazapis
    #                       unauthorized (401),
64 f7b1d2af Antony Chazapis
    #                       badRequest (400)
65 f7b1d2af Antony Chazapis
    
66 f7b1d2af Antony Chazapis
    try:
67 83dd59c5 Antony Chazapis
        meta = backend.get_object_meta(request.user, v_account, v_container, v_object)
68 e0f916bb Antony Chazapis
        public = backend.get_object_public(request.user, v_account, v_container, v_object)
69 cca6c617 Antony Chazapis
    except:
70 f7b1d2af Antony Chazapis
        raise ItemNotFound('Object does not exist')
71 f7b1d2af Antony Chazapis
    
72 e0f916bb Antony Chazapis
    if not public:
73 f7b1d2af Antony Chazapis
        raise ItemNotFound('Object does not exist')
74 8cb45c13 Antony Chazapis
    update_manifest_meta(request, v_account, meta)
75 f7b1d2af Antony Chazapis
    
76 f7b1d2af Antony Chazapis
    response = HttpResponse(status=204)
77 7bef5750 Antony Chazapis
    put_object_meta(response, meta, True)
78 f7b1d2af Antony Chazapis
    return response
79 f7b1d2af Antony Chazapis
80 f7b1d2af Antony Chazapis
@api_method('GET')
81 f7b1d2af Antony Chazapis
def object_read(request, v_account, v_container, v_object):
82 f7b1d2af Antony Chazapis
    # Normal Response Codes: 200, 206
83 f7b1d2af Antony Chazapis
    # Error Response Codes: serviceUnavailable (503),
84 f7b1d2af Antony Chazapis
    #                       rangeNotSatisfiable (416),
85 f7b1d2af Antony Chazapis
    #                       preconditionFailed (412),
86 f7b1d2af Antony Chazapis
    #                       itemNotFound (404),
87 f7b1d2af Antony Chazapis
    #                       unauthorized (401),
88 f7b1d2af Antony Chazapis
    #                       badRequest (400),
89 f7b1d2af Antony Chazapis
    #                       notModified (304)
90 f7b1d2af Antony Chazapis
    
91 f7b1d2af Antony Chazapis
    try:
92 83dd59c5 Antony Chazapis
        meta = backend.get_object_meta(request.user, v_account, v_container, v_object)
93 e0f916bb Antony Chazapis
        public = backend.get_object_public(request.user, v_account, v_container, v_object)
94 cca6c617 Antony Chazapis
    except:
95 f7b1d2af Antony Chazapis
        raise ItemNotFound('Object does not exist')
96 f7b1d2af Antony Chazapis
    
97 e0f916bb Antony Chazapis
    if not public:
98 f7b1d2af Antony Chazapis
        raise ItemNotFound('Object does not exist')
99 8cb45c13 Antony Chazapis
    update_manifest_meta(request, v_account, meta)
100 f7b1d2af Antony Chazapis
    
101 f7b1d2af Antony Chazapis
    # Evaluate conditions.
102 f7b1d2af Antony Chazapis
    validate_modification_preconditions(request, meta)
103 f7b1d2af Antony Chazapis
    try:
104 f7b1d2af Antony Chazapis
        validate_matching_preconditions(request, meta)
105 f7b1d2af Antony Chazapis
    except NotModified:
106 f7b1d2af Antony Chazapis
        response = HttpResponse(status=304)
107 f7b1d2af Antony Chazapis
        response['ETag'] = meta['hash']
108 f7b1d2af Antony Chazapis
        return response
109 f7b1d2af Antony Chazapis
    
110 8cb45c13 Antony Chazapis
    sizes = []
111 8cb45c13 Antony Chazapis
    hashmaps = []
112 8cb45c13 Antony Chazapis
    if 'X-Object-Manifest' in meta:
113 8cb45c13 Antony Chazapis
        try:
114 6d817842 Antony Chazapis
            src_container, src_name = split_container_object_string('/' + meta['X-Object-Manifest'])
115 8cb45c13 Antony Chazapis
            objects = backend.list_objects(request.user, v_account, src_container, prefix=src_name, virtual=False)
116 cca6c617 Antony Chazapis
        except:
117 8cb45c13 Antony Chazapis
            raise ItemNotFound('Object does not exist')
118 8cb45c13 Antony Chazapis
        
119 8cb45c13 Antony Chazapis
        try:
120 8cb45c13 Antony Chazapis
            for x in objects:
121 8cb45c13 Antony Chazapis
                s, h = backend.get_object_hashmap(request.user, v_account, src_container, x[0], x[1])
122 8cb45c13 Antony Chazapis
                sizes.append(s)
123 8cb45c13 Antony Chazapis
                hashmaps.append(h)
124 cca6c617 Antony Chazapis
        except:
125 8cb45c13 Antony Chazapis
            raise ItemNotFound('Object does not exist')
126 8cb45c13 Antony Chazapis
    else:
127 8cb45c13 Antony Chazapis
        try:
128 83b4c5fa Antony Chazapis
            s, h = backend.get_object_hashmap(request.user, v_account, v_container, v_object)
129 8cb45c13 Antony Chazapis
            sizes.append(s)
130 8cb45c13 Antony Chazapis
            hashmaps.append(h)
131 cca6c617 Antony Chazapis
        except:
132 8cb45c13 Antony Chazapis
            raise ItemNotFound('Object does not exist')
133 f7b1d2af Antony Chazapis
    
134 8cb45c13 Antony Chazapis
    return object_data_response(request, sizes, hashmaps, meta, True)
135 f7b1d2af Antony Chazapis
136 f7b1d2af Antony Chazapis
@api_method()
137 83b4c5fa Antony Chazapis
def method_not_allowed(request, **v_args):
138 f7b1d2af Antony Chazapis
    raise ItemNotFound('Object does not exist')