Skip to content

Commit

Permalink
Fix from required change
Browse files Browse the repository at this point in the history
Close the socket connection where it was created, in it's finally block.
Read the byte from input stream with out sleep the thread.
  • Loading branch information
jananithangavel committed Feb 26, 2018
1 parent 003c5d6 commit 16e5921
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.MessageContext;
Expand All @@ -28,14 +29,12 @@
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISOPackager;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Arrays;
import java.util.Base64;
import java.util.Iterator;
import javax.xml.namespace.QName;

/**
Expand All @@ -46,12 +45,22 @@ public class ISO8583MessageHandler {
private static final Log log = LogFactory.getLog(ISO8583MessageHandler.class);

public ISO8583MessageHandler(MessageContext messageContext, byte[] details, String host, int port) {
Socket socket = null;
try {
Socket socket = new Socket(host, port);
socket = new Socket(host, port);
clientHandler(messageContext, socket, details);
} catch (IOException e) {
handleException("Couldn't create Socket", e);
}
finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
log.error("Couldn't close the Socket", e);
}
}
}
}

/**
Expand All @@ -72,16 +81,11 @@ public void clientHandler(MessageContext messageContext, Socket connection, byte
outStream.flush();

/* Sender will receive the Acknowledgement here */
Thread.sleep(200);
int messageLength = inFromServer.available();
byte[] message = new byte[messageLength];
inFromServer.readFully(message, 0, messageLength);
unpackResponse(messageContext, message);
byte[] message = IOUtils.toByteArray(inFromServer);
unpackResponse(messageContext,message);
}
} catch (IOException e) {
handleException("An exception occurred in sending the iso8583 message", e);
} catch (InterruptedException e) {
handleException("An exception occurred in reading the iso8583 message", e);
} finally {
try {
if (outStream != null) {
Expand All @@ -90,7 +94,6 @@ public void clientHandler(MessageContext messageContext, Socket connection, byte
if (inFromServer != null) {
inFromServer.close();
}
connection.close();
} catch (IOException e) {
log.error("Couldn't close the I/O Streams", e);
}
Expand All @@ -112,7 +115,7 @@ public void unpackResponse(MessageContext messageContext, byte[] message) {
isoMsg.unpack(message);
messageBuilder(messageContext, isoMsg);
} catch (ISOException e) {
handleException("Couldn't unpack the message since message is not in ISO Standard :" + message, e);
handleException("Couldn't unpack the message since message is not in ISO Standard :" + Arrays.toString(message), e);
}
}

Expand Down

0 comments on commit 16e5921

Please sign in to comment.