forked from hishamkaram/geoserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyles.go
209 lines (192 loc) · 6.03 KB
/
styles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package geoserver
import (
"bytes"
"fmt"
"io"
"strconv"
)
// StyleService define all geoserver style operations
type StyleService interface {
GetStyles(workspaceName string) (styles []*Resource, err error)
CreateStyle(workspaceName string, styleName string) (created bool, err error)
UploadStyle(data io.Reader, workspaceName string, styleName string, overwrite bool) (success bool, err error)
DeleteStyle(workspaceName string, styleName string, purge bool) (deleted bool, err error)
GetStyle(workspaceName string, styleName string) (style *Style, err error)
StyleExists(workspaceName string, styleName string) (exists bool, err error)
}
//LanguageVersion style version
type LanguageVersion struct {
Version string `json:"version,omitempty"`
}
//Style holds geoserver style
type Style struct {
Name string `json:"name,omitempty"`
Format string `json:"format,omitempty"`
Filename string `json:"filename,omitempty"`
LanguageVersion *LanguageVersion `json:"languageVersion,omitempty"`
}
//StyleRequestBody is the api body
type StyleRequestBody struct {
Style *Style `json:"style,omitempty"`
}
// Styles holds a list of geoserver styles
type Styles struct {
Style []Style `json:"styles,omitempty"`
}
//GetStyles return list of geoserver styles and err if error occurred,
//if workspace is "" will return non-workspce styles
func (g *GeoServer) GetStyles(workspaceName string) (styles []*Resource, err error) {
if workspaceName != "" {
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := fmt.Sprintf("%srest/%sstyles", g.ServerURL, workspaceName)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
styles = nil
err = g.GetError(responseCode, response)
return
}
var stylesResponse struct {
Styles struct {
Style []*Resource `json:"style,omitempty"`
} `json:"styles,omitempty"`
}
g.DeSerializeJSON(response, &stylesResponse)
styles = stylesResponse.Styles.Style
return
}
//GetStyle return specific of geoserver style,
//if workspace is "" will return non-workspce styles
func (g *GeoServer) GetStyle(workspaceName string, styleName string) (style *Style, err error) {
if workspaceName != "" {
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceName, "styles", styleName)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
style = &Style{}
err = g.GetError(responseCode, response)
return
}
var stylesResponse StyleRequestBody
g.DeSerializeJSON(response, &stylesResponse)
style = stylesResponse.Style
return
}
//StyleExists return true if style exists in geoserver
func (g *GeoServer) StyleExists(workspaceName string, styleName string) (exists bool, err error) {
_, styleErr := g.GetStyle(workspaceName, styleName)
if styleErr != nil {
exists = false
err = styleErr
return
}
exists = true
return
}
//CreateStyle create geoserver empty sld with name and filename is(${styleName.sld}),
//if workspace is "" will create geoserver public style
func (g *GeoServer) CreateStyle(workspaceName string, styleName string) (created bool, err error) {
if workspaceName != "" {
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceName, "styles")
var style = Style{Name: styleName, Filename: fmt.Sprintf("%s.sld", styleName)}
serializedStyle, _ := g.SerializeStruct(StyleRequestBody{Style: &style})
data := bytes.NewBuffer(serializedStyle)
httpRequest := HTTPRequest{
Method: postMethod,
Accept: jsonType,
Data: data,
DataType: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusCreated {
g.logger.Error(string(response))
created = false
err = g.GetError(responseCode, response)
return
}
created = true
return
}
//UploadStyle upload geoserver sld,
//if workspace is "" will upload geoserver public style sld , return err if error occurred
func (g *GeoServer) UploadStyle(data io.Reader, workspaceName string, styleName string, overwrite bool) (success bool, err error) {
workspaceURL := ""
if workspaceName != "" {
workspaceURL = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceURL, "styles", styleName)
exists, _ := g.StyleExists(workspaceName, styleName)
if exists && !overwrite {
g.logger.Error(exists)
success = false
err = g.GetError(statusForbidden, []byte("Style Already Exists"))
return
}
if !exists {
created, uploadErr := g.CreateStyle(workspaceName, styleName)
if !created {
success = false
err = uploadErr
return
}
}
httpRequest := HTTPRequest{
Method: putMethod,
Accept: jsonType,
Data: data,
DataType: sldType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
success = false
err = g.GetError(responseCode, response)
return
}
success = true
return
}
//DeleteStyle delete geoserver style,
//if workspace is "" will delete geoserver public style , return err if error occurred
func (g *GeoServer) DeleteStyle(workspaceName string, styleName string, purge bool) (deleted bool, err error) {
if workspaceName != "" {
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceName, "styles", styleName)
httpRequest := HTTPRequest{
Method: deleteMethod,
Accept: jsonType,
URL: targetURL,
Query: map[string]string{"purge": strconv.FormatBool(purge)},
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
deleted = false
err = g.GetError(responseCode, response)
return
}
deleted = true
return
}