Commit 1bdb0818 authored by Miguel Mateo Montero's avatar Miguel Mateo Montero

final release

parent 70b9915b
......@@ -74,7 +74,7 @@
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
<version>4.9.0</version>
</dependency>
<dependency>
<groupId>eu.ari.fentec</groupId>
......
......@@ -33,11 +33,11 @@ import okhttp3.Response;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.bouncycastle.util.encoders.Base64;
/**
* @author A101866
* connector ot the Exchange server
* connector to the Exchange server
*
*/
public class ExchangeConnector {
......@@ -50,7 +50,7 @@ public class ExchangeConnector {
* @param eCoinTypeName
* @return
*/
public boolean wireTransferRequest(int funds, byte[] walletID, ArrayList<String> eCoinTypeName) {
public boolean wireTransferRequest(int funds, String walletID, ArrayList<String> eCoinTypeName) {
try {
String jsonS = getJsonTransfer(funds, walletID, eCoinTypeName);
......@@ -63,10 +63,12 @@ public class ExchangeConnector {
.build();
Response response;
OkHttpClient mClient = new OkHttpClient();
OkHttpClient mClient = OKHttpClientST.getInstance().getClient();
response = mClient.newCall(request).execute();
if (response.code() == 200) {
try {
if (response.code() == 200) {
return true;
} else {
logger.error("error while creating wiring transfer for merchant account to exchange.");
......@@ -74,6 +76,12 @@ public class ExchangeConnector {
logger.error(" exchange msg: " + response.message());
return false;
}
}catch (Exception i) {
logger.error(i,i);
return false;
}finally {
response.body().close();
}
} catch (Exception e) {
logger.error(e, e);
......@@ -87,10 +95,10 @@ public class ExchangeConnector {
* @param eCoinTypeName
* @return Body of the request to Exchange
*/
private String getJsonTransfer(int funds, byte[] walletID, ArrayList<String> eCoinTypeName) {
private String getJsonTransfer(int funds, String walletID, ArrayList<String> eCoinTypeName) {
JSONObject jo = new JSONObject();
jo.put("funds", funds);
jo.put("walletID", Base64.toBase64String(walletID));
jo.put("walletID", walletID);
for (String s : eCoinTypeName)
jo.append("ect", s);
......@@ -104,15 +112,15 @@ public class ExchangeConnector {
* @param walletID
* @return
*/
public boolean ecoinVerification(Econtract econtract, byte[] walletID) {
public String ecoinVerification(Econtract econtract, String walletId) {
try {
String contractS = new Utils().serializeEcontract(econtract);
String id = Base64.toBase64String(walletID);
JSONObject jo = new JSONObject();
jo.put("econtract", contractS);
jo.put("id", id);
jo.put("id", walletId);
String jsonS = jo.toString(4);
RequestBody body = RequestBody.create(MediaType.parse("Application/Json"), jsonS);
......@@ -122,21 +130,34 @@ public class ExchangeConnector {
.url(Configuration.getConfiguration().getProperty("EXCHANGE_END_POINT") + "ecoinVerify").post(body)
.build();
Response response;
OkHttpClient mClient = new OkHttpClient();
OkHttpClient mClient = OKHttpClientST.getInstance().getClient();
response = mClient.newCall(request).execute();
try {
if (response.code() == 200) {
return true;
return null;
} else {
String m = response.body().string();
logger.error("ecoin Verification fail");
logger.error(" exchange response: " + response.code());
logger.error(" exchange msg: " + response.message());
return false;
logger.error(" exchange msg: " + m);
return m;
}
}catch (Exception i) {
logger.error(i,i);
return "internal server error";
}finally {
response.body().close();
}
} catch (Exception e) {
logger.error(e, e);
return false;
return "internal server error";
}
}
}
/**
*
*FENTEC Functional Encryption Technologies
*Privacy-preserving and Auditable Digital Currency Use Case
*Copyright © 2019 Atos Spain SA
*
*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/>.
**/
package eu.ari.fentec.connector;
import java.util.List;
import okhttp3.OkHttpClient;
/**
* class to manage OKHttpClient as singleton
* @author A101866
*
*/
public class OKHttpClientST {
private static OKHttpClientST singletonInstance;
private OkHttpClient client;
/**
* private constructor of the class
*/
private OKHttpClientST() {
client = new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.build();
}
/**
* getter of the singleton
* @return
*/
public static OKHttpClientST getInstance() {
if (singletonInstance == null) {
singletonInstance = new OKHttpClientST();
}
return singletonInstance;
}
/**
* @return
*/
public OkHttpClient getClient() {
return client;
}
/**
* close connections
*/
public void closeConnections() {
client.dispatcher().cancelAll();
}
}
......@@ -61,9 +61,10 @@ public class TrustedAuthConnector {
.build();
Response response;
OkHttpClient mClient = new OkHttpClient();
OkHttpClient mClient = OKHttpClientST.getInstance().getClient();
response = mClient.newCall(request).execute();
try {
if (response.code() == 200) {
String id = response.message();
return id;
......@@ -73,7 +74,13 @@ public class TrustedAuthConnector {
logger.error("trusted authority msg: " + response.message());
return null;
}
}catch (Exception i) {
logger.error(i,i);
return null;
}finally {
response.body().close();
}
}catch (Exception e) {
logger.error(e,e);
return null;
......
......@@ -26,6 +26,7 @@ import java.util.Map.Entry;
/**
* Container of Merchants
* @author A101866
*
*/
......@@ -35,10 +36,16 @@ public class MerchantContainer {
private static MerchantContainer merchantContainer = null;
/**
* Class constructor
*/
private MerchantContainer () {
this.merchantHM = new HashMap<>();
}
/**
* @return
*/
public static MerchantContainer getMerchantContainer() {
if (merchantContainer == null)
merchantContainer = new MerchantContainer();
......@@ -46,14 +53,24 @@ public class MerchantContainer {
return merchantContainer;
}
/**
* @param name
* @return
*/
public Merchant getMerchant (String name) {
return this.merchantHM.get(name);
}
/**
* @param merchant
*/
public void addMerchant (Merchant merchant) {
merchantHM.put(merchant.getName(), merchant);
}
/**
* @return
*/
public Iterator<Entry<String, Merchant>> getIterator() {
Iterator<Entry<String, Merchant>> it = merchantHM.entrySet().iterator();
return it;
......
......@@ -26,14 +26,7 @@ import eu.ari.fentec.econtract.Product;
public interface MerchantIf {
/**
* This method transfer funds to the merchant account in the exchange
* @param funds
* @param exchange
* @param eCoinTypeName
* @return
*/
// public boolean wireTransferRequest(int funds, String eCoinTypeName);
/**
* this method request a contract for a list of products
......@@ -49,14 +42,11 @@ public interface MerchantIf {
* @param exchange
* @return
*/
public boolean econtractSendBack(String econtractS);
public String econtractSendBack(String econtractS);
/**
* print contracts information
*/
public void printEContracts();
/**
* @return array list of products
......
......@@ -41,6 +41,7 @@ public class MerchantMetadata {
Logger logger = Logger.getLogger(MerchantMetadata.class);
ArrayList<String> ectAL;
public String name;
public String id;
public PublicKey ecdsaPublicKey;
......@@ -56,10 +57,11 @@ public class MerchantMetadata {
* @param id
* @param products Array list of products it sells
*/
public MerchantMetadata(String name, String id, ArrayList<Product> products) {
public MerchantMetadata(String name, String id, ArrayList<Product> products, ArrayList<String> ectAL) {
try {
this.id = id;
this.name = name;
this.ectAL = ectAL;
KeyPair keyPair = new Utils().generateECDSAKeyPair();
this.ecdsaPublicKey = keyPair.getPublic();
this.ecdsaPrivateKey = keyPair.getPrivate();
......
......@@ -49,12 +49,7 @@ import eu.ari.fentec.utils.crypto.Utils;
/**
* @author A101866
*
* class to model the set of operation provided by the Merchant entity to the platform:
* - econtractSBack: payment of a eContract.
* - getContract: request contract proposal for a list of items
* - getMerchants: retrieve merchant names.
* - getProductList: retrieve products sold by a merchant
* - getProducts: retrieve products sold by a merchant
* Merchant API implementation
*
*/
@Path("/fentec")
......@@ -72,6 +67,9 @@ public class MerchantRsc {
return Response.status(Response.Status.OK).entity("Merchant says Hello!").build();
}
/**
* @return
*/
private boolean initialize() {
try {
PropertyConfigurator.configure("/opt/tomcat/conf/merchant_log4j.properties");
......@@ -95,8 +93,8 @@ public class MerchantRsc {
products.add(product2);
product3 = new Product( new Utils().getRandomBytesAsString(16), 20, "Boat ticket", 13);
products.add(product3);
merchantMetadata = new MerchantMetadata ("EMT", idS, products);
merchant = new Merchant(merchantMetadata);
merchantMetadata = new MerchantMetadata ("EMT", idS, products, al);
merchant = new Merchant(merchantMetadata,idS);
MerchantContainer.getMerchantContainer().addMerchant(merchant);
al = new ArrayList<>();
......@@ -109,8 +107,8 @@ public class MerchantRsc {
products.add(product2);
product3 = new Product( new Utils().getRandomBytesAsString(16), 30, "Pacifier", 14);
products.add(product3);
merchantMetadata = new MerchantMetadata ("BabyCare", idS, products);
merchant = new Merchant(merchantMetadata);
merchantMetadata = new MerchantMetadata ("BabyCare", idS, products, al);
merchant = new Merchant(merchantMetadata, idS);
MerchantContainer.getMerchantContainer().addMerchant(merchant);
al = new ArrayList<>();
......@@ -123,8 +121,8 @@ public class MerchantRsc {
products.add(product2);
product3 = new Product( new Utils().getRandomBytesAsString(16), 30, "Fruit", 15);
products.add(product3);
merchantMetadata = new MerchantMetadata ("Take&Eat", idS, products);
merchant = new Merchant(merchantMetadata);
merchantMetadata = new MerchantMetadata ("Take&Eat", idS, products, al);
merchant = new Merchant(merchantMetadata, idS);
MerchantContainer.getMerchantContainer().addMerchant(merchant);
al = new ArrayList<>();
......@@ -137,8 +135,8 @@ public class MerchantRsc {
products.add(product2);
product3 = new Product( new Utils().getRandomBytesAsString(16), 300, "Anything", 16);
products.add(product3);
merchantMetadata = new MerchantMetadata ("FakeMerchant", idS, products);
merchant = new Merchant(merchantMetadata);
merchantMetadata = new MerchantMetadata ("FakeMerchant", idS, products, al);
merchant = new Merchant(merchantMetadata,idS);
MerchantContainer.getMerchantContainer().addMerchant(merchant);
......@@ -152,6 +150,10 @@ public class MerchantRsc {
}
}
/**
* Get merchants list
* @return
*/
@GET
@Path("/merchants")
@Produces(MediaType.TEXT_PLAIN)
......@@ -199,6 +201,12 @@ public class MerchantRsc {
}
/**
* Get products of a Merchant
*
* @param name
* @return
*/
@GET
@Path("{name}/productList")
@Produces(MediaType.TEXT_PLAIN)
......@@ -238,7 +246,7 @@ public class MerchantRsc {
Merchant merchant = MerchantContainer.getMerchantContainer().getMerchant(merchantName);
String contract = merchant.econtractRequest(commandDetails);
logger.debug ("Merchant server getContract(/econtractRequest) contract:");
logger.debug ("Merchant server getContract(/econtractRequest) contract:" + contract);
if (contract != null)
return Response.status(Response.Status.OK).entity(contract).build();
else
......@@ -246,7 +254,7 @@ public class MerchantRsc {
}
/**
* econtract send-back
* econtract send-back / payment
*/
@POST
@Path("{name}/econtractSendBack")
......@@ -268,15 +276,15 @@ public class MerchantRsc {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Account not created at the Trusted Authority").build();
}
boolean result = merchant.econtractSendBack(commandDetails);
String result = merchant.econtractSendBack(commandDetails);
logger.debug ("Merchant server econtractSBack(/econtractSendBack) result:"+result);
if (result) {
if (result == null) {
endPaymentProcess = System.nanoTime();
elapsedTimePaymentProcess = endPaymentProcess - startPaymentProcess;
logger.error("MAMM;TIME;PAYMENTPROCESSING;"+";"+elapsedTimePaymentProcess);
logger.error("MAMM;TIME;PAYMENTPROCESSING;"+";"+elapsedTimePaymentProcess/1000000);
return Response.status(Response.Status.OK).build();
}else
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(result).build();
}catch(Exception e) {
logger.error(e,e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment