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.iso.*;
022import org.jpos.iso.packager.XMLPackager;
023
024import java.io.BufferedReader;
025import java.io.EOFException;
026import java.io.IOException;
027import java.io.InputStreamReader;
028import java.net.ServerSocket;
029import java.net.Socket;
030
031/**
032 * Implements an ISOChannel able to exchange <b>jPOS generated</b> 
033 * (or compliant) XML based ISO-8583 messages 
034 * @author  <a href="mailto:apr@cs.com.uy">Alejandro P. Revilla</a>
035 * @version $Id$
036 *
037 * @see ISOMsg
038 * @see ISOException
039 * @see ISOChannel
040 */
041public class XMLChannel extends BaseChannel {
042    BufferedReader reader = null;
043    /**
044     * Public constructor (used by Class.forName("...").newInstance())
045     */
046    public XMLChannel () {
047        super();
048    }
049    /**
050     * Constructs client ISOChannel
051     * @param host  server TCP Address
052     * @param port  server port number
053     * @param p     an ISOPackager
054     * @see ISOPackager
055     */
056    public XMLChannel (String host, int port, ISOPackager p) {
057        super(host, port, p);
058    }
059    /**
060     * Construct server ISOChannel
061     * @param p     an ISOPackager
062     * @see ISOPackager
063     * @exception IOException
064     */
065    public XMLChannel (ISOPackager p) throws IOException {
066        super(p);
067    }
068    /**
069     * constructs a server ISOChannel associated with a Server Socket
070     * @param p     an ISOPackager
071     * @param serverSocket where to accept a connection
072     * @exception IOException
073     * @see ISOPackager
074     */
075    public XMLChannel (ISOPackager p, ServerSocket serverSocket) 
076        throws IOException
077    {
078        super(p, serverSocket);
079    }
080    /**
081     * @return a byte array with the received message
082     * @exception IOException
083     */
084    protected byte[] streamReceive() throws IOException {
085        int sp = 0;
086        StringBuilder sb = new StringBuilder();
087        while (reader != null) {
088            String s = reader.readLine();
089            if (s == null)
090                throw new EOFException();
091            sb.append (s);
092            if (s.contains("<" + XMLPackager.ISOMSG_TAG))
093                sp++;
094            if (s.contains("</" + XMLPackager.ISOMSG_TAG + ">") && --sp <= 0)
095                break;
096        }
097        if (sb.length() == 0)
098            throw new EOFException();
099        return sb.toString().getBytes();
100    }
101
102
103    protected int getHeaderLength() { 
104        // XML Channel does not support header
105        return 0; 
106    }
107    protected void sendMessageHeader(ISOMsg m, int len) {
108        // XML Channel does not support header
109    }
110    protected void connect (Socket socket) throws IOException {
111        super.connect (socket);
112        reader = new BufferedReader (new InputStreamReader (serverIn));
113    }
114    public void disconnect () throws IOException {
115        super.disconnect ();
116        if (reader != null)
117            reader.close ();
118        reader = null;
119    }
120}