Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package NoSSL.stakeholder;
- import java.io.BufferedReader;
- import java.io.File;
- //import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.io.StringReader;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- import stakeholder.Bank;
- import messages.Authorization_Data;
- import messages.Authorization_Response_MB;
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- import parsers.Authorization_Data_Parser2;
- import parsers.Authorization_Response_MB_Parser;
- public class Merchant {
- //C:\Users\sce\workspace\SCE_HA
- //static final String AUTHORIZATION_REQUEST = "C://Users/sce/workspace/SCE_HA/src/message_data/AuthorizationRequestXML.xml";
- //static final String AUTHORIZATION_RESPONSE_CM = "C://Users/sce/workspace/SCE_HA/src/message_data/AuthorizationResponseCMXML.xml";
- public Merchant() {
- }
- public static void main(String[] args) throws IOException {
- String typeOfBehaviour = "";
- String messageType = "";
- DefaultHandler dh;
- SAXParser p1;
- SAXParserFactory spfactory = SAXParserFactory.newInstance();
- Authorization_Data message1 = new Authorization_Data();
- Authorization_Response_MB message2 = new Authorization_Response_MB();
- if (args != null && args.length > 0) {
- typeOfBehaviour = args[0];
- }
- // if (typeOfBehaviour.equals("server")) {
- int port = 4442;
- ServerSocket server;
- PrintWriter out;
- BufferedReader in;
- File file;
- try {
- server = new ServerSocket(port);
- System.out.println("Starting Merchant Server");
- try {
- Socket client = server.accept();
- out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
- in = new BufferedReader(new InputStreamReader(client.getInputStream()));
- /*
- * while (0 < (in.read())) {inputData=inputData +
- * (String)in.readLine(); } System.out.println(inputData);
- */
- StringBuffer stringbuffer = new StringBuffer();
- String input = in.readLine();
- //while (input != null) {
- while(!input.equals("***EOM***") && input != null){
- //System.out.println("reading input");
- stringbuffer.append(input);
- stringbuffer.append("\n");
- input = in.readLine();
- }
- String inputData = stringbuffer.toString();
- //Just to check if the message is received
- System.out.println("PRINTING INPUTDATA: " + inputData);
- //Checks what type of data has been sent, in order to know how to parse the message
- if(inputData.contains("authorization_data")){
- try{
- dh = new Authorization_Data_Parser2();
- p1 = spfactory.newSAXParser();
- p1.parse(new InputSource(new StringReader(inputData)), dh);
- message1 = ((Authorization_Data_Parser2)dh).results();
- System.out.println("AUTHORIZATION: " + message1.authorization_data());
- //Setting message type, in order to know which kind of message needs to be generated
- messageType = "authorization_request";
- System.out.println("Authorization Data is parsed");
- //BEGIN WAIT
- /*StringBuffer stringbuffer2 = new StringBuffer();
- //in.readline causes the consumer to wait for message from merchant
- String input2 = in.readLine();
- //while(input != null){
- while(!input2.equals("***EOM***") && input2 != null){
- //System.out.println("Consumer recevies message test while schleife");
- stringbuffer2.append(input2);
- stringbuffer2.append("\n");
- input2 = in.readLine();
- }
- String inputData2 = stringbuffer2.toString();
- System.out.println("PRINTING INPUTDATA AUTHORIZATION RESPONSE: " + inputData2);*/
- //END WAIT
- }catch(ParserConfigurationException e){
- System.out.println("Parsing has failed");
- }catch(SAXException e){
- System.out.println("SAXexception");
- }
- //Checks what type of data has been sent, in order to know how to parse the message
- } else if (inputData.contains("authorization_response_mb")){
- try{
- dh = new Authorization_Response_MB_Parser();
- p1 = spfactory.newSAXParser();
- p1.parse(new InputSource(new StringReader(inputData)),dh);
- message2 = ((Authorization_Response_MB_Parser)dh).results();
- System.out.println("AUTH RESPONSE MB"+ message2.authorization_response_MB());
- System.out.println("Authorization Response MB is parsed");
- //Setting message type, in order to know which kind of message needs to be generated
- messageType = "authorization_response_cm";
- }catch(ParserConfigurationException e){
- System.out.println("Parsing has failed");
- }catch(SAXException e){
- System.out.println("SAXexception");
- }
- }
- String authorization_response_cm = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
- "<!DOCTYPE MESSAGES SYSTEM \"C://Users/Thomas/Desktop/Eclipse/workspace/SCE_HA/src/message_data/Authorization_Response_CM_Schema.dtd\">\n"+
- "<AUTHORIZATION_RESPONSE_CM>\n" +
- "<authorization_response_cm> \n"+
- "<operation_id>"+message2.getId()+"</operation_id>\n"+
- "<panSender>"+message2.getPanSender()+"</panSender>\n"+
- "<panReceiver>"+message2.getPanReceiver()+"</panReceiver>\n"+
- "<expiration_date>"+message2.getExpirationData()+"</expiration_date>\n"+
- "<CVV2>"+message2.getCVV2()+"</CVV2>\n"+
- "<name>"+message2.getName()+"</name>\n"+
- "<last_name>"+message2.getLastName()+"</last_name>\n"+
- "<total_amount>"+message2.getTotalAmount()+"</total_amount>\n"+
- "<currency>"+message2.getCurrency()+"</currency>\n"+
- "</authorization_response_cm>\n"+
- "</AUTHORIZATION_RESPONSE_CM>";
- System.out.println("Message Authorization Response CM built");
- out.write(authorization_response_cm);
- out.write("\n***EOM***\n");
- out.flush();
- System.out.println("authorization response cm sent");
- //String input_bank1 = in.readLine();
- //while(input_bank1 != null){
- /* while(!input_bank1.equals("***EOM***") && input != null){
- System.out.println("Merchant recevies message test while schleife");
- stringbuffer.append(input_bank1);
- stringbuffer.append("\n");
- input_bank1 = in.readLine();
- }
- String inputData_bank1 = stringbuffer.toString();
- System.out.println("PRINTING INPUTDATA AUTHORIZATION RESPONSE MB: " + inputData_bank1);*/
- try {
- // Closing the communication channel by closing buffers
- // and socket
- //in.close();
- //out.close();
- client.close();
- System.out.println("Closed");
- } catch (IOException e) {
- System.out
- .println("Error when closing buffers and socket");
- e.printStackTrace();
- System.exit(-4);
- }
- } catch (IOException e) {
- System.out
- .println("Error when sending and receiving data through socket");
- e.printStackTrace();
- System.exit(-3);
- }
- } catch (IOException e) {
- System.out.println("Error when creating the socket");
- e.printStackTrace();
- System.exit(-2);
- }
- //System.exit(0);
- //}
- // Client Behaviour starts at this point
- // else if (typeOfBehaviour.equals("client")) {
- System.out.println("Client started");
- int portClient = 4443;
- String servername = "localhost";
- Socket socket;
- PrintWriter outClient;
- BufferedReader inClient;
- try {
- socket = new Socket(servername, portClient);
- try {
- // Open Streams for Sending and Receiving Data
- outClient = new PrintWriter(new OutputStreamWriter(
- socket.getOutputStream()));
- inClient = new BufferedReader(new InputStreamReader(
- socket.getInputStream()));
- //According to the previously set message type, a message is built
- if(messageType =="authorization_request"){
- String authorization_request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
- "<!DOCTYPE MESSAGES SYSTEM \"C://Users/Thomas/Desktop/Eclipse/workspace/SCE_HA/src/message_data/Authorization_Request_Schema.dtd\">\n"+
- "<AUTHORIZATION_REQUEST>\n" +
- "<authorization_request> \n"+
- "<operation_id>"+message1.getId()+"</operation_id>\n"+
- "<panSender>"+message1.getPanSender()+"</panSender>\n"+
- "<panReceiver>"+message1.getPanReceiver()+"</panReceiver>\n"+
- "<expiration_date>"+message1.getExpirationData()+"</expiration_date>\n"+
- "<CVV2>"+message1.getCVV2()+"</CVV2>\n"+
- "<name>"+message1.getName()+"</name>\n"+
- "<last_name>"+message1.getLastName()+"</last_name>\n"+
- "<total_amount>"+message1.getTotalAmount()+"</total_amount>\n"+
- "<currency>"+message1.getCurrency()+"</currency>\n"+
- "</authorization_request>\n"+
- "</AUTHORIZATION_REQUEST>";
- System.out.println("Authorization request built");
- outClient.write(authorization_request);
- outClient.write("\n***EOM***\n");
- outClient.flush();
- System.out.println("authorization request sent");
- // Problem receiving data through socket
- StringBuffer stringbuffer = new StringBuffer();
- String input2 = inClient.readLine();
- while(!input2.equals("***EOM***") && input2 != null){
- stringbuffer.append(input2);
- stringbuffer.append("\n");
- input2 = inClient.readLine();
- }
- String inputData = stringbuffer.toString();
- System.out.println("RESPONSE FROM BANK1 to MERCHANT: " + inputData);
- try{
- dh = new Authorization_Response_MB_Parser();
- p1 = spfactory.newSAXParser();
- p1.parse(new InputSource(new StringReader(inputData)),dh);
- message2 = ((Authorization_Response_MB_Parser)dh).results();
- System.out.println("AUTH RESPONSE MB"+ message2.authorization_response_MB());
- System.out.println("Authorization Response MB is parsed");
- //Setting message type, in order to know which kind of message needs to be generated
- messageType = "authorization_response_cm";
- }catch(ParserConfigurationException e){
- System.out.println("Parsing has failed");
- }catch(SAXException e){
- System.out.println("SAXexception");
- }
- //waiting to receive Authorization response MB
- /*StringBuffer stringbuffer = new StringBuffer();
- String input = inClient.readLine();
- //while(input != null){
- while(!input.equals("***EOM***") && input != null){
- stringbuffer.append(input);
- stringbuffer.append("\n");
- input = inClient.readLine();
- }
- String inputData = stringbuffer.toString();
- System.out.println("PRINTING INPUTDATA AUTHORIZATION RESPONSE: " + inputData);*/
- }else {
- // der hier muss auch im XML Format aufgebaut sein, siehe Authorization_Data und Request
- //String authorization_response_cm = "<authorization_response_cm id=" + message1.getId() + " type = authorization_response_cm><operation_id>" + message1.getId() + "</operation_id><panSender>" + message1.getPanSender() + "</panSender><panReceiver>" + message1.getPanReceiver() + "</panReceiver><expiration_date>" + message1.getExpirationData() + "</expiration_date><CVV2>"+ message1.getCVV2()+"</CVV2><name>"+ message1.getName()+"</name><last_name>"+message1.getLastName()+"</last_name><total_amount>"+message1.getTotalAmount()+"</total_amount><currency>"+message1.getCurrency()+"</currency></authorization_response_cm>";
- //outClient.print(authorization_response_cm);
- String authorization_response_cm = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
- "<!DOCTYPE MESSAGES SYSTEM \"C://Users/Thomas/Desktop/Eclipse/workspace/SCE_HA/src/message_data/Authorization_Response_CM_Schema.dtd\">\n"+
- "<AUTHORIZATION_RESPONSE_CM>\n" +
- "<authorization_response_cm> \n"+
- "<operation_id>"+message2.getId()+"</operation_id>\n"+
- "<panSender>"+message2.getPanSender()+"</panSender>\n"+
- "<panReceiver>"+message2.getPanReceiver()+"</panReceiver>\n"+
- "<expiration_date>"+message2.getExpirationData()+"</expiration_date>\n"+
- "<CVV2>"+message2.getCVV2()+"</CVV2>\n"+
- "<name>"+message2.getName()+"</name>\n"+
- "<last_name>"+message2.getLastName()+"</last_name>\n"+
- "<total_amount>"+message2.getTotalAmount()+"</total_amount>\n"+
- "<currency>"+message2.getCurrency()+"</currency>\n"+
- "</authorization_response_cm>\n"+
- "</AUTHORIZATION_RESPONSE_CM>";
- outClient.write("\n***EOM***\n");
- outClient.flush();
- System.out.println("Authorization Response CM built");
- outClient.write(authorization_response_cm);
- outClient.write("\n***EOM***\n");
- System.out.println("authorization response cm sent");
- }
- //outClient.flush();
- //here merchant is waiting for communication from bank1
- System.out.println("Closed");
- } catch (IOException e) {
- System.out
- .println("Error when sending and receiving data through socket");
- e.printStackTrace();
- System.exit(-5);
- }
- } catch (UnknownHostException e) {
- System.out.println("Impossible to conect to local host "
- + servername + "unresolved name");
- e.printStackTrace();
- System.exit(-3);
- } catch (IOException e) {
- System.out.println("Error when creating the Clientsocket");
- e.printStackTrace();
- System.exit(-2);
- }
- //System.exit(0);
- }
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement