BrightSide Workbench Full Report + Source Code
HandshakeUtil.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.financials.handshake;
20 
21 import java.io.IOException;
22 import java.util.Map;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.servlet.ServletContext;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.direct.DirectContent;
30 import org.turro.elephant.direct.DirectContents;
31 import org.turro.elephant.direct.IDirectContent;
32 import org.turro.financials.entity.Contract;
33 import org.turro.financials.entity.ContractHandshake;
34 import org.turro.financials.entity.Document;
35 import org.turro.financials.entity.Product;
36 import org.turro.http.ElephantPost;
37 import org.turro.http.ElephantResponse;
38 import org.turro.http.ElephantResponseType;
39 
44 @DirectContent(identifier="handshake")
45 public class HandshakeUtil implements IDirectContent {
46 
47  public static String getIdentifier() {
48  return HandshakeUtil.class.getAnnotation(DirectContent.class).identifier();
49  }
50 
51  @Override
52  public boolean itsMe(String id) {
53  return getIdentifier().equals(id);
54  }
55 
56  @Override
57  public boolean myTurn(HttpServletRequest request) {
58  return DirectContents.isYourTurn(request, getIdentifier());
59  }
60 
61  @Override
62  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
63  try {
64  if(!ElephantPost.isValid(request)) return;
65  Map<String, String[]> pars = request.getParameterMap();
66  HandshakeAction ha = HandshakeAction.getAction(pars.containsKey("action") ? pars.get("action")[0] : null);
68  switch(ha) {
69  case HANDSHAKE_STATUS:
70  hc = HandshakeContract.createHandshakeFromJson(pars.containsKey("contract") ? pars.get("contract")[0] : null);
71  // check handshake
72  HandshakeStatus status;
73  if(hc == null) {
75  } else {
77  if(ch == null) {
79  } else if(!ch.getContract().isActive()) {
80  status = HandshakeStatus.HS_BROKEN;
81  } else {
83  }
84  }
85  ElephantResponse.writeToResponse(response, ElephantResponseType.RESPONSE_CORRECT, "HANDSHAKE SERVER", status.toString());
86  break;
87  case HANDSHAKE_PETITION:
88  hc = HandshakeContract.createHandshakeFromJson(pars.containsKey("contract") ? pars.get("contract")[0] : null);
89  // save contractxxxx.hs
92  ElephantResponse.writeToResponse(response, ElephantResponseType.RESPONSE_CORRECT, "HANDSHAKE SERVER: PETITION SEND", status.toString());
93  break;
94  case HANDSHAKE_ACCEPTANCE:
95  hc = HandshakeContract.createHandshakeFromJson(pars.containsKey("contract") ? pars.get("contract")[0] : null);
96  // update remoteId from handshake
98  pars.containsKey("remoteServer") ? pars.get("remoteServer")[0] : null,
99  pars.containsKey("remoteId") ? pars.get("remoteId")[0] : null,
100  hc.remoteId)) {
102  ElephantResponse.writeToResponse(response, ElephantResponseType.RESPONSE_CORRECT, "HANDSHAKE SERVER: PETITION ACCEPTED", status.toString());
103  } else {
104  status = HandshakeStatus.HS_BROKEN;
105  ElephantResponse.writeToResponse(response, ElephantResponseType.RESPONSE_FAILED, "HANDSHAKE SERVER: PETITION FAILED", status.toString());
106  }
107  break;
108  case HANDSHAKE_DOCUMENT:
109  HandshakeDocument hd = HandshakeDocument.createHandshakeFromJson(pars.containsKey("document") ? pars.get("document")[0] : null);
110  if(hd != null) {
113  ElephantResponse.writeToResponse(response, ElephantResponseType.RESPONSE_CORRECT, "HANDSHAKE SERVER: DOCUMENT ACCEPTED", status.toString());
114  }
115  break;
116  case HANDSHAKE_PRODUCT:
117  break;
118  }
119  } catch (IOException ex) {
120  Logger.getLogger(HandshakeUtil.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
121  }
122  }
123 
124  public static ElephantResponse checkStatus(Contract contract) {
125  ContractHandshake handshake = contract.getContractHandshake();
126  if(handshake != null) {
127  try {
128  ElephantPost ep = new ElephantPost(DirectContents.createURL(handshake.getRemoteServer(), getIdentifier()));
129  ep.addParameter("contract", HandshakeContract.createJsonFromContract(contract));
130  ep.addParameter("action", HandshakeAction.HANDSHAKE_STATUS.toString());
131  return ep.doPost();
132  } catch (IOException ex) {
133  Logger.getLogger(HandshakeUtil.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
134  }
135  }
136  return null;
137  }
138 
139  public static ElephantResponse sendPetition(Contract contract, String remoteUrl) {
140  try {
141  ElephantPost ep = new ElephantPost(DirectContents.createURL(remoteUrl, getIdentifier()));
142  ep.addParameter("contract", HandshakeContract.createJsonFromContract(contract));
143  ep.addParameter("action", HandshakeAction.HANDSHAKE_PETITION.toString());
144  return ep.doPost();
145  } catch (IOException ex) {
146  Logger.getLogger(HandshakeUtil.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
147  }
148  return null;
149  }
150 
151  public static ElephantResponse sendAcceptance(Contract contract, String remoteServer, Long remoteId) {
152  ContractHandshake handshake = contract.getContractHandshake();
153  if(handshake != null) {
154  try {
155  ElephantPost ep = new ElephantPost(DirectContents.createURL(handshake.getRemoteServer(), getIdentifier()));
156  ep.addParameter("remoteServer", remoteServer + "");
157  ep.addParameter("remoteId", remoteId + "");
158  ep.addParameter("contract", HandshakeContract.createJsonFromContract(contract));
159  ep.addParameter("action", HandshakeAction.HANDSHAKE_ACCEPTANCE.toString());
160  return ep.doPost();
161  } catch (IOException ex) {
162  Logger.getLogger(HandshakeUtil.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
163  }
164  }
165  return null;
166  }
167 
168  public static ElephantResponse sendDocument(Document document) {
169  Contract contract = document.getContract();
170  ContractHandshake handshake = contract != null ? contract.getContractHandshake() : null;
171  if(handshake != null) {
172  try {
173  ElephantPost ep = new ElephantPost(DirectContents.createURL(handshake.getRemoteServer(), getIdentifier()));
174  ep.addParameter("document", HandshakeDocument.createJsonFromDocument(document));
175  ep.addParameter("action", HandshakeAction.HANDSHAKE_DOCUMENT.toString());
176  return ep.doPost();
177  } catch (IOException ex) {
178  Logger.getLogger(HandshakeUtil.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
179  }
180  }
181  return null;
182  }
183 
184  public static ElephantResponse sendProduct(Contract contract, Product product) {
185  ContractHandshake handshake = contract != null ? contract.getContractHandshake() : null;
186  if(handshake != null) {
187  try {
188  ElephantPost ep = new ElephantPost(DirectContents.createURL(handshake.getRemoteServer(), getIdentifier()));
189  ep.addParameter("contract", HandshakeContract.createJsonFromContract(contract));
190  ep.addParameter("product", HandshakeProduct.createJsonFromProduct(product));
191  ep.addParameter("action", HandshakeAction.HANDSHAKE_PRODUCT.toString());
192  return ep.doPost();
193  } catch (IOException ex) {
194  Logger.getLogger(HandshakeUtil.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
195  }
196  }
197  return null;
198  }
199 
200 }
static boolean isYourTurn(HttpServletRequest request, String path)
static String createURL(String server, String id)
ContractHandshake getContractHandshake()
Definition: Contract.java:328
static void saveHandshakePetition(HandshakeContract hc)
static String createJsonFromContract(Contract contract)
static ContractHandshake getContractHandshakeFromHandshake(HandshakeContract hc)
static boolean updateRemoteId(String remoteServer, String remoteId, long contractId)
static HandshakeContract createHandshakeFromJson(String value)
static HandshakeDocument createHandshakeFromJson(String value)
static void saveHandshakeDocument(HandshakeDocument hd)
static String createJsonFromDocument(Document document)
static String createJsonFromProduct(Product product)
static ElephantResponse sendAcceptance(Contract contract, String remoteServer, Long remoteId)
static ElephantResponse sendDocument(Document document)
static ElephantResponse sendPetition(Contract contract, String remoteUrl)
static ElephantResponse checkStatus(Contract contract)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
static ElephantResponse sendProduct(Contract contract, Product product)
boolean myTurn(HttpServletRequest request)
ElephantResponse doPost()
void addParameter(String name, String value)
static boolean isValid(HttpServletRequest request)
static void writeToResponse(HttpServletResponse response, ElephantResponseType type, String message, String code)
static HandshakeAction getAction(String value)