Statistics
| Branch: | Tag: | Revision:

root / pithos / public / functions.py @ 4a1c29ea

History | View | Annotate | Download (5.5 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
# 
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
# 
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
# 
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
# 
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import logging
35

    
36
from django.http import HttpResponse
37

    
38
from pithos.api.faults import (Fault, BadRequest, ItemNotFound)
39
from pithos.api.util import (put_object_headers, update_manifest_meta,
40
    validate_modification_preconditions, validate_matching_preconditions,
41
    object_data_response, api_method)
42

    
43

    
44
logger = logging.getLogger(__name__)
45

    
46

    
47
def object_demux(request, v_account, v_container, v_object):
48
    if request.method == 'HEAD':
49
        return object_meta(request, v_account, v_container, v_object)
50
    elif request.method == 'GET':
51
        return object_read(request, v_account, v_container, v_object)
52
    else:
53
        return method_not_allowed(request)
54

    
55
@api_method('HEAD', user_required=False)
56
def object_meta(request, v_account, v_container, v_object):
57
    # Normal Response Codes: 204
58
    # Error Response Codes: serviceUnavailable (503),
59
    #                       itemNotFound (404),
60
    #                       badRequest (400)
61
    
62
    try:
63
        meta = request.backend.get_object_meta(request.user_uniq, v_account,
64
                                                v_container, v_object)
65
        public = request.backend.get_object_public(request.user_uniq, v_account,
66
                                                    v_container, v_object)
67
    except:
68
        raise ItemNotFound('Object does not exist')
69
    
70
    if not public:
71
        raise ItemNotFound('Object does not exist')
72
    update_manifest_meta(request, v_account, meta)
73
    
74
    response = HttpResponse(status=200)
75
    put_object_headers(response, meta, True)
76
    return response
77

    
78
@api_method('GET', user_required=False)
79
def object_read(request, v_account, v_container, v_object):
80
    # Normal Response Codes: 200, 206
81
    # Error Response Codes: serviceUnavailable (503),
82
    #                       rangeNotSatisfiable (416),
83
    #                       preconditionFailed (412),
84
    #                       itemNotFound (404),
85
    #                       badRequest (400),
86
    #                       notModified (304)
87
    
88
    try:
89
        meta = request.backend.get_object_meta(request.user_uniq, v_account,
90
                                                v_container, v_object)
91
        public = request.backend.get_object_public(request.user_uniq, v_account,
92
                                                    v_container, v_object)
93
    except:
94
        raise ItemNotFound('Object does not exist')
95
    
96
    if not public:
97
        raise ItemNotFound('Object does not exist')
98
    update_manifest_meta(request, v_account, meta)
99
    
100
    # Evaluate conditions.
101
    validate_modification_preconditions(request, meta)
102
    try:
103
        validate_matching_preconditions(request, meta)
104
    except NotModified:
105
        response = HttpResponse(status=304)
106
        response['ETag'] = meta['ETag']
107
        return response
108
    
109
    sizes = []
110
    hashmaps = []
111
    if 'X-Object-Manifest' in meta:
112
        try:
113
            src_container, src_name = split_container_object_string('/' + meta['X-Object-Manifest'])
114
            objects = request.backend.list_objects(request.user_uniq, v_account,
115
                                src_container, prefix=src_name, virtual=False)
116
        except:
117
            raise ItemNotFound('Object does not exist')
118
        
119
        try:
120
            for x in objects:
121
                s, h = request.backend.get_object_hashmap(request.user_uniq,
122
                                        v_account, src_container, x[0], x[1])
123
                sizes.append(s)
124
                hashmaps.append(h)
125
        except:
126
            raise ItemNotFound('Object does not exist')
127
    else:
128
        try:
129
            s, h = request.backend.get_object_hashmap(request.user_uniq, v_account,
130
                                                        v_container, v_object)
131
            sizes.append(s)
132
            hashmaps.append(h)
133
        except:
134
            raise ItemNotFound('Object does not exist')
135
    
136
    return object_data_response(request, sizes, hashmaps, meta, True)
137

    
138
@api_method(user_required=False)
139
def method_not_allowed(request, **v_args):
140
    raise ItemNotFound('Object does not exist')