Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3712 SLD styles version 1.1.0 are not saved correctly from Style Editor #3718

Merged
merged 3 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions web/client/actions/__tests__/styleeditor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ describe('Test the styleeditor actions', () => {
const code = '* { stroke: #333333; }';
const format = 'css';
const init = true;
const languageVersion = { version: '1.0.0' };
const retval = updateTemporaryStyle({
temporaryId,
templateId,
code,
format,
init
init,
languageVersion
});
expect(retval).toExist();
expect(retval.type).toBe(UPDATE_TEMPORARY_STYLE);
Expand All @@ -65,6 +67,7 @@ describe('Test the styleeditor actions', () => {
expect(retval.code).toBe(code);
expect(retval.format).toBe(format);
expect(retval.init).toBe(init);
expect(retval.languageVersion).toEqual(languageVersion);
});
it('updateStatus', () => {
const status = 'edit';
Expand Down Expand Up @@ -92,13 +95,15 @@ describe('Test the styleeditor actions', () => {
const code = '* { stroke: #333333; }';
const format = 'css';
const init = true;
const retval = selectStyleTemplate({ code, templateId, format, init });
const languageVersion = { version: '1.0.0' };
const retval = selectStyleTemplate({ code, templateId, format, init, languageVersion });
expect(retval).toExist();
expect(retval.type).toBe(SELECT_STYLE_TEMPLATE);
expect(retval.templateId).toBe(templateId);
expect(retval.code).toBe(code);
expect(retval.format).toBe(format);
expect(retval.init).toBe(init);
expect(retval.languageVersion).toEqual(languageVersion);
});
it('createStyle', () => {
const settings = { title: 'Title', _abstract: ''};
Expand Down
10 changes: 6 additions & 4 deletions web/client/actions/styleeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ function updateStatus(status) {
* @param {object} styleProps { code, templateId, format, init } init set initialCode
* @return {object} of type `SELECT_STYLE_TEMPLATE` styleProps
*/
function selectStyleTemplate({ code, templateId, format, init } = {}) {
function selectStyleTemplate({ code, templateId, format, languageVersion, init } = {}) {
return {
type: SELECT_STYLE_TEMPLATE,
code,
templateId,
format,
init
init,
languageVersion
};
}
/**
Expand All @@ -70,14 +71,15 @@ function selectStyleTemplate({ code, templateId, format, init } = {}) {
* @param {object} styleProps { temporaryId, templateId, code, format, init } init set initialCode
* @return {object} of type `UPDATE_TEMPORARY_STYLE` styleProps
*/
function updateTemporaryStyle({ temporaryId, templateId, code, format, init } = {}) {
function updateTemporaryStyle({ temporaryId, templateId, code, format, languageVersion, init } = {}) {
return {
type: UPDATE_TEMPORARY_STYLE,
temporaryId,
templateId,
code,
format,
init
init,
languageVersion
};
}
/**
Expand Down
30 changes: 23 additions & 7 deletions web/client/api/geoserver/Styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,27 @@ const { getNameParts, stringifyNameParts } = require('../../utils/StyleEditorUti
const contentTypes = {
css: 'application/vnd.geoserver.geocss+css',
sld: 'application/vnd.ogc.sld+xml',
// sldse: 'application/vnd.ogc.se+xml',
sldse: 'application/vnd.ogc.se+xml',
zip: 'application/zip'
};

const formatRequestData = ({options = {}, format, baseUrl, name, workspace}, isNameParam) => {
/**
* get correct content type based on format and version of style
* @param {string} format
* @param {object} languageVersion eg: { version: "1.0.0" }
*/
const getContentType = (format, languageVersion) => {
if (format === 'sld') {
return languageVersion && languageVersion.version && languageVersion.version === '1.1.0'
? contentTypes.sldse
: contentTypes.sld;
}
// set content type to sld if is missed
// to avoid 415 error with unknown content types
return contentTypes[format] || contentTypes.sld;
};

const formatRequestData = ({options = {}, format, baseUrl, name, workspace, languageVersion}, isNameParam) => {
const paramName = isNameParam ? {name: encodeURIComponent(name)} : {};
const opts = {
...options,
Expand All @@ -26,7 +42,7 @@ const formatRequestData = ({options = {}, format, baseUrl, name, workspace}, isN
},
headers: {
...(options.headers || {}),
'Content-Type': contentTypes[format]
'Content-Type': getContentType(format, languageVersion)
}
};
const url = `${baseUrl}rest/${workspace && `workspaces/${workspace}/` || ''}styles${!isNameParam ? `/${encodeURIComponent(name)}` : '.json'}`;
Expand Down Expand Up @@ -73,9 +89,9 @@ const Api = {
* @param {string} params.code style code
* @return {object} response
*/
createStyle: ({baseUrl, code, options, format = 'sld', styleName}) => {
createStyle: ({baseUrl, code, options, format = 'sld', styleName, languageVersion}) => {
const {name, workspace} = getNameParts(styleName);
const data = formatRequestData({options, format, baseUrl, name, workspace}, true);
const data = formatRequestData({options, format, baseUrl, name, workspace, languageVersion}, true);
return axios.post(data.url, code, data.options);
},
/**
Expand All @@ -88,9 +104,9 @@ const Api = {
* @param {string} params.code style code
* @return {object} response
*/
updateStyle: ({baseUrl, code, options, format = 'sld', styleName}) => {
updateStyle: ({baseUrl, code, options, format = 'sld', styleName, languageVersion}) => {
const {name, workspace} = getNameParts(styleName);
const data = formatRequestData({options, format, baseUrl, name, workspace});
const data = formatRequestData({options, format, baseUrl, name, workspace, languageVersion});
return axios.put(data.url, code, data.options);
},
/**
Expand Down
91 changes: 89 additions & 2 deletions web/client/api/geoserver/__tests__/Styles-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
* LICENSE file in the root directory of this source tree.
*/

var expect = require('expect');
var API = require('../Styles');
import expect from 'expect';
import API from '../Styles';
import MockAdapter from 'axios-mock-adapter';
import axios from '../../../libs/ajax';

let mockAxios;

describe('Test styles rest API', () => {
it('save style', (done) => {
Expand Down Expand Up @@ -87,3 +91,86 @@ describe('Test styles rest API', () => {
});
});
});

describe('Test styles rest API, Content Type of SLD', () => {

beforeEach(done => {
mockAxios = new MockAdapter(axios);
setTimeout(done);
});

afterEach(done => {
mockAxios.restore();
setTimeout(done);
});

it('test createStyle with sld version 1.1.0', (done) => {

mockAxios.onPost(/\/styles/).reply((config) => {
expect(config.headers['Content-Type']).toBe('application/vnd.ogc.se+xml');
expect(config.url).toBe('/geoserver/rest/styles.json');
done();
return [ 200, {}];
});

API.createStyle({
baseUrl: '/geoserver/',
code: '<StyledLayerDescriptor></StyledLayerDescriptor>',
format: 'sld',
styleName: 'style_name',
languageVersion: { version: '1.1.0' }
});
});

it('test createStyle with sld version 1.0.0', (done) => {

mockAxios.onPost(/\/styles/).reply((config) => {
expect(config.headers['Content-Type']).toBe('application/vnd.ogc.sld+xml');
expect(config.url).toBe('/geoserver/rest/styles.json');
done();
return [ 200, {}];
});

API.createStyle({
baseUrl: '/geoserver/',
code: '<StyledLayerDescriptor></StyledLayerDescriptor>',
format: 'sld',
styleName: 'style_name',
languageVersion: { version: '1.0.0' }
});
});

it('test updateStyle with sld version 1.1.0', (done) => {
mockAxios.onPut(/\/styles/).reply((config) => {
expect(config.headers['Content-Type']).toBe('application/vnd.ogc.se+xml');
expect(config.url).toBe('/geoserver/rest/styles/style_name');
done();
return [ 200, {}];
});

API.updateStyle({
baseUrl: '/geoserver/',
code: '<StyledLayerDescriptor></StyledLayerDescriptor>',
format: 'sld',
styleName: 'style_name',
languageVersion: { version: '1.1.0' }
});
});

it('test updateStyle with sld version 1.0.0', (done) => {
mockAxios.onPut(/\/styles/).reply((config) => {
expect(config.headers['Content-Type']).toBe('application/vnd.ogc.sld+xml');
expect(config.url).toBe('/geoserver/rest/styles/style_name');
done();
return [ 200, {}];
});

API.updateStyle({
baseUrl: '/geoserver/',
code: '<StyledLayerDescriptor></StyledLayerDescriptor>',
format: 'sld',
styleName: 'style_name',
languageVersion: { version: '1.0.0' }
});
});
});
13 changes: 11 additions & 2 deletions web/client/components/styleeditor/StyleList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
const React = require('react');

const { Glyphicon: GlyphiconRB } = require('react-bootstrap');

const BorderLayout = require('../layout/BorderLayout');
const emptyState = require('../misc/enhancers/emptyState');
const withLocal = require("../misc/enhancers/localizedProps");
Expand All @@ -27,6 +26,16 @@ const SideGrid = emptyState(
}
)(require('../misc/cardgrids/SideGrid'));

// get the text to use in the icon
const getFormatText = (format) => {
const text = {
sld: 'SLD',
css: 'CSS',
mbstyle: 'MBS'
};
return text[format] || format || '';
};

/**
* Component for rendering a grid of style templates.
* @memberof components.styleeditor
Expand Down Expand Up @@ -83,7 +92,7 @@ const StyleList = ({
backgroundColor="#333333"
texts={[
{
text: style.format.toUpperCase(),
text: getFormatText(style.format).toUpperCase(),
fill: formatColors[style.format] || '#f2f2f2',
style: {
fontSize: 70,
Expand Down
5 changes: 3 additions & 2 deletions web/client/components/styleeditor/StyleToolbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ const StyleToolbar = ({
onSelectStyle = () => {},
onEditStyle = () => {},
onUpdate = () => {},
onSetDefault = () => {}
onSetDefault = () => {},
disableCodeEditing
}) => (
<div>
<Toolbar
Expand Down Expand Up @@ -101,7 +102,7 @@ const StyleToolbar = ({
glyph: 'code',
tooltipId: 'styleeditor.editSelectedStyle',
visible: !status && editEnabled ? true : false,
disabled: !!loading || defaultStyles.indexOf(selectedStyle) !== -1,
disabled: !!loading || defaultStyles.indexOf(selectedStyle) !== -1 || disableCodeEditing,
onClick: () => onEditStyle()
},
{
Expand Down
Loading