-
Notifications
You must be signed in to change notification settings - Fork 17
/
SchemaRegistryBrowserController.java
220 lines (186 loc) · 9.29 KB
/
SchemaRegistryBrowserController.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
package at.esque.kafka;
import at.esque.kafka.alerts.ConfirmationAlert;
import at.esque.kafka.alerts.ErrorAlert;
import at.esque.kafka.cluster.ClusterConfig;
import at.esque.kafka.cluster.SslSocketFactoryCreator;
import at.esque.kafka.controls.FilterableListView;
import at.esque.kafka.controls.JsonTreeView;
import at.esque.kafka.controls.KafkaEsqueCodeArea;
import at.esque.kafka.dialogs.SubjectConfigDialog;
import at.esque.kafka.handlers.ConfigHandler;
import io.confluent.kafka.schemaregistry.client.rest.RestService;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;
import org.kordamp.ikonli.fontawesome.FontAwesome;
import org.kordamp.ikonli.javafx.FontIcon;
import io.confluent.kafka.schemaregistry.client.rest.entities.Schema;
import javax.net.ssl.SSLSocketFactory;
import java.util.Collections;
public class SchemaRegistryBrowserController {
private RestService schemaRegistryRestService;
@FXML
private FilterableListView<String> subjectListView;
@FXML
private KafkaEsqueCodeArea schemaTextArea;
@FXML
private ComboBox<Integer> versionComboBox;
@FXML
private JsonTreeView jsonTreeView;
@FXML
private Label schemaIdLabel;
public void setup(ClusterConfig selectedConfig, ConfigHandler configHandler) {
schemaRegistryRestService = new RestService(selectedConfig.getSchemaRegistry());
if (selectedConfig.isSchemaRegistrySuppressCertPathValidation() || selectedConfig.isSchemaRegistryUseSsl()) {
SSLSocketFactory sslSocketFactory = SslSocketFactoryCreator.buildSSlSocketFactory(selectedConfig, configHandler);
schemaRegistryRestService.setSslSocketFactory(sslSocketFactory);
}
schemaRegistryRestService.configure(configHandler.getSchemaRegistryAuthProperties(selectedConfig));
jsonTreeView.jsonStringProperty().bind(schemaTextArea.textProperty());
try {
versionComboBox.getSelectionModel().selectedItemProperty().addListener(((observable1, oldValue1, newValue1) -> {
if (newValue1 == null) {
schemaTextArea.setText("");
return;
}
try {
Schema schema = schemaRegistryRestService.getVersion(subjectListView.getListView().getSelectionModel().getSelectedItem(), newValue1);
schemaIdLabel.setText(String.valueOf(schema.getId()));
schemaTextArea.setText(JsonUtils.formatJson(schema.getSchema()));
} catch (Exception e) {
ErrorAlert.show(e, getWindow());
}
}));
subjectListView.getListView().setCellFactory(param -> subjectListCellFactory());
subjectListView.setListComparator(String::compareTo);
subjectListView.getListView().getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
try {
if (newValue != null) {
versionComboBox.setItems(FXCollections.observableArrayList(schemaRegistryRestService.getAllVersions(newValue)));
} else {
versionComboBox.setItems(FXCollections.emptyObservableList());
}
if (versionComboBox.getItems().size() > 0) {
versionComboBox.getSelectionModel().select(versionComboBox.getItems().size() - 1);
}
} catch (Exception e) {
ErrorAlert.show(e);
}
});
subjectListView.setItems(FXCollections.observableArrayList(schemaRegistryRestService.getAllSubjects()));
} catch (Exception e) {
ErrorAlert.show(e);
}
}
public void deleteSchema() {
String subject = subjectListView.getListView().getSelectionModel().getSelectedItem();
Integer version = versionComboBox.getSelectionModel().getSelectedItem();
if (subject != null && version != null && ConfirmationAlert.show("Delete Version", "Version [" + version + "] of subject [" + subject + "] will be deleted.", "Are you sure you want to delete this version?", getWindow())) {
try {
schemaRegistryRestService.deleteSchemaVersion(Collections.emptyMap(), subject, "" + version);
subjectListView.setItems(FXCollections.observableArrayList(schemaRegistryRestService.getAllSubjects()));
} catch (Exception e) {
ErrorAlert.show(e, getWindow());
}
}
}
public void deleteSubject() {
String subject = subjectListView.getListView().getSelectionModel().getSelectedItem();
if (subject != null && ConfirmationAlert.show("Delete Subject", "Subject [" + subject + "] will be deleted.", "Are you sure you want to delete this subject?", getWindow())) {
try {
schemaRegistryRestService.deleteSubject(Collections.emptyMap(), subject);
subjectListView.setItems(FXCollections.observableArrayList(schemaRegistryRestService.getAllSubjects()));
} catch (Exception e) {
ErrorAlert.show(e, getWindow());
}
}
}
private ListCell<String> subjectListCellFactory() {
ListCell<String> cell = new ListCell<>();
ContextMenu contextMenu = new ContextMenu();
MenuItem deleteItem = new MenuItem();
deleteItem.setGraphic(new FontIcon(FontAwesome.TRASH));
deleteItem.textProperty().set("delete");
deleteItem.setOnAction(event -> {
deleteSubject();
});
MenuItem configItem = new MenuItem();
configItem.setGraphic(new FontIcon(FontAwesome.COG));
configItem.textProperty().set("Configure Compatibility Level");
configItem.setOnAction(event -> {
SubjectConfigDialog.show(schemaRegistryRestService, subjectListView.getListView().getSelectionModel().getSelectedItem());
});
contextMenu.getItems().add(configItem);
contextMenu.getItems().add(deleteItem);
cell.textProperty().bind(cell.itemProperty());
cell.emptyProperty().addListener((obs, wasEmpty, isNowEmpty) -> {
if (isNowEmpty) {
cell.setContextMenu(null);
} else {
cell.setContextMenu(contextMenu);
}
});
return cell;
}
public void addSubjectAndSchema(ActionEvent actionEvent) {
showCreateSchemaDialog(null);
}
public void addSchemaClick(ActionEvent actionEvent) {
showCreateSchemaDialog(subjectListView.getListView().getSelectionModel().getSelectedItem());
}
public void checkSchemaClick(ActionEvent actionEvent) {
showSchemaCompatibilityCheckDialog(subjectListView.getListView().getSelectionModel().getSelectedItem(), versionComboBox.getSelectionModel().getSelectedItem());
}
private void showCreateSchemaDialog(String selectedSubject) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/createSchema.fxml"));
Parent root1 = fxmlLoader.load();
CreateSchemaController controller = fxmlLoader.getController();
Stage stage = new Stage();
controller.setup(selectedSubject, schemaRegistryRestService, stage);
stage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/kafkaesque.png")));
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Add Schema");
stage.setScene(Main.createStyledScene(root1, -1, -1));
stage.setOnCloseRequest(event -> controller.cleanup());
stage.show();
} catch (Exception e) {
ErrorAlert.show(e, getWindow());
}
}
private void showSchemaCompatibilityCheckDialog(String selectedSubject, Integer selectedVersion) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/compatibilityCheckSchema.fxml"));
Parent root1 = fxmlLoader.load();
SchemaCompatibilityCheckController controller = fxmlLoader.getController();
Stage stage = new Stage();
controller.setup(selectedSubject, selectedVersion.toString(), schemaRegistryRestService, stage);
stage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/kafkaesque.png")));
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Check Schema Compatibility");
stage.setScene(Main.createStyledScene(root1, -1, -1));
stage.setOnCloseRequest(event -> controller.cleanup());
stage.show();
} catch (Exception e) {
ErrorAlert.show(e, getWindow());
}
}
public void refreshSubjects(ActionEvent actionEvent) {
try {
subjectListView.getListView().getSelectionModel().select(null);
subjectListView.setItems(FXCollections.observableArrayList(schemaRegistryRestService.getAllSubjects()));
} catch (Exception e) {
ErrorAlert.show(e, getWindow());
}
}
private Window getWindow() {
return schemaTextArea.getScene().getWindow();
}
}