annotate pkg/controllers/geostyling.go @ 913:876d1f5433be geo-style

Started with direct applying style after modification via controller. WIP.
author Sascha L. Teichmann <teichmann@intevation.de>
date Tue, 02 Oct 2018 23:29:55 +0200
parents ad9272460ef3
children 4bf3a3a20ce1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
1 package controllers
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
2
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
3 import (
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
4 "bytes"
871
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
5 "fmt"
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
6 "io"
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
7 "log"
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
8 "net/http"
871
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
9
913
876d1f5433be Started with direct applying style after modification via controller. WIP.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 873
diff changeset
10 "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
11 "gemma.intevation.de/gemma/pkg/models"
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
12 "github.com/gorilla/mux"
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
13 )
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
14
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
15 const (
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
16 maxStyleSize = 5 * 1024 * 1024
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
17 styleName = "style"
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
18 )
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
19
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
20 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
21
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
22 f, _, err := req.FormFile(styleName)
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
23 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
24 return "", err
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
25 }
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
26 defer f.Close()
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 var buf bytes.Buffer
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
29
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
30 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
31 return "", err
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
32 }
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
33 return buf.String(), nil
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
34 }
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
35
871
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
36 func supportedWMSFeature(name string) bool {
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
37 return len(models.InternalServices.Filter(
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
38 models.IntAnd(models.IntWMS, models.IntByName(name)))) > 0
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
39 }
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
40
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
41 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
42
871
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
43 feature := mux.Vars(req)["feature"]
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
44
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
45 // only allow internal WMS features
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
46 if !supportedWMSFeature(feature) {
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
47 http.Error(rw,
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
48 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
49 http.StatusNotFound)
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
50 return
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
51 }
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
52
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
53 style, err := extractStyle(req)
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
54 if err != nil {
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
55 log.Printf("error: %v\n", err)
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
56 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
57 return
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
58 }
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
59
871
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
60 log.Printf("uploaded file length: %d\n", len(style))
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
61
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
62 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
63 log.Printf("error: %v\n", err)
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
64 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
65 return
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
66 }
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
67
913
876d1f5433be Started with direct applying style after modification via controller. WIP.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 873
diff changeset
68 geoserver.ReconfigureStyle(feature)
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
69 }