Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / server2 / handler.py
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import time
6
7 from appengine_wrappers import taskqueue
8 from commit_tracker import CommitTracker
9 from cron_servlet import CronServlet
10 from instance_servlet import InstanceServlet
11 from object_store_creator import ObjectStoreCreator
12 from patch_servlet import PatchServlet
13 from refresh_servlet import RefreshServlet
14 from servlet import Servlet, Request, Response
15 from test_servlet import TestServlet
16
17
18 _DEFAULT_SERVLET = InstanceServlet.GetConstructor()
19
20
21 class _EnqueueServlet(Servlet):
22   '''This Servlet can be used to manually enqueue tasks on the default
23   taskqueue. Useful for when an admin wants to manually force a specific
24   DataSource refresh, but the refresh operation takes longer than the 60 sec
25   timeout of a non-taskqueue request. For example, you might query
26
27   /_enqueue/_refresh/content_providers/cr-native-client?commit=123ff65468dcafff0
28
29   which will enqueue a task (/_refresh/content_providers/cr-native-client) to
30   refresh the NaCl documentation cache for commit 123ff65468dcafff0.
31
32   Access to this servlet should always be restricted to administrative users.
33   '''
34   def __init__(self, request):
35     Servlet.__init__(self, request)
36
37   def Get(self):
38     queue = taskqueue.Queue()
39     queue.add(taskqueue.Task(url='/%s' % self._request.path,
40                              params=self._request.arguments))
41     return Response.Ok('Task enqueued.')
42
43
44 class _QueryCommitServlet(Servlet):
45   '''Provides read access to the commit ID cache within the server. For example:
46
47   /_query_commit/master
48
49   will return the commit ID stored under the commit key "master" within the
50   commit cache. Currently "master" is the only named commit we cache, and it
51   corresponds to the commit ID whose data currently populates the data cache
52   used by live instances.
53   '''
54   def __init__(self, request):
55     Servlet.__init__(self, request)
56
57   def Get(self):
58     object_store_creator = ObjectStoreCreator(start_empty=False)
59     commit_tracker = CommitTracker(object_store_creator)
60     return Response.Ok(commit_tracker.Get(self._request.path).Get())
61
62
63 _SERVLETS = {
64   'cron': CronServlet,
65   'enqueue': _EnqueueServlet,
66   'patch': PatchServlet,
67   'query_commit': _QueryCommitServlet,
68   'refresh': RefreshServlet,
69   'test': TestServlet,
70 }
71
72
73 class Handler(Servlet):
74   def Get(self):
75     path = self._request.path
76
77     if path.startswith('_'):
78       servlet_path = path[1:]
79       if not '/' in servlet_path:
80         servlet_path += '/'
81       servlet_name, servlet_path = servlet_path.split('/', 1)
82       servlet = _SERVLETS.get(servlet_name)
83       if servlet is None:
84         return Response.NotFound('"%s" servlet not found' %  servlet_path)
85     else:
86       servlet_path = path
87       servlet = _DEFAULT_SERVLET
88
89     return servlet(Request(servlet_path,
90                            self._request.host,
91                            self._request.headers,
92                            self._request.arguments)).Get()