2
0
mirror of https://github.com/xcat2/confluent.git synced 2024-11-22 09:32:21 +00:00

Preserve file extensions when client requests data by extension

Previously, following links in the output would not work correctly.  Have caller preference
for extension denoted type propogate into link targets.
This commit is contained in:
Jarrod Johnson 2014-04-30 15:54:22 -04:00
parent 6069eceb23
commit 934ea4bd5f
2 changed files with 34 additions and 25 deletions

View File

@ -215,13 +215,13 @@ def _pick_mimetype(env):
if the '.json' scheme doesn't cut it.
"""
if env['PATH_INFO'].endswith('.json'):
return 'application/json; charset=utf-8'
return 'application/json; charset=utf-8', '.json'
elif env['PATH_INFO'].endswith('.html'):
return 'text/html'
return 'text/html', '.html'
elif 'application/json' in env['HTTP_ACCEPT']:
return 'application/json; charset=utf-8'
return 'application/json; charset=utf-8', ''
else:
return 'text/html'
return 'text/html', ''
def _assign_consessionid(consolesession):
@ -248,7 +248,7 @@ def resourcehandler(env, start_response):
def resourcehandler_backend(env, start_response):
"""Function to handle new wsgi requests
"""
mimetype = _pick_mimetype(env)
mimetype, extension = _pick_mimetype(env)
reqbody = None
reqtype = None
if 'CONTENT_LENGTH' in env and int(env['CONTENT_LENGTH']) > 0:
@ -362,16 +362,17 @@ def resourcehandler_backend(env, start_response):
return
pagecontent = ""
if mimetype == 'text/html':
for datum in _assemble_html(hdlr, resource, lquerydict, url):
for datum in _assemble_html(hdlr, resource, lquerydict, url,
extension):
pagecontent += datum
else:
for datum in _assemble_json(hdlr, resource, url):
for datum in _assemble_json(hdlr, resource, url, extension):
pagecontent += datum
start_response('200 OK', headers)
yield pagecontent
def _assemble_html(responses, resource, querydict, url):
def _assemble_html(responses, resource, querydict, url, extension):
yield '<html><head><title>' \
'Confluent REST Explorer: ' + url + '</title></head>' \
'<body><form action="' + \
@ -388,15 +389,17 @@ def _assemble_html(responses, resource, querydict, url):
iscollection = True
elif resource[-1] == '/':
iscollection = True
yield '<a rel="collection" href="../">../</a><br>'
yield '<a rel="collection" href="../{0}">../{0}</a><br>'.format(
extension)
else:
iscollection = False
yield '<a rel="collection" href="./">./</a><br>'
yield '<a rel="collection" href="./{0}">./{0}</a><br>'.format(
extension)
pendingrsp = []
for rsp in responses:
if isinstance(rsp, confluent.messages.LinkRelation):
yield rsp.html() + "<br>"
yield rsp.html(extension) + "<br>"
else:
pendingrsp.append(rsp)
for rsp in pendingrsp:
@ -420,26 +423,28 @@ def _assemble_html(responses, resource, querydict, url):
'</form></body></html>')
def _assemble_json(responses, resource, url):
def _assemble_json(responses, resource, url, extension):
#NOTE(jbjohnso) I'm considering giving up on yielding bit by bit
#in json case over http. Notably, duplicate key values from plugin
#overwrite, but we'd want to preserve them into an array instead.
#the downside is that http would just always blurt it ll out at
#once and hold on to all the data in memory
links = {
'self': {"href": resource},
'self': {"href": resource + extension},
}
if url == '/':
pass
elif resource[-1] == '/':
links['collection'] = {"href": "../"}
links['collection'] = {"href": "../" + extension}
else:
links['collection'] = {"href": "./"}
links['collection'] = {"href": "./" + extension}
rspdata = {}
for rsp in responses:
if isinstance(rsp, confluent.messages.LinkRelation):
haldata = rsp.raw()
for hk in haldata.iterkeys():
if 'href' in haldata[hk]:
haldata[hk]['href'] += extension
if hk in links:
if isinstance(links[hk], list):
links[hk].append(haldata[hk])

View File

@ -63,7 +63,7 @@ class ConfluentMessage(object):
def strip_node(self, node):
self.kvpairs = self.kvpairs[node]
def html(self):
def html(self, extension=''):
#this is used to facilitate the api explorer feature
snippet = ""
for key in self.kvpairs.iterkeys():
@ -134,7 +134,7 @@ class DeletedResource(ConfluentMessage):
class ConfluentChoiceMessage(ConfluentMessage):
valid_values = set()
def html(self):
def html(self, extension=''):
snippet = ""
for key in self.kvpairs.iterkeys():
val = self.kvpairs[key]
@ -171,11 +171,13 @@ class LinkRelation(ConfluentMessage):
"""
return {self.rel: {"href": self.href}}
def html(self):
def html(self, extension=''):
"""Provide an html representation of the link relation.
This is used by the API explorer aspect of httpapi"""
return '<a href="{0}" rel="{1}">{0}</a>'.format(self.href, self.rel)
return '<a href="{0}{2}" rel="{1}">{0}{2}</a>'.format(self.href,
self.rel,
extension)
# return '<a href="%s" rel="%s">%s</a><input type="submit"
# name="restexprerorop" value="delete:%s"' % (self.href, self.rel,
# self.href, self.href)
@ -187,14 +189,16 @@ class ChildCollection(LinkRelation):
self.href = collname
self.candelete = candelete
def html(self):
def html(self, extension=''):
if self.candelete:
return ('<a href="{0}" rel="{1}">{0}</a> . . . . . . . . . . . . '
'<button type="submit" name="restexplorerop" '
'value="delete" formaction="{0}">delete'
'</button>').format(self.href, self.rel)
return (
'<a href="{0}{2}" rel="{1}">{0}{2}</a> . . . . . . . . . . . . '
'<button type="submit" name="restexplorerop" '
'value="delete" formaction="{0}">delete'
'</button>').format(self.href, self.rel, extension)
else:
return '<a href="{0}" rel="{0}">{0}</a>'.format(self.href)
return '<a href="{0}{1}" rel="{0}">{0}{1}</a>'.format(self.href,
extension)
def get_input_message(path, operation, inputdata, nodes=None):