001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.iso.channel;
020
021import org.jpos.core.Configuration;
022import org.jpos.core.ConfigurationException;
023import org.jpos.iso.FSDISOMsg;
024import org.jpos.iso.ISOException;
025import org.jpos.iso.ISOMsg;
026import org.jpos.util.FSDMsg;
027import org.jpos.util.LogEvent;
028import org.jpos.util.Logger;
029
030import java.io.IOException;
031import java.nio.charset.Charset;
032import org.jpos.iso.ISOUtil;
033
034/** {@link NACChannel} variant that uses {@code FSDMsg} as its message factory. */
035public class FSDChannel extends NACChannel {
036    /** Default constructor; no instance state to initialise. */
037    public FSDChannel() {}
038    String schema;
039    Charset charset;
040
041    @Override
042    public ISOMsg createMsg() {
043        FSDMsg fsdmsg = new FSDMsg (schema);
044        fsdmsg.setCharset(charset);
045        return new FSDISOMsg (fsdmsg);
046    }
047
048    @Override
049    public void setConfiguration (Configuration cfg)
050        throws ConfigurationException 
051    {
052        super.setConfiguration (cfg);
053        schema = cfg.get ("schema");
054        charset = Charset.forName(cfg.get("charset", ISOUtil.CHARSET.displayName()));
055    }
056
057    @Override
058    public void send (ISOMsg m)
059        throws IOException, ISOException {
060      if(m instanceof FSDISOMsg) {
061        FSDMsg fsd = ((FSDISOMsg) m).getFSDMsg();
062        fsd.setCharset(charset);
063      }
064      super.send(m);
065    }
066
067    @Override
068    protected int getMessageLength() throws IOException, ISOException {
069        int len = super.getMessageLength();
070        LogEvent evt = new LogEvent (this, "fsd-channel-debug");
071        evt.addMessage ("received message length: " + len);
072        Logger.log (evt);
073        return len;
074    }
075}
076