-
Notifications
You must be signed in to change notification settings - Fork 11
/
PublishService.groovy
480 lines (407 loc) · 21 KB
/
PublishService.groovy
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* Copyright (C) 2016 Atlas of Living Australia
* All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*/
package au.org.ala.spatial.service
import au.org.ala.spatial.util.UploadSpatialResource
import grails.converters.JSON
import org.apache.commons.io.FileUtils
class PublishService {
def grailsApplication
def manageLayersService
def tasksService
def fileService
def dataSource
def layerDao
def fieldDao
def objectDao
// Unpacks a published zip file and performs some actions.
// Run time should be kept to a minimum because a spatial-slave is waiting for this to complete
// before flagging task as finished.
//
// returns error map
Map publish(zip) {
//TODO: use a queue
// unpack zip
fileService.unzip(zip.getPath(), zip.getParent(), false)
// read spec.json
def path = zip.getParent()
def spec = grails.converters.JSON.parse(FileUtils.readFileToString(new File(path + '/spec.json')))
// deploy outputs
spec.output.each { k, output ->
if ('file'.equalsIgnoreCase(k) || 'metadata'.equalsIgnoreCase(k)) {
// no activity required. The unzip takes care of this
} else if ('delete'.equalsIgnoreCase(k)) {
delete(output, path)
} else if ('shapefile'.equalsIgnoreCase(k) || 'raster'.equalsIgnoreCase(k) || 'layer'.equalsIgnoreCase(k) ||
'layers'.equalsIgnoreCase(k) || 'envelopes'.equalsIgnoreCase(k)) {
// note: identify single area shapefile and raster as contextual layer variations
// put into geoserver or as new layer
spec.history.putAll(layerToGeoserver(output, path))
// update output.wms
// update output.geoserverLayerName
// register as a layer (single area or contextual or environmental)
} else if ('sld'.equalsIgnoreCase(k) ) {
//skipSLDCreation
addStyle(output, path)
} else if ('areas'.equalsIgnoreCase(k)) {
addArea(output, path)
} else if ('sql'.equalsIgnoreCase(k)) {
// trigger sql execution
spec.history.putAll(runSql(output, path))
} else if ('append'.equalsIgnoreCase(k)) {
if (output.files != null) {
def idx = output.files.get(0).indexOf('?')
def file = output.files.get(0).substring(0, idx)
def append = output.files.get(0).substring(idx + 1) + '\n'
FileUtils.writeStringToFile(new File(grailsApplication.config.data.dir + file), append, true)
}
}
}
// create requested tasks
// deploy outputs
spec.output.each { k, output ->
if ('process'.equalsIgnoreCase(k)) {
output.files.each { file ->
def pos = file.toString().indexOf(' ')
def name
def input
def tag
if (pos <= 0) {
name = file
input = [:]
tag = ''
} else {
name = file.substring(0, pos)
input = JSON.parse(file.substring(pos + 1))
tag = file.substring(pos + 1)
}
tasksService.create(name, tag, input)
}
}
}
// create download zip
spec.output.each { k, output ->
if ('download'.equalsIgnoreCase(k)) {
fileService.zip(zip.getParent() + File.separator + "download.zip", zip.getParent(), output.files)
}
}
//delete zip
zip.delete()
spec
}
def runSqlStatement(sql) {
def errors = [:]
def conn = dataSource.getConnection()
def statement = conn.createStatement()
try {
try {
statement.execute(sql)
} catch (err) {
errors.put(String.valueOf(System.currentTimeMillis()), 'failed to run sql: ' + sql)
log.error 'failed to run sql: ' + sql, err
}
} catch (err) {
log.error err
} finally {
if (statement != null) {
statement.close()
}
if (conn != null) {
conn.close()
}
}
errors
}
def addStyle(output, path) {
def errors = [:]
try {
if (grailsApplication.config.geoserver.canDeploy.toBoolean()) {
def geoserverUrl = grailsApplication.config.geoserver.url
def geoserverUsername = grailsApplication.config.geoserver.username
def geoserverPassword = grailsApplication.config.geoserver.password
output.files.each { file ->
def p = (file.startsWith('/') ? grailsApplication.config.data.dir + file : path + '/' + file)
def name = new File(p).getName().replace(".sld", "")
//Create style
String extra = "";
String out = UploadSpatialResource.loadCreateStyle(geoserverUrl + "/rest/styles/",
extra, geoserverUsername, geoserverPassword, name)
if (!out.startsWith("200") && !out.startsWith("201")) {
//ignore errors
errors.put(String.valueOf(System.currentTimeMillis()), out)
}
//Upload sld
out = UploadSpatialResource.loadSld(geoserverUrl + "/rest/styles/" + name,
extra, geoserverUsername, geoserverPassword, p);
if (!out.startsWith("200") && !out.startsWith("201")) {
errors.put(String.valueOf(System.currentTimeMillis()), out)
} else {
//when the sld is for a field, apply to the layer as the default sld
def field = fieldDao.getFieldById(name, false)
if (field != null) {
def layer = layerDao.getLayerById(Integer.parseInt(field.spid), false)
if (layer != null) {
//Apply style
String data = "<layer><enabled>true</enabled><defaultStyle><name>" + name +
"</name></defaultStyle></layer>";
out = UploadSpatialResource.assignSld(geoserverUrl + "/rest/layers/ALA:" + layer.name, extra,
geoserverUsername, geoserverPassword, data)
if (!out.startsWith("200") && !out.startsWith("201")) {
//ignore errors
// errors.put(String.valueOf(System.currentTimeMillis()), out)
}
//add Style to layer styles
data = "<style><name>" + name + "</name></style>"
out = UploadSpatialResource.assignSld(geoserverUrl + "/rest/layers/ALA:" + layer.name + "/styles", extra,
geoserverUsername, geoserverPassword, data)
if (!out.startsWith("200") && !out.startsWith("201")) {
//ignore errors
// errors.put(String.valueOf(System.currentTimeMillis()), out)
}
}
}
}
}
}
} catch (err) {
log.error 'failed to upload sld: ' + output + ', ' + path, err
}
errors
}
def delete(output, path) {
def errors = [:]
try {
output.files.each { file ->
def p = (file.startsWith('/') ? grailsApplication.config.data.dir + file : path + '/' + file)
def f = new File(p)
if (f.exists()) {
try {
f.delete()
} catch (err) {
log.error 'failed to delete file: ' + file + ', ' + path, err
}
}
}
} catch (err) {
log.error 'failed to delete file: ' + output + ', ' + path, err
}
errors
}
def addArea(output, path) {
def errors = [:]
try {
def newAreas = []
output.files.each { json ->
def values = JSON.parse(json)
def p = (values.file.startsWith('/') ? grailsApplication.config.data.dir + values.file : path + '/' + values.file)
String wkt = FileUtils.readFileToString(new File(p))
String generatedPid = objectDao.createUserUploadedObject(wkt, values.name, values.description, null);
newAreas.add(generatedPid)
}
//replace areas with pids
output.files = newAreas
} catch (err) {
log.error 'failed to upload area: ' + output + ', ' + path, err
}
errors
}
def runSql(output, path) {
def errors = [:]
def conn = dataSource.getConnection()
def statement = conn.createStatement()
try {
output.files.each { file ->
def p = (file.startsWith('/') ? grailsApplication.config.data.dir + file : path + '/' + file)
try {
statement.execute(FileUtils.readFileToString(new File(p)))
} catch (err) {
errors.put(String.valueOf(System.currentTimeMillis()), 'failed to run sql: ' + p)
log.error 'failed to run sql: ' + p, err
}
}
} catch (err) {
log.error err
} finally {
if (statement != null) {
statement.close()
}
if (conn != null) {
conn.close()
}
}
errors
}
def callGeoserver(String type, String urlPath, String file, String resource) {
return manageLayersService.httpCall(type,
grailsApplication.config.geoserver.url + urlPath,
grailsApplication.config.geoserver.username,
grailsApplication.config.geoserver.password,
file, resource, "text/plain")
}
def callGeoserver(String type, String urlPath, String file, String resource, String contentType) {
return manageLayersService.httpCall(type,
grailsApplication.config.geoserver.url + urlPath,
grailsApplication.config.geoserver.username,
grailsApplication.config.geoserver.password,
file, resource, contentType)
}
def callGeoserverDelete(String urlPath) {
def getResponse = callGeoserver("GET", urlPath, null, null)
// only delete when there is a response status code 2xx
if (getResponse && getResponse[0].startsWith("2")) {
return callGeoserver("DELETE", urlPath, null, null)
} else {
return null;
}
}
def layerToGeoserver(output, path) {
def errors = [:]
if (grailsApplication.config.geoserver.canDeploy.toBoolean()) {
def geoserverUrl = grailsApplication.config.geoserver.url
def geoserverUsername = grailsApplication.config.geoserver.username
def geoserverPassword = grailsApplication.config.geoserver.password
output.files.each { f ->
def p = path == null ? f : (f.startsWith('/') ? grailsApplication.config.data.dir + f : path + '/' + f)
def file = f
if (f.startsWith("{")) {
// parse 'file' out of JSON
def values = JSON.parse(f)
p = (values.file.startsWith('/') ? grailsApplication.config.data.dir + values.file : path + '/' + values.file)
file = values.file
}
if (!file.endsWith('.tif') && !file.endsWith('.shp')) {
if (new File(p + '.tif').exists()) {
p = p + '.tif'
file = file + '.tif'
} else if (new File(p + '.shp').exists()) {
p = p + '.shp'
file = file + '.shp'
}
}
if (file.endsWith('.tif')) {
def geotiff = new File(p)
def sld = new File(p.replace(".tif", ".sld"))
def name = geotiff.getName().replace('.tif', '')
if (geotiff.exists()) {
try {
//TODO: Why is.prj interfering with Geoserver discovering .tif is EPSG:4326?
def oldPrj = new File(p.replace('.tif', '.prj'))
def tmpPrj = new File(p.replace('.tif', '.prj.tmp'))
if (oldPrj.exists()) FileUtils.moveFile(oldPrj, tmpPrj)
//attempt to delete
callGeoserverDelete("/rest/workspaces/ALA/coveragestores/" + name)
if (grailsApplication.config.geoserver.remote.geoserver_data_dir) {
// delete the tif file if it exists
callGeoserverDelete("/rest/resource/data/" + name + ".tif")
// delete the prj file if it exists
callGeoserverDelete("/rest/resource/data/" + name + ".prj")
// upload the tif file
callGeoserver("PUT", "/rest/resource/data/" + name + ".tif", geotiff.getPath(), null)
// create the layer
callGeoserver("PUT", "/rest/workspaces/ALA/coveragestores/" + name + "/external.geotiff?configure=first",
null, "file://" + grailsApplication.config.geoserver.remote.geoserver_data_dir + "/data/" + name + ".tif")
// upload the prj file
if (tmpPrj.exists()) {
callGeoserver("PUT", "/rest/resource/data/" + name + ".prj", tmpPrj.getPath(), null)
}
} else {
String[] result = callGeoserver("PUT", "/rest/workspaces/ALA/coveragestores/" + name + "/external.geotiff?configure=first",
null, "file://" + geotiff.getPath());
if (result[0] != "200" && result[0] != "201") {
errors.put(String.valueOf(System.currentTimeMillis()), result[0] + ": " + result[1])
}
}
//return prj
if (tmpPrj.exists()) FileUtils.moveFile(tmpPrj, oldPrj)
if (sld.exists()) {
//Create style
String extra = "";
String out = UploadSpatialResource.loadCreateStyle(geoserverUrl + "/rest/styles/",
extra, geoserverUsername, geoserverPassword, name)
if (!out.startsWith("200") && !out.startsWith("201")) {
errors.put(String.valueOf(System.currentTimeMillis()), out)
}
//Upload sld
out = UploadSpatialResource.loadSld(geoserverUrl + "/rest/styles/" + name,
extra, geoserverUsername, geoserverPassword, sld.getPath());
if (!out.startsWith("200") && !out.startsWith("201")) {
errors.put(String.valueOf(System.currentTimeMillis()), out)
}
//Apply style
String data = "<layer><enabled>true</enabled><defaultStyle><name>" + name +
"</name></defaultStyle></layer>";
out = UploadSpatialResource.assignSld(geoserverUrl + "/rest/layers/ALA:" + name, extra,
geoserverUsername, geoserverPassword, data)
if (!out.startsWith("200") && !out.startsWith("201")) {
errors.put(String.valueOf(System.currentTimeMillis()), out)
}
}
} catch (err) {
log.error 'failed to upload geotiff to geoserver: ' + geotiff.getPath(), err
}
}
} else if (file.endsWith('.shp')) {
def shp = new File(p)
def name = shp.getName().replace('.shp', '')
def sld = new File(name + ".sld")
callGeoserverDelete("/rest/workspaces/ALA/datastores/" + name)
if (grailsApplication.config.geoserver.remote.geoserver_data_dir) {
for (String filetype : ["shp", "prj", "shx", "dbf", "fix", "sbn", "sbx", "fbn", "fbx", "qix", "cpg", "shp.xml", "atx", "mxs", "ixs", "ain", "aih"]) {
// delete file if it exists
callGeoserverDelete("/rest/resource/data/" + name + "." + filetype)
// upload the file
File uploadFile = new File(shp.getPath().replace(".shp", "." + filetype))
if (uploadFile.exists()) {
callGeoserver("PUT", "/rest/resource/data/" + name + "." + filetype, uploadFile.getPath(), null)
}
}
// create the layer
callGeoserver("PUT", "/rest/workspaces/ALA/datastores/" + name + "/external.shp",
null, "file://" + shp.getPath())
} else {
if (grailsApplication.config.geoserver.spatialservice.colocated.toBoolean()) {
String[] result = callGeoserver("PUT", "/rest/workspaces/ALA/datastores/" + name + "/external.shp",
null, "file://" + shp.getPath())
if (!"201".equals(result[0])) {
errors.put(String.valueOf(System.currentTimeMillis()), 'failed to upload shp to geoserver: ' + shp.getPath())
log.error 'Failed to upload shp to geoserver: ' + shp.getPath() + ". Check geoserver logs for details"
}
} else {
for (String filetype : ["shp", "prj", "shx", "dbf", "fix", "sbn", "sbx", "fbn", "fbx", "qix", "cpg", "shp.xml", "atx", "mxs", "ixs", "ain", "aih"]) {
// upload the file
File uploadFile = new File(shp.getPath().replace(".shp", "." + filetype))
if (uploadFile.exists()) {
String[] result = callGeoserver("PUT", "/rest/workspaces/ALA/datastores/" + name + "/file.shp",
uploadFile.getPath(), null, "application/octet-stream")
if (!"201".equals(result[0])) {
errors.put(String.valueOf(System.currentTimeMillis()), 'failed to upload file to geoserver: ' + uploadFile.getPath())
log.error 'Failed to load shp into co-located geoserver: ' + shp.getPath() + ". Check geoserver logs for details"
}
}
}
}
}
if (sld.exists()) {
//Create style
def out = UploadSpatialResource.sld(geoserverUrl + "/rest/styles/", geoserverUsername, geoserverPassword, name, sld.getPath())
if (!out.startsWith("200") && !out.startsWith("201")) {
errors.put(String.valueOf(System.currentTimeMillis()), out)
}
}
}
}
}
errors
}
}