From 4ae9e3467d754eae1bf9ab9e16c05f3d11b0430b Mon Sep 17 00:00:00 2001 From: mbarto Date: Tue, 13 Dec 2016 10:08:08 +0100 Subject: [PATCH] Saidaipparla print preview (#1347) * fix #1331 added proxy call instead of original * fix * Fixed usage of CORS for print result * Added unit tests --- web/client/localConfig.json | 5 ++- web/client/plugins/print/index.js | 4 +-- web/client/utils/ConfigUtils.js | 35 ++++++++++++++++++- .../utils/__tests__/ConfigUtils-test.js | 10 ++++++ web/src/main/resources/proxy.properties | 4 +-- 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/web/client/localConfig.json b/web/client/localConfig.json index 58bb307720..96614d6659 100644 --- a/web/client/localConfig.json +++ b/web/client/localConfig.json @@ -1,5 +1,8 @@ { - "proxyUrl": "/mapstore/proxy/?url=", + "proxyUrl": { + "url": "/mapstore/proxy/?url=", + "useCORS": ["http://demo.geo-solutions.it/geoserver"] + }, "geoStoreUrl": "/mapstore/rest/geostore/", "printUrl": "http://demo.geo-solutions.it/geoserver/pdf/info.json", "bingApiKey": "AhuXBu7ipR1gNbBfXhtUAyCZ6rkC5PkWpxs2MnMRZ1ZupxQfivjLCch22ozKSCAn", diff --git a/web/client/plugins/print/index.js b/web/client/plugins/print/index.js index 6259715f07..b4ce81c40b 100644 --- a/web/client/plugins/print/index.js +++ b/web/client/plugins/print/index.js @@ -7,7 +7,7 @@ */ const {connect} = require('react-redux'); const {compose} = require('redux'); - +const ConfigUtils = require('../../utils/ConfigUtils'); const {Input} = require('react-bootstrap'); const {setPrintParameter, changePrintZoomLevel, changeMapPrintPreview, printCancel} = @@ -128,7 +128,7 @@ const PrintSubmit = connect((state) => ({ }))(require('../../components/print/PrintSubmit')); const PrintPreview = connect((state) => ({ - url: state.print && state.print.pdfUrl, + url: state.print && ConfigUtils.getProxiedUrl(state.print.pdfUrl), scale: state.controls && state.controls.print && state.controls.print.viewScale || 0.5, currentPage: state.controls && state.controls.print && state.controls.print.currentPage || 1, pages: state.controls && state.controls.print && state.controls.print.pages || 1 diff --git a/web/client/utils/ConfigUtils.js b/web/client/utils/ConfigUtils.js index 3838c87e09..985acc8461 100644 --- a/web/client/utils/ConfigUtils.js +++ b/web/client/utils/ConfigUtils.js @@ -7,7 +7,6 @@ */ var Proj4js = require('proj4'); var React = require('react'); - var url = require('url'); var axios = require('axios'); @@ -22,6 +21,8 @@ const centerPropType = React.PropTypes.shape({ crs: React.PropTypes.string }); +const {isObject} = require('lodash'); + const urlQuery = url.parse(window.location.href, true).query; const isMobile = require('ismobilejs'); @@ -227,6 +228,38 @@ var ConfigUtils = { getProxyUrl: function(config) { return config.proxyUrl ? config.proxyUrl : defaultConfig.proxyUrl; }, + getProxiedUrl: function(uri, config = {}) { + let sameOrigin = !(uri.indexOf("http") === 0); + let urlParts = !sameOrigin && uri.match(/([^:]*:)\/\/([^:]*:?[^@]*@)?([^:\/\?]*):?([^\/\?]*)/); + // ajax.addAuthenticationToAxios(config); + if (urlParts) { + let location = window.location; + sameOrigin = + urlParts[1] === location.protocol && + urlParts[3] === location.hostname; + let uPort = urlParts[4]; + let lPort = location.port; + let defaultPort = location.protocol.indexOf("https") === 0 ? 443 : 80; + uPort = uPort === "" ? defaultPort + "" : uPort + ""; + lPort = lPort === "" ? defaultPort + "" : lPort + ""; + sameOrigin = sameOrigin && uPort === lPort; + } + if (!sameOrigin) { + let proxyUrl = ConfigUtils.getProxyUrl(config); + if (proxyUrl) { + let useCORS = []; + if (isObject(proxyUrl)) { + useCORS = proxyUrl.useCORS || []; + proxyUrl = proxyUrl.url; + } + const isCORS = useCORS.reduce((found, current) => found || uri.indexOf(current) === 0, false); + if (!isCORS) { + return proxyUrl + encodeURIComponent(uri); + } + } + } + return uri; + }, /** * Utility to detect browser properties. * Code from leaflet-src.js diff --git a/web/client/utils/__tests__/ConfigUtils-test.js b/web/client/utils/__tests__/ConfigUtils-test.js index e4180b7391..af32616b3e 100644 --- a/web/client/utils/__tests__/ConfigUtils-test.js +++ b/web/client/utils/__tests__/ConfigUtils-test.js @@ -268,4 +268,14 @@ describe('ConfigUtils', () => { expect(layer.url[0]).toBe(ConfigUtils.getConfigProp('proxyUrl')); expect(layer.url[1]).toBe(ConfigUtils.getConfigProp('proxyUrl')); }); + + it('proxied url', () => { + expect(ConfigUtils.getProxiedUrl('http://remote.url')).toBe(ConfigUtils.getConfigProp('proxyUrl') + encodeURIComponent('http://remote.url')); + expect(ConfigUtils.getProxiedUrl('http://remote.cors', { + proxyUrl: { + url: 'myproxy', + useCORS: ['http://remote.cors'] + } + })).toBe('http://remote.cors'); + }); }); diff --git a/web/src/main/resources/proxy.properties b/web/src/main/resources/proxy.properties index 1817a8f987..27a31dfc82 100644 --- a/web/src/main/resources/proxy.properties +++ b/web/src/main/resources/proxy.properties @@ -16,7 +16,7 @@ defaultStreamByteSize=1024 # ########### #hostnameWhitelist = localhost,demo1.geo-solutions.it,demo.geo-solutions.it -mimetypeWhitelist = application/force-download,text/html,text/plain,application/xml,text/xml,application/vnd.ogc.sld+xml,application/vnd.ogc.gml,application/json,application/vnd.ogc.wms_xml,application/x-www-form-urlencoded,image/png +mimetypeWhitelist = application/force-download,text/html,text/plain,application/xml,text/xml,application/vnd.ogc.sld+xml,application/vnd.ogc.gml,application/json,application/vnd.ogc.wms_xml,application/x-www-form-urlencoded,image/png,application/pdf methodsWhitelist = GET,POST,PUT @@ -27,4 +27,4 @@ reqtypeWhitelist.capabilities = .*[Gg]et[Cc]apabilities.* reqtypeWhitelist.featureinfo = .*[Gg]et[Ff]eature[Ii]nfo.* reqtypeWhitelist.csw = .*csw.* reqtypeWhitelist.geostore = .*geostore.* -reqtypeWhitelist.generic = (.*exist.*)|(.*servicebox.*)|(.*pdf.*)|(.*map.*)|(.*d.*)|(.*ows.*) \ No newline at end of file +reqtypeWhitelist.generic = (.*exist.*)|(.*servicebox.*)|(.*pdf.*)|(.*map.*)|(.*d.*)|(.*ows.*)