/
RestService.java
1737 lines (1602 loc) · 78.5 KB
/
RestService.java
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.web.rest;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.MDC;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.sql.ISqlTemplate;
import org.jumpmind.db.sql.Row;
import org.jumpmind.exception.IoException;
import org.jumpmind.symmetric.ISymmetricEngine;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.io.data.writer.StructureDataWriter.PayloadType;
import org.jumpmind.symmetric.model.BatchAck;
import org.jumpmind.symmetric.model.BatchAckResult;
import org.jumpmind.symmetric.model.IncomingBatch;
import org.jumpmind.symmetric.model.IncomingBatch.Status;
import org.jumpmind.symmetric.model.NetworkedNode;
import org.jumpmind.symmetric.model.NodeChannel;
import org.jumpmind.symmetric.model.NodeGroupLink;
import org.jumpmind.symmetric.model.NodeHost;
import org.jumpmind.symmetric.model.NodeSecurity;
import org.jumpmind.symmetric.model.OutgoingBatch;
import org.jumpmind.symmetric.model.OutgoingBatchSummary;
import org.jumpmind.symmetric.model.OutgoingBatchWithPayload;
import org.jumpmind.symmetric.model.ProcessInfo;
import org.jumpmind.symmetric.model.ProcessInfoKey;
import org.jumpmind.symmetric.model.ProcessInfoKey.ProcessType;
import org.jumpmind.symmetric.model.Trigger;
import org.jumpmind.symmetric.model.TriggerRouter;
import org.jumpmind.symmetric.service.IAcknowledgeService;
import org.jumpmind.symmetric.service.IConfigurationService;
import org.jumpmind.symmetric.service.IDataExtractorService;
import org.jumpmind.symmetric.service.IDataLoaderService;
import org.jumpmind.symmetric.service.IDataService;
import org.jumpmind.symmetric.service.INodeService;
import org.jumpmind.symmetric.service.IOutgoingBatchService;
import org.jumpmind.symmetric.service.IRegistrationService;
import org.jumpmind.symmetric.service.ITriggerRouterService;
import org.jumpmind.symmetric.statistic.IStatisticManager;
import org.jumpmind.symmetric.web.ServerSymmetricEngine;
import org.jumpmind.symmetric.web.SymmetricEngineHolder;
import org.jumpmind.symmetric.web.WebConstants;
import org.jumpmind.symmetric.web.rest.model.Batch;
import org.jumpmind.symmetric.web.rest.model.BatchAckResults;
import org.jumpmind.symmetric.web.rest.model.BatchResult;
import org.jumpmind.symmetric.web.rest.model.BatchResults;
import org.jumpmind.symmetric.web.rest.model.BatchSummaries;
import org.jumpmind.symmetric.web.rest.model.BatchSummary;
import org.jumpmind.symmetric.web.rest.model.ChannelStatus;
import org.jumpmind.symmetric.web.rest.model.Engine;
import org.jumpmind.symmetric.web.rest.model.EngineList;
import org.jumpmind.symmetric.web.rest.model.Heartbeat;
import org.jumpmind.symmetric.web.rest.model.Node;
import org.jumpmind.symmetric.web.rest.model.NodeList;
import org.jumpmind.symmetric.web.rest.model.NodeStatus;
import org.jumpmind.symmetric.web.rest.model.PullDataResults;
import org.jumpmind.symmetric.web.rest.model.QueryResults;
import org.jumpmind.symmetric.web.rest.model.RegistrationInfo;
import org.jumpmind.symmetric.web.rest.model.RestError;
import org.jumpmind.symmetric.web.rest.model.SendSchemaRequest;
import org.jumpmind.symmetric.web.rest.model.SendSchemaResponse;
import org.jumpmind.symmetric.web.rest.model.TableName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartFile;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
/**
* This is a REST API for SymmetricDS. The API will be active only if
* rest.api.enable=true. The property is turned off by default. The REST API is
* available at http://hostname:port/api for the stand alone SymmetricDS
* installation.
*
* <p>
* <b>General HTTP Responses to the methods:</b>
* <ul>
* <li>
* ALL Methods may return the following HTTP responses.<br>
* <br>
* In general:<br>
* <ul>
* <li>HTTP 2xx = Success</li>
* <li>HTTP 4xx = Problem on the caller (client) side</li>
* <li>HTTP 5xx - Problem on the REST service side</li>
* </ul>
* ALL Methods
* <ul>
* <li>HTTP 401 - Unauthorized. You have not successfully authenticated.
* Authentication details are in the response body.</li>
* <li>HTTP 404 - Not Found. You attempted to perform an operation on a resource
* that doesn't exist. I.E. you tried to start or stop an engine that doesn't
* exist.</li>
* <li>HTTP 405 - Method Not Allowed. I.E. you attempted a service call that
* uses the default engine (/engine/identity vs engine/{engine}/identity) and
* there was more than one engine found on the server.</li>
* <li>HTTP 500 - Internal Server Error. Something went wrong on the server /
* service, and we couldn't fulfill the request. Details are in the response
* body.</li>
* </ul>
* </li>
* <li>
* GET Methods
* <ul>
* <li>HTTP 200 - Success with result contained in the response body.</li>
* <li>HTTP 204 - Success with no results. Your GET request completed
* successfully, but found no matching entities.</li>
* </ul>
* </ul>
* </p>
*/
@Controller
public class RestService {
protected final Logger log = LoggerFactory.getLogger(getClass());
@Autowired
ServletContext context;
/**
* Provides a list of {@link Engine} that are configured on the node.
*
* @return {@link EngineList} - Engines configured on the node <br>
*
* <pre>
* Example xml reponse is as follows:<br><br>
* {@code
* <enginelist>
* <engines>
* <name>RootSugarDB-root</name>
* </engines>
* </enginelist>
* }
* <br>
* Example json response is as follows:<br><br>
* {"engines":[{"name":"RootSugarDB-root"}]}
* </pre>
*/
@ApiOperation(value = "Obtain a list of configured Engines")
@RequestMapping(value = "/enginelist", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final EngineList getEngineList() {
EngineList list = new EngineList();
Collection<ServerSymmetricEngine> engines = getSymmetricEngineHolder().getEngines()
.values();
for (ISymmetricEngine engine : engines) {
if (engine.getParameterService().is(ParameterConstants.REST_API_ENABLED)) {
list.addEngine(new Engine(engine.getEngineName()));
}
}
return list;
}
/**
* Provides Node information for the single engine
*
* return {@link Node}<br>
*
* <pre>
* Example xml reponse is as follows:<br><br>
* {@code
* <node>
* <batchInErrorCount>0</batchInErrorCount>
* <batchToSendCount>0</batchToSendCount>
* <externalId>server01</externalId>
* <initialLoaded>true</initialLoaded>
* <lastHeartbeat>2012-12-20T09:26:02-05:00</lastHeartbeat>
* <name>server01</name>
* <registered>true</registered>
* <registrationServer>true</registrationServer>
* <reverseInitialLoaded>false</reverseInitialLoaded>
* <syncUrl>http://machine-name:31415/sync/RootSugarDB-root</syncUrl>
* </node>
* }
* <br>
* Example json response is as follows:<br><br>
* {"name":"server01","externalId":"server01","registrationServer":true,"syncUrl":"http://machine-name:31415/sync/RootSugarDB-root","batchToSendCount":0,"batchInErrorCount":0,"lastHeartbeat":1356013562000,"registered":true,"initialLoaded":true,"reverseInitialLoaded":false}
* </pre>
*/
@ApiOperation(value = "Obtain node information for the single engine")
@RequestMapping(value = "engine/node", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final Node getNode() {
return nodeImpl(getSymmetricEngine());
}
/**
* Provides Node information for the specified engine
*/
@ApiOperation(value = "Obtain node information for he specified engine")
@RequestMapping(value = "engine/{engine}/node", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final Node getNode(@PathVariable("engine") String engineName) {
return nodeImpl(getSymmetricEngine(engineName));
}
/**
* Provides a list of children that are registered with this engine.
*
* return {@link Node}<br>
*
* <pre>
* Example xml reponse is as follows:<br><br>
* {@code
* <nodelist>
* <nodes>
* <batchInErrorCount>0</batchInErrorCount>
* <batchToSendCount>0</batchToSendCount>
* <externalId>client01</externalId>
* <initialLoaded>true</initialLoaded>
* <name>client01</name>
* <registered>true</registered>
* <registrationServer>false</registrationServer>
* <reverseInitialLoaded>false</reverseInitialLoaded>
* <syncUrl>http://machine-name:31418/sync/ClientSugarDB-client01</syncUrl>
* </nodes>
* </nodelist>
* }
* <br>
* Example json response is as follows:<br><br>
* {"nodes":[{"name":"client01","externalId":"client01","registrationServer":false,"syncUrl":"http://gwilmer-laptop:31418/sync/ClientSugarDB-client01","batchToSendCount":0,"batchInErrorCount":0,"lastHeartbeat":null,"registered":true,"initialLoaded":true,"reverseInitialLoaded":false}]}
* </pre>
*/
@ApiOperation(value = "Obtain list of children for the single engine")
@RequestMapping(value = "engine/children", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final NodeList getChildren() {
return childrenImpl(getSymmetricEngine());
}
/**
* Provides a list of children {@link Node} that are registered with this
* engine.
*/
@ApiOperation(value = "Obtain list of children for the specified engine")
@RequestMapping(value = "engine/{engine}/children", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final NodeList getChildrenByEngine(@PathVariable("engine") String engineName) {
return childrenImpl(getSymmetricEngine(engineName));
}
/**
* Takes a snapshot for this engine and streams it to the client. The result
* of this call is a stream that should be written to a zip file. The zip
* contains configuration and operational information about the installation
* and can be used to diagnose state of the node
*/
@ApiOperation(value = "Take a diagnostic snapshot for the single engine")
@RequestMapping(value = "engine/snapshot", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final void getSnapshot(HttpServletResponse resp) {
getSnapshot(getSymmetricEngine().getEngineName(), resp);
}
/**
* Executes a select statement on the node and returns results. <br>
* Example json response is as follows:<br>
* <br>
* {"nbrResults":1,"results":[{"rowNum":1,"columnData":[{"ordinal":1,"name":
* "node_id","value":"root"}]}]}
*
*/
@ApiOperation(value = "Execute the specified SQL statement on the single engine")
@RequestMapping(value = "engine/querynode", method = {RequestMethod.GET, RequestMethod.POST})
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final QueryResults getQueryNode(@RequestParam(value = "query") String sql) {
return queryNodeImpl(getSymmetricEngine(), sql);
}
/**
* Executes a select statement on the node and returns results.
*/
@ApiOperation(value = "Execute the specified SQL statement for the specified engine")
@RequestMapping(value = "engine/{engine}/querynode", method = {RequestMethod.GET, RequestMethod.POST})
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final QueryResults getQueryNode(@PathVariable("engine") String engineName,
@RequestParam(value = "query") String sql) {
return queryNodeImpl(getSymmetricEngine(engineName), sql);
}
/**
* Takes a snapshot for the specified engine and streams it to the client.
*/
@ApiOperation(value = "Take a diagnostic snapshot for the specified engine")
@RequestMapping(value = "engine/{engine}/snapshot", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final void getSnapshot(@PathVariable("engine") String engineName,
HttpServletResponse resp) {
BufferedInputStream bis = null;
try {
ISymmetricEngine engine = getSymmetricEngine(engineName);
File file = engine.snapshot();
resp.setHeader("Content-Disposition",
String.format("attachment; filename=%s", file.getName()));
bis = new BufferedInputStream(new FileInputStream(file));
IOUtils.copy(bis, resp.getOutputStream());
} catch (IOException e) {
throw new IoException(e);
} finally {
IOUtils.closeQuietly(bis);
}
}
/**
* Loads a configuration profile for the single engine on the node.
*
* @param file
* A file stream that contains the profile itself.
*/
@ApiOperation(value = "Load a configuration file to the single engine")
@RequestMapping(value = "engine/profile", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postProfile(@RequestParam MultipartFile file) {
loadProfileImpl(getSymmetricEngine(), file);
}
/**
* Loads a configuration profile for the specified engine on the node.
*
* @param file
* A file stream that contains the profile itself.
*/
@ApiOperation(value = "Load a configuration file to the specified engine")
@RequestMapping(value = "engine/{engine}/profile", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postProfileByEngine(@PathVariable("engine") String engineName,
@RequestParam(value = "file") MultipartFile file) {
loadProfileImpl(getSymmetricEngine(engineName), file);
}
/**
* Starts the single engine on the node
*/
@ApiOperation(value = "Start the single engine")
@RequestMapping(value = "engine/start", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postStart() {
startImpl(getSymmetricEngine());
}
/**
* Starts the specified engine on the node
*/
@ApiOperation(value = "Start the specified engine")
@RequestMapping(value = "engine/{engine}/start", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postStartByEngine(@PathVariable("engine") String engineName) {
startImpl(getSymmetricEngine(engineName));
}
/**
* Stops the single engine on the node
*/
@ApiOperation(value = "Stop the single engine")
@RequestMapping(value = "engine/stop", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postStop() {
stopImpl(getSymmetricEngine());
}
/**
* Stops the specified engine on the node
*/
@ApiOperation(value = "Stop the specified engine")
@RequestMapping(value = "engine/{engine}/stop", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postStopByEngine(@PathVariable("engine") String engineName) {
stopImpl(getSymmetricEngine(engineName));
}
/**
* Creates instances of triggers for each entry configured table/trigger for
* the single engine on the node
*/
@ApiOperation(value = "Sync triggers on the single engine")
@RequestMapping(value = "engine/synctriggers", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postSyncTriggers(
@RequestParam(required = false, value = "force") boolean force) {
syncTriggersImpl(getSymmetricEngine(), force);
}
/**
* Creates instances of triggers for each entry configured table/trigger for
* the specified engine on the node
*/
@ApiOperation(value = "Sync triggers on the specified engine")
@RequestMapping(value = "engine/{engine}/synctriggers", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postSyncTriggersByEngine(@PathVariable("engine") String engineName,
@RequestParam(required = false, value = "force") boolean force) {
syncTriggersImpl(getSymmetricEngine(engineName), force);
}
@ApiOperation(value = "Sync triggers on the single engine for a table")
@RequestMapping(value = "engine/synctriggers/{table}", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postSyncTriggersByTable(@PathVariable("table") String tableName,
@RequestParam(required = false, value = "catalog") String catalogName,
@RequestParam(required = false, value = "schema") String schemaName,
@RequestParam(required = false, value = "force") boolean force) {
syncTriggersByTableImpl(getSymmetricEngine(), catalogName, schemaName, tableName, force);
}
@ApiOperation(value = "Sync triggers on the specific engine for a table")
@RequestMapping(value = "engine/{engine}/synctriggers/{table}", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postSyncTriggersByTable(@PathVariable("engine") String engineName,
@PathVariable("table") String tableName,
@RequestParam(required = false, value = "catalog") String catalogName,
@RequestParam(required = false, value = "schema") String schemaName,
@RequestParam(required = false, value = "force") boolean force) {
syncTriggersByTableImpl(getSymmetricEngine(engineName), catalogName, schemaName, tableName,
force);
}
/**
* Send schema updates for all tables or a list of tables to a list of nodes
* or to all nodes in a group.
* <p>
* Example json request to send all tables to all nodes in group:<br>
* { "nodeGroupIdToSendTo": "target_group_name" }
* <p>
* Example json request to send all tables to a list of nodes:<br>
* { "nodeIdsToSendTo": [ "1", "2" ] }
* <p>
* Example json request to send a table to a list of nodes:<br>
* { "nodeIdsToSendTo": ["1", "2"], "tablesToSend": [ { "catalogName": "", "schemaName": "", "tableName": "A" } ] }
* <p>
* Example json response:
* { "nodeIdsSentTo": { "1": [ { "catalogName": null, "schemaName": null, "tableName": "A" } ] } }
*
* @param engineName
* @param request
* @return {@link SendSchemaResponse}
*/
@ApiOperation(value = "Send schema updates for all tables or a list of tables to a list of nodes or to all nodes in a group.")
@RequestMapping(value = "engine/{engine}/sendschema", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final SendSchemaResponse postSendSchema(@PathVariable("engine") String engineName,
@RequestBody SendSchemaRequest request) {
return sendSchemaImpl(getSymmetricEngine(engineName), request);
}
/**
* Send schema updates for all tables or a list of tables to a list of nodes
* or to all nodes in a group. See
* {@link RestService#postSendSchema(String, SendSchemaRequest)} for
* additional details.
*
* @param request
* @return {@link SendSchemaResponse}
*/
@ApiOperation(value = "Send schema updates for all tables or a list of tables to a list of nodes or to all nodes in a group.")
@RequestMapping(value = "engine/sendschema", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final SendSchemaResponse postSendSchema(@RequestBody SendSchemaRequest request) {
return sendSchemaImpl(getSymmetricEngine(), request);
}
/**
* Removes instances of triggers for each entry configured table/trigger for
* the single engine on the node
*/
@ApiOperation(value = "Drop triggers on the single engine")
@RequestMapping(value = "engine/droptriggers", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postDropTriggers() {
dropTriggersImpl(getSymmetricEngine());
}
/**
* Removes instances of triggers for each entry configured table/trigger for
* the specified engine on the node
*/
@ApiOperation(value = "Drop triggers on the specified engine")
@RequestMapping(value = "engine/{engine}/droptriggers", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postDropTriggersByEngine(@PathVariable("engine") String engineName) {
dropTriggersImpl(getSymmetricEngine(engineName));
}
/**
* Removes instances of triggers for the specified table for the single
* engine on the node
*/
@ApiOperation(value = "Drop triggers for the specified table on the single engine")
@RequestMapping(value = "engine/table/{table}/droptriggers", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postDropTriggersByTable(@PathVariable("table") String tableName) {
dropTriggersImpl(getSymmetricEngine(), tableName);
}
/**
* Removes instances of triggers for the specified table for the single
* engine on the node
*
*/
@ApiOperation(value = "Drop triggers for the specified table on the specified engine")
@RequestMapping(value = "engine/{engine}/table/{table}/droptriggers", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postDropTriggersByEngineByTable(@PathVariable("engine") String engineName,
@PathVariable("table") String tableName) {
dropTriggersImpl(getSymmetricEngine(engineName), tableName);
}
/**
* Installs and starts a new node
*
* @param file
* A file stream that contains the node's properties.
*/
@ApiOperation(value = "Load a configuration file to the single engine")
@RequestMapping(value = "engine/install", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postInstall(@RequestParam MultipartFile file) {
try {
Properties properties = new Properties();
properties.load(file.getInputStream());
getSymmetricEngineHolder().install(properties);
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
/**
* Uninstalls all SymmetricDS objects from the given node (database) for the
* single engine on the node
*/
@ApiOperation(value = "Uninstall SymmetricDS on the single engine")
@RequestMapping(value = "engine/uninstall", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postUninstall() {
uninstallImpl(getSymmetricEngine());
}
/**
* Uninstalls all SymmetricDS objects from the given node (database) for the
* specified engine on the node
*
*/
@ApiOperation(value = "Uninstall SymmetricDS on the specified engine")
@RequestMapping(value = "engine/{engine}/uninstall", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postUninstallByEngine(@PathVariable("engine") String engineName) {
uninstallImpl(getSymmetricEngine(engineName));
}
/**
* Reinitializes the given node (database) for the single engine on the node
*/
@ApiOperation(value = "Reinitiailize SymmetricDS on the single engine")
@RequestMapping(value = "engine/reinitialize", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postReinitialize() {
reinitializeImpl(getSymmetricEngine());
}
/**
* Reinitializes the given node (database) for the specified engine on the
* node
*
*/
@ApiOperation(value = "Reinitiailize SymmetricDS on the specified engine")
@RequestMapping(value = "engine/{engine}/reinitialize", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postReinitializeByEngine(@PathVariable("engine") String engineName) {
reinitializeImpl(getSymmetricEngine(engineName));
}
/**
* Refreshes cache for the single engine on the node
*/
@ApiOperation(value = "Refresh caches on the single engine")
@RequestMapping(value = "engine/refreshcache", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postClearCaches() {
clearCacheImpl(getSymmetricEngine());
}
/**
* Refreshes cache for the specified engine on the node node
*
*/
@ApiOperation(value = "Refresh caches on the specified engine")
@RequestMapping(value = "engine/{engine}/refreshcache", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postClearCachesByEngine(@PathVariable("engine") String engineName) {
clearCacheImpl(getSymmetricEngine(engineName));
}
/**
* Returns an overall status for the single engine of the node.
*
* @return {@link NodeStatus}
*
* <pre>
* Example xml reponse is as follows:<br><br>
* {@code
* <nodestatus>
* <batchInErrorCount>0</batchInErrorCount>
* <batchToSendCount>0</batchToSendCount>
* <databaseType>Microsoft SQL Server</databaseType>
* <databaseVersion>9.0</databaseVersion>
* <deploymentType>professional</deploymentType>
* <externalId>root</externalId>
* <initialLoaded>true</initialLoaded>
* <lastHeartbeat>2012-11-17 14:52:19.267</lastHeartbeat>
* <nodeGroupId>RootSugarDB</nodeGroupId>
* <nodeId>root</nodeId>
* <registered>true</registered>
* <registrationServer>false</registrationServer>
* <started>true</started>
* <symmetricVersion>3.1.10</symmetricVersion>
* <syncEnabled>true</syncEnabled>
* <syncUrl>http://my-machine-name:31415/sync/RootSugarDB-root</syncUrl>
* </nodestatus>
* }
* <br>
* Example json response is as follows:<br><br>
* {"started":true,"registered":true,"registrationServer":false,"initialLoaded":true,
* "nodeId":"root","nodeGroupId":"RootSugarDB","externalId":"root",
* "syncUrl":"http://my-machine-name:31415/sync/RootSugarDB-root","databaseType":"Microsoft SQL Server",
* "databaseVersion":"9.0","syncEnabled":true,"createdAtNodeId":null,"batchToSendCount":0,
* "batchInErrorCount":0,"deploymentType":"professional","symmetricVersion":"3.1.10",
* "lastHeartbeat":"2012-11-17 15:15:00.033","hearbeatInterval":null}
* </pre>
*/
@ApiOperation(value = "Obtain the status of the single engine")
@RequestMapping(value = "/engine/status", method = RequestMethod.GET)
@ResponseBody
public final NodeStatus getStatus() {
return nodeStatusImpl(getSymmetricEngine());
}
/**
* Returns an overall status for the specified engine of the node.
*
* @return {@link NodeStatus}
*/
@ApiOperation(value = "Obtain the status of the specified engine")
@RequestMapping(value = "/engine/{engine}/status", method = RequestMethod.GET)
@ResponseBody
public final NodeStatus getStatusByEngine(@PathVariable("engine") String engineName) {
return nodeStatusImpl(getSymmetricEngine(engineName));
}
/**
* Returns status of each channel for the single engine of the node.
*
* @return Set<{@link ChannelStatus}>
*/
@ApiOperation(value = "Obtain the channel status of the single engine")
@RequestMapping(value = "/engine/channelstatus", method = RequestMethod.GET)
@ResponseBody
public final Set<ChannelStatus> getChannelStatus() {
return channelStatusImpl(getSymmetricEngine());
}
/**
* Returns status of each channel for the specified engine of the node.
*
* @return Set<{@link ChannelStatus}>
*/
@ApiOperation(value = "Obtain the channel status of the specified engine")
@RequestMapping(value = "/engine/{engine}/channelstatus", method = RequestMethod.GET)
@ResponseBody
public final Set<ChannelStatus> getChannelStatusByEngine(
@PathVariable("engine") String engineName) {
return channelStatusImpl(getSymmetricEngine(engineName));
}
/**
* Removes (unregisters and cleans up) a node for the single engine
*/
@ApiOperation(value = "Remove specified node (unregister and clean up) for the single engine")
@RequestMapping(value = "/engine/removenode", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postRemoveNode(@RequestParam(value = "nodeId") String nodeId) {
postRemoveNodeByEngine(nodeId, getSymmetricEngine().getEngineName());
}
/**
* Removes (unregisters and cleans up) a node for the single engine
*/
@ApiOperation(value = "Remove specified node (unregister and clean up) for the specified engine")
@RequestMapping(value = "/engine/{engine}/removenode", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void postRemoveNodeByEngine(@RequestParam(value = "nodeId") String nodeId,
@PathVariable("engine") String engineName) {
getSymmetricEngine(engineName).removeAndCleanupNode(nodeId);
}
/**
* Requests the server to add this node to the synchronization scenario as a
* "pull only" node
*
* @param externalId
* The external id for this node
* @param nodeGroup
* The node group to which this node belongs
* @param databaseType
* The database type for this node
* @param databaseVersion
* The database version for this node
* @param hostName
* The host name of the machine on which the client is running
* @return {@link RegistrationInfo}
*
* <pre>
* Example json response is as follows:<br/><br/>
* {"registered":false,"nodeId":null,"syncUrl":null,"nodePassword":null}<br>
* In the above example, the node attempted to register, but was not able to successfully register
* because registration was not open on the server. Checking the "registered" element will allow you
* to determine whether the node was successfully registered.<br/><br/>
* The following example shows the results from the registration after registration has been opened
* on the server for the given node.<br/><br/>
* {"registered":true,"nodeId":"001","syncUrl":"http://myserverhost:31415/sync/server-000","nodePassword":"1880fbffd2bc2d00e1d58bd0c734ff"}<br/>
* The nodeId, syncUrl and nodePassword should be stored for subsequent calls to the REST API.
* </pre>
*/
@ApiOperation(value = "Register the specified node for the single engine")
@RequestMapping(value = "/engine/registernode", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final RegistrationInfo postRegisterNode(
@RequestParam(value = "externalId") String externalId,
@RequestParam(value = "nodeGroupId") String nodeGroupId,
@RequestParam(value = "databaseType") String databaseType,
@RequestParam(value = "databaseVersion") String databaseVersion,
@RequestParam(value = "hostName") String hostName) {
return postRegisterNode(getSymmetricEngine().getEngineName(), externalId, nodeGroupId,
databaseType, databaseVersion, hostName);
}
@ApiOperation(value = "Register the specified node for the specified engine")
@RequestMapping(value = "/engine/{engine}/registernode", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final RegistrationInfo postRegisterNode(@PathVariable("engine") String engineName,
@RequestParam(value = "externalId") String externalId,
@RequestParam(value = "nodeGroupId") String nodeGroupId,
@RequestParam(value = "databaseType") String databaseType,
@RequestParam(value = "databaseVersion") String databaseVersion,
@RequestParam(value = "hostName") String hostName) {
ISymmetricEngine engine = getSymmetricEngine(engineName);
IRegistrationService registrationService = engine.getRegistrationService();
INodeService nodeService = engine.getNodeService();
RegistrationInfo regInfo = new org.jumpmind.symmetric.web.rest.model.RegistrationInfo();
try {
org.jumpmind.symmetric.model.Node processedNode = registrationService
.registerPullOnlyNode(externalId, nodeGroupId, databaseType, databaseVersion);
regInfo.setRegistered(processedNode.isSyncEnabled());
if (regInfo.isRegistered()) {
regInfo.setNodeId(processedNode.getNodeId());
NodeSecurity nodeSecurity = nodeService.findNodeSecurity(processedNode.getNodeId());
regInfo.setNodePassword(nodeSecurity.getNodePassword());
org.jumpmind.symmetric.model.Node modelNode = nodeService.findIdentity();
regInfo.setSyncUrl(modelNode.getSyncUrl());
// do an initial heartbeat
Heartbeat heartbeat = new Heartbeat();
heartbeat.setNodeId(regInfo.getNodeId());
heartbeat.setHostName(hostName);
Date now = new Date();
heartbeat.setCreateTime(now);
heartbeat.setLastRestartTime(now);
heartbeat.setHeartbeatTime(now);
this.heartbeatImpl(engine, heartbeat);
}
// TODO: Catch a RegistrationRedirectException and redirect.
} catch (IOException e) {
throw new IoException(e);
}
return regInfo;
}
/**
* Pulls pending batches (data) for a given node.
*
* @param nodeId
* The node id of the node requesting to pull data
* @param securityToken
* The security token or password used to authenticate the pull.
* The security token is provided during the registration
* process.
* @param useJdbcTimestampFormat
* @param useUpsertStatements
* @param useDelimitedIdentifiers
* @param hostName
* The name of the host machine requesting the pull. Only
* required if you have the rest heartbeat on pull paramter set.
* @return {@link PullDataResults}
*
* Example json response is as follows:<br/>
* <br/>
* {"nbrBatches":2,"batches":[{"batchId":20,"sqlStatements":[
* "insert into table1 (field1, field2) values (value1,value2);"
* ,"update table1 set field1=value1;"
* ]},{"batchId":21,"sqlStatements"
* :["insert into table2 (field1, field2) values (value1,value2);"
* ,"update table2 set field1=value1;"]}]}<BR>
* <br/>
* If there are no batches to be pulled, the json response will look
* as follows:<br/>
* <br/>
* {"nbrBatches":0,"batches":[]} </pre>
*/
@ApiOperation(value = "Pull pending batches for the specified node for the single engine")
@RequestMapping(value = "/engine/pulldata", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final PullDataResults getPullData(
@RequestParam(value = WebConstants.NODE_ID) String nodeId,
@ApiParam(value="This the password for the nodeId being passed in. The password is stored in the node_security table")
@RequestParam(value = WebConstants.SECURITY_TOKEN) String securityToken,
@RequestParam(value = "useJdbcTimestampFormat", required = false, defaultValue = "true") boolean useJdbcTimestampFormat,
@RequestParam(value = "useUpsertStatements", required = false, defaultValue = "false") boolean useUpsertStatements,
@RequestParam(value = "useDelimitedIdentifiers", required = false, defaultValue = "true") boolean useDelimitedIdentifiers,
@RequestParam(value = "hostName", required = false) String hostName) {
return getPullData(getSymmetricEngine().getEngineName(), nodeId, securityToken,
useJdbcTimestampFormat, useUpsertStatements, useDelimitedIdentifiers, hostName);
}
@ApiOperation(value = "Pull pending batches for the specified node for the specified engine")
@RequestMapping(value = "/engine/{engine}/pulldata", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final PullDataResults getPullData(
@PathVariable("engine") String engineName,
@RequestParam(value = WebConstants.NODE_ID) String nodeId,
@ApiParam(value="This the password for the nodeId being passed in. The password is stored in the node_security table.")
@RequestParam(value = WebConstants.SECURITY_TOKEN) String securityToken,
@RequestParam(value = "useJdbcTimestampFormat", required = false, defaultValue = "true") boolean useJdbcTimestampFormat,
@RequestParam(value = "useUpsertStatements", required = false, defaultValue = "false") boolean useUpsertStatements,
@RequestParam(value = "useDelimitedIdentifiers", required = false, defaultValue = "true") boolean useDelimitedIdentifiers,
@RequestParam(value = "hostName", required = false) String hostName) {
ISymmetricEngine engine = getSymmetricEngine(engineName);
IDataExtractorService dataExtractorService = engine.getDataExtractorService();
IStatisticManager statisticManager = engine.getStatisticManager();
INodeService nodeService = engine.getNodeService();
org.jumpmind.symmetric.model.Node targetNode = nodeService.findNode(nodeId);
if (securityVerified(nodeId, engine, securityToken)) {
ProcessInfo processInfo = statisticManager.newProcessInfo(new ProcessInfoKey(
nodeService.findIdentityNodeId(), nodeId, ProcessType.REST_PULL_HANLDER));
try {
PullDataResults results = new PullDataResults();
List<OutgoingBatchWithPayload> extractedBatches = dataExtractorService
.extractToPayload(processInfo, targetNode, PayloadType.SQL,
useJdbcTimestampFormat, useUpsertStatements,
useDelimitedIdentifiers);
List<Batch> batches = new ArrayList<Batch>();
for (OutgoingBatchWithPayload outgoingBatchWithPayload : extractedBatches) {
if (outgoingBatchWithPayload.getStatus() == org.jumpmind.symmetric.model.OutgoingBatch.Status.LD
|| outgoingBatchWithPayload.getStatus() == org.jumpmind.symmetric.model.OutgoingBatch.Status.IG) {
Batch batch = new Batch();
batch.setBatchId(outgoingBatchWithPayload.getBatchId());
batch.setChannelId(outgoingBatchWithPayload.getChannelId());
batch.setSqlStatements(outgoingBatchWithPayload.getPayload());
batches.add(batch);
}
}
results.setBatches(batches);
results.setNbrBatches(batches.size());
processInfo.setStatus(org.jumpmind.symmetric.model.ProcessInfo.Status.OK);
if (engine.getParameterService().is(ParameterConstants.REST_HEARTBEAT_ON_PULL)
&& hostName != null) {
Heartbeat heartbeat = new Heartbeat();
heartbeat.setNodeId(nodeId);
heartbeat.setHeartbeatTime(new Date());
heartbeat.setHostName(hostName);
this.heartbeatImpl(engine, heartbeat);
}
return results;
} finally {
if (processInfo.getStatus() != org.jumpmind.symmetric.model.ProcessInfo.Status.OK) {
processInfo.setStatus(org.jumpmind.symmetric.model.ProcessInfo.Status.ERROR);
}
}
} else {
throw new NotAllowedException();
}
}
/**
* Sends a heartbeat to the server for the given node.
*
* @param nodeID
* - Required - The client nodeId this to which this heartbeat
* belongs See {@link Heartbeat} for request body requirements
*/
@ApiOperation(value = "Send a heartbeat for the single engine")
@RequestMapping(value = "/engine/heartbeat", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public final void putHeartbeat(
@ApiParam(value="This the password for the nodeId being passed in. The password is stored in the node_security table.")
@RequestParam(value = WebConstants.SECURITY_TOKEN) String securityToken,
@RequestBody Heartbeat heartbeat) {
if (securityVerified(heartbeat.getNodeId(), getSymmetricEngine(), securityToken)) {
putHeartbeat(getSymmetricEngine().getEngineName(), securityToken, heartbeat);