Skip to content

Commit d1d6bec

Browse files
author
Robin Duda
committed
support locking the target index in the web ui.
1 parent ab24665 commit d1d6bec

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

configuration.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"elastic_host": "localhost",
55
"elastic_tls": false,
66
"default_index": "excelastic",
7+
"index_lock": false,
78
"authentication": false,
89
"basic": "username:password"
910
}

src/main/java/com/codingchili/Controller/Website.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.vertx.core.eventbus.DeliveryOptions;
77
import io.vertx.core.eventbus.MessageConsumer;
88
import io.vertx.core.http.HttpServer;
9+
import io.vertx.core.http.HttpServerRequest;
910
import io.vertx.core.json.JsonObject;
1011
import io.vertx.ext.web.*;
1112
import io.vertx.ext.web.handler.*;
@@ -62,6 +63,7 @@ public void start(Future<Void> start) {
6263
context.put("connected", ElasticWriter.isConnected());
6364
context.put("tls", Configuration.isElasticTLS());
6465
context.put("index", Configuration.getDefaultIndex());
66+
context.put("indexLocked", Configuration.isIndexLocked());
6567
context.put("supportedFiles", String.join(", ", ParserFactory.getSupportedExtensions()));
6668
context.next();
6769
});
@@ -147,7 +149,9 @@ private Future<Integer> onComplete(RoutingContext context, String fileName) {
147149
// when the file has been read from disk, parsed and imported.
148150
return Future.<Integer>future().setHandler(result -> {
149151
if (result.succeeded()) {
150-
String index = context.request().params().get(INDEX);
152+
String index = getIndexFromRequest(context.request());
153+
154+
151155
logger.info(String.format("Imported file '%s' successfully into '%s'.", fileName, index));
152156

153157
context.put(INDEX, index);
@@ -163,14 +167,20 @@ private Future<Integer> onComplete(RoutingContext context, String fileName) {
163167
});
164168
}
165169

170+
private String getIndexFromRequest(HttpServerRequest request) {
171+
// only read the index from the request if the target index is not locked.
172+
return (Configuration.isIndexLocked()) ?
173+
Configuration.getDefaultIndex() : request.params().get(INDEX);
174+
}
175+
166176
/**
167177
* Parses a file upload request, converting the excel payload into json and waits
168178
* for elasticsearch to complete indexing.
169179
*
170180
* @param uploadedFileName the actual file on disk that contains the uploaded file.
171-
* @param params upload parameters
172-
* @param fileName the name of the uploaded file
173-
* @param future callback on completed parse + indexing.
181+
* @param params upload parameters
182+
* @param fileName the name of the uploaded file
183+
* @param future callback on completed parse + indexing.
174184
*/
175185
private void parse(String uploadedFileName, MultiMap params, String fileName, Future<Integer> future) {
176186
vertx.executeBlocking(blocking -> {

src/main/java/com/codingchili/Model/Configuration.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class Configuration {
2626
private static boolean SECURITY;
2727
private static String BASIC_AUTH;
2828
private static boolean ELASTIC_TLS;
29+
private static boolean INDEX_LOCK;
2930

3031
static {
3132
JsonObject configuration = getConfiguration();
@@ -36,6 +37,7 @@ public class Configuration {
3637
BASIC_AUTH = configuration.getString("basic", "username:password");
3738
ELASTIC_TLS = configuration.getBoolean("elastic_tls", false);
3839
DEFAULT_INDEX = configuration.getString("default_index", generateDefaultIndex());
40+
INDEX_LOCK = configuration.getBoolean("index_lock", false);
3941
}
4042

4143
private static JsonObject getConfiguration() {
@@ -118,7 +120,14 @@ public static String getElasticURL() {
118120
/**
119121
* @return the default index to use for importing.
120122
*/
121-
public static Object getDefaultIndex() {
123+
public static String getDefaultIndex() {
122124
return DEFAULT_INDEX;
123125
}
126+
127+
/**
128+
* @return true if the user should not be allowed to specify the import index.
129+
*/
130+
public static boolean isIndexLocked() {
131+
return INDEX_LOCK;
132+
}
124133
}

src/main/resources/templates/index.jade

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ html(lang='en')
2727
.form-group
2828
label.col-lg-3.control-label(for='index') Index
2929
.col-lg-9
30-
input#index.form-control(type='text', name='index', placeholder='name of index', value='#{context.index}')
30+
input#index.form-control(type='text', name='index', placeholder='name of index', value='#{context.index}' disabled=context.indexLocked)
3131
.form-group
3232
label.col-lg-3.control-label(for='mapping') Mapping
3333
.col-lg-9

0 commit comments

Comments
 (0)