package com.ibm.ctg.samples.clientblocker;

import java.net.InetAddress;
import java.util.*;

import com.ibm.ctg.ha.*;

public class ClientBlocker implements CICSRequestExit {

   /**
    * List of IP addresses that are allowed to call CICS programs
    */
   private List<String> whiteList = Arrays.asList("127.0.0.1");

   @Override
   public String getCICSServer(Map<RequestDetails, Object> details) throws InvalidRequestException {
      String server = details.get(RequestDetails.Server).toString();
      String clientIP = ((InetAddress) details.get(RequestDetails.ClientLocation)).getHostAddress();
      if (!whiteList.contains(clientIP)) {
         throw new InvalidRequestException("Client not allowed to call CICS");
      }
      return server;
   }
   
   @Override
   public int getRetryCount() {
      //Return 0 as we don't want to retry requests in this exit
      return 0;
   }
   
   @Override
   public void eventFired(ExitEvent event, Map<ExitEventData, Object> eventData) {
      switch (event) {
      case Command:
         //Get the command that was passed in
         String commandData = eventData.get(ExitEventData.CommandData).toString();
         if (commandData.startsWith("ADD")) {
            int location = commandData.indexOf("=");
            //Create a new list with the supplied IP address added and set it as the
            //current list
            if (location > 0) {
               ArrayList<String> newWhiteList = new ArrayList<String>(whiteList);
               newWhiteList.add(commandData.substring(location + 1));
               whiteList = newWhiteList;
            }
         } else if (commandData.startsWith("DEL")) {
            int location = commandData.indexOf("=");
            //Create a new list with the supplied IP address removed and set it
            //as the current list
            if (location > 0) {
               ArrayList<String> newWhiteList = new ArrayList<String>(whiteList);
               newWhiteList.remove(commandData.substring(location + 1));
               whiteList = newWhiteList;
            }
         } else if (commandData.startsWith("LIST")) {
            //Write the list of allowed IP addresses to the standard output stream
            StringBuffer list = new StringBuffer("Allowed IP addresses:");
            for (String ip : whiteList) {
               list.append("\n\t");
               list.append(ip);
            }
            System.out.println(list);
         }
         break;
      case ShutDown:
         // do nothing
         break;
      }
   }
}

@@ -1,8 +1,606 @@
+/*%0A Copyright IBM Corporation 2014%0A%0A LICENSE: Apache License%0A          Version 2.0, January 2004%0A          http://www.apache.org/licenses/%0A%0A The following code is sample code created by IBM Corporation.%0A This sample code is not part of any standard IBM product and%0A is provided to you solely for the purpose of assisting you in%0A the development of your applications.  The code is provided%0A 'as is', without warranty or condition of any kind.  IBM shall%0A not be liable for any damages arising out of your use of the%0A sample code, even if IBM has been advised of the possibility%0A of such damages.%0A*/%0A
 package 
@@ -1349,27 +1349,24 @@
 erver;%0A   %7D%0A
-   
 %0A   @Overrid
@@ -1485,19 +1485,16 @@
 0;%0A   %7D%0A
-   
 %0A   @Ove

Add code copyright header