Skip to content

Commit

Permalink
Saidaipparla print preview (#1347)
Browse files Browse the repository at this point in the history
* fix #1331 added proxy call instead of original

* fix

* Fixed usage of CORS for print result

* Added unit tests
  • Loading branch information
mbarto authored Dec 13, 2016
1 parent 7c110f9 commit 4ae9e34
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
5 changes: 4 additions & 1 deletion web/client/localConfig.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions web/client/plugins/print/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} =
Expand Down Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion web/client/utils/ConfigUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
var Proj4js = require('proj4');
var React = require('react');

var url = require('url');

var axios = require('axios');
Expand All @@ -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');
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions web/client/utils/__tests__/ConfigUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
4 changes: 2 additions & 2 deletions web/src/main/resources/proxy.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.*)
reqtypeWhitelist.generic = (.*exist.*)|(.*servicebox.*)|(.*pdf.*)|(.*map.*)|(.*d.*)|(.*ows.*)

0 comments on commit 4ae9e34

Please sign in to comment.