Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.28 KB | None | 0 0
  1. package NoSSL.stakeholder;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. //import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStreamWriter;
  9. import java.io.PrintWriter;
  10. import java.io.StringReader;
  11. import java.net.ServerSocket;
  12. import java.net.Socket;
  13. import java.net.UnknownHostException;
  14.  
  15. import javax.xml.parsers.ParserConfigurationException;
  16. import javax.xml.parsers.SAXParser;
  17. import javax.xml.parsers.SAXParserFactory;
  18.  
  19. import stakeholder.Bank;
  20.  
  21. import messages.Authorization_Data;
  22. import messages.Authorization_Response_MB;
  23.  
  24. import org.xml.sax.InputSource;
  25. import org.xml.sax.SAXException;
  26. import org.xml.sax.helpers.DefaultHandler;
  27.  
  28. import parsers.Authorization_Data_Parser2;
  29. import parsers.Authorization_Response_MB_Parser;
  30.  
  31. public class Merchant {
  32.     //C:\Users\sce\workspace\SCE_HA
  33.    
  34.     //static final String AUTHORIZATION_REQUEST = "C://Users/sce/workspace/SCE_HA/src/message_data/AuthorizationRequestXML.xml";
  35.     //static final String AUTHORIZATION_RESPONSE_CM = "C://Users/sce/workspace/SCE_HA/src/message_data/AuthorizationResponseCMXML.xml";
  36.  
  37.     public Merchant() {
  38.  
  39.     }
  40.  
  41.     public static void main(String[] args) throws IOException {
  42.  
  43.        
  44.         String typeOfBehaviour = "";
  45.         String messageType = "";
  46.         DefaultHandler dh;
  47.         SAXParser p1;
  48.         SAXParserFactory spfactory = SAXParserFactory.newInstance();
  49.         Authorization_Data message1 = new Authorization_Data();
  50.         Authorization_Response_MB message2 = new Authorization_Response_MB();
  51.  
  52.         if (args != null && args.length > 0) {
  53.  
  54.             typeOfBehaviour = args[0];
  55.         }
  56.  
  57. //      if (typeOfBehaviour.equals("server")) {
  58.  
  59.             int port = 4442;
  60.             ServerSocket server;
  61.             PrintWriter out;
  62.             BufferedReader in;
  63.             File file;
  64.  
  65.             try {
  66.                 server = new ServerSocket(port);
  67.  
  68.                 System.out.println("Starting Merchant Server");
  69.                
  70.                 try {
  71.  
  72.                     Socket client = server.accept();
  73.  
  74.                     out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
  75.                     in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  76.  
  77.                     /*
  78.                      * while (0 < (in.read())) {inputData=inputData +
  79.                      * (String)in.readLine(); } System.out.println(inputData);
  80.                      */
  81.  
  82.                     StringBuffer stringbuffer = new StringBuffer();
  83.                     String input = in.readLine();
  84.  
  85.                     //while (input != null) {
  86.                     while(!input.equals("***EOM***") && input != null){
  87.                        
  88.                         //System.out.println("reading input");
  89.                        
  90.                         stringbuffer.append(input);
  91.                         stringbuffer.append("\n");
  92.                         input = in.readLine();
  93.                     }
  94.                    
  95.                     String inputData = stringbuffer.toString();
  96.                    
  97.                     //Just to check if the message is received
  98.                     System.out.println("PRINTING INPUTDATA: " + inputData);
  99.                    
  100.                     //Checks what type of data has been sent, in order to know how to parse the message
  101.                     if(inputData.contains("authorization_data")){
  102.                     try{
  103.                         dh = new Authorization_Data_Parser2();
  104.                        
  105.                         p1 = spfactory.newSAXParser();
  106.                        
  107.                         p1.parse(new InputSource(new StringReader(inputData)), dh);
  108.                        
  109.                         message1 = ((Authorization_Data_Parser2)dh).results();
  110.                        
  111.                        
  112.                         System.out.println("AUTHORIZATION: " + message1.authorization_data());
  113.                        
  114.                         //Setting message type, in order to know which kind of message needs to be generated
  115.                         messageType = "authorization_request";
  116.                         System.out.println("Authorization Data is parsed");
  117.                        
  118.                         //BEGIN WAIT
  119.                         /*StringBuffer stringbuffer2 = new StringBuffer();
  120.                        
  121.                         //in.readline causes the consumer to wait for message from merchant
  122.                         String input2 = in.readLine();
  123.                        
  124.                         //while(input != null){
  125.                         while(!input2.equals("***EOM***") && input2 != null){
  126.                            
  127.                             //System.out.println("Consumer recevies message test while schleife");
  128.                             stringbuffer2.append(input2);
  129.                             stringbuffer2.append("\n");
  130.                             input2 = in.readLine();
  131.                         }
  132.                        
  133.                         String inputData2 = stringbuffer2.toString();
  134.                         System.out.println("PRINTING INPUTDATA AUTHORIZATION RESPONSE: " + inputData2);*/
  135.                         //END WAIT
  136.                        
  137.                        
  138.                        
  139.                     }catch(ParserConfigurationException e){
  140.                         System.out.println("Parsing has failed");
  141.                     }catch(SAXException e){
  142.                         System.out.println("SAXexception");
  143.                     }
  144.                    
  145.                    
  146.                     //Checks what type of data has been sent, in order to know how to parse the message
  147.                     } else if (inputData.contains("authorization_response_mb")){
  148.                        
  149.                         try{
  150.                             dh = new Authorization_Response_MB_Parser();
  151.                            
  152.                             p1 = spfactory.newSAXParser();
  153.                            
  154.                             p1.parse(new InputSource(new StringReader(inputData)),dh);
  155.                            
  156.                             message2 = ((Authorization_Response_MB_Parser)dh).results();
  157.                            
  158.                             System.out.println("AUTH RESPONSE MB"+ message2.authorization_response_MB());
  159.                            
  160.                             System.out.println("Authorization Response MB is parsed");
  161.                            
  162.                             //Setting message type, in order to know which kind of message needs to be generated
  163.                             messageType = "authorization_response_cm";
  164.                         }catch(ParserConfigurationException e){
  165.                             System.out.println("Parsing has failed");
  166.                         }catch(SAXException e){
  167.                             System.out.println("SAXexception");
  168.                         }
  169.                
  170.                     }
  171.                    
  172.                    
  173.                     String authorization_response_cm = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
  174.                     "<!DOCTYPE MESSAGES SYSTEM \"C://Users/Thomas/Desktop/Eclipse/workspace/SCE_HA/src/message_data/Authorization_Response_CM_Schema.dtd\">\n"+
  175.                     "<AUTHORIZATION_RESPONSE_CM>\n" +
  176.                         "<authorization_response_cm> \n"+
  177.                             "<operation_id>"+message2.getId()+"</operation_id>\n"+
  178.                             "<panSender>"+message2.getPanSender()+"</panSender>\n"+
  179.                             "<panReceiver>"+message2.getPanReceiver()+"</panReceiver>\n"+
  180.                             "<expiration_date>"+message2.getExpirationData()+"</expiration_date>\n"+
  181.                             "<CVV2>"+message2.getCVV2()+"</CVV2>\n"+
  182.                             "<name>"+message2.getName()+"</name>\n"+
  183.                             "<last_name>"+message2.getLastName()+"</last_name>\n"+
  184.                             "<total_amount>"+message2.getTotalAmount()+"</total_amount>\n"+
  185.                             "<currency>"+message2.getCurrency()+"</currency>\n"+
  186.                         "</authorization_response_cm>\n"+
  187.                     "</AUTHORIZATION_RESPONSE_CM>";
  188.                    
  189.                    
  190.                     System.out.println("Message Authorization Response CM built");
  191.                     out.write(authorization_response_cm);
  192.                     out.write("\n***EOM***\n");
  193.                    
  194.                     out.flush();
  195.                                                    
  196.                     System.out.println("authorization response cm sent");
  197.  
  198.  
  199.                     //String input_bank1 = in.readLine();
  200.                    
  201.                     //while(input_bank1 != null){
  202. /*                  while(!input_bank1.equals("***EOM***") && input != null){
  203.                        
  204.                         System.out.println("Merchant recevies message test while schleife");
  205.                         stringbuffer.append(input_bank1);
  206.                         stringbuffer.append("\n");
  207.                         input_bank1 = in.readLine();
  208.                     }
  209.                    
  210.                     String inputData_bank1 = stringbuffer.toString();
  211.                     System.out.println("PRINTING INPUTDATA AUTHORIZATION RESPONSE MB: " + inputData_bank1);*/
  212.                    
  213.  
  214.                     try {
  215.                         // Closing the communication channel by closing buffers
  216.                         // and socket
  217.                         //in.close();
  218.                         //out.close();
  219.                         client.close();
  220.                         System.out.println("Closed");
  221.                     } catch (IOException e) {
  222.                         System.out
  223.                                 .println("Error when closing buffers and socket");
  224.                         e.printStackTrace();
  225.                         System.exit(-4);
  226.                     }
  227.                 } catch (IOException e) {
  228.                     System.out
  229.                             .println("Error when sending and receiving data through socket");
  230.                     e.printStackTrace();
  231.                     System.exit(-3);
  232.                 }
  233.             } catch (IOException e) {
  234.                 System.out.println("Error when creating the socket");
  235.                 e.printStackTrace();
  236.                 System.exit(-2);
  237.             }
  238.  
  239.             //System.exit(0);
  240.  
  241.         //}
  242.  
  243. // Client Behaviour starts at this point
  244.        
  245.        
  246.        
  247.        
  248.    
  249.  
  250. //      else if (typeOfBehaviour.equals("client")) {
  251.  
  252.             System.out.println("Client started");
  253.  
  254.             int portClient = 4443;
  255.             String servername = "localhost";
  256.             Socket socket;
  257.             PrintWriter outClient;
  258.             BufferedReader inClient;
  259.  
  260.             try {
  261.                 socket = new Socket(servername, portClient);
  262.  
  263.                 try {
  264.                     // Open Streams for Sending and Receiving Data
  265.  
  266.                     outClient = new PrintWriter(new OutputStreamWriter(
  267.                             socket.getOutputStream()));
  268.                     inClient = new BufferedReader(new InputStreamReader(
  269.                             socket.getInputStream()));
  270.  
  271.                    
  272.                    
  273.                     //According to the previously set message type, a message is built
  274.                     if(messageType =="authorization_request"){     
  275.                         String authorization_request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
  276.                             "<!DOCTYPE MESSAGES SYSTEM \"C://Users/Thomas/Desktop/Eclipse/workspace/SCE_HA/src/message_data/Authorization_Request_Schema.dtd\">\n"+
  277.                             "<AUTHORIZATION_REQUEST>\n" +
  278.                                 "<authorization_request> \n"+
  279.                                     "<operation_id>"+message1.getId()+"</operation_id>\n"+
  280.                                     "<panSender>"+message1.getPanSender()+"</panSender>\n"+
  281.                                     "<panReceiver>"+message1.getPanReceiver()+"</panReceiver>\n"+
  282.                                     "<expiration_date>"+message1.getExpirationData()+"</expiration_date>\n"+
  283.                                     "<CVV2>"+message1.getCVV2()+"</CVV2>\n"+
  284.                                     "<name>"+message1.getName()+"</name>\n"+
  285.                                     "<last_name>"+message1.getLastName()+"</last_name>\n"+
  286.                                     "<total_amount>"+message1.getTotalAmount()+"</total_amount>\n"+
  287.                                     "<currency>"+message1.getCurrency()+"</currency>\n"+
  288.                                 "</authorization_request>\n"+
  289.                             "</AUTHORIZATION_REQUEST>";
  290.                            
  291.                         System.out.println("Authorization request built"); 
  292.                        
  293.                        
  294.                         outClient.write(authorization_request);
  295.                         outClient.write("\n***EOM***\n");
  296.                         outClient.flush();
  297.                        
  298.                         System.out.println("authorization request sent");
  299.                        
  300.  
  301.                         // Problem receiving data through socket
  302.                        
  303.                         StringBuffer stringbuffer = new StringBuffer();
  304.  
  305.                         String input2 = inClient.readLine();
  306.                         while(!input2.equals("***EOM***") && input2 != null){
  307.  
  308.                             stringbuffer.append(input2);
  309.                             stringbuffer.append("\n");
  310.                             input2 = inClient.readLine();
  311.                         }
  312.                        
  313.                         String inputData = stringbuffer.toString();
  314.                        
  315.                         System.out.println("RESPONSE FROM BANK1 to MERCHANT: " + inputData);
  316.  
  317.                         try{
  318.                             dh = new Authorization_Response_MB_Parser();
  319.                            
  320.                             p1 = spfactory.newSAXParser();
  321.                            
  322.                             p1.parse(new InputSource(new StringReader(inputData)),dh);
  323.                            
  324.                             message2 = ((Authorization_Response_MB_Parser)dh).results();
  325.                            
  326.                             System.out.println("AUTH RESPONSE MB"+ message2.authorization_response_MB());
  327.                            
  328.                             System.out.println("Authorization Response MB is parsed");
  329.                            
  330.                             //Setting message type, in order to know which kind of message needs to be generated
  331.                             messageType = "authorization_response_cm";
  332.                         }catch(ParserConfigurationException e){
  333.                             System.out.println("Parsing has failed");
  334.                         }catch(SAXException e){
  335.                             System.out.println("SAXexception");
  336.                         }
  337.                        
  338.                        
  339.                         //waiting to receive Authorization response MB
  340.                         /*StringBuffer stringbuffer = new StringBuffer();
  341.                         String input = inClient.readLine();
  342.                        
  343.                         //while(input != null){
  344.                         while(!input.equals("***EOM***") && input != null){
  345.  
  346.                             stringbuffer.append(input);
  347.                             stringbuffer.append("\n");
  348.                             input = inClient.readLine();
  349.                         }
  350.                        
  351.                         String inputData = stringbuffer.toString();
  352.                         System.out.println("PRINTING INPUTDATA AUTHORIZATION RESPONSE: " + inputData);*/
  353.        
  354.                        
  355.                            
  356.                    
  357.                 }else {
  358.                        
  359.                         // der hier muss auch im XML Format aufgebaut sein, siehe Authorization_Data und Request
  360.                         //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>";
  361.                         //outClient.print(authorization_response_cm);
  362.                    
  363.                         String authorization_response_cm = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
  364.                         "<!DOCTYPE MESSAGES SYSTEM \"C://Users/Thomas/Desktop/Eclipse/workspace/SCE_HA/src/message_data/Authorization_Response_CM_Schema.dtd\">\n"+
  365.                         "<AUTHORIZATION_RESPONSE_CM>\n" +
  366.                             "<authorization_response_cm> \n"+
  367.                                 "<operation_id>"+message2.getId()+"</operation_id>\n"+
  368.                                 "<panSender>"+message2.getPanSender()+"</panSender>\n"+
  369.                                 "<panReceiver>"+message2.getPanReceiver()+"</panReceiver>\n"+
  370.                                 "<expiration_date>"+message2.getExpirationData()+"</expiration_date>\n"+
  371.                                 "<CVV2>"+message2.getCVV2()+"</CVV2>\n"+
  372.                                 "<name>"+message2.getName()+"</name>\n"+
  373.                                 "<last_name>"+message2.getLastName()+"</last_name>\n"+
  374.                                 "<total_amount>"+message2.getTotalAmount()+"</total_amount>\n"+
  375.                                 "<currency>"+message2.getCurrency()+"</currency>\n"+
  376.                             "</authorization_response_cm>\n"+
  377.                         "</AUTHORIZATION_RESPONSE_CM>";
  378.                         outClient.write("\n***EOM***\n");
  379.                         outClient.flush();
  380.                    
  381.                         System.out.println("Authorization Response CM built");
  382.                         outClient.write(authorization_response_cm);        
  383.                         outClient.write("\n***EOM***\n");
  384.                        
  385.                         System.out.println("authorization response cm sent");
  386.                        
  387.                     }
  388.                    
  389.                     //outClient.flush();
  390.                    
  391.                     //here merchant is waiting for communication from bank1
  392.                    
  393.  
  394.                     System.out.println("Closed");
  395.  
  396.                 } catch (IOException e) {
  397.                     System.out
  398.                             .println("Error when sending and receiving data through socket");
  399.                     e.printStackTrace();
  400.                     System.exit(-5);
  401.                 }
  402.             } catch (UnknownHostException e) {
  403.                 System.out.println("Impossible to conect to local host "
  404.                         + servername + "unresolved name");
  405.                 e.printStackTrace();
  406.                 System.exit(-3);
  407.             } catch (IOException e) {
  408.                 System.out.println("Error when creating the Clientsocket");
  409.                 e.printStackTrace();
  410.                 System.exit(-2);
  411.             }
  412.  
  413.             //System.exit(0);
  414.         }
  415. //  }
  416.  
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement