Skip to content

Commit

Permalink
- Proxy Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
afabiani committed Oct 30, 2018
1 parent 309a719 commit feb3659
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
22 changes: 16 additions & 6 deletions geonode/geoserver/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1586,14 +1586,24 @@ def style_update(request, url):
sld_body = '<?xml version="1.0" encoding="UTF-8"?>%s' % request.body
# add style in GN and associate it to layer
if request.method == 'POST':
style = Style(name=style_name, sld_body=sld_body, sld_url=url)
style.save()
layer = Layer.objects.get(alternate=layer_name)
style.layer_styles.add(layer)
style, created = Style.objects.get_or_create(name=style_name)
style.sld_body = sld_body
style.sld_url = url
style.save()
affected_layers.append(layer)
layer = None
try:
layer = Layer.objects.get(name=layer_name)
except BaseException:
try:
layer = Layer.objects.get(alternate=layer_name)
except BaseException:
pass
if layer:
style.layer_styles.add(layer)
style.save()
affected_layers.append(layer)
elif request.method == 'PUT': # update style in GN
style = Style.objects.get(name=style_name)
style, created = Style.objects.get_or_create(name=style_name)
style.sld_body = sld_body
style.sld_url = url
if len(elm_user_style_title.text) > 0:
Expand Down
16 changes: 2 additions & 14 deletions geonode/geoserver/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,6 @@ def strip_prefix(path, prefix):

path = strip_prefix(request.get_full_path(), proxy_path)

access_token = None
if request and 'access_token' in request.session:
access_token = request.session['access_token']

if access_token and 'access_token' not in path:
query_separator = '&' if '?' in path else '?'
path = ('%s%saccess_token=%s' %
(path, query_separator, access_token))

raw_url = str(
"".join([ogc_server_settings.LOCATION, downstream_path, path]))

Expand All @@ -494,7 +485,6 @@ def strip_prefix(path, prefix):
import posixpath
raw_url = urljoin(ogc_server_settings.LOCATION,
posixpath.join(workspace, layername, downstream_path, path))

if downstream_path in ('rest/styles') and len(request.body) > 0:
if ws:
# Lets try
Expand All @@ -514,9 +504,7 @@ def strip_prefix(path, prefix):
re.match(r'/(ows).*$', path, re.IGNORECASE)):
_url = str("".join([ogc_server_settings.LOCATION, '', path[1:]]))
raw_url = _url

url = urlsplit(raw_url)

affected_layers = None
if request.method in ("POST", "PUT"):
if downstream_path in ('rest/styles', 'rest/layers',
Expand All @@ -530,8 +518,8 @@ def strip_prefix(path, prefix):
elif downstream_path == 'rest/styles':
logger.info(
"[geoserver_proxy] Updating Style to ---> url %s" %
url.path)
affected_layers = style_update(request, raw_url)
url.geturl())
affected_layers = style_update(request, url.geturl())

kwargs = {'affected_layers': affected_layers}
return proxy(request, url=raw_url, response_callback=_response_callback, **kwargs)
Expand Down
20 changes: 11 additions & 9 deletions geonode/proxy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

from slugify import Slugify
from httplib import HTTPConnection, HTTPSConnection
from urlparse import urlsplit, urljoin
from urlparse import urlparse, urlsplit, urljoin
from django.conf import settings
from django.http import HttpResponse
from django.utils.http import is_safe_url
Expand Down Expand Up @@ -159,20 +159,20 @@ def proxy(request, url=None, response_callback=None,
if request and 'access_token' in request.session:
access_token = request.session['access_token']

if access_token:
if 'HTTP_AUTHORIZATION' in request.META:
auth = request.META.get(
'HTTP_AUTHORIZATION',
request.META.get('HTTP_AUTHORIZATION2'))
if auth:
headers['Authorization'] = auth
elif access_token:
# TODO: Bearer is currently cutted of by Djano / GeoServer
if request.method in ("POST", "PUT"):
headers['Authorization'] = 'Bearer %s' % access_token
if access_token and 'access_token' not in locator:
query_separator = '&' if '?' in locator else '?'
locator = ('%s%saccess_token=%s' %
(locator, query_separator, access_token))
elif 'HTTP_AUTHORIZATION' in request.META:
auth = request.META.get(
'HTTP_AUTHORIZATION',
request.META.get('HTTP_AUTHORIZATION2'))
if auth:
headers['Authorization'] = auth

site_url = urlsplit(settings.SITEURL)

Expand All @@ -191,7 +191,9 @@ def proxy(request, url=None, response_callback=None,
conn = HTTPSConnection(url.hostname, url.port)
else:
conn = HTTPConnection(url.hostname, url.port)
conn.request(request.method, locator.encode('utf8'), request.body, headers)
parsed = urlparse(raw_url)
parsed._replace(path=locator.encode('utf8'))
conn.request(request.method, parsed.geturl(), request.body, headers)
response = conn.getresponse()
content = response.read()
status = response.status
Expand Down

0 comments on commit feb3659

Please sign in to comment.