Skip to content
Snippets Groups Projects
Commit 21296106 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

ES: Add Product index

ES: Add category index
ES plugin: start authentication using challenge and token
parent fa82f44d
Branches
Tags
No related merge requests found
Showing
with 1133 additions and 53 deletions
Subproject commit 7fe6f85bd6150ce16c3a1d894836d6d757274175 Subproject commit 740da406a9b2f3c72f5c31bb7ad4a42c18e212c5
...@@ -20,34 +20,26 @@ ...@@ -20,34 +20,26 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- JNA -->
<dependency> <dependency>
<groupId>net.java.dev.jna</groupId> <groupId>io.ucoin</groupId>
<artifactId>jna</artifactId> <artifactId>ucoinj-elasticsearch</artifactId>
<scope>runtime</scope> <version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.xbib.elasticsearch.plugin</groupId>
<artifactId>elasticsearch-transport-websocket</artifactId>
<version>1.4.0.0</version>
</dependency> </dependency>
<!-- Unit tests --> <!-- LOGGING DEPENDENCIES - SLF4J -->
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId> <artifactId>slf4j-log4j12</artifactId>
<scope>test</scope> <optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<scope>test</scope> <optional>true</optional>
</dependency> </dependency>
</dependencies> </dependencies>
......
package io.ucoin.ucoinj.elasticsearch.action;
import org.elasticsearch.common.inject.AbstractModule;
public class ExampleRestModule extends AbstractModule {
@Override protected void configure() {
bind(HelloRestHandler.class).asEagerSingleton();
}
}
\ No newline at end of file
package io.ucoin.ucoinj.elasticsearch.action;
import io.ucoin.ucoinj.elasticsearch.action.currency.RestCurrencyIndexAction;
import io.ucoin.ucoinj.elasticsearch.action.product.RestProductIndexAction;
import io.ucoin.ucoinj.elasticsearch.action.security.RestSecurityAuthAction;
import io.ucoin.ucoinj.elasticsearch.action.security.RestSecurityGetChallengeAction;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Module;
public class RestModule extends AbstractModule implements Module {
@Override protected void configure() {
bind(RestCurrencyIndexAction.class).asEagerSingleton();
bind(RestProductIndexAction.class).asEagerSingleton();
bind(RestSecurityGetChallengeAction.class).asEagerSingleton();
bind(RestSecurityAuthAction.class).asEagerSingleton();
}
}
\ No newline at end of file
package io.ucoin.ucoinj.elasticsearch.action; package io.ucoin.ucoinj.elasticsearch.action.currency;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.*; import org.elasticsearch.rest.*;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.OK; import static org.elasticsearch.rest.RestStatus.OK;
public class HelloRestHandler extends BaseRestHandler { public class RestCurrencyIndexAction extends BaseRestHandler {
@Inject @Inject
public HelloRestHandler(Settings settings, RestController controller, Client client) { public RestCurrencyIndexAction(Settings settings, RestController controller, Client client) {
super(settings, controller, client); super(settings, controller, client);
controller.registerHandler(GET, "/_hello", this); controller.registerHandler(RestRequest.Method.POST, "/currency", this);
} }
@Override @Override
protected void handleRequest(RestRequest restRequest, RestChannel restChannel, Client client) throws Exception { protected void handleRequest(RestRequest restRequest, RestChannel restChannel, Client client) throws Exception {
String who = restRequest.param("who"); String json = restRequest.content().toUtf8();
String whoSafe = (who!=null) ? who : "world"; //ServiceLocator.instance().getCurrencyIndexerService().indexCurrency();
restChannel.sendResponse(new BytesRestResponse(OK, "Hello, " + whoSafe + "!")); String currencyName = "";
restChannel.sendResponse(new BytesRestResponse(OK, currencyName));
} }
} }
\ No newline at end of file
package io.ucoin.ucoinj.elasticsearch.action.product;
import io.ucoin.ucoinj.elasticsearch.service.ServiceLocator;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.*;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestStatus.OK;
public class RestProductIndexAction extends BaseRestHandler {
private static final ESLogger log = ESLoggerFactory.getLogger(RestProductIndexAction.class.getName());
@Inject
public RestProductIndexAction(Settings settings, RestController controller, Client client) {
super(settings, controller, client);
controller.registerHandler(POST, "/product", this);
}
@Override
protected void handleRequest(final RestRequest request, RestChannel restChannel, Client client) throws Exception {
String productId = ServiceLocator.instance().getProductIndexerService().indexProductFromJson(request.content().toUtf8());
restChannel.sendResponse(new BytesRestResponse(OK, productId));
}
}
\ No newline at end of file
package io.ucoin.ucoinj.elasticsearch.action.security;
import io.ucoin.ucoinj.core.client.model.bma.gson.GsonUtils;
import io.ucoin.ucoinj.core.util.StringUtils;
import io.ucoin.ucoinj.elasticsearch.security.challenge.ChallengeMessageStore;
import io.ucoin.ucoinj.elasticsearch.security.token.SecurityTokenStore;
import io.ucoin.ucoinj.elasticsearch.service.ServiceLocator;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.*;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestStatus.FORBIDDEN;
import static org.elasticsearch.rest.RestStatus.OK;
public class RestSecurityAuthAction extends BaseRestHandler {
private static final ESLogger log = ESLoggerFactory.getLogger(RestSecurityAuthAction.class.getName());
private ChallengeMessageStore challengeMessageStore;
private SecurityTokenStore securityTokenStore;
@Inject
public RestSecurityAuthAction(Settings settings, RestController controller, Client client,
ChallengeMessageStore challengeMessageStore,
SecurityTokenStore securityTokenStore) {
super(settings, controller, client);
this.challengeMessageStore = challengeMessageStore;
this.securityTokenStore = securityTokenStore;
controller.registerHandler(POST, "/auth", this);
}
@Override
protected void handleRequest(final RestRequest request, RestChannel restChannel, Client client) throws Exception {
AuthData authData = GsonUtils.newBuilder().create().fromJson(request.content().toUtf8(), AuthData.class);
// TODO Authorization: Basic instead ?
if (StringUtils.isNotBlank(authData.pubkey)) {
if (challengeMessageStore.validateChallenge(authData.challenge)) {
boolean signatureOK = ServiceLocator.instance().getCryptoService().verify(authData.challenge, authData.signature, authData.pubkey);
if (signatureOK) {
String token = securityTokenStore.createNewToken(authData.challenge, authData.signature, authData.pubkey);
restChannel.sendResponse(new BytesRestResponse(OK, token));
return;
}
}
}
restChannel.sendResponse(new BytesRestResponse(FORBIDDEN, Boolean.FALSE.toString()));
}
class AuthData {
public String pubkey;
public String challenge;
public String signature;
}
}
\ No newline at end of file
package io.ucoin.ucoinj.elasticsearch.action.security;
import io.ucoin.ucoinj.elasticsearch.security.challenge.ChallengeMessageStore;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.*;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.OK;
public class RestSecurityGetChallengeAction extends BaseRestHandler {
private ChallengeMessageStore challengeMessageStore;
@Inject
public RestSecurityGetChallengeAction(Settings settings, RestController controller, Client client, ChallengeMessageStore challengeMessageStore) {
super(settings, controller, client);
this.challengeMessageStore = challengeMessageStore;
controller.registerHandler(GET, "/auth", this);
}
@Override
protected void handleRequest(final RestRequest request, RestChannel restChannel, Client client) throws Exception {
restChannel.sendResponse(new BytesRestResponse(OK, challengeMessageStore.createNewChallenge()));
}
}
\ No newline at end of file
package io.ucoin.ucoinj.elasticsearch.plugin; package io.ucoin.ucoinj.elasticsearch.plugin;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import io.ucoin.ucoinj.elasticsearch.action.ExampleRestModule; import io.ucoin.ucoinj.elasticsearch.action.RestModule;
import io.ucoin.ucoinj.elasticsearch.action.HelloRestHandler; import io.ucoin.ucoinj.elasticsearch.security.SecurityModule;
import org.elasticsearch.common.inject.Module; import org.elasticsearch.common.inject.Module;
import org.elasticsearch.rest.RestModule;
import java.util.Collection; import java.util.Collection;
...@@ -20,15 +19,11 @@ public class Plugin extends org.elasticsearch.plugins.Plugin { ...@@ -20,15 +19,11 @@ public class Plugin extends org.elasticsearch.plugins.Plugin {
return "uCoinj ElasticSearch Plugin"; return "uCoinj ElasticSearch Plugin";
} }
/*
@Override @Override
public Collection<Module> nodeModules() { public Collection<Module> nodeModules() {
Collection<Module> modules = Lists.newArrayList(); Collection<Module> modules = Lists.newArrayList();
modules.add(ExampleRestModule.class); modules.add(new SecurityModule());
modules.add(new RestModule());
return modules; return modules;
}*/
public void onModule(RestModule module) {
module.addRestAction(HelloRestHandler.class);
} }
} }
\ No newline at end of file
package io.ucoin.ucoinj.elasticsearch.security;
import io.ucoin.ucoinj.elasticsearch.security.challenge.ChallengeMessageStore;
import io.ucoin.ucoinj.elasticsearch.security.token.SecurityTokenStore;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Module;
public class SecurityModule extends AbstractModule implements Module {
@Override protected void configure() {
bind(ChallengeMessageStore.class).asEagerSingleton();
bind(SecurityTokenStore.class).asEagerSingleton();
}
}
\ No newline at end of file
package io.ucoin.ucoinj.elasticsearch.security.challenge;
/*
* #%L
* uCoinj :: UI Wicket
* %%
* Copyright (C) 2014 - 2016 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.google.common.base.Preconditions;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import io.ucoin.ucoinj.core.util.ObjectUtils;
import io.ucoin.ucoinj.core.util.StringUtils;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings;
import java.util.concurrent.TimeUnit;
/**
* Created by blavenie on 06/01/16.
*/
public class ChallengeMessageStore {
private static final ESLogger log = ESLoggerFactory.getLogger(ChallengeMessageStore.class.getName());
private String prefix;
private long validityDurationInSeconds;
private LoadingCache<String, String> chalengeMessageCache;
@Inject
public ChallengeMessageStore(Settings settings) {
this.prefix = settings.get("ucoinj.auth.challenge.prefix", "ucoinj-challenge-");
this.validityDurationInSeconds = settings.getAsInt("ucoinj.auth.challengeValidityDuration", 10);
this.chalengeMessageCache = initGeneratedMessageCache();
}
public boolean validateChallenge(String challenge) {
Preconditions.checkArgument(StringUtils.isNotBlank(challenge));
String storedChallenge = chalengeMessageCache.getIfPresent(challenge);
// if no value in cache => maybe challenge expired
return ObjectUtils.equals(storedChallenge, challenge);
}
public String createNewChallenge() {
String challenge = newChallenge();
chalengeMessageCache.put(challenge, challenge);
return newChallenge();
}
/* -- internal methods -- */
protected String newChallenge() {
return String.valueOf(prefix + System.currentTimeMillis() * System.currentTimeMillis());
}
protected LoadingCache<String, String> initGeneratedMessageCache() {
return CacheBuilder.newBuilder()
.expireAfterWrite(validityDurationInSeconds, TimeUnit.SECONDS)
.build(new CacheLoader<String, String>() {
@Override
public String load(String challenge) throws Exception {
// not used. Filled manually
return null;
}
});
}
}
package io.ucoin.ucoinj.elasticsearch.security.token;
/*
* #%L
* uCoinj :: UI Wicket
* %%
* Copyright (C) 2014 - 2016 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.google.common.base.Preconditions;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import io.ucoin.ucoinj.core.util.ObjectUtils;
import io.ucoin.ucoinj.core.util.StringUtils;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings;
import java.util.concurrent.TimeUnit;
/**
* Created by blavenie on 06/01/16.
*/
public class SecurityTokenStore {
private static final ESLogger log = ESLoggerFactory.getLogger(SecurityTokenStore.class.getName());
private String prefix;
private long validityDurationInSeconds;
private LoadingCache<String, String> tokenCache;
@Inject
public SecurityTokenStore(Settings settings) {
this.prefix = settings.get("ucoinj.auth.token.prefix", "ucoinj-");
this.validityDurationInSeconds = settings.getAsInt("ucoinj.auth.tokenValidityDuration", 30*60 /*30min*/ );
this.tokenCache = initGeneratedMessageCache();
}
public boolean validateToken(String token) {
Preconditions.checkArgument(StringUtils.isNotBlank(token));
String storedToken = tokenCache.getIfPresent(token);
// if no value in cache => maybe token expired
return ObjectUtils.equals(storedToken, token);
}
public String createNewToken(String challenge, String signature, String pubkey) {
String token = newToken(challenge, signature, pubkey);
tokenCache.put(challenge, challenge);
return token;
}
/* -- internal methods -- */
protected String newToken(String challenge, String signature, String pubkey) {
return String.valueOf(pubkey + ":" + challenge + "|" + signature);
}
protected LoadingCache<String, String> initGeneratedMessageCache() {
return CacheBuilder.newBuilder()
.expireAfterWrite(validityDurationInSeconds, TimeUnit.SECONDS)
.build(new CacheLoader<String, String>() {
@Override
public String load(String challenge) throws Exception {
// not used. Filled manually
return null;
}
});
}
}
name=ucoinj-elasticsearch
description=uCoinj :: ElasticSearch Plugin
version=0.1-SNAPSHOT
site=false
jvm=true
classname=io.ucoin.ucoinj.elasticsearch.plugin.Plugin
java.version=1.7
elasticsearch.version=2.1.1
isolated=false
\ No newline at end of file
...@@ -7,16 +7,19 @@ log4j.appender.stdout=org.apache.log4j.ConsoleAppender ...@@ -7,16 +7,19 @@ log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %5p (%c:%L) - %m%n log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %5p (%c:%L) - %m%n
# ucoin levels # File output
log4j.logger.io.ucoin.ucoinj=INFO
#log4j.logger.io.ucoin.ucoinj.core.client.service=DEBUG
log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.file=target/ucoinj-elasticsearch-plugin.log log4j.appender.file.file=target/ucoinj-elasticsearch-plugin.log
log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=4 log4j.appender.file.MaxBackupIndex=4
log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %5p %c - %m%n log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %5p %c - %m%n
# uCoinJ levels
log4j.logger.io.ucoin.ucoinj=INFO
log4j.logger.io.ucoin.ucoinj.elasticsearch=DEBUG
#log4j.logger.io.ucoin.ucoinj.elasticsearch.action=DEBUG
#log4j.logger.io.ucoin.ucoinj.core.client.service=DEBUG
# Other frameworks levels
log4j.logger.org.elasticsearch=INFO
\ No newline at end of file
...@@ -45,10 +45,12 @@ ...@@ -45,10 +45,12 @@
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId> <artifactId>slf4j-log4j12</artifactId>
<optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<optional>true</optional>
</dependency> </dependency>
<!-- Elastic Search --> <!-- Elastic Search -->
...@@ -61,6 +63,13 @@ ...@@ -61,6 +63,13 @@
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
</dependency> </dependency>
<!-- JNA -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<scope>provided</scope>
</dependency>
<!-- JNA (need for OS shutdown hook) --> <!-- JNA (need for OS shutdown hook) -->
<dependency> <dependency>
<groupId>net.java.dev.jna</groupId> <groupId>net.java.dev.jna</groupId>
......
...@@ -33,16 +33,18 @@ import io.ucoin.ucoinj.elasticsearch.service.ServiceLocator; ...@@ -33,16 +33,18 @@ import io.ucoin.ucoinj.elasticsearch.service.ServiceLocator;
import io.ucoin.ucoinj.elasticsearch.util.Desktop; import io.ucoin.ucoinj.elasticsearch.util.Desktop;
import io.ucoin.ucoinj.elasticsearch.util.DesktopPower; import io.ucoin.ucoinj.elasticsearch.util.DesktopPower;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.nuiton.config.ApplicationConfig; import org.nuiton.config.ApplicationConfig;
import org.nuiton.i18n.I18n; import org.nuiton.i18n.I18n;
import org.nuiton.i18n.init.DefaultI18nInitializer; import org.nuiton.i18n.init.DefaultI18nInitializer;
import org.nuiton.i18n.init.UserI18nInitializer; import org.nuiton.i18n.init.UserI18nInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.Arrays;
import java.util.List;
import java.util.Locale;
public class Main { public class Main {
...@@ -55,7 +57,7 @@ public class Main { ...@@ -55,7 +57,7 @@ public class Main {
+ "* %s\n" // sub-title + "* %s\n" // sub-title
+ TITLE_EMPTY_LINE + TITLE_SEPARATOR_LINE; + TITLE_EMPTY_LINE + TITLE_SEPARATOR_LINE;
private static final Logger log = LoggerFactory.getLogger(Main.class); private static final ESLogger log = ESLoggerFactory.getLogger(Main.class.getName());
public static void main(String[] args) { public static void main(String[] args) {
Main main = new Main(); Main main = new Main();
......
...@@ -34,6 +34,7 @@ public class HelpAction { ...@@ -34,6 +34,7 @@ public class HelpAction {
.append(" start Start elastic search node\n") .append(" start Start elastic search node\n")
.append(" index Index blocks from BMA Node\n") .append(" index Index blocks from BMA Node\n")
.append(" reset-data Reset indexed data for the uCoin node's currency\n") .append(" reset-data Reset indexed data for the uCoin node's currency\n")
.append(" reset-data-product Reset indexed products data\n")
.append("\n") .append("\n")
.append("\n") .append("\n")
.append("Options:\n\n") .append("Options:\n\n")
......
...@@ -31,6 +31,8 @@ import io.ucoin.ucoinj.core.client.service.bma.BlockchainRemoteService; ...@@ -31,6 +31,8 @@ import io.ucoin.ucoinj.core.client.service.bma.BlockchainRemoteService;
import io.ucoin.ucoinj.core.util.websocket.WebsocketClientEndpoint; import io.ucoin.ucoinj.core.util.websocket.WebsocketClientEndpoint;
import io.ucoin.ucoinj.elasticsearch.config.Configuration; import io.ucoin.ucoinj.elasticsearch.config.Configuration;
import io.ucoin.ucoinj.elasticsearch.service.BlockIndexerService; import io.ucoin.ucoinj.elasticsearch.service.BlockIndexerService;
import io.ucoin.ucoinj.elasticsearch.service.CategoryIndexerService;
import io.ucoin.ucoinj.elasticsearch.service.ProductIndexerService;
import io.ucoin.ucoinj.elasticsearch.service.ServiceLocator; import io.ucoin.ucoinj.elasticsearch.service.ServiceLocator;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -76,7 +78,7 @@ public class IndexerAction { ...@@ -76,7 +78,7 @@ public class IndexerAction {
} }
} }
public void resetData() { public void resetCurrencyBlocks() {
BlockchainRemoteService blockchainService = ServiceLocator.instance().getBlockchainRemoteService(); BlockchainRemoteService blockchainService = ServiceLocator.instance().getBlockchainRemoteService();
BlockIndexerService indexerService = ServiceLocator.instance().getBlockIndexerService(); BlockIndexerService indexerService = ServiceLocator.instance().getBlockIndexerService();
Configuration config = Configuration.instance(); Configuration config = Configuration.instance();
...@@ -94,22 +96,55 @@ public class IndexerAction { ...@@ -94,22 +96,55 @@ public class IndexerAction {
log.info(String.format("Reset data for index [%s]", currencyName)); log.info(String.format("Reset data for index [%s]", currencyName));
// Check if index exists // Delete then create index on currency
boolean indexExists = indexerService.existsIndex(currencyName); boolean indexExists = indexerService.existsIndex(currencyName);
if (indexExists) { if (indexExists) {
log.debug(String.format("Deleting index [%s]", currencyName));
indexerService.deleteIndex(currencyName); indexerService.deleteIndex(currencyName);
log.debug(String.format("Creating index [%s]", currencyName));
indexerService.createIndex(currencyName); indexerService.createIndex(currencyName);
} }
log.info(String.format("Successfully reset data for index [%s]", currencyName)); log.info(String.format("Successfully reset data for index [%s]", currencyName));
} catch(Exception e) { } catch(Exception e) {
log.error("Error during reset data: " + e.getMessage(), e); log.error("Error during reset data: " + e.getMessage(), e);
} }
} }
public void resetProducts() {
ProductIndexerService productIndexerService = ServiceLocator.instance().getProductIndexerService();
try {
// Delete then create index on product
boolean indexExists = productIndexerService.existsIndex();
if (indexExists) {
productIndexerService.deleteIndex();
}
log.info(String.format("Successfully reset products data"));
} catch(Exception e) {
log.error("Error during reset products data: " + e.getMessage(), e);
}
}
public void resetCategories() {
CategoryIndexerService categoryIndexerService = ServiceLocator.instance().getCategoryIndexerService();
try {
// Delete then create index on product
boolean indexExists = categoryIndexerService.existsIndex();
if (indexExists) {
categoryIndexerService.deleteIndex();
}
// Init data
categoryIndexerService.createIndex();
categoryIndexerService.initCategories();
log.info(String.format("Successfully re-initialized categories data"));
} catch(Exception e) {
log.error("Error during reset categories data: " + e.getMessage(), e);
}
}
/* -- -- */ /* -- -- */
protected Peer checkConfigAndGetPeer(Configuration config) { protected Peer checkConfigAndGetPeer(Configuration config) {
......
...@@ -27,9 +27,7 @@ package io.ucoin.ucoinj.elasticsearch.action; ...@@ -27,9 +27,7 @@ package io.ucoin.ucoinj.elasticsearch.action;
import io.ucoin.ucoinj.core.util.CommandLinesUtils; import io.ucoin.ucoinj.core.util.CommandLinesUtils;
import io.ucoin.ucoinj.core.util.StringUtils; import io.ucoin.ucoinj.core.util.StringUtils;
import io.ucoin.ucoinj.elasticsearch.config.Configuration; import io.ucoin.ucoinj.elasticsearch.config.Configuration;
import io.ucoin.ucoinj.elasticsearch.service.CurrencyIndexerService; import io.ucoin.ucoinj.elasticsearch.service.*;
import io.ucoin.ucoinj.elasticsearch.service.ElasticSearchService;
import io.ucoin.ucoinj.elasticsearch.service.ServiceLocator;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -74,8 +72,19 @@ public class NodeAction { ...@@ -74,8 +72,19 @@ public class NodeAction {
// Create indexed if need // Create indexed if need
{
// Currency index
CurrencyIndexerService currencyIndexerService = ServiceLocator.instance().getCurrencyIndexerService(); CurrencyIndexerService currencyIndexerService = ServiceLocator.instance().getCurrencyIndexerService();
currencyIndexerService.createIndexIfNotExists(); currencyIndexerService.createIndexIfNotExists();
// Product index
ProductIndexerService productIndexerService = ServiceLocator.instance().getProductIndexerService();
productIndexerService.createIndexIfNotExists();
// Category index
CategoryIndexerService categoryIndexerService = ServiceLocator.instance().getCategoryIndexerService();
categoryIndexerService.createIndexIfNotExists();
}
} }
/*public void stop() { /*public void stop() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment