/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2015 Alejandro P. Revilla
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jpos.iso.filter;

import org.jpos.core.Configurable;
import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
import org.jpos.iso.*;
import org.jpos.util.LogEvent;

import java.net.InetSocketAddress;
import java.net.Socket;


/**
 * ChannelInfoFilter sets information about the channel
 * in the outgoing/incoming ISOMsg
 */
public class ChannelInfoFilter implements ISOFilter, Configurable {
    int channelNameField;
    int socketInfoField;
    public ChannelInfoFilter() {
        super();
    }
   /**
    * @param cfg
    * <ul>
    *  <li>channel-name: put the channel name in the given field</li>
    *  <li>socket-info: put socket information in the given field 
    *  (if the channel is an instance of BaseChannel)
    *  </li>
    * </ul>
    */
    public void setConfiguration (Configuration cfg) 
        throws ConfigurationException
    {
        channelNameField = cfg.getInt ("channel-name", 0);
        socketInfoField  = cfg.getInt ("socket-info", 0);
    }

    public ISOMsg filter (ISOChannel channel, ISOMsg m, LogEvent evt) {
        try {
            if (channelNameField > 0)
                m.set (Integer.toString(channelNameField), channel.getName());
            if (socketInfoField > 0 && channel instanceof BaseChannel) {
                Socket socket = ((BaseChannel)channel).getSocket();
                InetSocketAddress remoteAddr = 
                    (InetSocketAddress) socket.getRemoteSocketAddress();
                InetSocketAddress localAddr = 
                    (InetSocketAddress) socket.getLocalSocketAddress();

                StringBuilder sb = new StringBuilder();
                if (channelNameField == socketInfoField) {
                    sb.append (channel.getName());
                    sb.append (' ');
                }
                sb.append (localAddr.getAddress().getHostAddress());
                sb.append (':');
                sb.append (Integer.toString (localAddr.getPort()));
                sb.append (' ');
                sb.append (remoteAddr.getAddress().getHostAddress());
                sb.append (':');
                sb.append (Integer.toString (remoteAddr.getPort()));
                m.set (Integer.toString(socketInfoField), sb.toString());
            }
        } catch (ISOException e) {
            evt.addMessage (e);
        }
        return m;
    }
}


@@ -1114,16 +1114,44 @@
 Msg%0A */%0A
+@SuppressWarnings(%22unused%22)%0A
 public c
@@ -1214,19 +1214,22 @@
 e %7B%0A    
+Str
 in
-t
+g
  channel
@@ -1247,11 +1247,14 @@
     
+Str
 in
-t
+g
  soc
@@ -1713,20 +1713,16 @@
  cfg.get
-Int 
 (%22channe
@@ -1730,17 +1730,20 @@
 -name%22, 
-0
+null
 );%0A     
@@ -1775,12 +1775,8 @@
 .get
-Int 
 (%22so
@@ -1787,17 +1787,20 @@
 -info%22, 
-0
+null
 );%0A    %7D
@@ -1920,19 +1920,23 @@
 meField 
-%3E 0
+!= null
 )%0A      
@@ -1951,33 +1951,16 @@
   m.set 
-(Integer.toString
 (channel
@@ -1968,17 +1968,16 @@
 ameField
-)
 , channe
@@ -2026,11 +2026,15 @@
 eld 
-%3E 0
+!= null
  && 
@@ -2456,43 +2456,48 @@
 if (
-channelNameField == socketInfoField
+socketInfoField.equals(channelNameField)
 ) %7B%0A
@@ -2999,33 +2999,16 @@
   m.set 
-(Integer.toString
 (socketI
@@ -3015,17 +3015,16 @@
 nfoField
-)
 , sb.toS
@@ -3150,9 +3150,8 @@
     %7D%0A%7D%0A
-%0A

Handle channel-name and socket-info as Strings