Mercurial > gemma
annotate pkg/controllers/geostyling.go @ 2006:35acb7f9ae0c
Do anything else before expectedly failing role creation
Creating roles during database setup expectedly fails in case there
already is another gemma database in the cluster. Doing it at the end
of the transaction ensures it does not hide errors in other commands
in the script.
In passing, add the default admin via the designated view to ensure it
will become a correctly set up application user.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Thu, 24 Jan 2019 17:23:43 +0100 |
parents | 22148eb0f986 |
children | 4a9a1e323e11 |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
914
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
914
diff
changeset
|
2 // without warranty, see README.md and license for details. |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
914
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
914
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
914
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
914
diff
changeset
|
6 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
914
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
914
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
914
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
914
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
914
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
914
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
914
diff
changeset
|
13 |
870
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
14 package controllers |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
15 |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
17 "bytes" |
871
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
18 "fmt" |
870
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
19 "io" |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
20 "log" |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
21 "net/http" |
871
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
22 |
914
4bf3a3a20ce1
Finished direct updating when style is uploaded via controller.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
913
diff
changeset
|
23 "github.com/gorilla/mux" |
4bf3a3a20ce1
Finished direct updating when style is uploaded via controller.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
913
diff
changeset
|
24 |
913
876d1f5433be
Started with direct applying style after modification via controller. WIP.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
873
diff
changeset
|
25 "gemma.intevation.de/gemma/pkg/geoserver" |
871
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
26 "gemma.intevation.de/gemma/pkg/models" |
870
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
27 ) |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
28 |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
29 const ( |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
30 maxStyleSize = 5 * 1024 * 1024 |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
31 styleName = "style" |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
32 ) |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
33 |
873
ad9272460ef3
Do the XSLT to adjust the layer name when updating the style column in the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
871
diff
changeset
|
34 func extractStyle(req *http.Request) (string, error) { |
870
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
35 |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
36 f, _, err := req.FormFile(styleName) |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
37 if err != nil { |
873
ad9272460ef3
Do the XSLT to adjust the layer name when updating the style column in the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
871
diff
changeset
|
38 return "", err |
870
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
39 } |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
40 defer f.Close() |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
41 |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
42 var buf bytes.Buffer |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
43 |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
44 if _, err := io.Copy(&buf, io.LimitReader(f, maxStyleSize)); err != nil { |
873
ad9272460ef3
Do the XSLT to adjust the layer name when updating the style column in the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
871
diff
changeset
|
45 return "", err |
870
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
46 } |
873
ad9272460ef3
Do the XSLT to adjust the layer name when updating the style column in the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
871
diff
changeset
|
47 return buf.String(), nil |
870
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
48 } |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
49 |
871
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
50 func supportedWMSFeature(name string) bool { |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
51 return len(models.InternalServices.Filter( |
914
4bf3a3a20ce1
Finished direct updating when style is uploaded via controller.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
913
diff
changeset
|
52 models.IntAnd( |
4bf3a3a20ce1
Finished direct updating when style is uploaded via controller.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
913
diff
changeset
|
53 models.IntWMS, |
4bf3a3a20ce1
Finished direct updating when style is uploaded via controller.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
913
diff
changeset
|
54 models.IntByName(name)))) > 0 |
871
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
55 } |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
56 |
870
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
57 func uploadStyle(rw http.ResponseWriter, req *http.Request) { |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
58 |
871
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
59 feature := mux.Vars(req)["feature"] |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
60 |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
61 // only allow internal WMS features |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
62 if !supportedWMSFeature(feature) { |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
63 http.Error(rw, |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
64 fmt.Sprintf("WMS feature %s is not found.", feature), |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
65 http.StatusNotFound) |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
66 return |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
67 } |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
68 |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
69 style, err := extractStyle(req) |
870
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
70 if err != nil { |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
71 log.Printf("error: %v\n", err) |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
72 http.Error(rw, "error: "+err.Error(), http.StatusBadRequest) |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
73 return |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
74 } |
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
75 |
1760
22148eb0f986
More on harmonizing logging.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
76 log.Printf("info: uploaded file length: %d\n", len(style)) |
871
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
77 |
873
ad9272460ef3
Do the XSLT to adjust the layer name when updating the style column in the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
871
diff
changeset
|
78 if err := models.UpdateInternalStyle(req, feature, style); err != nil { |
871
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
79 log.Printf("error: %v\n", err) |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
80 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
81 return |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
82 } |
f0b6852c14d1
More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
870
diff
changeset
|
83 |
913
876d1f5433be
Started with direct applying style after modification via controller. WIP.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
873
diff
changeset
|
84 geoserver.ReconfigureStyle(feature) |
914
4bf3a3a20ce1
Finished direct updating when style is uploaded via controller.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
913
diff
changeset
|
85 |
4bf3a3a20ce1
Finished direct updating when style is uploaded via controller.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
913
diff
changeset
|
86 // Nothing to return |
4bf3a3a20ce1
Finished direct updating when style is uploaded via controller.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
913
diff
changeset
|
87 rw.WriteHeader(http.StatusNoContent) |
870
29c11f4bf9db
Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
88 } |