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

- [web] implement rest/currency/add

- [web] Load NaCL library at startup
- [client] add a REST client to registernew currency
- [client] add a REST client to get all currency names
parent 53cdf02a
Branches
Tags
No related merge requests found
......@@ -25,24 +25,20 @@ package io.ucoin.client.ui.config;
*/
import io.ucoin.client.core.config.Configuration;
import org.apache.commons.io.FileUtils;
import io.ucoin.client.core.service.ServiceLocator;
import io.ucoin.client.core.technical.UCoinTechnicalException;
import org.apache.commons.lang3.StringUtils;
import org.nuiton.config.ApplicationConfig;
import org.nuiton.i18n.I18n;
import org.nuiton.i18n.init.DefaultI18nInitializer;
import org.nuiton.i18n.init.UserI18nInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
public class WebConfiguration extends Configuration {
private static final String CONFIG_FILE_NAME = "ucoin-web.config";
private static final String CONFIG_FILE_NAME = "ucoinj-web.config";
private static final String CONFIG_FILE_ENV_PROPERTY = CONFIG_FILE_NAME;
......@@ -53,7 +49,14 @@ public class WebConfiguration extends Configuration {
private static final Logger log = LoggerFactory.getLogger(WebConfiguration.class);
static {
instance = new WebConfiguration(getWebConfigFile());
String configFile = getWebConfigFile();
if (log.isDebugEnabled()) {
log.debug(String.format("Loading configuration from file [%s]", configFile));
}
if (new File(configFile).exists() == false) {
log.warn(String.format("Configuration file not found [%s]. Make sure the path is correct.", configFile));
}
instance = new WebConfiguration(configFile);
initDefault();
}
......@@ -74,12 +77,8 @@ public class WebConfiguration extends Configuration {
public WebConfiguration(String file, String... args) {
super(file, args);
// Init i18n
/*try {
initI18n();
} catch (IOException e) {
throw new UCoinTechnicalException("i18n initialization failed", e);
}*/
// Init Crypto (NaCL lib...)
initCrypto();
}
public String getVersionAsString() {
......@@ -109,36 +108,16 @@ public class WebConfiguration extends Configuration {
return configFile;
}
protected void initI18n() throws IOException {
// --------------------------------------------------------------------//
// init i18n
// --------------------------------------------------------------------//
File i18nDirectory = new File(getDataDirectory(), "i18n");
if (i18nDirectory.exists()) {
// clean i18n cache
FileUtils.cleanDirectory(i18nDirectory);
}
FileUtils.forceMkdir(i18nDirectory);
if (log.isDebugEnabled()) {
log.debug("I18N directory: " + i18nDirectory);
}
Locale i18nLocale = getI18nLocale();
protected void initCrypto() {
if (log.isInfoEnabled()) {
log.info(String.format("Starts i18n with locale [%s] at [%s]",
i18nLocale, i18nDirectory));
}
I18n.init(new UserI18nInitializer(
i18nDirectory, new DefaultI18nInitializer(getI18nBundleName())),
i18nLocale);
log.info("Starts Sodium (NaCL) library");
}
protected static String getI18nBundleName() {
return "ucoin-web-i18n";
try {
// This call will load the sodium library
ServiceLocator.instance().getCryptoService();
} catch (Throwable e) {
throw new UCoinTechnicalException("Crypto lib (NaCL) initialization failed. Make sure sodium has been installed (or add library into a ./lib directory).", e);
}
}
}
......@@ -26,8 +26,17 @@
<body>
<wicket:extend>
<label for="text-3"><wicket:message key="login.username"/></label>
<input type="text" data-clear-btn="true" name="text-3" id="text-3" value="">
<form action="http://localhost:8080/rest/currency/add" method="get">
<label for="text-pubkey"><wicket:message key="currency.register.pubkey"/></label>
<input type="text" data-clear-btn="true" name="pubkey" id="text-pubkey" value=""/>
<label for="text-currency"><wicket:message key="currency.register.currencyJson"/></label>
<input type="text" data-clear-btn="true" name="currency" id="text-currency" value=""/>
<label for="text-sig"><wicket:message key="currency.register.signature"/></label>
<input type="text" data-clear-btn="true" name="sig" id="text-sig" value=""/>
<input type="submit"/>
</form>
</wicket:extend>
</body>
......
......@@ -23,27 +23,23 @@ package io.ucoin.client.ui.service.rest;
*/
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import io.ucoin.client.core.model.Currency;
import io.ucoin.client.core.service.CryptoService;
import io.ucoin.client.core.service.ServiceLocator;
import io.ucoin.client.core.service.search.CurrencyIndexerService;
import io.ucoin.client.core.technical.UCoinTechnicalException;
import io.ucoin.client.core.technical.crypto.CryptoUtils;
import io.ucoin.client.core.technical.jackson.JacksonUtils;
import io.ucoin.client.core.service.search.InvalidSignatureException;
import io.ucoin.client.core.technical.gson.GsonUtils;
import io.ucoin.client.ui.application.UcoinSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wicketstuff.rest.annotations.MethodMapping;
import org.wicketstuff.rest.annotations.ResourcePath;
import org.wicketstuff.rest.annotations.parameters.PathParam;
import org.wicketstuff.rest.contenthandling.json.objserialdeserial.JacksonObjectSerialDeserial;
import org.wicketstuff.rest.annotations.parameters.RequestParam;
import org.wicketstuff.rest.contenthandling.json.objserialdeserial.GsonObjectSerialDeserial;
import org.wicketstuff.rest.contenthandling.mimetypes.RestMimeTypes;
import org.wicketstuff.rest.contenthandling.webserialdeserial.TextualWebSerialDeserial;
import org.wicketstuff.rest.resource.AbstractRestResource;
import org.wicketstuff.rest.utils.http.HttpMethod;
import java.io.IOException;
@ResourcePath("/rest/currency")
public class CurrencyRestService extends AbstractRestResource<TextualWebSerialDeserial> {
......@@ -54,20 +50,20 @@ public class CurrencyRestService extends AbstractRestResource<TextualWebSerialDe
private static final long serialVersionUID = 1L;
private boolean debug;
private ObjectMapper objectMapper;
private static ObjectMapper staticObjectMapper;
private static Gson gson;
public CurrencyRestService() {
super(new TextualWebSerialDeserial("UTF-8", RestMimeTypes.APPLICATION_JSON,
new JacksonObjectSerialDeserial(initObjectMapper())));
new GsonObjectSerialDeserial(initGson())));
debug = log.isDebugEnabled();
objectMapper = staticObjectMapper;
staticObjectMapper = null;
}
protected static ObjectMapper initObjectMapper() {
staticObjectMapper = JacksonUtils.newObjectMapper();
return staticObjectMapper;
private static Gson initGson() {
if (gson == null) {
gson = GsonUtils.newBuilder().create();
}
return gson;
}
@MethodMapping("/{currencyId}")
......@@ -76,24 +72,38 @@ public class CurrencyRestService extends AbstractRestResource<TextualWebSerialDe
return service.getCurrencyById(currencyId);
}
@MethodMapping(value = "/add", httpMethod = HttpMethod.POST)
public void saveCurrency(@PathParam("pubkey") String pubkey, @PathParam("currency") String jsonCurrency, @PathParam("sig") String signature) throws InvalidSignatureException {
@MethodMapping(value = "/add", produces = RestMimeTypes.TEXT_PLAIN)
public String saveCurrency(@RequestParam("pubkey") String pubkey, @RequestParam("currency") String jsonCurrency, @RequestParam("sig") String signature) throws InvalidSignatureException {
if (log.isDebugEnabled()) {
log.debug(String.format("Asking to add new currency:\n - pubkey: %s\n - currency: %s\n - signature: %s", pubkey, jsonCurrency, signature));
}
CurrencyIndexerService service = ServiceLocator.instance().getCurrencyIndexerService();
service.registerCurrency(pubkey, jsonCurrency, signature);
/*CryptoService cryptoService = ServiceLocator.instance().getCryptoService();
// Check the signature
CryptoService cryptoService = ServiceLocator.instance().getCryptoService();
if (!cryptoService.verify(jsonCurrency, signature, pubkey)) {
String currencyName = GsonUtils.getValueFromJSONAsString(jsonCurrency, "currencyName");
log.warn(String.format("Currency not added, because bad signature. currency [%s]", currencyName));
throw new InvalidSignatureException("Bad signature");
}
// Apply the save (will check if pubkey match the owner public key)
Currency currency = null;
try {
currency = objectMapper.readValue(CryptoUtils.decodeUTF8(jsonCurrency), Currency.class);
} catch (IOException e) {
throw new UCoinTechnicalException(e.getMessage(), e);
currency = gson.fromJson(jsonCurrency, Currency.class);
Preconditions.checkNotNull(currency);
Preconditions.checkNotNull(currency.getCurrencyName());
} catch(Throwable t) {
log.error("Error while reading currency JSON: " + jsonCurrency);
return "Parse error. Currency not readable.";
}
CurrencyIndexerService service = ServiceLocator.instance().getCurrencyIndexerService();
service.saveCurrency(currency, pubkey);
*/
return "Currency added";
}
/* -- Internal methods -- */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment