-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathlayergroups.go
187 lines (172 loc) · 6.24 KB
/
layergroups.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
package geoserver
import (
"bytes"
"encoding/json"
"fmt"
)
//PublishedGroupLayers geoserver published layers
type PublishedGroupLayers []*GroupPublishableItem
//GroupPublishableItem geoserver Group
type GroupPublishableItem struct {
Type string `json:"@type,omitempty" xml:"type"`
Name string `json:"name,omitempty" xml:"name"`
Href string `json:"href,omitempty" xml:"href"`
}
//LayerGroupKeywords geoserver layergroups keywords
type LayerGroupKeywords struct {
Keyword []*string `json:"keyword,omitempty"`
}
//Publishables Geoserver Published Layers
type Publishables struct {
Published PublishedGroupLayers `json:"published" xml:"published"`
}
//UnmarshalJSON custom deserialization to handle published layers of group
func (u *PublishedGroupLayers) UnmarshalJSON(data []byte) error {
var raw interface{}
err := json.Unmarshal(data, &raw)
if err != nil {
return err
}
switch raw := raw.(type) {
case map[string]interface{}:
var layers PublishedGroupLayers
*u = append(layers, &GroupPublishableItem{Name: raw["name"].(string), Href: raw["href"].(string), Type: raw["@type"].(string)})
case []interface{}:
var publishedGroupLayers []*GroupPublishableItem
json.Unmarshal(data, &publishedGroupLayers)
*u = publishedGroupLayers
}
return nil
}
//LayerGroupStyles geoserver layergroup styles
type LayerGroupStyles struct {
Style []*Resource `json:"style,omitempty" xml:"style"`
}
//LayerGroup geoserver layergroup details
type LayerGroup struct {
Name string `json:"name,omitempty" xml:"name"`
Mode string `json:"mode,omitempty" xml:"mode"`
Title string `json:"title,omitempty" xml:"title"`
Workspace *Resource `json:"workspace,omitempty" xml:"workspace"`
Publishables Publishables `json:"publishables,omitempty" xml:"publishables"`
Styles LayerGroupStyles `json:"styles,omitempty" xml:"styles"`
Bounds NativeBoundingBox `json:"bounds,omitempty" xml:"bounds"`
MetadataLinks []*MetadataLink `json:"metadataLinks,omitempty" xml:"metadataLinks"`
Keywords LayerGroupKeywords `json:"keywords,omitempty" xml:"keywords"`
}
type layerGroupResponse struct {
LayerGroups struct {
LayerGroup []*Resource `json:"layerGroup,omitempty"`
} `json:"layerGroups,omitempty"`
}
type layerGroupDetailsResponse struct {
LayerGroup *LayerGroup `json:"layerGroup,omitempty"`
}
// LayerGroupService define geoserver layergroup operations
type LayerGroupService interface {
GetLayerGroups(workspaceName string) (layerGroups []*Resource, err error)
GetLayerGroup(workspaceName string, layerGroupName string) (layer *LayerGroup, err error)
CreateLayerGroup(workspaceName string, layerGroup *LayerGroup) (created bool, err error)
DeleteLayerGroup(workspaceName string, layerGroupName string) (deleted bool, err error)
}
//GetLayerGroups get all layergroups from workspace in geoserver else return error,
//if workspace is "" the it will return all public layers in geoserver
func (g *GeoServer) GetLayerGroups(workspaceName string) (layerGroups []*Resource, err error) {
if workspaceName != "" {
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceName, "layergroups")
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
layerGroups = nil
err = g.GetError(responseCode, response)
return
}
var layerGroupList layerGroupResponse
g.DeSerializeJSON(response, &layerGroupList)
layerGroups = layerGroupList.LayerGroups.LayerGroup
return
}
//GetLayerGroup get specific LayerGroup in a workspace from geoserver else return error,
//if workspace is "" the it will return geoserver public layer with ${layerName}
func (g *GeoServer) GetLayerGroup(workspaceName string, layerGroupName string) (layerGroup *LayerGroup, err error) {
if workspaceName != "" {
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceName, "layergroups", layerGroupName)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
layerGroup = &LayerGroup{}
err = g.GetError(responseCode, response)
return
}
var layerGroupResponse layerGroupDetailsResponse
g.DeSerializeJSON(response, &layerGroupResponse)
layerGroup = layerGroupResponse.LayerGroup
return
}
//CreateLayerGroup create specific LayerGroup in geoserver return created=true else created=false and the error,
//if workspace is "" the it will return geoserver public layer with ${layerName}
func (g *GeoServer) CreateLayerGroup(workspaceName string, layerGroup *LayerGroup) (created bool, err error) {
if workspaceName != "" {
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
group := layerGroupDetailsResponse{LayerGroup: layerGroup}
serializedGroup, _ := g.SerializeStruct(group)
targetURL := g.ParseURL("rest", workspaceName, "layergroups")
data := bytes.NewBuffer(serializedGroup)
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
}
//DeleteLayerGroup delete geoserver layergroup else return error,
//if workspace is "" will delete public layergroup with name ${layerGroupName} if exists
func (g *GeoServer) DeleteLayerGroup(workspaceName string, layerGroupName string) (deleted bool, err error) {
if workspaceName != "" {
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceName, "layergroups", layerGroupName)
httpRequest := HTTPRequest{
Method: deleteMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
deleted = false
err = g.GetError(responseCode, response)
return
}
deleted = true
return
}