From 70a3b3328de58cfac991bb6c2e2d6b622e6b26c1 Mon Sep 17 00:00:00 2001 From: GME Date: Mon, 15 Apr 2019 20:58:58 +0200 Subject: [PATCH] Clean API --- .../java/blockchain/configuration/Config.java | 11 ----- .../WalletImplementation.java | 10 ---- .../java/restService/SessionResource.java | 8 +-- src/main/java/restService/StringResponse.java | 41 ---------------- .../java/restService/TransactionResource.java | 32 ++++++------ src/main/java/restService/UserResource.java | 49 +++++++++---------- src/main/java/restService/WalletResource.java | 41 +++++----------- .../UserImplementationTest.java | 2 +- 8 files changed, 57 insertions(+), 137 deletions(-) delete mode 100644 src/main/java/restService/StringResponse.java diff --git a/src/main/java/blockchain/configuration/Config.java b/src/main/java/blockchain/configuration/Config.java index c3f8938..8b8d881 100644 --- a/src/main/java/blockchain/configuration/Config.java +++ b/src/main/java/blockchain/configuration/Config.java @@ -1,21 +1,16 @@ package blockchain.configuration; public class Config { - //TEST - public static final String u2 = "woEXRrZdwXiu"; //ORGANIZATION public static final String ORG1_MSP = "Org1MSP"; - public static final String ORG1 = "Org1"; //ADMIN public static final String ADMIN = "admin"; - public static final String ADMIN_PASSWORD = "adminpw"; //USER - //public static final String UserPWD = "ZIidLJHQbLcC"; public static final String UserPWD = "OelXVCdHnYPe"; @@ -44,23 +39,17 @@ public class Config { public static final String ORG1_PEER_0_URL = "grpc://vps577432.ovh.net:7051"; //public static final String ORG1_PEER_0_URL = "grpc://93.30.148.59:7051"; - //public static final String ORG1_PEER_0_URL = "grpc://93.30.148.59:8051"; //ublic static final String ORG1_PEER_0_URL = "grpc://vps592802.ovh.net:7051"; //PEER 1 public static final String ORG_PEER = "peer1.org1.example.com"; - public static final String ORG_PEER_URL = "grpc://vps592802.ovh.net:9051"; - //EVENTHUB - public static final String ORG_PEER_EVENT_URL = "grpc://vps592802.ovh.net:9053"; //ADMIN FILE TEST public static final String ADMIN_FILE_PATH = "msp/AdminFiles"; - public static final String ADMIN_KEY_PATH = ADMIN_FILE_PATH+"/keystore"; - public static final String ADMIN_CERT_PATH = ADMIN_FILE_PATH+"/admincerts"; } diff --git a/src/main/java/restImplementation/WalletImplementation.java b/src/main/java/restImplementation/WalletImplementation.java index fac038a..df76f5f 100644 --- a/src/main/java/restImplementation/WalletImplementation.java +++ b/src/main/java/restImplementation/WalletImplementation.java @@ -27,7 +27,6 @@ public class WalletImplementation { User user = userDao.getUserWithHash(wallet.getUser_hash()); wallet.setWallet_hash(generateWalletHash(user.getName()+wallet.getType()+user.getFirstname()+ RandomStringUtils.randomAlphabetic(10))); - transactionWrapper.sendTransaction("initWallet",new String[]{wallet.getWallet_hash(),wallet.getType(),wallet.getUser_hash()}); //Create Wallet in Blockchain wallet.setBalance(0.0); @@ -35,17 +34,8 @@ public class WalletImplementation { long now = Instant.now().toEpochMilli(); wallet.setCreation_date(now); wallet.setModification_date(now); - walletDao.addWallet(wallet); // add wallet to replica DB - /* - response.put("wallet_hash",wallet.getWallet_hash()); - response.put("wallet_type",wallet.getType()); - response.put("wallet_balance",wallet.getBalance().toString()); - response.put("owner_hash",wallet.getUser_hash()); - response.put("response","true"); - */ - return wallet; } diff --git a/src/main/java/restService/SessionResource.java b/src/main/java/restService/SessionResource.java index 7e2f92d..1f3b8d8 100644 --- a/src/main/java/restService/SessionResource.java +++ b/src/main/java/restService/SessionResource.java @@ -20,8 +20,8 @@ public class SessionResource { int session_id = sessionImplementation.startSession(user_hash,user_email); return ResponseEntity.status(HttpStatus.OK).body("{\"session_id\":"+session_id+"}"); } catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @@ -34,8 +34,8 @@ public class SessionResource { sessionImplementation.endSession(session_id); return ResponseEntity.status(HttpStatus.OK).body(""); } catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } diff --git a/src/main/java/restService/StringResponse.java b/src/main/java/restService/StringResponse.java deleted file mode 100644 index 9872e90..0000000 --- a/src/main/java/restService/StringResponse.java +++ /dev/null @@ -1,41 +0,0 @@ -package restService; - -public class StringResponse { - private String response; - private String user_hash; - private Double userBalance; - - public StringResponse(String response){ - this.response = response; - } - - public StringResponse(String response, Double userBalance){ - this.response=response; - this.userBalance=userBalance; - } - - public StringResponse(String response, String userHash){ - this.response=response; - this.user_hash=userHash; - } - - public String getResponse() { - return response; - } - - public void setResponse(String response) { - this.response = response; - } - - public String getUserHash() { - return user_hash; - } - - public void setUserHash(String userHash) { - this.user_hash = userHash; - } - - public Double getUserBalance(){return userBalance;} - - public void setUserBalance(Double userBalance){this.userBalance=userBalance;} -} diff --git a/src/main/java/restService/TransactionResource.java b/src/main/java/restService/TransactionResource.java index 957e5d3..e0bcd6b 100644 --- a/src/main/java/restService/TransactionResource.java +++ b/src/main/java/restService/TransactionResource.java @@ -29,8 +29,8 @@ public class TransactionResource { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"response\":\""+mapResponse.get("message")+"\"}"); } }catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @@ -47,8 +47,8 @@ public class TransactionResource { return new ResponseEntity("{\"response\":\"error\"}", HttpStatus.NOT_FOUND); } }catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @@ -62,8 +62,8 @@ public class TransactionResource { List> listTransaction = transactionImplementation.getAllSentTransaction(user_hash); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } //GET SENT FROM WALLET @@ -75,8 +75,8 @@ public class TransactionResource { List listTransaction = transactionImplementation.getSentTransaction(wallet_hash); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @@ -91,8 +91,8 @@ public class TransactionResource { List> listTransaction = transactionImplementation.getAllReceivedTransaction(user_hash); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } //GET RECEIVED FROM WALLET @@ -104,8 +104,8 @@ public class TransactionResource { List listTransaction = transactionImplementation.getReceivedTransaction(wallet_hash); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @@ -117,8 +117,8 @@ public class TransactionResource { List listTransaction = transactionImplementation.getLatestTransactions(user_hash); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @@ -130,8 +130,8 @@ public class TransactionResource { List listTransaction = transactionImplementation.getAllTransactions(user_hash); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } } diff --git a/src/main/java/restService/UserResource.java b/src/main/java/restService/UserResource.java index 9fcb9e0..b6d0215 100644 --- a/src/main/java/restService/UserResource.java +++ b/src/main/java/restService/UserResource.java @@ -30,26 +30,26 @@ public class UserResource { String r = "{\"response\":\"ok\",\"user_hash\":\""+userHashResponse.get("user_hash")+"\"}"; return ResponseEntity.status(HttpStatus.OK).body(r); }else { - StringResponse responseS = new StringResponse("User already exist"); - return ResponseEntity.status(HttpStatus.FOUND).body(responseS); + String r = "{\"response\":\"User already exist\"}"; + return ResponseEntity.status(HttpStatus.FOUND).body(r); } }catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @RequestMapping(value = "/approve", method = RequestMethod.POST, produces = "application/json") @ResponseStatus(HttpStatus.OK) - public ResponseEntity approveUser(@Valid @RequestBody User user){ + public ResponseEntity approveUser(@Valid @RequestBody User user){ try{ UserImplementation userImplementation = new UserImplementation(); userImplementation.approveUser(user); - StringResponse response = new StringResponse("Ok",user.getUser_hash()); - return ResponseEntity.status(HttpStatus.OK).body(response); + String r = "{\"response\":\"ok\",\"user_hash\":\""+user.getUser_hash()+"\"}"; + return ResponseEntity.status(HttpStatus.OK).body(r); }catch (Exception e){ - StringResponse response = new StringResponse("Error: "+e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @@ -61,30 +61,29 @@ public class UserResource { Map response = userImplementation.userLogger(user); switch (response.get("response")){ case "Not Exist" : { - StringResponse responseS = new StringResponse("Not Found"); - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(responseS); + String r = "{\"response\":\"Not Found\"}"; + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(r); } case "Not Allowed" :{ - StringResponse responseS = new StringResponse("Wrong authentication"); - return ResponseEntity.status(HttpStatus.FORBIDDEN).body(responseS); + String r = "{\"response\":\"Wrong authentication\"}"; + return ResponseEntity.status(HttpStatus.FORBIDDEN).body(r); } case "" :{ - StringResponse responseS = new StringResponse("Error"); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\"Error\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } case "true":{ - //StringResponse responseS = new StringResponse("Ok",response.get("user_hash")); String r = "{\"response\":\"ok\",\"user_hash\":\""+response.get("user_hash")+"\"}"; return ResponseEntity.status(HttpStatus.OK).body(r); } default:{ - StringResponse responseS = new StringResponse("Error"); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\"Error\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } }catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @@ -106,15 +105,15 @@ public class UserResource { @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json") @ResponseStatus(HttpStatus.OK) - public ResponseEntity removeUser(@Valid @RequestBody User user){ + public ResponseEntity removeUser(@Valid @RequestBody User user){ try{ UserImplementation userImplementation = new UserImplementation(); userImplementation.deleteUser(user); - StringResponse responseS = new StringResponse("Ok"); - return ResponseEntity.status(HttpStatus.OK).body(responseS); + String r = "{\"response\":\"ok\"}"; + return ResponseEntity.status(HttpStatus.OK).body(r); }catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } diff --git a/src/main/java/restService/WalletResource.java b/src/main/java/restService/WalletResource.java index f22f1c5..ede609d 100644 --- a/src/main/java/restService/WalletResource.java +++ b/src/main/java/restService/WalletResource.java @@ -19,27 +19,10 @@ public class WalletResource { try{ WalletImplementation walletImplementation = new WalletImplementation(); Wallet walletResponse = walletImplementation.createWallet(wallet); - - /* - Wallet returnWallet = new Wallet(); - returnWallet.setWallet_hash(walletResponse.get("wallet_hash")); - returnWallet.setUser_hash(wallet.getUser_hash()); - returnWallet.setBalance(Double.parseDouble(walletResponse.get("wallet_balance"))); - returnWallet.setType(walletResponse.get("wallet_type")); - */ - return ResponseEntity.status(HttpStatus.OK).body(walletResponse); - /* - if(Boolean.parseBoolean(walletResponse.get("response"))){ - - }else { - StringResponse responseS = new StringResponse("Wallet already exist"); - return ResponseEntity.status(HttpStatus.FOUND).body(responseS); - } - */ }catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @@ -51,8 +34,8 @@ public class WalletResource { JsonArray wallets = walletImplementation.getAllUserWallets(user_hash); return ResponseEntity.status(HttpStatus.OK).body(wallets); } catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @@ -68,8 +51,8 @@ public class WalletResource { return ResponseEntity.status(HttpStatus.NOT_FOUND).body("{\"response\":null}"); } }catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @@ -81,8 +64,8 @@ public class WalletResource { walletImplementation.setBalanceToWallet(BalanceWallet.getAssociation_hash(), BalanceWallet.getAssocation_pwd(), BalanceWallet.getWallet_hash(),BalanceWallet.getAmount()); return ResponseEntity.status(HttpStatus.OK).body("{\"response\":\"ok\"}"); } catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @@ -94,8 +77,8 @@ public class WalletResource { walletImplementation.transferWallet(wallet.getWallet_hash(),wallet.getUser_hash()); return ResponseEntity.status(HttpStatus.OK).body("{\"response\":\"ok\"}"); } catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @@ -108,8 +91,8 @@ public class WalletResource { walletImplementation.deleteWallet(walletHash,userHash); return ResponseEntity.status(HttpStatus.OK).body("{\"response\":\"ok\"}"); }catch (Exception e){ - StringResponse responseS = new StringResponse(e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); + String r = "{\"response\":\""+e.getMessage()+"\"}"; + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } } diff --git a/src/test/java/restImplementation/UserImplementationTest.java b/src/test/java/restImplementation/UserImplementationTest.java index c53f8aa..e19d3ed 100644 --- a/src/test/java/restImplementation/UserImplementationTest.java +++ b/src/test/java/restImplementation/UserImplementationTest.java @@ -5,7 +5,7 @@ import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Logger; import org.junit.Ignore; import org.junit.Test; -import restService.StringResponse; + import java.util.Map; @Ignore