From 63838f0e3ed8be0d74531b7dd929ef9c3e814c93 Mon Sep 17 00:00:00 2001 From: blavenie <benoit.lavenier@e-is.pro> Date: Tue, 10 Jan 2017 15:46:23 +0100 Subject: [PATCH] - Fix i18n initailisation (get bundle for each plugin) - Remove guava dependency (in modules core-client and core-shared) - Remove gson (keep as optional in module core-client) - Add /node/summary Rest action --- duniter4j-core-client/pom.xml | 24 +++- .../core/client/config/Configuration.java | 4 +- .../client/config/ConfigurationOption.java | 2 +- .../duniter/core/client/model/ModelUtils.java | 2 +- .../model/bma/gson/JsonAttributeParser.java | 6 +- .../model/bma/gson/MultimapTypeAdapter.java | 2 +- .../model/bma/jackson/JacksonUtils.java | 9 +- .../bma/BlockchainRemoteServiceImpl.java | 39 +++--- .../service/bma/NetworkRemoteServiceImpl.java | 7 +- .../bma/TransactionRemoteServiceImpl.java | 13 +- .../service/bma/WotRemoteServiceImpl.java | 5 +- .../CurrencyRegistryRemoteService.java | 4 - .../CurrencyRegistryRemoteServiceImpl.java | 59 +-------- .../service/local/CurrencyServiceImpl.java | 3 +- .../client/service/local/PeerServiceImpl.java | 9 +- .../CurrencyRegistryRemoteServiceTest.java | 19 --- duniter4j-core-shared/pom.xml | 4 - .../org/duniter/core/test/TestResource.java | 2 +- .../org/duniter/core/util/ObjectUtils.java | 21 ++-- .../org/duniter/core/util/Preconditions.java | 55 +++++++++ .../websocket/WebsocketClientEndpoint.java | 2 +- duniter4j-es-assembly/pom.xml | 81 +++++++++--- .../src/main/assembly/standalone.xml | 14 ++- .../src/test/es-home/config/logging.yml | 2 +- duniter4j-es-core/pom.xml | 25 +++- .../org/duniter/elasticsearch/PluginInit.java | 2 - .../duniter/elasticsearch/PluginSettings.java | 70 +++++------ .../elasticsearch/i18n/I18nInitializer.java | 72 +++++++++++ .../elasticsearch/rest/RestModule.java | 14 ++- .../currency/RestCurrencyIndexAction.java | 27 ++-- .../rest/node/RestNodeSummaryGetAction.java | 83 +++++++++++++ .../challenge/ChallengeMessageStore.java | 2 +- .../security/token/SecurityTokenStore.java | 2 +- .../service/AbstractService.java | 52 ++++---- .../service/AbstractSynchroService.java | 2 +- .../service/BlockchainService.java | 63 +++++----- .../service/CurrencyService.java | 45 ++++--- .../service/changes/ChangeEvent.java | 5 +- .../service/changes/ChangeService.java | 2 +- .../service/changes/ChangeSource.java | 2 +- .../elasticsearch/threadpool/ThreadPool.java | 2 +- .../websocket/WebSocketServer.java | 5 +- .../elasticsearch/gchange/PluginInit.java | 6 +- .../elasticsearch/gchange/PluginSettings.java | 115 +++++++++++++++++- .../gchange/service/AbstractService.java | 31 +++++ .../service/CitiesRegistryService.java | 40 +++--- .../gchange/service/CommentService.java | 4 +- .../service/CommentUserEventService.java | 87 ++++++++----- .../gchange/service/MarketService.java | 2 +- .../gchange/service/RegistryService.java | 3 +- .../gchange/service/SynchroService.java | 4 +- duniter4j-es-user/pom.xml | 7 ++ .../duniter/elasticsearch/user/Plugin.java | 1 + .../elasticsearch/user/PluginInit.java | 19 ++- .../elasticsearch/user/PluginSettings.java | 94 ++++++++++++-- .../elasticsearch/user/model/Message.java | 2 +- .../elasticsearch/user/model/UserEvent.java | 2 +- .../user/service/AbstractService.java | 31 +++++ .../service/BlockchainUserEventService.java | 4 +- .../user/service/GroupService.java | 10 +- .../user/service/HistoryService.java | 13 +- .../user/service/MessageService.java | 4 +- .../user/service/SynchroService.java | 4 +- .../user/service/UserEventService.java | 6 +- .../user/service/UserService.java | 6 +- pom.xml | 12 +- 66 files changed, 930 insertions(+), 440 deletions(-) create mode 100644 duniter4j-core-shared/src/main/java/org/duniter/core/util/Preconditions.java create mode 100644 duniter4j-es-core/src/main/java/org/duniter/elasticsearch/i18n/I18nInitializer.java create mode 100644 duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/node/RestNodeSummaryGetAction.java create mode 100644 duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/AbstractService.java create mode 100644 duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/AbstractService.java diff --git a/duniter4j-core-client/pom.xml b/duniter4j-core-client/pom.xml index 419f80c7..82402e40 100644 --- a/duniter4j-core-client/pom.xml +++ b/duniter4j-core-client/pom.xml @@ -42,13 +42,11 @@ <dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-client</artifactId> - <version>1.12</version> </dependency> <dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-container-grizzly-client</artifactId> - <version>1.12</version> </dependency> <dependency> @@ -75,6 +73,10 @@ <groupId>org.nuiton</groupId> <artifactId>nuiton-config</artifactId> </dependency> + <dependency> + <groupId>org.nuiton</groupId> + <artifactId>nuiton-version</artifactId> + </dependency> <dependency> <groupId>org.nuiton.i18n</groupId> <artifactId>nuiton-i18n</artifactId> @@ -82,6 +84,7 @@ <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> + <scope>provided</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> @@ -107,6 +110,22 @@ </dependencies> <build> + <resources> + <resource> + <directory>src/main/filtered-resources</directory> + <filtering>true</filtering> + <includes> + <include>*.config</include> + <include>*.properties</include> + </includes> + </resource> + <resource> + <directory>src/main/resources</directory> + <includes> + <include>**/*</include> + </includes> + </resource> + </resources> <testResources> <testResource> <directory>src/test/resources</directory> @@ -146,6 +165,7 @@ </execution> </executions> </plugin> + </plugins> </build> </project> diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/config/Configuration.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/config/Configuration.java index aa06a3a1..e6d06adb 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/config/Configuration.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/config/Configuration.java @@ -30,6 +30,7 @@ import org.nuiton.config.ApplicationConfigHelper; import org.nuiton.config.ApplicationConfigProvider; import org.nuiton.config.ArgumentsParserException; import org.nuiton.version.Version; +import org.nuiton.version.VersionBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -211,7 +212,8 @@ public class Configuration { } public Version getVersion() { - return applicationConfig.getOptionAsVersion(ConfigurationOption.VERSION.getKey()); + String versionStr = applicationConfig.getOption(ConfigurationOption.VERSION.getKey()); + return VersionBuilder.create(versionStr).build(); } public File getI18nDirectory() { diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/config/ConfigurationOption.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/config/ConfigurationOption.java index 53c2de68..41d80093 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/config/ConfigurationOption.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/config/ConfigurationOption.java @@ -25,7 +25,7 @@ package org.duniter.core.client.config; */ import org.nuiton.config.ConfigOptionDef; -import org.nuiton.util.Version; +import org.nuiton.version.Version; import java.io.File; import java.net.URL; diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/ModelUtils.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/ModelUtils.java index 5b024359..67a7390d 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/ModelUtils.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/ModelUtils.java @@ -24,7 +24,7 @@ package org.duniter.core.client.model; import java.util.*; -import com.google.common.base.Preconditions; +import org.duniter.core.util.Preconditions; import org.duniter.core.client.model.local.Certification; import org.duniter.core.client.model.local.Movement; import org.duniter.core.util.CollectionUtils; diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/JsonAttributeParser.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/JsonAttributeParser.java index 0cab493e..aac0d7ed 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/JsonAttributeParser.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/JsonAttributeParser.java @@ -23,7 +23,7 @@ package org.duniter.core.client.model.bma.gson; */ import org.duniter.core.exception.TechnicalException; -import org.duniter.core.util.ObjectUtils; +import org.duniter.core.util.Preconditions; import java.text.DecimalFormat; import java.text.ParseException; @@ -43,7 +43,7 @@ public class JsonAttributeParser { private String attributeName; public JsonAttributeParser(String attributeName) { - ObjectUtils.checkNotNull(attributeName); + Preconditions.checkNotNull(attributeName); this.attributeName = attributeName; this.numericPattern = Pattern.compile(String.format(REGEX_ATTRIBUTE_NUMERIC_VALUE, attributeName)); @@ -53,7 +53,7 @@ public class JsonAttributeParser { } public Number getValueAsNumber(String jsonString) { - ObjectUtils.checkNotNull(jsonString); + Preconditions.checkNotNull(jsonString); Matcher matcher = numericPattern.matcher(jsonString); diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/MultimapTypeAdapter.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/MultimapTypeAdapter.java index 0a21950c..d8b5a9b2 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/MultimapTypeAdapter.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/MultimapTypeAdapter.java @@ -23,7 +23,7 @@ package org.duniter.core.client.model.bma.gson; */ -import com.google.common.base.Preconditions; +import org.duniter.core.util.Preconditions; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import com.google.gson.*; diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JacksonUtils.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JacksonUtils.java index c6b577f8..b7118cac 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JacksonUtils.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JacksonUtils.java @@ -14,6 +14,9 @@ import java.util.List; */ public abstract class JacksonUtils extends SimpleModule { + public static final String REGEX_ATTRIBUTE_REPLACE = "[,]?[\"\\s\\n\\r]*%s[\"]?[\\s\\n\\r]*:[\\s\\n\\r]*\"[^\"]+\""; + + public static ObjectMapper newObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); @@ -29,7 +32,6 @@ public abstract class JacksonUtils extends SimpleModule { // Network module.addDeserializer(NetworkPeering.Endpoint.class, new EndpointDeserializer()); - objectMapper.registerModule(module); return objectMapper; @@ -54,4 +56,9 @@ public abstract class JacksonUtils extends SimpleModule { public static List<String> getArrayValuesFromJSONAsInt(String jsonString) { return new JsonArrayParser().getValuesAsList(jsonString); } + + public static String removeAttribute(String jsonString, String attributeName) { + return jsonString.replaceAll(String.format(REGEX_ATTRIBUTE_REPLACE, attributeName), ""); + } + } diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/BlockchainRemoteServiceImpl.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/BlockchainRemoteServiceImpl.java index 4eaed7bb..b0735d8c 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/BlockchainRemoteServiceImpl.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/BlockchainRemoteServiceImpl.java @@ -22,7 +22,11 @@ package org.duniter.core.client.service.bma; * #L% */ -import com.google.common.base.Preconditions; +import org.duniter.core.util.Preconditions; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.message.BasicNameValuePair; import org.duniter.core.client.config.Configuration; import org.duniter.core.client.model.bma.*; import org.duniter.core.client.model.bma.gson.JsonArrayParser; @@ -33,16 +37,11 @@ import org.duniter.core.client.service.ServiceLocator; import org.duniter.core.client.service.exception.*; import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; -import org.duniter.core.util.ObjectUtils; import org.duniter.core.util.StringUtils; import org.duniter.core.util.cache.Cache; import org.duniter.core.util.cache.SimpleCache; import org.duniter.core.util.crypto.CryptoUtils; import org.duniter.core.util.websocket.WebsocketClientEndpoint; -import org.apache.http.NameValuePair; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.message.BasicNameValuePair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -325,10 +324,10 @@ public class BlockchainRemoteServiceImpl extends BaseRemoteServiceImpl implement * @throws PubkeyAlreadyUsedException if pubkey already used by another member */ public void checkNotMemberIdentity(Peer peer, Identity identity) throws UidAlreadyUsedException, PubkeyAlreadyUsedException { - ObjectUtils.checkNotNull(peer); - ObjectUtils.checkNotNull(identity); - ObjectUtils.checkArgument(StringUtils.isNotBlank(identity.getUid())); - ObjectUtils.checkArgument(StringUtils.isNotBlank(identity.getPubkey())); + Preconditions.checkNotNull(peer); + Preconditions.checkNotNull(identity); + Preconditions.checkArgument(StringUtils.isNotBlank(identity.getUid())); + Preconditions.checkArgument(StringUtils.isNotBlank(identity.getPubkey())); // Read membership data from the UID BlockchainMemberships result = getMembershipByPubkeyOrUid(peer, identity.getUid()); @@ -353,7 +352,7 @@ public class BlockchainRemoteServiceImpl extends BaseRemoteServiceImpl implement * @throws UidMatchAnotherPubkeyException is uid already used by another pubkey */ public void loadAndCheckMembership(Peer peer, Wallet wallet) throws UidMatchAnotherPubkeyException { - ObjectUtils.checkNotNull(wallet); + Preconditions.checkNotNull(wallet); // Load membership data loadMembership(null, peer, wallet.getIdentity(), true); @@ -375,7 +374,7 @@ public class BlockchainRemoteServiceImpl extends BaseRemoteServiceImpl implement public BlockchainMemberships getMembershipByUid(long currencyId, String uid) { - ObjectUtils.checkArgument(StringUtils.isNotBlank(uid)); + Preconditions.checkArgument(StringUtils.isNotBlank(uid)); BlockchainMemberships result = getMembershipByPubkeyOrUid(currencyId, uid); if (result == null || !uid.equals(result.getUid())) { @@ -385,7 +384,7 @@ public class BlockchainRemoteServiceImpl extends BaseRemoteServiceImpl implement } public BlockchainMemberships getMembershipByPublicKey(long currencyId, String pubkey) { - ObjectUtils.checkArgument(StringUtils.isNotBlank(pubkey)); + Preconditions.checkArgument(StringUtils.isNotBlank(pubkey)); BlockchainMemberships result = getMembershipByPubkeyOrUid(currencyId, pubkey); if (result == null || !pubkey.equals(result.getPubkey())) { @@ -398,9 +397,9 @@ public class BlockchainRemoteServiceImpl extends BaseRemoteServiceImpl implement * Request to integrate the wot */ public void requestMembership(Wallet wallet) { - ObjectUtils.checkNotNull(wallet); - ObjectUtils.checkNotNull(wallet.getCurrencyId()); - ObjectUtils.checkNotNull(wallet.getCertTimestamp()); + Preconditions.checkNotNull(wallet); + Preconditions.checkNotNull(wallet.getCurrencyId()); + Preconditions.checkNotNull(wallet.getCertTimestamp()); BlockchainBlock block = getCurrentBlock(wallet.getCurrencyId()); @@ -605,10 +604,10 @@ public class BlockchainRemoteServiceImpl extends BaseRemoteServiceImpl implement protected void loadMembership(Long currencyId, Peer peer, Identity identity, boolean checkLookupForNonMember) { - ObjectUtils.checkNotNull(identity); - ObjectUtils.checkArgument(StringUtils.isNotBlank(identity.getUid())); - ObjectUtils.checkArgument(StringUtils.isNotBlank(identity.getPubkey())); - ObjectUtils.checkArgument(peer != null || currencyId != null); + Preconditions.checkNotNull(identity); + Preconditions.checkArgument(StringUtils.isNotBlank(identity.getUid())); + Preconditions.checkArgument(StringUtils.isNotBlank(identity.getPubkey())); + Preconditions.checkArgument(peer != null || currencyId != null); // Read membership data from the UID BlockchainMemberships result = peer != null diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/NetworkRemoteServiceImpl.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/NetworkRemoteServiceImpl.java index b00078db..48902b02 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/NetworkRemoteServiceImpl.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/NetworkRemoteServiceImpl.java @@ -30,6 +30,7 @@ import org.duniter.core.client.model.bma.NetworkPeering; import org.duniter.core.client.model.bma.NetworkPeers; import org.duniter.core.client.model.local.Peer; import org.duniter.core.util.ObjectUtils; +import org.duniter.core.util.Preconditions; import org.duniter.core.util.StringUtils; /** @@ -64,7 +65,7 @@ public class NetworkRemoteServiceImpl extends BaseRemoteServiceImpl implements N @Override public List<Peer> findPeers(Peer peer, String status, EndpointProtocol endpointProtocol, Integer currentBlockNumber, String currentBlockHash) { - ObjectUtils.checkNotNull(peer); + Preconditions.checkNotNull(peer); List<Peer> result = new ArrayList<Peer>(); @@ -115,7 +116,7 @@ public class NetworkRemoteServiceImpl extends BaseRemoteServiceImpl implements N } protected Integer parseBlockNumber(NetworkPeers.Peer remotePeer) { - ObjectUtils.checkNotNull(remotePeer); + Preconditions.checkNotNull(remotePeer); if (remotePeer.block == null) { return null; @@ -134,7 +135,7 @@ public class NetworkRemoteServiceImpl extends BaseRemoteServiceImpl implements N } protected String parseBlockHash(NetworkPeers.Peer remotePeer) { - ObjectUtils.checkNotNull(remotePeer); + Preconditions.checkNotNull(remotePeer); if (remotePeer.block == null) { return null; diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/TransactionRemoteServiceImpl.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/TransactionRemoteServiceImpl.java index 053f37ef..b927f4e5 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/TransactionRemoteServiceImpl.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/TransactionRemoteServiceImpl.java @@ -37,6 +37,7 @@ import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; import org.duniter.core.util.CollectionUtils; import org.duniter.core.util.ObjectUtils; +import org.duniter.core.util.Preconditions; import org.duniter.core.util.StringUtils; import org.duniter.core.util.crypto.DigestUtils; import org.apache.http.NameValuePair; @@ -199,9 +200,9 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen } public TxHistory getTxHistory(long currencyId, String pubKey, long fromBlockNumber, long toBlockNumber) { - ObjectUtils.checkNotNull(pubKey); - ObjectUtils.checkArgument(fromBlockNumber >= 0); - ObjectUtils.checkArgument(fromBlockNumber <= toBlockNumber); + Preconditions.checkNotNull(pubKey); + Preconditions.checkArgument(fromBlockNumber >= 0); + Preconditions.checkArgument(fromBlockNumber <= toBlockNumber); if (log.isDebugEnabled()) { log.debug(String.format("Get TX history by pubKey [%s], from block [%s -> %s]", pubKey, fromBlockNumber, toBlockNumber)); @@ -222,9 +223,9 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen int locktime, long amount, String comment) throws InsufficientCreditException { - ObjectUtils.checkNotNull(wallet); - ObjectUtils.checkArgument(StringUtils.isNotBlank(wallet.getCurrency())); - ObjectUtils.checkArgument(StringUtils.isNotBlank(wallet.getPubKeyHash())); + Preconditions.checkNotNull(wallet); + Preconditions.checkArgument(StringUtils.isNotBlank(wallet.getCurrency())); + Preconditions.checkArgument(StringUtils.isNotBlank(wallet.getPubKeyHash())); // Retrieve the wallet sources TxSource sourceResults = getSources(wallet.getCurrencyId(), wallet.getPubKeyHash()); diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/WotRemoteServiceImpl.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/WotRemoteServiceImpl.java index 07ce3f27..3e42c346 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/WotRemoteServiceImpl.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/WotRemoteServiceImpl.java @@ -34,6 +34,7 @@ import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; import org.duniter.core.util.CollectionUtils; import org.duniter.core.util.ObjectUtils; +import org.duniter.core.util.Preconditions; import org.duniter.core.util.crypto.CryptoUtils; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; @@ -221,8 +222,8 @@ public class WotRemoteServiceImpl extends BaseRemoteServiceImpl implements WotRe } public Collection<Certification> getCertifications(long currencyId, String uid, String pubkey, boolean isMember) { - ObjectUtils.checkNotNull(uid); - ObjectUtils.checkNotNull(pubkey); + Preconditions.checkNotNull(uid); + Preconditions.checkNotNull(pubkey); if (isMember) { return getCertificationsByPubkeyForMember(currencyId, pubkey, true); diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/elasticsearch/CurrencyRegistryRemoteService.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/elasticsearch/CurrencyRegistryRemoteService.java index 67c04224..1bc90988 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/elasticsearch/CurrencyRegistryRemoteService.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/elasticsearch/CurrencyRegistryRemoteService.java @@ -47,8 +47,4 @@ public interface CurrencyRegistryRemoteService extends Service { boolean isNodeAlive(Peer peer); List<String> getAllCurrencyNames(); - - void registerNewCurrency(Wallet wallet, Currency currency); - - void registerNewCurrency(String pubkey, String jsonCurrency, String signature); } diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/elasticsearch/CurrencyRegistryRemoteServiceImpl.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/elasticsearch/CurrencyRegistryRemoteServiceImpl.java index cd2019ef..b2ab891f 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/elasticsearch/CurrencyRegistryRemoteServiceImpl.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/elasticsearch/CurrencyRegistryRemoteServiceImpl.java @@ -24,24 +24,17 @@ package org.duniter.core.client.service.elasticsearch; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.utils.URIBuilder; import org.duniter.core.beans.InitializingBean; import org.duniter.core.client.config.Configuration; -import org.duniter.core.client.model.bma.gson.GsonUtils; import org.duniter.core.client.model.bma.jackson.JacksonUtils; -import org.duniter.core.client.model.elasticsearch.Currency; import org.duniter.core.client.model.local.Peer; -import org.duniter.core.client.model.local.Wallet; -import org.duniter.core.client.service.ServiceLocator; import org.duniter.core.client.service.bma.BaseRemoteServiceImpl; import org.duniter.core.exception.TechnicalException; -import org.duniter.core.service.CryptoService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.Closeable; import java.io.IOException; -import java.net.URISyntaxException; import java.util.Collections; import java.util.List; @@ -51,9 +44,8 @@ import java.util.List; public class CurrencyRegistryRemoteServiceImpl extends BaseRemoteServiceImpl implements CurrencyRegistryRemoteService, InitializingBean, Closeable{ private static final Logger log = LoggerFactory.getLogger(CurrencyRegistryRemoteServiceImpl.class); - private final static String URL_STATUS = "/"; - private final static String URL_ALL_CURRENCY_NAMES = "/currency/simple/_search?_source=currencyName"; - private final static String URL_ADD_CURRENCY = "/rest/currency/add"; + private final static String URL_STATUS = "/node/summary"; + private final static String URL_ALL_CURRENCY_NAMES = "/currency/record/_search?_source=currencyName"; private Configuration config; private Peer peer; @@ -120,53 +112,6 @@ public class CurrencyRegistryRemoteServiceImpl extends BaseRemoteServiceImpl imp return currencyNames; } - @Override - public void registerNewCurrency(Wallet wallet, Currency currency) { - if (log.isDebugEnabled()) { - log.debug("Registering a new currency..."); - } - - String currencyJson = GsonUtils.newBuilder().create().toJson(currency); - CryptoService cryptoService = ServiceLocator.instance().getCryptoService(); - String signature = cryptoService.sign(currencyJson, wallet.getSecKey()); - - registerNewCurrency( - wallet.getPubKeyHash(), - currencyJson, - signature); - - // get currency - //HttpGet httpGet = new HttpGet(getAppendedPath("/currency/simple/_search?_source=currencyName")); - //String jsonString = executeRequest(httpGet, String.class); - - } - - @Override - public void registerNewCurrency(String pubkey, String jsonCurrency, String signature) { - if (log.isDebugEnabled()) { - log.debug("Registering a new currency..."); - } - - URIBuilder builder = getURIBuilder(config.getNodeElasticSearchUrl(), URL_ADD_CURRENCY); - builder.addParameter("pubkey", pubkey); - builder.addParameter("currency", jsonCurrency); - builder.addParameter("sig", signature); - - HttpGet httpGet; - try { - httpGet = new HttpGet(builder.build()); - } - catch(URISyntaxException e) { - throw new TechnicalException(e); - } - - String result = executeRequest(httpGet, String.class); - - if (log.isDebugEnabled()) { - log.debug("Server response, after currency registration: " + result); - } - } - /* -- protected methods -- */ diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/CurrencyServiceImpl.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/CurrencyServiceImpl.java index a1481686..86b11cbb 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/CurrencyServiceImpl.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/CurrencyServiceImpl.java @@ -28,6 +28,7 @@ import org.duniter.core.client.model.local.Currency; import org.duniter.core.client.service.ServiceLocator; import org.duniter.core.client.service.bma.BlockchainRemoteService; import org.duniter.core.util.ObjectUtils; +import org.duniter.core.util.Preconditions; import org.duniter.core.util.StringUtils; import org.duniter.core.util.cache.Cache; import org.duniter.core.util.cache.SimpleCache; @@ -136,7 +137,7 @@ public class CurrencyServiceImpl implements CurrencyService, InitializingBean { * @return */ public Long getCurrencyIdByName(String currencyName) { - ObjectUtils.checkArgument(StringUtils.isNotBlank(currencyName)); + Preconditions.checkArgument(StringUtils.isNotBlank(currencyName)); // Search from currencies for (Map.Entry<Long, Currency> entry : mCurrencyCache.entrySet()) { diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/PeerServiceImpl.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/PeerServiceImpl.java index c40d34a8..d8f36924 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/PeerServiceImpl.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/PeerServiceImpl.java @@ -30,6 +30,7 @@ import org.duniter.core.client.service.ServiceLocator; import org.duniter.core.exception.TechnicalException; import org.duniter.core.util.CollectionUtils; import org.duniter.core.util.ObjectUtils; +import org.duniter.core.util.Preconditions; import org.duniter.core.util.StringUtils; import org.duniter.core.util.cache.Cache; import org.duniter.core.util.cache.SimpleCache; @@ -68,10 +69,10 @@ public class PeerServiceImpl implements PeerService, InitializingBean { } public Peer save(final Peer peer) { - ObjectUtils.checkNotNull(peer); - ObjectUtils.checkNotNull(peer.getCurrencyId()); - ObjectUtils.checkArgument(StringUtils.isNotBlank(peer.getHost())); - ObjectUtils.checkArgument(peer.getPort() >= 0); + Preconditions.checkNotNull(peer); + Preconditions.checkNotNull(peer.getCurrencyId()); + Preconditions.checkArgument(StringUtils.isNotBlank(peer.getHost())); + Preconditions.checkArgument(peer.getPort() >= 0); Peer result; // Create diff --git a/duniter4j-core-client/src/test/java/org/duniter/core/client/service/elasticsearch/CurrencyRegistryRemoteServiceTest.java b/duniter4j-core-client/src/test/java/org/duniter/core/client/service/elasticsearch/CurrencyRegistryRemoteServiceTest.java index 371731ce..2976fb38 100644 --- a/duniter4j-core-client/src/test/java/org/duniter/core/client/service/elasticsearch/CurrencyRegistryRemoteServiceTest.java +++ b/duniter4j-core-client/src/test/java/org/duniter/core/client/service/elasticsearch/CurrencyRegistryRemoteServiceTest.java @@ -25,11 +25,8 @@ package org.duniter.core.client.service.elasticsearch; import org.duniter.core.client.TestResource; import org.duniter.core.client.config.Configuration; import org.duniter.core.client.model.Currency; -import org.duniter.core.client.model.bma.gson.GsonUtils; -import org.duniter.core.client.model.bma.jackson.JacksonUtils; import org.duniter.core.client.model.local.Wallet; import org.duniter.core.client.service.ServiceLocator; -import org.duniter.core.service.CryptoService; import org.duniter.core.util.crypto.CryptoUtils; import org.junit.*; import org.slf4j.Logger; @@ -77,22 +74,6 @@ public class CurrencyRegistryRemoteServiceTest { } } - @Test - public void registerCurrency() throws Exception { - Currency currency = new Currency(); - currency.setCurrencyName("register-test-" + System.currentTimeMillis()); - - String currencyJson = JacksonUtils.newObjectMapper().writeValueAsString(currency); - - String pubKey = resource.getFixtures().getUserPublicKey(); - String secretKey = resource.getFixtures().getUserSecretKey(); - - CryptoService cryptoService = ServiceLocator.instance().getCryptoService(); - String signature = cryptoService.sign(currencyJson, secretKey); - - service.registerNewCurrency(pubKey, currencyJson, signature); - } - /* -- internal methods -- */ protected Wallet createTestWallet() { diff --git a/duniter4j-core-shared/pom.xml b/duniter4j-core-shared/pom.xml index 5f244338..329d1ba5 100644 --- a/duniter4j-core-shared/pom.xml +++ b/duniter4j-core-shared/pom.xml @@ -40,10 +40,6 @@ <groupId>com.lambdaworks</groupId> <artifactId>scrypt</artifactId> </dependency> - <dependency> - <groupId>com.google.guava</groupId> - <artifactId>guava</artifactId> - </dependency> <dependency> <groupId>javax.websocket</groupId> diff --git a/duniter4j-core-shared/src/main/java/org/duniter/core/test/TestResource.java b/duniter4j-core-shared/src/main/java/org/duniter/core/test/TestResource.java index 4bd2475e..367c7595 100644 --- a/duniter4j-core-shared/src/main/java/org/duniter/core/test/TestResource.java +++ b/duniter4j-core-shared/src/main/java/org/duniter/core/test/TestResource.java @@ -23,7 +23,7 @@ package org.duniter.core.test; */ -import com.google.common.base.Preconditions; +import org.duniter.core.util.Preconditions; import org.duniter.core.util.FileUtils; import org.duniter.core.util.StringUtils; import org.abstractj.kalium.NaCl; diff --git a/duniter4j-core-shared/src/main/java/org/duniter/core/util/ObjectUtils.java b/duniter4j-core-shared/src/main/java/org/duniter/core/util/ObjectUtils.java index 533ea93a..8f4b175c 100644 --- a/duniter4j-core-shared/src/main/java/org/duniter/core/util/ObjectUtils.java +++ b/duniter4j-core-shared/src/main/java/org/duniter/core/util/ObjectUtils.java @@ -28,29 +28,24 @@ package org.duniter.core.util; */ public class ObjectUtils { + @Deprecated public static void checkNotNull(Object value) { - if (value == null) { - throw new NullPointerException(); - } + Preconditions.checkNotNull(value); } + @Deprecated public static void checkNotNull(Object value, String message) { - if (value == null) { - throw new NullPointerException(message); - } + Preconditions.checkNotNull(value, message); } - + @Deprecated public static void checkArgument(boolean value, String message) { - if (!value) { - throw new IllegalArgumentException(message); - } + Preconditions.checkNotNull(value, message); } + @Deprecated public static void checkArgument(boolean value) { - if (!value) { - throw new IllegalArgumentException(); - } + Preconditions.checkNotNull(value); } public static boolean equals(Object o1, Object o2) { diff --git a/duniter4j-core-shared/src/main/java/org/duniter/core/util/Preconditions.java b/duniter4j-core-shared/src/main/java/org/duniter/core/util/Preconditions.java new file mode 100644 index 00000000..bf7868d1 --- /dev/null +++ b/duniter4j-core-shared/src/main/java/org/duniter/core/util/Preconditions.java @@ -0,0 +1,55 @@ +package org.duniter.core.util; + +/* + * #%L + * UCoin Java Client :: Core API + * %% + * Copyright (C) 2014 - 2015 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% + */ + + +/** + * Created by eis on 22/12/14. + */ +public class Preconditions { + + public static void checkNotNull(Object value) { + if (value == null) { + throw new NullPointerException(); + } + } + + public static void checkNotNull(Object value, String message) { + if (value == null) { + throw new NullPointerException(message); + } + } + + + public static void checkArgument(boolean value, String message) { + if (!value) { + throw new IllegalArgumentException(message); + } + } + + public static void checkArgument(boolean value) { + if (!value) { + throw new IllegalArgumentException(); + } + } +} diff --git a/duniter4j-core-shared/src/main/java/org/duniter/core/util/websocket/WebsocketClientEndpoint.java b/duniter4j-core-shared/src/main/java/org/duniter/core/util/websocket/WebsocketClientEndpoint.java index 3f2bea6e..6a58a068 100644 --- a/duniter4j-core-shared/src/main/java/org/duniter/core/util/websocket/WebsocketClientEndpoint.java +++ b/duniter4j-core-shared/src/main/java/org/duniter/core/util/websocket/WebsocketClientEndpoint.java @@ -195,7 +195,7 @@ public class WebsocketClientEndpoint implements Closeable { } catch (Exception e) { notifyConnectionError(e); if (!this.autoReconnect) throw new TechnicalException(e); - log.warn(String.format("[%s] Unable to connect [%s]. Retrying in 10s...", endpointURI.toString(), e.getMessage())); + log.warn(String.format("[%s] Unable to connect [%s]. Retrying in 10s...", endpointURI.toString(), e.getMessage()), e); } // wait 10s, then try again diff --git a/duniter4j-es-assembly/pom.xml b/duniter4j-es-assembly/pom.xml index d6b31ac0..3d127216 100644 --- a/duniter4j-es-assembly/pom.xml +++ b/duniter4j-es-assembly/pom.xml @@ -26,6 +26,50 @@ <assembly.skip>false</assembly.skip> </properties> + <dependencies> + <dependency> + <groupId>javax.websocket</groupId> + <artifactId>javax.websocket-api</artifactId> + <version>1.1</version> + </dependency> + <dependency> + <groupId>org.glassfish.tyrus</groupId> + <artifactId>tyrus-client</artifactId> + <version>${tyrus.version}</version> + </dependency> + <dependency> + <groupId>org.glassfish.tyrus</groupId> + <artifactId>tyrus-server</artifactId> + <version>${tyrus.version}</version> + </dependency> + <dependency> + <groupId>org.glassfish.tyrus</groupId> + <artifactId>tyrus-container-grizzly-client</artifactId> + <version>${tyrus.version}</version> + </dependency> + <dependency> + <groupId>org.glassfish.tyrus</groupId> + <artifactId>tyrus-container-grizzly-server</artifactId> + <version>${tyrus.version}</version> + </dependency> + <dependency> + <groupId>net.java.dev.jna</groupId> + <artifactId>jna</artifactId> + <version>${jna.version}</version> + </dependency> + <dependency> + <groupId>net.java.dev.jna</groupId> + <artifactId>jna-platform</artifactId> + <version>${jna.version}</version> + <exclusions> + <exclusion> + <groupId>net.java.dev.jna</groupId> + <artifactId>jna</artifactId> + </exclusion> + </exclusions> + </dependency> + </dependencies> + <build> <plugins> @@ -267,6 +311,22 @@ <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> + <dependency> + <groupId>net.java.dev.jna</groupId> + <artifactId>jna</artifactId> + <version>${jna.version}</version> + </dependency> + <dependency> + <groupId>net.java.dev.jna</groupId> + <artifactId>jna-platform</artifactId> + <version>${jna.version}</version> + <exclusions> + <exclusion> + <groupId>net.java.dev.jna</groupId> + <artifactId>jna</artifactId> + </exclusion> + </exclusions> + </dependency> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> @@ -279,29 +339,18 @@ </dependency> <dependency> <groupId>org.glassfish.tyrus</groupId> - <artifactId>tyrus-server</artifactId> + <artifactId>tyrus-container-grizzly-client</artifactId> <version>${tyrus.version}</version> </dependency> <dependency> <groupId>org.glassfish.tyrus</groupId> - <artifactId>tyrus-container-grizzly-client</artifactId> + <artifactId>tyrus-server</artifactId> <version>${tyrus.version}</version> </dependency> <dependency> - <groupId>net.java.dev.jna</groupId> - <artifactId>jna</artifactId> - <version>${jna.version}</version> - </dependency> - <dependency> - <groupId>net.java.dev.jna</groupId> - <artifactId>jna-platform</artifactId> - <version>${jna.version}</version> - <exclusions> - <exclusion> - <groupId>net.java.dev.jna</groupId> - <artifactId>jna</artifactId> - </exclusion> - </exclusions> + <groupId>org.glassfish.tyrus</groupId> + <artifactId>tyrus-container-grizzly-server</artifactId> + <version>${tyrus.version}</version> </dependency> </dependencies> <executions> diff --git a/duniter4j-es-assembly/src/main/assembly/standalone.xml b/duniter4j-es-assembly/src/main/assembly/standalone.xml index e9d6a6ee..be7c8367 100644 --- a/duniter4j-es-assembly/src/main/assembly/standalone.xml +++ b/duniter4j-es-assembly/src/main/assembly/standalone.xml @@ -62,17 +62,29 @@ <include>logging.yml</include> </includes> </fileSet> + + <!-- websocket lib (tyrus ) + <fileSet> + <directory>target/elasticsearch-${elasticsearch.version}</directory> + <outputDirectory>lib</outputDirectory> + <includes> + <include>elasticsearch.yml</include> + <include>logging.yml</include> + </includes> + </fileSet>--> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> - <useProjectArtifact>true</useProjectArtifact> + <useProjectArtifact>false</useProjectArtifact> <useTransitiveFiltering>true</useTransitiveFiltering> <includes> <include>javax.websocket:javax.websocket-api</include> <include>org.glassfish.tyrus:tyrus-client</include> <include>org.glassfish.tyrus:tyrus-container-grizzly-client</include> + <include>org.glassfish.tyrus:tyrus-server</include> + <include>org.glassfish.tyrus:tyrus-container-grizzly-server</include> </includes> <fileMode>0555</fileMode> </dependencySet> diff --git a/duniter4j-es-assembly/src/test/es-home/config/logging.yml b/duniter4j-es-assembly/src/test/es-home/config/logging.yml index ec1d38e7..6b4d5cab 100644 --- a/duniter4j-es-assembly/src/test/es-home/config/logging.yml +++ b/duniter4j-es-assembly/src/test/es-home/config/logging.yml @@ -24,7 +24,7 @@ logger: duniter : DEBUG duniter.security : ERROR duniter.user.event : INFO - duniter.network.p2p: TRACE + duniter.network.p2p: INFO security: DEBUG diff --git a/duniter4j-es-core/pom.xml b/duniter4j-es-core/pom.xml index ef6efaa2..96fa90a8 100644 --- a/duniter4j-es-core/pom.xml +++ b/duniter4j-es-core/pom.xml @@ -33,8 +33,21 @@ <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </exclusion> + <exclusion> + <groupId>org.glassfish.tyrus</groupId> + <artifactId>tyrus-client</artifactId> + </exclusion> + <exclusion> + <groupId>org.glassfish.tyrus</groupId> + <artifactId>tyrus-container-grizzly-client</artifactId> + </exclusion> + <exclusion> + <groupId>javax.websocket</groupId> + <artifactId>javax.websocket-api</artifactId> + </exclusion> </exclusions> </dependency> + <!-- LOGGING DEPENDENCIES - SLF4J --> <dependency> <groupId>org.slf4j</groupId> @@ -67,11 +80,12 @@ <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> - <scope>compile</scope> + <scope>provided</scope> </dependency> <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna-platform</artifactId> + <scope>provided</scope> <exclusions> <exclusion> <groupId>net.java.dev.jna</groupId> @@ -79,13 +93,22 @@ </exclusion> </exclusions> </dependency> + + <!-- Websocket --> + <dependency> + <groupId>javax.websocket</groupId> + <artifactId>javax.websocket-api</artifactId> + <scope>provided</scope> + </dependency> <dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-server</artifactId> + <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-container-grizzly-server</artifactId> + <scope>provided</scope> </dependency> <!-- Unit test --> diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/PluginInit.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/PluginInit.java index 110b3a0d..b8dba6ff 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/PluginInit.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/PluginInit.java @@ -47,7 +47,6 @@ public class PluginInit extends AbstractLifecycleComponent<PluginInit> { private final ThreadPool threadPool; private final Injector injector; private final static ESLogger logger = Loggers.getLogger("duniter.core"); - private final Client client; @Inject public PluginInit(Client client, Settings settings, PluginSettings pluginSettings, ThreadPool threadPool, final Injector injector) { @@ -55,7 +54,6 @@ public class PluginInit extends AbstractLifecycleComponent<PluginInit> { this.pluginSettings = pluginSettings; this.threadPool = threadPool; this.injector = injector; - this.client = client; } @Override diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/PluginSettings.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/PluginSettings.java index 5fc11369..7b1cd329 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/PluginSettings.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/PluginSettings.java @@ -32,6 +32,7 @@ import org.duniter.core.client.config.ConfigurationProvider; import org.duniter.core.client.model.local.Peer; import org.duniter.core.exception.TechnicalException; import org.duniter.core.util.StringUtils; +import org.duniter.elasticsearch.i18n.I18nInitializer; import org.duniter.elasticsearch.service.ServiceLocator; import org.elasticsearch.common.component.*; import org.elasticsearch.common.inject.Inject; @@ -48,6 +49,8 @@ import org.nuiton.i18n.init.UserI18nInitializer; import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; import java.util.Set; @@ -62,6 +65,8 @@ public class PluginSettings extends AbstractLifecycleComponent<PluginSettings> { protected final Settings settings; + private List<String> i18nBundleNames = new ArrayList<>(); // Default + /** * Delegate application config. */ @@ -76,9 +81,11 @@ public class PluginSettings extends AbstractLifecycleComponent<PluginSettings> { this.applicationConfig = new ApplicationConfig(); // Cascade the application config to the client module - clientConfig = new org.duniter.core.client.config.Configuration(applicationConfig); + clientConfig = new org.duniter.core.client.config.Configuration(this.applicationConfig); Configuration.setInstance(clientConfig); + // Set the default bundle name + addI18nBundleName(getI18nBundleName()); } @Override @@ -134,6 +141,8 @@ public class PluginSettings extends AbstractLifecycleComponent<PluginSettings> { catch(IOException e) { logger.error(String.format("Could not init i18n: %s", e.getMessage()), e); } + + initVersion(applicationConfig); } @Override @@ -250,38 +259,6 @@ public class PluginSettings extends AbstractLifecycleComponent<PluginSettings> { return settings.getAsInt("duniter.data.sync.port", 80); } - public boolean getMailEnable() { - return settings.getAsBoolean("duniter.mail.enable", Boolean.TRUE); - } - - public String getMailSmtpHost() { - return settings.get("duniter.mail.smtp.host", "localhost"); - } - - public int getMailSmtpPort() { - return settings.getAsInt("duniter.mail.smtp.port", 25); - } - - public String getMailSmtpUsername() { - return settings.get("duniter.mail.smtp.username"); - } - - public String getMailSmtpPassword() { - return settings.get("duniter.mail.smtp.password"); - } - - public String getMailAdmin() { - return settings.get("duniter.mail.admin"); - } - - public String getMailFrom() { - return settings.get("duniter.mail.from", "no-reply@duniter.fr"); - } - - public String getMailSubjectPrefix() { - return settings.get("duniter.mail.subject.prefix", "[Duniter4j ES]"); - } - public String getWebSocketHost() { return settings.get("network.host", "locahost"); } @@ -326,12 +303,35 @@ public class PluginSettings extends AbstractLifecycleComponent<PluginSettings> { i18nLocale, i18nDirectory)); } - I18n.init(new UserI18nInitializer( - i18nDirectory, new DefaultI18nInitializer(getI18nBundleName())), + I18n.init(new I18nInitializer(i18nDirectory, getI18nBundleNames()), i18nLocale); } protected String getI18nBundleName() { return "duniter4j-es-core-i18n"; } + + protected String[] getI18nBundleNames() { + return i18nBundleNames.toArray(new String[i18nBundleNames.size()]); + } + + public void addI18nBundleName(String i18nBundleName) { + if (!this.i18nBundleNames.contains(i18nBundleName)) { + this.i18nBundleNames.add(i18nBundleName); + } + } + + /** + * Override the version default option, from the MANIFEST implementation version (if any) + * @param applicationConfig + */ + protected void initVersion(ApplicationConfig applicationConfig) { + // Override application version + String implementationVersion = this.getClass().getPackage().getSpecificationVersion(); + if (implementationVersion != null) { + applicationConfig.setDefaultOption( + ConfigurationOption.VERSION.getKey(), + implementationVersion); + } + } } diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/i18n/I18nInitializer.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/i18n/I18nInitializer.java new file mode 100644 index 00000000..6517a675 --- /dev/null +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/i18n/I18nInitializer.java @@ -0,0 +1,72 @@ +package org.duniter.elasticsearch.i18n; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.i18n.bundle.I18nBundle; +import org.nuiton.i18n.init.DefaultI18nInitializer; +import org.nuiton.i18n.init.UserI18nInitializer; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by blavenie on 10/01/17. + */ +public class I18nInitializer extends org.nuiton.i18n.init.I18nInitializer{ + private static final Log log = LogFactory.getLog(UserI18nInitializer.class); + protected final File userDirectory; + + private String[] bundleNames; + private String i18nPath; + private List<UserI18nInitializer> delegates; + + + public I18nInitializer(File userDirectory, String[] bundleNames) throws NullPointerException { + this((String)null, userDirectory, bundleNames); + } + + public I18nInitializer(String i18nPath, File userDirectory, String[] bundleNames) throws NullPointerException { + super(); + + this.i18nPath = i18nPath; + this.bundleNames = bundleNames; + this.userDirectory = userDirectory; + this.delegates = createDelegates(userDirectory, bundleNames); + + if(userDirectory == null) { + throw new NullPointerException("parameter \'userDirectory\' can not be null"); + } + } + + public File getUserDirectory() { + return this.userDirectory; + } + + + @Override + public I18nBundle[] resolvBundles() throws Exception { + + List<I18nBundle> result = new ArrayList<>(); + for(DefaultI18nInitializer delegate: delegates) { + I18nBundle[] bundles = delegate.resolvBundles(); + for(I18nBundle bundle: bundles) { + result.add(bundle); + } + } + + return result.toArray(new I18nBundle[result.size()]); + } + + /* -- private methods -- */ + + private List<UserI18nInitializer> createDelegates(File userDirectory, String[] bundleNames) { + List<UserI18nInitializer> result = new ArrayList<>(); + for(String bundleName: bundleNames) { + UserI18nInitializer delegate = new UserI18nInitializer(userDirectory, new DefaultI18nInitializer(bundleName)); + result.add(delegate); + } + return result; + } + +} diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/RestModule.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/RestModule.java index 2e3050d0..7957f63d 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/RestModule.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/RestModule.java @@ -24,6 +24,7 @@ package org.duniter.elasticsearch.rest; import org.duniter.elasticsearch.rest.attachment.RestImageAttachmentAction; import org.duniter.elasticsearch.rest.currency.RestCurrencyIndexAction; +import org.duniter.elasticsearch.rest.node.RestNodeSummaryGetAction; import org.duniter.elasticsearch.rest.security.RestSecurityAuthAction; import org.duniter.elasticsearch.rest.security.RestSecurityController; import org.duniter.elasticsearch.rest.security.RestSecurityFilter; @@ -35,11 +36,8 @@ public class RestModule extends AbstractModule implements Module { @Override protected void configure() { - // Attachment - bind(RestImageAttachmentAction.class).asEagerSingleton(); - - // Currency - bind(RestCurrencyIndexAction.class).asEagerSingleton(); + // Common + bind(RestNodeSummaryGetAction.class).asEagerSingleton(); // Authentication & Security bind(RestSecurityGetChallengeAction.class).asEagerSingleton(); @@ -47,5 +45,11 @@ public class RestModule extends AbstractModule implements Module { bind(RestSecurityFilter.class).asEagerSingleton(); bind(RestSecurityController.class).asEagerSingleton(); + // Attachment as image + bind(RestImageAttachmentAction.class).asEagerSingleton(); + + // Currency + bind(RestCurrencyIndexAction.class).asEagerSingleton(); + } } \ No newline at end of file diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/currency/RestCurrencyIndexAction.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/currency/RestCurrencyIndexAction.java index 100df9ec..d10d70e3 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/currency/RestCurrencyIndexAction.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/currency/RestCurrencyIndexAction.java @@ -22,33 +22,26 @@ package org.duniter.elasticsearch.rest.currency; * #L% */ +import org.duniter.elasticsearch.rest.AbstractRestPostIndexAction; +import org.duniter.elasticsearch.rest.security.RestSecurityController; +import org.duniter.elasticsearch.service.CurrencyService; 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.RestStatus.OK; +import org.elasticsearch.rest.RestController; /** * A rest to post a request to process a new currency/peer. * - * TODO : - * - add security, to allow only request from admin (check signature against settings keyring) */ -public class RestCurrencyIndexAction extends BaseRestHandler { +public class RestCurrencyIndexAction extends AbstractRestPostIndexAction { @Inject - public RestCurrencyIndexAction(Settings settings, RestController controller, Client client) { - super(settings, controller, client); - controller.registerHandler(RestRequest.Method.POST, "/currency", this); - } - - @Override - protected void handleRequest(RestRequest restRequest, RestChannel restChannel, Client client) throws Exception { - String json = restRequest.content().toUtf8(); - //ServiceLocator.instance().getRegistryCurrencyIndexerService().indexCurrency(); - String currencyName = ""; - restChannel.sendResponse(new BytesRestResponse(OK, currencyName)); + public RestCurrencyIndexAction(Settings settings, RestController controller, Client client, + RestSecurityController securityController, CurrencyService currencyService) { + super(settings, controller, client, securityController, + CurrencyService.INDEX, CurrencyService.RECORD_TYPE, + (json) -> currencyService.indexCurrencyFromJson(json)); } } \ No newline at end of file diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/node/RestNodeSummaryGetAction.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/node/RestNodeSummaryGetAction.java new file mode 100644 index 00000000..d90a5501 --- /dev/null +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/node/RestNodeSummaryGetAction.java @@ -0,0 +1,83 @@ +package org.duniter.elasticsearch.rest.node; + +/* + * #%L + * duniter4j-elasticsearch-plugin + * %% + * 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 org.duniter.core.client.config.Configuration; +import org.duniter.core.exception.TechnicalException; +import org.duniter.elasticsearch.rest.AbstractRestPostIndexAction; +import org.duniter.elasticsearch.rest.XContentRestResponse; +import org.duniter.elasticsearch.rest.XContentThrowableRestResponse; +import org.duniter.elasticsearch.rest.security.RestSecurityController; +import org.duniter.elasticsearch.service.CurrencyService; +import org.elasticsearch.client.Client; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.rest.*; + +import java.io.IOException; + +/** + * A rest to post a request to process a new currency/peer. + * + */ +public class RestNodeSummaryGetAction extends BaseRestHandler { + + @Inject + public RestNodeSummaryGetAction(Settings settings, RestController controller, Client client, RestSecurityController securityController) { + super(settings, controller, client); + securityController.allow(RestRequest.Method.GET, "/node/summary"); + controller.registerHandler(RestRequest.Method.GET, "/node/summary", this); + } + + @Override + protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception { + XContentBuilder content = createSummary(); + channel.sendResponse(new XContentRestResponse(request, RestStatus.OK, content)); + } + + + public XContentBuilder createSummary() { + try { + XContentBuilder mapping = XContentFactory.jsonBuilder().startObject() + .startObject("duniter") + + // software + .field("software", "duniter4j-elasticsearch") + + // version + .field("version", Configuration.instance().getVersion().toString()) + + // status + .field("status", RestStatus.OK.getStatus()) + + .endObject().endObject(); + + return mapping; + } + catch(IOException ioe) { + throw new TechnicalException(String.format("Error while generating JSON for [/node/summary]: %s", ioe.getMessage()), ioe); + } + } +} \ No newline at end of file diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/security/challenge/ChallengeMessageStore.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/security/challenge/ChallengeMessageStore.java index 4a918717..2847036d 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/security/challenge/ChallengeMessageStore.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/security/challenge/ChallengeMessageStore.java @@ -22,7 +22,7 @@ package org.duniter.elasticsearch.security.challenge; * #L% */ -import com.google.common.base.Preconditions; +import org.duniter.core.util.Preconditions; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/security/token/SecurityTokenStore.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/security/token/SecurityTokenStore.java index bfaee8c3..65bd3da5 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/security/token/SecurityTokenStore.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/security/token/SecurityTokenStore.java @@ -22,7 +22,7 @@ package org.duniter.elasticsearch.security.token; * #L% */ -import com.google.common.base.Preconditions; +import org.duniter.core.util.Preconditions; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/AbstractService.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/AbstractService.java index 487f18ca..93b705e0 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/AbstractService.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/AbstractService.java @@ -24,19 +24,19 @@ package org.duniter.elasticsearch.service; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Joiner; -import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; -import com.google.gson.JsonSyntaxException; import org.apache.commons.collections4.MapUtils; -import org.apache.commons.lang3.concurrent.ConcurrentException; import org.duniter.core.beans.Bean; +import org.duniter.core.client.model.bma.jackson.JacksonUtils; import org.duniter.core.client.model.elasticsearch.Record; import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; import org.duniter.core.util.CollectionUtils; +import org.duniter.core.util.Preconditions; import org.duniter.core.util.StringUtils; import org.duniter.elasticsearch.PluginSettings; import org.duniter.elasticsearch.exception.AccessDeniedException; @@ -62,8 +62,6 @@ import org.elasticsearch.client.Requests; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHitField; @@ -71,7 +69,6 @@ import org.nuiton.i18n.I18n; import java.io.*; import java.util.*; -import java.util.concurrent.ExecutionException; import java.util.regex.Pattern; /** @@ -79,9 +76,6 @@ import java.util.regex.Pattern; */ public abstract class AbstractService implements Bean { - protected static final String JSON_STRING_PROPERTY_REGEX = "[,]?[\"\\s\\n\\r]*%s[\"]?[\\s\\n\\r]*:[\\s\\n\\r]*\"[^\"]+\""; - protected static final String REGEX_WORD_SEPARATOR = "[-\\t@# _]+"; - protected static final String REGEX_SPACE = "[\\t\\n\\r ]+"; protected final ESLogger logger; protected final Client client; @@ -184,34 +178,30 @@ public abstract class AbstractService implements Bean { readAndVerifyIssuerSignature(recordJson, actualObj); return actualObj; } - catch(IOException | JsonSyntaxException e) { + catch(IOException e) { throw new InvalidFormatException("Invalid record JSON: " + e.getMessage(), e); } } protected void readAndVerifyIssuerSignature(String recordJson, JsonNode actualObj) throws ElasticsearchException { - try { - Set<String> fieldNames = ImmutableSet.copyOf(actualObj.fieldNames()); - if (!fieldNames.contains(Record.PROPERTY_ISSUER) - || !fieldNames.contains(Record.PROPERTY_SIGNATURE)) { - throw new InvalidFormatException(String.format("Invalid record JSON format. Required fields [%s,%s]", Record.PROPERTY_ISSUER, Record.PROPERTY_SIGNATURE)); - } - String issuer = getMandatoryField(actualObj, Record.PROPERTY_ISSUER).asText(); - String signature = getMandatoryField(actualObj, Record.PROPERTY_SIGNATURE).asText(); - - String recordNoSign = recordJson.replaceAll(String.format(JSON_STRING_PROPERTY_REGEX, Record.PROPERTY_SIGNATURE), "") - .replaceAll(String.format(JSON_STRING_PROPERTY_REGEX, Record.PROPERTY_HASH), ""); + Set<String> fieldNames = ImmutableSet.copyOf(actualObj.fieldNames()); + if (!fieldNames.contains(Record.PROPERTY_ISSUER) + || !fieldNames.contains(Record.PROPERTY_SIGNATURE)) { + throw new InvalidFormatException(String.format("Invalid record JSON format. Required fields [%s,%s]", Record.PROPERTY_ISSUER, Record.PROPERTY_SIGNATURE)); + } + String issuer = getMandatoryField(actualObj, Record.PROPERTY_ISSUER).asText(); + String signature = getMandatoryField(actualObj, Record.PROPERTY_SIGNATURE).asText(); - if (!cryptoService.verify(recordNoSign, signature, issuer)) { - throw new InvalidSignatureException("Invalid signature of JSON string"); - } + // Remove hash and signature + recordJson = JacksonUtils.removeAttribute(recordJson, Record.PROPERTY_SIGNATURE); + recordJson = JacksonUtils.removeAttribute(recordJson, Record.PROPERTY_HASH); - // TODO: check issuer is in the WOT ? - } - catch(JsonSyntaxException e) { - throw new InvalidFormatException("Invalid record JSON: " + e.getMessage(), e); + if (!cryptoService.verify(recordJson, signature, issuer)) { + throw new InvalidSignatureException("Invalid signature of JSON string"); } + + // TODO: check issuer is in the WOT ? } protected void checkSameDocumentIssuer(String index, String type, String id, String expectedIssuer) throws ElasticsearchException { @@ -305,7 +295,7 @@ public abstract class AbstractService implements Bean { } return result; } - catch(SearchPhaseExecutionException | JsonSyntaxException e) { + catch(SearchPhaseExecutionException e) { // Failed or no item on index throw new TechnicalException(String.format("[%s/%s] Unable to retrieve fields [%s] for id [%s]", index, type, @@ -347,7 +337,7 @@ public abstract class AbstractService implements Bean { } return result; } - catch(SearchPhaseExecutionException | JsonSyntaxException e) { + catch(SearchPhaseExecutionException e) { // Failed or no item on index throw new TechnicalException(String.format("[%s/%s] Unable to retrieve field [%s] for ids [%s]", index, type, fieldName, @@ -424,7 +414,7 @@ public abstract class AbstractService implements Bean { } return null; } - catch(SearchPhaseExecutionException | JsonSyntaxException | IOException e) { + catch(SearchPhaseExecutionException | IOException e) { // Failed to get source throw new TechnicalException(String.format("[%s/%s] Error while getting [%s]", index, type, diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/AbstractSynchroService.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/AbstractSynchroService.java index 5ebf469a..dca7183e 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/AbstractSynchroService.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/AbstractSynchroService.java @@ -24,7 +24,7 @@ package org.duniter.elasticsearch.service; import com.fasterxml.jackson.databind.JsonNode; import com.google.common.base.Joiner; -import com.google.common.base.Preconditions; +import org.duniter.core.util.Preconditions; import org.apache.commons.io.IOUtils; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ByteArrayEntity; diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/BlockchainService.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/BlockchainService.java index 59814fd8..7c765a24 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/BlockchainService.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/BlockchainService.java @@ -31,7 +31,6 @@ import com.google.common.collect.Lists; import org.duniter.core.client.model.bma.BlockchainBlock; import org.duniter.core.client.model.bma.BlockchainParameters; import org.duniter.core.client.model.bma.EndpointProtocol; -import org.duniter.core.client.model.bma.gson.GsonUtils; import org.duniter.core.client.model.bma.gson.JsonAttributeParser; import org.duniter.core.client.model.bma.jackson.JacksonUtils; import org.duniter.core.client.model.local.Peer; @@ -44,7 +43,7 @@ import org.duniter.core.model.NullProgressionModel; import org.duniter.core.model.ProgressionModel; import org.duniter.core.model.ProgressionModelImpl; import org.duniter.core.util.CollectionUtils; -import org.duniter.core.util.ObjectUtils; +import org.duniter.core.util.Preconditions; import org.duniter.core.util.StringUtils; import org.duniter.core.util.websocket.WebsocketClientEndpoint; import org.duniter.elasticsearch.PluginSettings; @@ -298,9 +297,9 @@ public class BlockchainService extends AbstractService { } public void createBlock(BlockchainBlock block) throws org.duniter.elasticsearch.exception.DuplicateIndexIdException { - ObjectUtils.checkNotNull(block, "block could not be null") ; - ObjectUtils.checkNotNull(block.getCurrency(), "block attribute 'blockchain' could not be null"); - ObjectUtils.checkNotNull(block.getNumber(), "block attribute 'number' could not be null"); + Preconditions.checkNotNull(block, "block could not be null") ; + Preconditions.checkNotNull(block.getCurrency(), "block attribute 'blockchain' could not be null"); + Preconditions.checkNotNull(block.getNumber(), "block attribute 'number' could not be null"); BlockchainBlock existingBlock = getBlockById(block.getCurrency(), block.getNumber()); if (existingBlock != null) { @@ -318,10 +317,10 @@ public class BlockchainService extends AbstractService { * @throws DuplicateIndexIdException */ public void saveBlock(BlockchainBlock block, boolean updateWhenSameHash, boolean wait) throws DuplicateIndexIdException { - ObjectUtils.checkNotNull(block, "block could not be null") ; - ObjectUtils.checkNotNull(block.getCurrency(), "block attribute 'blockchain' could not be null"); - ObjectUtils.checkNotNull(block.getNumber(), "block attribute 'number' could not be null"); - ObjectUtils.checkNotNull(block.getHash(), "block attribute 'hash' could not be null"); + Preconditions.checkNotNull(block, "block could not be null") ; + Preconditions.checkNotNull(block.getCurrency(), "block attribute 'blockchain' could not be null"); + Preconditions.checkNotNull(block.getNumber(), "block attribute 'number' could not be null"); + Preconditions.checkNotNull(block.getHash(), "block attribute 'hash' could not be null"); BlockchainBlock existingBlock = getBlockById(block.getCurrency(), block.getNumber()); @@ -364,10 +363,10 @@ public class BlockchainService extends AbstractService { } public void indexBlock(BlockchainBlock block, boolean wait) { - ObjectUtils.checkNotNull(block); - ObjectUtils.checkArgument(StringUtils.isNotBlank(block.getCurrency())); - ObjectUtils.checkNotNull(block.getHash()); - ObjectUtils.checkNotNull(block.getNumber()); + Preconditions.checkNotNull(block); + Preconditions.checkArgument(StringUtils.isNotBlank(block.getCurrency())); + Preconditions.checkNotNull(block.getHash()); + Preconditions.checkNotNull(block.getNumber()); // Serialize into JSON // WARN: must use GSON, to have same JSON result (e.g identities and joiners field must be converted into String) @@ -402,8 +401,8 @@ public class BlockchainService extends AbstractService { * @param json block as JSON */ public BlockchainService indexBlockFromJson(String currencyName, int number, byte[] json, boolean refresh, boolean wait) { - ObjectUtils.checkNotNull(json); - ObjectUtils.checkArgument(json.length > 0); + Preconditions.checkNotNull(json); + Preconditions.checkArgument(json.length > 0); // Preparing indexBlocksFromNode IndexRequestBuilder indexRequest = client.prepareIndex(currencyName, BLOCK_TYPE) @@ -428,8 +427,8 @@ public class BlockchainService extends AbstractService { * @param json block as json */ public BlockchainService indexLastBlockFromJson(Peer peer, String json) { - ObjectUtils.checkNotNull(json); - ObjectUtils.checkArgument(json.length() > 0); + Preconditions.checkNotNull(json); + Preconditions.checkArgument(json.length() > 0); indexBlockFromJson(peer, json, true /*refresh*/, true /*is current*/, true/*check fork*/, true/*wait*/); @@ -443,8 +442,8 @@ public class BlockchainService extends AbstractService { * @param wait need to wait until processed ? */ public BlockchainService indexBlockFromJson(Peer peer, String json, boolean refresh, boolean isCurrent, boolean detectFork, boolean wait) { - ObjectUtils.checkNotNull(json); - ObjectUtils.checkArgument(json.length() > 0); + Preconditions.checkNotNull(json); + Preconditions.checkArgument(json.length() > 0); String currencyName = blockCurrencyParser.getValueAsString(json); int number = blockNumberParser.getValueAsInt(json); @@ -493,10 +492,10 @@ public class BlockchainService extends AbstractService { * @param currentBlock */ public void indexCurrentBlock(BlockchainBlock currentBlock, boolean wait) { - ObjectUtils.checkNotNull(currentBlock); - ObjectUtils.checkArgument(StringUtils.isNotBlank(currentBlock.getCurrency())); - ObjectUtils.checkNotNull(currentBlock.getHash()); - ObjectUtils.checkNotNull(currentBlock.getNumber()); + Preconditions.checkNotNull(currentBlock); + Preconditions.checkArgument(StringUtils.isNotBlank(currentBlock.getCurrency())); + Preconditions.checkNotNull(currentBlock.getHash()); + Preconditions.checkNotNull(currentBlock.getNumber()); // Serialize into JSON // WARN: must use GSON, to have same JSON result (e.g identities and joiners field must be converted into String) @@ -517,9 +516,9 @@ public class BlockchainService extends AbstractService { * @pram wait need to wait until block processed ? */ public void indexCurrentBlockFromJson(String currencyName, String json, boolean wait) { - ObjectUtils.checkNotNull(json); - ObjectUtils.checkArgument(json.length() > 0); - ObjectUtils.checkArgument(StringUtils.isNotBlank(currencyName)); + Preconditions.checkNotNull(json); + Preconditions.checkArgument(json.length() > 0); + Preconditions.checkArgument(StringUtils.isNotBlank(currencyName)); // Preparing indexBlocksFromNode IndexRequestBuilder indexRequest = client.prepareIndex(currencyName, BLOCK_TYPE) @@ -886,12 +885,12 @@ public class BlockchainService extends AbstractService { * @param tryCounter */ protected Collection<String> indexMissingBlocksFromOtherPeers(Peer peer, BlockchainBlock currentBlock, Collection<String> sortedMissingBlocks, int tryCounter) { - ObjectUtils.checkNotNull(peer); - ObjectUtils.checkNotNull(currentBlock); - ObjectUtils.checkNotNull(currentBlock.getHash()); - ObjectUtils.checkNotNull(currentBlock.getNumber()); - ObjectUtils.checkArgument(CollectionUtils.isNotEmpty(sortedMissingBlocks)); - ObjectUtils.checkArgument(tryCounter >= 1); + Preconditions.checkNotNull(peer); + Preconditions.checkNotNull(currentBlock); + Preconditions.checkNotNull(currentBlock.getHash()); + Preconditions.checkNotNull(currentBlock.getNumber()); + Preconditions.checkArgument(CollectionUtils.isNotEmpty(sortedMissingBlocks)); + Preconditions.checkArgument(tryCounter >= 1); NetworkRemoteService networkRemoteService = ServiceLocator.instance().getNetworkRemoteService(); BlockchainRemoteService blockchainRemoteService = ServiceLocator.instance().getBlockchainRemoteService(); diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/CurrencyService.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/CurrencyService.java index 3b7b23fc..d22d597a 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/CurrencyService.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/CurrencyService.java @@ -25,14 +25,10 @@ package org.duniter.elasticsearch.service; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.base.Preconditions; import com.google.common.collect.Lists; -import com.google.gson.Gson; -import com.google.gson.JsonSyntaxException; import org.apache.commons.lang3.ArrayUtils; import org.duniter.core.client.model.bma.BlockchainBlock; import org.duniter.core.client.model.bma.BlockchainParameters; -import org.duniter.core.client.model.bma.gson.GsonUtils; import org.duniter.core.client.model.bma.jackson.JacksonUtils; import org.duniter.core.client.model.elasticsearch.Currency; import org.duniter.core.client.model.local.Peer; @@ -40,13 +36,12 @@ import org.duniter.core.client.service.bma.BlockchainRemoteService; import org.duniter.core.client.service.exception.HttpConnectException; import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; -import org.duniter.core.util.ObjectUtils; +import org.duniter.core.util.Preconditions; import org.duniter.core.util.StringUtils; import org.duniter.elasticsearch.PluginSettings; import org.duniter.elasticsearch.exception.AccessDeniedException; import org.duniter.elasticsearch.exception.DuplicateIndexIdException; import org.duniter.elasticsearch.exception.InvalidSignatureException; -import org.duniter.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.search.SearchPhaseExecutionException; @@ -62,7 +57,6 @@ import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHitField; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.util.List; import java.util.Objects; @@ -71,8 +65,10 @@ import java.util.Objects; */ public class CurrencyService extends AbstractService { + protected static final String REGEX_WORD_SEPARATOR = "[-\\t@# _]+"; + public static final String INDEX = "currency"; - public static final String CURRENCY_TYPE = "record"; + public static final String RECORD_TYPE = "record"; private final ObjectMapper objectMapper; private BlockchainRemoteService blockchainRemoteService; @@ -116,7 +112,7 @@ public class CurrencyService extends AbstractService { //.put("analyzer", createDefaultAnalyzer()) .build(); createIndexRequestBuilder.setSettings(indexSettings); - createIndexRequestBuilder.addMapping(CURRENCY_TYPE, createCurrencyType()); + createIndexRequestBuilder.addMapping(RECORD_TYPE, createCurrencyType()); createIndexRequestBuilder.execute().actionGet(); return this; @@ -136,6 +132,19 @@ public class CurrencyService extends AbstractService { return !StringUtils.isEmpty(pubkey); } + + /** + * Add a new currency + * TODO : + * - add security, to allow only request from admin (check signature against settings keyring) + * + * @param json + * @return + */ + public String indexCurrencyFromJson(String json) { + throw new TechnicalException("Not implemented yet. Received JSON: " + json); + } + /** * Retrieve the blockchain data, from peer * @@ -194,7 +203,7 @@ public class CurrencyService extends AbstractService { */ public void indexCurrency(Currency currency) { try { - ObjectUtils.checkNotNull(currency.getCurrencyName()); + org.duniter.core.util.Preconditions.checkNotNull(currency.getCurrencyName()); // Fill tags if (ArrayUtils.isEmpty(currency.getTags())) { @@ -215,7 +224,7 @@ public class CurrencyService extends AbstractService { byte[] json = objectMapper.writeValueAsBytes(currency); // Preparing indexBlocksFromNode - IndexRequestBuilder indexRequest = client.prepareIndex(INDEX, CURRENCY_TYPE) + IndexRequestBuilder indexRequest = client.prepareIndex(INDEX, RECORD_TYPE) .setId(currency.getCurrencyName()) .setSource(json); @@ -260,8 +269,8 @@ public class CurrencyService extends AbstractService { * @throws AccessDeniedException if exists and user if not the original blockchain sender */ public void saveCurrency(Currency currency, String senderPubkey) throws DuplicateIndexIdException { - ObjectUtils.checkNotNull(currency, "currency could not be null") ; - ObjectUtils.checkNotNull(currency.getCurrencyName(), "currency attribute 'currencyName' could not be null"); + Preconditions.checkNotNull(currency, "currency could not be null") ; + Preconditions.checkNotNull(currency.getCurrencyName(), "currency attribute 'currencyName' could not be null"); String previousSenderPubkey = getSenderPubkeyByCurrencyId(currency.getCurrencyName()); @@ -306,7 +315,7 @@ public class CurrencyService extends AbstractService { throw new InvalidSignatureException("Bad signature"); } - Currency currency = null; + Currency currency; try { currency = objectMapper.readValue(jsonCurrency, Currency.class); Preconditions.checkNotNull(currency); @@ -323,7 +332,7 @@ public class CurrencyService extends AbstractService { public XContentBuilder createCurrencyType() { try { - XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject(CURRENCY_TYPE) + XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject(RECORD_TYPE) .startObject("properties") // blockchain name @@ -350,7 +359,7 @@ public class CurrencyService extends AbstractService { return mapping; } catch(IOException ioe) { - throw new TechnicalException(String.format("Error while getting mapping for index [%s/%s]: %s", INDEX, CURRENCY_TYPE, ioe.getMessage()), ioe); + throw new TechnicalException(String.format("Error while getting mapping for index [%s/%s]: %s", INDEX, RECORD_TYPE, ioe.getMessage()), ioe); } } @@ -368,7 +377,7 @@ public class CurrencyService extends AbstractService { // Prepare request SearchRequestBuilder searchRequest = client .prepareSearch(INDEX) - .setTypes(CURRENCY_TYPE) + .setTypes(RECORD_TYPE) .setSearchType(SearchType.QUERY_AND_FETCH); // If more than a word, search on terms match @@ -391,7 +400,7 @@ public class CurrencyService extends AbstractService { } } } - catch(SearchPhaseExecutionException | JsonSyntaxException | IOException e) { + catch(SearchPhaseExecutionException | IOException e) { // Failed or no item on index } diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/changes/ChangeEvent.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/changes/ChangeEvent.java index 4f68be7a..a3b07217 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/changes/ChangeEvent.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/changes/ChangeEvent.java @@ -40,7 +40,6 @@ package org.duniter.elasticsearch.service.changes; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.gson.JsonSyntaxException; import org.duniter.core.exception.TechnicalException; import org.duniter.elasticsearch.exception.InvalidFormatException; import org.elasticsearch.common.bytes.BytesReference; @@ -139,12 +138,12 @@ public class ChangeEvent { BytesReference source = null; if (sourceNode != null) { // TODO : fill bytes reference from source - //source = sourceNode. + //source = } ChangeEvent event = new ChangeEvent(index, type, id, timestamp, operation, version, source); return event; - } catch (IOException | JsonSyntaxException e) { + } catch (IOException e) { throw new InvalidFormatException("Invalid record JSON: " + e.getMessage(), e); } } diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/changes/ChangeService.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/changes/ChangeService.java index 2e078f3f..2faa3099 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/changes/ChangeService.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/changes/ChangeService.java @@ -38,7 +38,7 @@ package org.duniter.elasticsearch.service.changes; limitations under the License. */ -import com.google.common.base.Preconditions; +import org.duniter.core.util.Preconditions; import org.duniter.core.util.CollectionUtils; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.logging.ESLogger; diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/changes/ChangeSource.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/changes/ChangeSource.java index bf21d9dc..3f9276d4 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/changes/ChangeSource.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/changes/ChangeSource.java @@ -39,7 +39,7 @@ package org.duniter.elasticsearch.service.changes; */ import com.google.common.base.Joiner; -import com.google.common.base.Preconditions; +import org.duniter.core.util.Preconditions; import com.google.common.collect.ImmutableSet; import org.duniter.core.util.StringUtils; diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/threadpool/ThreadPool.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/threadpool/ThreadPool.java index bc19dcca..4818cbf5 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/threadpool/ThreadPool.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/threadpool/ThreadPool.java @@ -22,7 +22,7 @@ package org.duniter.elasticsearch.threadpool; * #L% */ -import com.google.common.base.Preconditions; +import org.duniter.core.util.Preconditions; import com.google.common.collect.Lists; import org.elasticsearch.action.admin.cluster.stats.ClusterStatsRequestBuilder; import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse; diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/websocket/WebSocketServer.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/websocket/WebSocketServer.java index c0ac3524..fdb89c30 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/websocket/WebSocketServer.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/websocket/WebSocketServer.java @@ -54,6 +54,7 @@ import java.util.List; public class WebSocketServer { + public static final String WS_PATH = "/ws"; private final ESLogger log = Loggers.getLogger("duniter.ws"); private List<Class<?>> endPoints = new ArrayList<>(); @@ -84,10 +85,10 @@ public class WebSocketServer { private void startServer(String host, int port, Class<?>[] endPoints) { - final Server server = new Server(host, port, "/ws", null, endPoints) ; + final Server server = new Server(host, port, WS_PATH, null, endPoints) ; try { - log.info("Starting Websocket server..."); + log.info(String.format("Starting Websocket server... [%s:%s%s]", host, port, WS_PATH)); AccessController.doPrivileged(new PrivilegedAction() { @Override public Object run() { diff --git a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/PluginInit.java b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/PluginInit.java index 51b73d86..5b781ea3 100644 --- a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/PluginInit.java +++ b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/PluginInit.java @@ -26,6 +26,7 @@ import org.duniter.elasticsearch.gchange.service.MarketService; import org.duniter.elasticsearch.gchange.service.RegistryService; import org.duniter.elasticsearch.gchange.service.SynchroService; import org.duniter.elasticsearch.threadpool.ThreadPool; +import org.duniter.elasticsearch.user.PluginSettings; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.inject.Inject; @@ -58,9 +59,8 @@ public class PluginInit extends AbstractLifecycleComponent<PluginInit> { createIndices(); // Waiting cluster back to GREEN or YELLOW state, before synchronize - threadPool.scheduleOnClusterHealthStatus(() -> { - synchronize(); - }, ClusterHealthStatus.YELLOW, ClusterHealthStatus.GREEN); + threadPool.scheduleOnClusterHealthStatus(() -> synchronize(), + ClusterHealthStatus.YELLOW, ClusterHealthStatus.GREEN); }, ClusterHealthStatus.YELLOW, ClusterHealthStatus.GREEN); } diff --git a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/PluginSettings.java b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/PluginSettings.java index 48498ce0..826e938d 100644 --- a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/PluginSettings.java +++ b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/PluginSettings.java @@ -54,13 +54,124 @@ import static org.nuiton.i18n.I18n.t; * @author Benoit Lavenier <benoit.lavenier@e-is.pro> * @since 1.0 */ -public class PluginSettings extends org.duniter.elasticsearch.user.PluginSettings { +public class PluginSettings extends AbstractLifecycleComponent<PluginSettings> { + + private org.duniter.elasticsearch.user.PluginSettings delegate; @Inject - public PluginSettings(org.elasticsearch.common.settings.Settings settings) { + public PluginSettings(org.elasticsearch.common.settings.Settings settings, + org.duniter.elasticsearch.user.PluginSettings delegate) { super(settings); + this.delegate = delegate; + + // Add i18n bundle name + delegate.addI18nBundleName(getI18nBundleName()); + + } + + @Override + protected void doStart() { + + } + + @Override + protected void doStop() { + + } + + @Override + protected void doClose() { + + } + + public org.duniter.elasticsearch.user.PluginSettings getDelegate() { + return delegate; + } + + /* -- delegate methods -- */ + + public boolean reloadIndices() { + return delegate.reloadIndices(); + } + + public boolean enableDataSync() { + return delegate.enableDataSync(); + } + + public boolean getMailEnable() { + return delegate.getMailEnable(); + } + + public String getMailSmtpHost() { + return delegate.getMailSmtpHost(); + } + + public int getMailSmtpPort() { + return delegate.getMailSmtpPort(); + } + + public String getMailSmtpUsername() { + return delegate.getMailSmtpUsername(); + } + + public String getMailSmtpPassword() { + return delegate.getMailSmtpPassword(); + } + + public String getMailAdmin() { + return delegate.getMailAdmin(); + } + + public String getMailFrom() { + return delegate.getMailFrom(); + } + + public String getMailSubjectPrefix() { + return delegate.getMailSubjectPrefix(); + } + + public String getClusterName() { + return delegate.getClusterName(); + } + + public String getNodeBmaHost() { + return delegate.getNodeBmaHost(); + } + + public int getNodeBmaPort() { + return delegate.getNodeBmaPort(); + } + + public int getIndexBulkSize() { + return delegate.getIndexBulkSize(); + } + + public boolean enableBlockchainSync() { + return delegate.enableBlockchainSync(); + } + + public String getKeyringSalt() { + return delegate.getKeyringSalt(); + } + + public String getKeyringPassword() { + return delegate.getKeyringPassword(); } + public String getKeyringPublicKey() { + return delegate.getKeyringPublicKey(); + } + + public String getKeyringSecretKey() { + return delegate.getKeyringSecretKey(); + } + + public String getDefaultStringAnalyzer() { + return delegate.getDefaultStringAnalyzer(); + } + + /* -- protected methods -- */ + protected String getI18nBundleName() { return "duniter4j-es-gchange-i18n"; } diff --git a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/AbstractService.java b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/AbstractService.java new file mode 100644 index 00000000..87b5fb67 --- /dev/null +++ b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/AbstractService.java @@ -0,0 +1,31 @@ +package org.duniter.elasticsearch.gchange.service; + +import org.duniter.core.service.CryptoService; +import org.duniter.elasticsearch.gchange.PluginSettings; +import org.elasticsearch.client.Client; + +/** + * Created by blavenie on 10/01/17. + */ +public abstract class AbstractService extends org.duniter.elasticsearch.user.service.AbstractService { + + protected PluginSettings pluginSettings; + + public AbstractService(String loggerName, Client client, PluginSettings pluginSettings) { + this(loggerName, client, pluginSettings, null); + } + + public AbstractService(Client client, PluginSettings pluginSettings) { + this(client, pluginSettings, null); + } + + public AbstractService(Client client, PluginSettings pluginSettings, CryptoService cryptoService) { + this("duniter.gchange", client, pluginSettings, cryptoService); + } + + public AbstractService(String loggerName, Client client, PluginSettings pluginSettings, CryptoService cryptoService) { + super(loggerName, client, pluginSettings.getDelegate(), cryptoService); + this.pluginSettings = pluginSettings; + } + +} diff --git a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/CitiesRegistryService.java b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/CitiesRegistryService.java index e6f72a72..dadcc55a 100644 --- a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/CitiesRegistryService.java +++ b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/CitiesRegistryService.java @@ -24,14 +24,8 @@ package org.duniter.elasticsearch.gchange.service; import com.fasterxml.jackson.core.JsonProcessingException; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.duniter.core.client.model.bma.gson.GsonUtils; import org.duniter.core.exception.TechnicalException; -import org.duniter.core.util.StringUtils; -import org.duniter.elasticsearch.gchange.PluginSettings; +import org.duniter.elasticsearch.PluginSettings; import org.duniter.elasticsearch.service.AbstractService; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.client.Client; @@ -41,8 +35,7 @@ import org.elasticsearch.common.logging.ESLoggerFactory; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import java.io.*; -import java.util.Map; +import java.io.IOException; /** * Created by Benoit on 30/03/2015. @@ -57,15 +50,12 @@ public class CitiesRegistryService extends AbstractService { private static final String CITIES_SOURCE_FILE2 = "/home/blavenie/git/ucoin-io/duniter4j/duniter4j-elasticsearch/src/main/misc/geoflar-communes-2015.geojson"; - public static final String INDEX_NAME = "registry"; - public static final String INDEX_TYPE = "city"; - - private final Gson gson; + public static final String INDEX = "registry"; + public static final String CITY_TYPE = "city"; @Inject public CitiesRegistryService(Client client, PluginSettings settings) { super(client, settings); - gson = GsonUtils.newBuilder().create(); } /** @@ -73,12 +63,12 @@ public class CitiesRegistryService extends AbstractService { * @throws JsonProcessingException */ public void deleteIndex() throws JsonProcessingException { - deleteIndexIfExists(INDEX_NAME); + deleteIndexIfExists(INDEX); } public boolean existsIndex() { - return super.existsIndex(INDEX_NAME); + return super.existsIndex(INDEX); } /** @@ -86,12 +76,12 @@ public class CitiesRegistryService extends AbstractService { */ public void createIndexIfNotExists() { try { - if (!existsIndex(INDEX_NAME)) { + if (!existsIndex(INDEX)) { createIndex(); } } catch(JsonProcessingException e) { - throw new TechnicalException(String.format("Error while creating index [%s]", INDEX_NAME)); + throw new TechnicalException(String.format("Error while creating index [%s]", INDEX)); } } @@ -100,16 +90,16 @@ public class CitiesRegistryService extends AbstractService { * @throws JsonProcessingException */ public void createIndex() throws JsonProcessingException { - log.info(String.format("Creating index [%s/%s]", INDEX_NAME, INDEX_TYPE)); + log.info(String.format("Creating index [%s/%s]", INDEX, CITY_TYPE)); - CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(INDEX_NAME); + CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(INDEX); org.elasticsearch.common.settings.Settings indexSettings = org.elasticsearch.common.settings.Settings.settingsBuilder() .put("number_of_shards", 1) .put("number_of_replicas", 1) //.put("analyzer", createDefaultAnalyzer()) .build(); createIndexRequestBuilder.setSettings(indexSettings); - createIndexRequestBuilder.addMapping(INDEX_TYPE, createIndexMapping()); + createIndexRequestBuilder.addMapping(CITY_TYPE, createIndexMapping()); createIndexRequestBuilder.execute().actionGet(); } @@ -121,7 +111,7 @@ public class CitiesRegistryService extends AbstractService { //File bulkFile = createCitiesBulkFile2(); // Insert cities - //bulkFromFile(bulkFile, INDEX_NAME, INDEX_TYPE); + //bulkFromFile(bulkFile, INDEX, CITY_TYPE); } @@ -131,7 +121,7 @@ public class CitiesRegistryService extends AbstractService { public XContentBuilder createIndexMapping() { try { - XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject(INDEX_TYPE) + XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject(CITY_TYPE) .startObject("properties") // city @@ -151,10 +141,11 @@ public class CitiesRegistryService extends AbstractService { return mapping; } catch(IOException ioe) { - throw new TechnicalException(String.format("Error while getting mapping for index [%s/%s]: %s", INDEX_NAME, INDEX_TYPE, ioe.getMessage()), ioe); + throw new TechnicalException(String.format("Error while getting mapping for index [%s/%s]: %s", INDEX, CITY_TYPE, ioe.getMessage()), ioe); } } + /* public File createCitiesBulkFile() { File result = new File(pluginSettings.getTempDirectory(), CITIES_BULK_FILENAME); @@ -355,4 +346,5 @@ public class CitiesRegistryService extends AbstractService { return result; } + */ } diff --git a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/CommentService.java b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/CommentService.java index 82da51d7..deadf771 100644 --- a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/CommentService.java +++ b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/CommentService.java @@ -32,9 +32,9 @@ import org.duniter.core.service.CryptoService; import org.duniter.elasticsearch.exception.DocumentNotFoundException; import org.duniter.elasticsearch.exception.NotFoundException; import org.duniter.elasticsearch.gchange.PluginSettings; -import org.duniter.elasticsearch.gchange.model.market.MarketRecord; import org.duniter.elasticsearch.gchange.model.event.GchangeEventCodes; -import org.duniter.elasticsearch.service.AbstractService; +import org.duniter.elasticsearch.gchange.model.market.MarketRecord; +import org.duniter.elasticsearch.gchange.service.AbstractService; import org.duniter.elasticsearch.threadpool.ThreadPool; import org.duniter.elasticsearch.user.model.UserEvent; import org.duniter.elasticsearch.user.service.HistoryService; diff --git a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/CommentUserEventService.java b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/CommentUserEventService.java index bb78a826..8ff57dbd 100644 --- a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/CommentUserEventService.java +++ b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/CommentUserEventService.java @@ -23,6 +23,8 @@ package org.duniter.elasticsearch.gchange.service; */ +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableList; import org.apache.commons.collections4.MapUtils; @@ -33,10 +35,10 @@ import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; import org.duniter.core.util.StringUtils; import org.duniter.core.util.websocket.WebsocketClientEndpoint; -import org.duniter.elasticsearch.PluginSettings; -import org.duniter.elasticsearch.gchange.model.market.MarketRecord; +import org.duniter.elasticsearch.gchange.PluginSettings; import org.duniter.elasticsearch.gchange.model.event.GchangeEventCodes; -import org.duniter.elasticsearch.service.AbstractService; +import org.duniter.elasticsearch.gchange.model.market.MarketRecord; +import org.duniter.elasticsearch.gchange.service.AbstractService; import org.duniter.elasticsearch.service.BlockchainService; import org.duniter.elasticsearch.service.changes.ChangeEvent; import org.duniter.elasticsearch.service.changes.ChangeService; @@ -50,7 +52,9 @@ import org.elasticsearch.common.inject.Inject; import org.nuiton.i18n.I18n; import java.io.IOException; -import java.util.*; +import java.util.Collection; +import java.util.List; +import java.util.Map; /** * Created by Benoit on 30/03/2015. @@ -83,6 +87,8 @@ public class CommentUserEventService extends AbstractService implements ChangeSe public final String recordType; + public final boolean trace; + @Inject public CommentUserEventService(Client client, PluginSettings settings, CryptoService cryptoService, BlockchainService blockchainService, @@ -92,12 +98,14 @@ public class CommentUserEventService extends AbstractService implements ChangeSe this.userService = userService; this.userEventService = userEventService; this.objectMapper = JacksonUtils.newObjectMapper(); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); this.changeListenSources = ImmutableList.of( new ChangeSource(MarketService.INDEX, MarketService.RECORD_COMMENT_TYPE), new ChangeSource(RegistryService.INDEX, RegistryService.RECORD_COMMENT_TYPE)); ChangeService.registerListener(this); this.enable = pluginSettings.enableBlockchainSync(); + this.trace = logger.isTraceEnabled(); this.recordType = MarketService.RECORD_TYPE; // same as RegistryService.RECORD_TYPE @@ -114,33 +122,28 @@ public class CommentUserEventService extends AbstractService implements ChangeSe @Override public void onChange(ChangeEvent change) { + RecordComment comment; - try { - switch (change.getOperation()) { - case CREATE: - if (change.getSource() != null) { - RecordComment comment = objectMapper.readValue(change.getSource().streamInput(), RecordComment.class); - processCreateComment(change.getIndex(), change.getType(), change.getId(), comment); - } - break; - case INDEX: - if (change.getSource() != null) { - RecordComment comment = objectMapper.readValue(change.getSource().streamInput(), RecordComment.class); - processUpdateComment(change.getIndex(), change.getType(), change.getId(), comment); - } - break; - - // on DELETE : remove user event on block (using link - case DELETE: - processCommentDelete(change); - - break; - } + switch (change.getOperation()) { + case CREATE: + comment = readComment(change); + if (comment != null) { + processCreateComment(change.getIndex(), change.getType(), change.getId(), comment); + } + break; + case INDEX: + comment = readComment(change); + if (comment != null) { + processUpdateComment(change.getIndex(), change.getType(), change.getId(), comment); + } + break; + // on DELETE : remove user event on block (using link + case DELETE: + processCommentDelete(change); + break; } - catch(IOException e) { - throw new TechnicalException(String.format("Unable to parse received comment %s", change.getId()), e); - } + } @Override @@ -203,8 +206,8 @@ public class CommentUserEventService extends AbstractService implements ChangeSe private void processCreateComment(String index, String type, String commentId, RecordComment comment) { processUpdateOrCreateComment(index, type, commentId, comment, - GchangeEventCodes.NEW_COMMENT, String.format("duniter.%s.event.newComment", index.toLowerCase()), - GchangeEventCodes.NEW_REPLY_COMMENT, String.format("duniter.%s.event.newReplyComment", index.toLowerCase())); + GchangeEventCodes.NEW_COMMENT, String.format("duniter.%s.event.newComment", index.toLowerCase()), + GchangeEventCodes.NEW_REPLY_COMMENT, String.format("duniter.%s.event.newReplyComment", index.toLowerCase())); } /** @@ -218,8 +221,8 @@ public class CommentUserEventService extends AbstractService implements ChangeSe private void processUpdateComment(String index, String type, String commentId, RecordComment comment) { processUpdateOrCreateComment(index, type, commentId, comment, - GchangeEventCodes.UPDATE_COMMENT, String.format("duniter.%s.event.updateComment", index.toLowerCase()), - GchangeEventCodes.UPDATE_REPLY_COMMENT, String.format("duniter.%s.event.updateReplyComment", index.toLowerCase())); + GchangeEventCodes.UPDATE_COMMENT, String.format("duniter.%s.event.updateComment", index.toLowerCase()), + GchangeEventCodes.UPDATE_REPLY_COMMENT, String.format("duniter.%s.event.updateReplyComment", index.toLowerCase())); } @@ -304,5 +307,23 @@ public class CommentUserEventService extends AbstractService implements ChangeSe userEventService.deleteEventsByReference(new UserEvent.Reference(change.getIndex(), change.getType(), change.getId())); } - + private RecordComment readComment(ChangeEvent change) { + try { + if (change.getSource() != null) { + return objectMapper.readValue(change.getSource().streamInput(), RecordComment.class); + } + return null; + } catch (JsonProcessingException e) { + if (trace) { + logger.warn(String.format("Bad format for comment [%s]: %s. Skip this comment", change.getId(), e.getMessage()), e); + } + else { + logger.warn(String.format("Bad format for comment [%s]: %s. Skip this comment", change.getId(), e.getMessage())); + } + return null; + } + catch (IOException e) { + throw new TechnicalException(String.format("Unable to parse received comment %s", change.getId()), e); + } + } } diff --git a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/MarketService.java b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/MarketService.java index 89869930..9e9a5a20 100644 --- a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/MarketService.java +++ b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/MarketService.java @@ -28,7 +28,7 @@ import org.duniter.core.client.service.bma.WotRemoteService; import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; import org.duniter.elasticsearch.gchange.PluginSettings; -import org.duniter.elasticsearch.service.AbstractService; +import org.duniter.elasticsearch.gchange.service.AbstractService; import org.duniter.elasticsearch.service.ServiceLocator; import org.duniter.elasticsearch.threadpool.ThreadPool; import org.duniter.elasticsearch.user.service.UserEventService; diff --git a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/RegistryService.java b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/RegistryService.java index 8a8c770c..b090936b 100644 --- a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/RegistryService.java +++ b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/RegistryService.java @@ -24,11 +24,10 @@ package org.duniter.elasticsearch.gchange.service; import com.fasterxml.jackson.core.JsonProcessingException; -import org.duniter.core.client.service.bma.BlockchainRemoteService; import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; import org.duniter.elasticsearch.gchange.PluginSettings; -import org.duniter.elasticsearch.service.AbstractService; +import org.duniter.elasticsearch.gchange.service.AbstractService; import org.duniter.elasticsearch.user.service.UserEventService; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.action.index.IndexRequestBuilder; diff --git a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/SynchroService.java b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/SynchroService.java index 9dc5144a..6498406d 100644 --- a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/SynchroService.java +++ b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/SynchroService.java @@ -24,12 +24,12 @@ package org.duniter.elasticsearch.gchange.service; import org.duniter.core.client.model.local.Peer; import org.duniter.core.service.CryptoService; -import org.duniter.elasticsearch.gchange.PluginSettings; import org.duniter.elasticsearch.gchange.model.Protocol; import org.duniter.elasticsearch.model.SynchroResult; import org.duniter.elasticsearch.service.AbstractSynchroService; import org.duniter.elasticsearch.service.ServiceLocator; import org.duniter.elasticsearch.threadpool.ThreadPool; +import org.duniter.elasticsearch.user.PluginSettings; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; @@ -41,7 +41,7 @@ public class SynchroService extends AbstractSynchroService { @Inject public SynchroService(Client client, PluginSettings settings, CryptoService cryptoService, ThreadPool threadPool, final ServiceLocator serviceLocator) { - super(client, settings, cryptoService, threadPool, serviceLocator); + super(client, settings.getDelegate(), cryptoService, threadPool, serviceLocator); } public void synchronize() { diff --git a/duniter4j-es-user/pom.xml b/duniter4j-es-user/pom.xml index 3f2d3177..4b6013b3 100644 --- a/duniter4j-es-user/pom.xml +++ b/duniter4j-es-user/pom.xml @@ -24,6 +24,13 @@ <artifactId>elasticsearch</artifactId> <scope>provided</scope> </dependency> + + <!-- Websocket --> + <dependency> + <groupId>javax.websocket</groupId> + <artifactId>javax.websocket-api</artifactId> + <scope>provided</scope> + </dependency> </dependencies> <build> diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/Plugin.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/Plugin.java index 66e19396..85ed1b4e 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/Plugin.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/Plugin.java @@ -68,6 +68,7 @@ public class Plugin extends org.elasticsearch.plugins.Plugin { modules.add(new WebSocketModule()); + return modules; } diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/PluginInit.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/PluginInit.java index 5e51af22..9d05f816 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/PluginInit.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/PluginInit.java @@ -64,19 +64,16 @@ public class PluginInit extends AbstractLifecycleComponent<PluginInit> { // Waiting cluster back to GREEN or YELLOW state, before synchronize threadPool.scheduleOnClusterHealthStatus(() -> { synchronize(); + + // Notify admin + injector.getInstance(UserEventService.class) + .notifyAdmin(new UserEvent( + UserEvent.EventType.INFO, + UserEventCodes.NODE_STARTED.name(), + I18n.n("duniter.event.NODE_STARTED"), + clusterName)); }, ClusterHealthStatus.YELLOW, ClusterHealthStatus.GREEN); }, ClusterHealthStatus.YELLOW, ClusterHealthStatus.GREEN); - - // When started - threadPool.scheduleOnStarted(() -> { - // Notify admin - injector.getInstance(UserEventService.class) - .notifyAdmin(new UserEvent( - UserEvent.EventType.INFO, - UserEventCodes.NODE_STARTED.name(), - I18n.n("duniter.event.NODE_STARTED"), - clusterName)); - }); } @Override diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/PluginSettings.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/PluginSettings.java index 9669ad28..b727961d 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/PluginSettings.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/PluginSettings.java @@ -23,6 +23,8 @@ package org.duniter.elasticsearch.user; */ +import org.elasticsearch.common.component.AbstractLifecycleComponent; +import org.elasticsearch.common.component.LifecycleListener; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; @@ -31,31 +33,52 @@ import org.elasticsearch.common.settings.Settings; * @author Benoit Lavenier <benoit.lavenier@e-is.pro> * @since 1.0 */ -public class PluginSettings extends org.duniter.elasticsearch.PluginSettings { +public class PluginSettings extends AbstractLifecycleComponent<PluginSettings> { + + private org.duniter.elasticsearch.PluginSettings delegate; @Inject - public PluginSettings(Settings settings) { + public PluginSettings(Settings settings, org.duniter.elasticsearch.PluginSettings delegate) { super(settings); + this.delegate = delegate; + + // Add i18n bundle name + delegate.addI18nBundleName(getI18nBundleName()); } - public String getDefaultStringAnalyzer() { - return settings.get("duniter.string.analyzer", "english"); + @Override + protected void doStart() { + } - public String getKeyringSalt() { - return settings.get("duniter.keyring.salt"); + @Override + protected void doClose() { + } - public String getKeyringPassword() { - return settings.get("duniter.keyring.password"); + @Override + protected void doStop() { + } - public String getKeyringPublicKey() { - return settings.get("duniter.keyring.pub"); + public org.duniter.elasticsearch.PluginSettings getDelegate() { + return delegate; } - public String getKeyringSecretKey() { - return settings.get("duniter.keyring.sec"); + public String getDefaultStringAnalyzer() { + return delegate.getDefaultStringAnalyzer(); + } + + public boolean reloadIndices() { + return delegate.reloadIndices(); + } + + public boolean enableDataSync() { + return delegate.enableDataSync(); + } + + public boolean getMailEnable() { + return settings.getAsBoolean("duniter.mail.enable", Boolean.TRUE); } public String getMailSmtpHost() { @@ -86,7 +109,54 @@ public class PluginSettings extends org.duniter.elasticsearch.PluginSettings { return settings.get("duniter.mail.subject.prefix", "[Duniter4j ES]"); } + /* -- delegate methods -- */ + + public String getClusterName() { + return delegate.getClusterName(); + } + + public String getNodeBmaHost() { + return delegate.getNodeBmaHost(); + } + + public int getNodeBmaPort() { + return delegate.getNodeBmaPort(); + } + + public int getIndexBulkSize() { + return delegate.getIndexBulkSize(); + } + + public boolean enableBlockchainSync() { + return delegate.enableBlockchainSync(); + } + + public String getKeyringSalt() { + return delegate.getKeyringSalt(); + } + + public String getKeyringPassword() { + return delegate.getKeyringPassword(); + } + + public String getKeyringPublicKey() { + return delegate.getKeyringPublicKey(); + } + + public String getKeyringSecretKey() { + return delegate.getKeyringSecretKey(); + } + + public void addI18nBundleName(String bundleName) { + delegate.addI18nBundleName(bundleName); + } + + + /* -- protected methods -- */ + protected String getI18nBundleName() { return "duniter4j-es-user-i18n"; } + + } diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/model/Message.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/model/Message.java index 8fc76320..5ac4d34f 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/model/Message.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/model/Message.java @@ -27,7 +27,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.base.Preconditions; +import org.duniter.core.util.Preconditions; import org.duniter.core.client.model.elasticsearch.Record; import org.duniter.core.exception.TechnicalException; import org.nuiton.i18n.I18n; diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/model/UserEvent.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/model/UserEvent.java index 31ab24c5..95c9e97a 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/model/UserEvent.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/model/UserEvent.java @@ -27,7 +27,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.base.Preconditions; +import org.duniter.core.util.Preconditions; import org.duniter.core.client.model.elasticsearch.Record; import org.duniter.core.exception.TechnicalException; import org.nuiton.i18n.I18n; diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/AbstractService.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/AbstractService.java new file mode 100644 index 00000000..cc5c6ed5 --- /dev/null +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/AbstractService.java @@ -0,0 +1,31 @@ +package org.duniter.elasticsearch.user.service; + +import org.duniter.core.service.CryptoService; +import org.duniter.elasticsearch.user.PluginSettings; +import org.elasticsearch.client.Client; + +/** + * Created by blavenie on 10/01/17. + */ +public abstract class AbstractService extends org.duniter.elasticsearch.service.AbstractService { + + protected PluginSettings pluginSettings; + + public AbstractService(String loggerName, Client client, PluginSettings pluginSettings) { + this(loggerName, client, pluginSettings, null); + } + + public AbstractService(Client client, PluginSettings pluginSettings) { + this(client, pluginSettings, null); + } + + public AbstractService(Client client, PluginSettings pluginSettings, CryptoService cryptoService) { + this("duniter.user", client, pluginSettings, cryptoService); + } + + public AbstractService(String loggerName, Client client, PluginSettings pluginSettings, CryptoService cryptoService) { + super(loggerName, client, pluginSettings.getDelegate(), cryptoService); + this.pluginSettings = pluginSettings; + } + +} diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/BlockchainUserEventService.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/BlockchainUserEventService.java index b39102a0..a3447cb8 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/BlockchainUserEventService.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/BlockchainUserEventService.java @@ -33,8 +33,8 @@ import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; import org.duniter.core.util.CollectionUtils; import org.duniter.core.util.websocket.WebsocketClientEndpoint; -import org.duniter.elasticsearch.PluginSettings; -import org.duniter.elasticsearch.service.AbstractService; +import org.duniter.elasticsearch.user.PluginSettings; +import org.duniter.elasticsearch.user.service.AbstractService; import org.duniter.elasticsearch.service.BlockchainService; import org.duniter.elasticsearch.service.changes.ChangeEvent; import org.duniter.elasticsearch.service.changes.ChangeService; diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/GroupService.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/GroupService.java index fa276af2..a1f5f542 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/GroupService.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/GroupService.java @@ -25,17 +25,13 @@ package org.duniter.elasticsearch.user.service; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; -import com.google.common.base.Preconditions; import org.apache.commons.collections4.MapUtils; -import org.duniter.core.client.model.ModelUtils; -import org.duniter.core.client.model.elasticsearch.Record; import org.duniter.core.client.model.elasticsearch.UserGroup; -import org.duniter.core.client.model.elasticsearch.UserProfile; import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; -import org.duniter.elasticsearch.PluginSettings; import org.duniter.elasticsearch.exception.AccessDeniedException; -import org.duniter.elasticsearch.service.AbstractService; +import org.duniter.elasticsearch.user.service.AbstractService; +import org.duniter.elasticsearch.user.PluginSettings; import org.elasticsearch.action.ListenableActionFuture; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.action.index.IndexResponse; @@ -63,7 +59,7 @@ public class GroupService extends AbstractService { public GroupService(Client client, PluginSettings settings, CryptoService cryptoService) { - super("duniter." + INDEX, client, settings,cryptoService); + super("duniter." + INDEX, client, settings, cryptoService); } /** diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/HistoryService.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/HistoryService.java index 7e60639d..beaf8da4 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/HistoryService.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/HistoryService.java @@ -25,21 +25,19 @@ package org.duniter.elasticsearch.user.service; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; -import com.google.gson.JsonSyntaxException; import org.duniter.core.client.model.elasticsearch.DeleteRecord; import org.duniter.core.client.model.elasticsearch.MessageRecord; import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; -import org.duniter.core.util.StringUtils; -import org.duniter.elasticsearch.PluginSettings; import org.duniter.elasticsearch.exception.NotFoundException; -import org.duniter.elasticsearch.service.AbstractService; +import org.duniter.elasticsearch.user.service.AbstractService; +import org.duniter.elasticsearch.user.PluginSettings; import org.duniter.elasticsearch.user.model.UserEvent; -import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; -import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.index.IndexResponse; -import org.elasticsearch.action.search.*; +import org.elasticsearch.action.search.SearchRequestBuilder; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; @@ -47,7 +45,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.search.SearchHit; import java.io.IOException; diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/MessageService.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/MessageService.java index 58559972..71dee94f 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/MessageService.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/MessageService.java @@ -28,9 +28,9 @@ import com.fasterxml.jackson.databind.JsonNode; import org.duniter.core.client.model.ModelUtils; import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; -import org.duniter.elasticsearch.PluginSettings; +import org.duniter.elasticsearch.user.PluginSettings; import org.duniter.elasticsearch.exception.InvalidSignatureException; -import org.duniter.elasticsearch.service.AbstractService; +import org.duniter.elasticsearch.user.service.AbstractService; import org.duniter.elasticsearch.user.model.Message; import org.duniter.elasticsearch.user.model.UserEvent; import org.duniter.elasticsearch.user.model.UserEventCodes; diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/SynchroService.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/SynchroService.java index c77368a6..f39c137e 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/SynchroService.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/SynchroService.java @@ -25,7 +25,7 @@ package org.duniter.elasticsearch.user.service; import org.duniter.core.client.model.elasticsearch.Protocol; import org.duniter.core.client.model.local.Peer; import org.duniter.core.service.CryptoService; -import org.duniter.elasticsearch.PluginSettings; +import org.duniter.elasticsearch.user.PluginSettings; import org.duniter.elasticsearch.model.SynchroResult; import org.duniter.elasticsearch.service.ServiceLocator; import org.duniter.elasticsearch.service.AbstractSynchroService; @@ -41,7 +41,7 @@ public class SynchroService extends AbstractSynchroService { @Inject public SynchroService(Client client, PluginSettings settings, CryptoService cryptoService, ThreadPool threadPool, final ServiceLocator serviceLocator) { - super(client, settings, cryptoService, threadPool, serviceLocator); + super(client, settings.getDelegate(), cryptoService, threadPool, serviceLocator); } public void synchronize() { diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/UserEventService.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/UserEventService.java index 66f71249..d795b763 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/UserEventService.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/UserEventService.java @@ -25,22 +25,21 @@ package org.duniter.elasticsearch.user.service; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; -import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.gson.JsonSyntaxException; import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; import org.duniter.core.service.MailService; +import org.duniter.core.util.Preconditions; import org.duniter.core.util.StringUtils; import org.duniter.core.util.crypto.CryptoUtils; import org.duniter.core.util.crypto.KeyPair; -import org.duniter.elasticsearch.PluginSettings; import org.duniter.elasticsearch.exception.InvalidSignatureException; -import org.duniter.elasticsearch.service.AbstractService; import org.duniter.elasticsearch.service.changes.ChangeEvent; import org.duniter.elasticsearch.service.changes.ChangeService; import org.duniter.elasticsearch.service.changes.ChangeSource; import org.duniter.elasticsearch.threadpool.ThreadPool; +import org.duniter.elasticsearch.user.PluginSettings; import org.duniter.elasticsearch.user.model.UserEvent; import org.duniter.elasticsearch.user.model.UserProfile; import org.elasticsearch.action.ActionFuture; @@ -63,7 +62,6 @@ import org.nuiton.i18n.I18n; import java.io.IOException; import java.util.*; -import java.util.concurrent.TimeUnit; /** * Created by Benoit on 30/03/2015. diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/UserService.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/UserService.java index cc25ccf0..75a4c039 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/UserService.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/UserService.java @@ -25,14 +25,14 @@ package org.duniter.elasticsearch.user.service; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; -import com.google.common.base.Preconditions; +import org.duniter.core.util.Preconditions; import org.apache.commons.collections4.MapUtils; import org.duniter.core.client.model.ModelUtils; import org.duniter.core.client.model.elasticsearch.UserProfile; import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; import org.duniter.core.service.MailService; -import org.duniter.elasticsearch.PluginSettings; +import org.duniter.elasticsearch.user.PluginSettings; import org.duniter.elasticsearch.exception.AccessDeniedException; import org.duniter.elasticsearch.service.AbstractService; import org.elasticsearch.action.ListenableActionFuture; @@ -63,7 +63,7 @@ public class UserService extends AbstractService { public UserService(Client client, PluginSettings settings, CryptoService cryptoService) { - super("duniter." + INDEX, client, settings,cryptoService); + super("duniter." + INDEX, client, settings.getDelegate(), cryptoService); } /** diff --git a/pom.xml b/pom.xml index 136788fd..5d361087 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ <file.encoding>UTF-8</file.encoding> <log4j.version>1.2.17</log4j.version> <slf4j.version>1.7.5</slf4j.version> - <guava.version>19.0</guava.version> + <guava.version>18.0</guava.version> <xalan.version>2.7.1</xalan.version> <xerces.version>2.9.0</xerces.version> <xml-apis.version>2.0.2</xml-apis.version> @@ -25,9 +25,10 @@ <scrypt.version>1.4.0</scrypt.version> <elasticsearch.version>2.3.3</elasticsearch.version> <jna.version>4.1.0</jna.version> - <tyrus.version>1.12</tyrus.version> + <tyrus.version>1.13</tyrus.version> <nuitonConfigVersion>3.0-rc-4</nuitonConfigVersion> + <nuitonVersionVersion>1.0-rc-2</nuitonVersionVersion> <nuitonI18nVersion>3.5</nuitonI18nVersion> <!-- UI versions --> @@ -171,6 +172,11 @@ <artifactId>nuiton-config</artifactId> <version>${nuitonConfigVersion}</version> </dependency> + <dependency> + <groupId>org.nuiton</groupId> + <artifactId>nuiton-version</artifactId> + <version>${nuitonVersionVersion}</version> + </dependency> <dependency> <groupId>org.nuiton.i18n</groupId> <artifactId>nuiton-i18n</artifactId> @@ -358,7 +364,7 @@ <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> - <version>2.6.4</version> + <version>2.6.6</version> </dependency> <!-- WebSocket --> -- GitLab