From a6a32a56fb565d155fd9ae3826b6bbdede6688be Mon Sep 17 00:00:00 2001 From: jfarrell Date: Fri, 6 Sep 2013 13:07:56 -0400 Subject: [PATCH] THRIFT-2169: JavaME Library causes "java.io.IOException: No Response Entries Available" after using client for some time Client: javame Patch: Omkar Aradhya K S Ensures the connection is closed. Also added spacing cleanup. --- .../apache/thrift/transport/THttpClient.java | 108 +++++++++--------- 1 file changed, 52 insertions(+), 56 deletions(-) diff --git a/lib/javame/src/org/apache/thrift/transport/THttpClient.java b/lib/javame/src/org/apache/thrift/transport/THttpClient.java index 451a2e52..e6ffba4e 100644 --- a/lib/javame/src/org/apache/thrift/transport/THttpClient.java +++ b/lib/javame/src/org/apache/thrift/transport/THttpClient.java @@ -42,6 +42,8 @@ public class THttpClient extends TTransport { private InputStream inputStream_ = null; + private HttpConnection connection = null; + private int connectTimeout_ = 0; private int readTimeout_ = 0; @@ -49,7 +51,7 @@ public class THttpClient extends TTransport { private Hashtable customHeaders_ = null; public THttpClient(String url) throws TTransportException { - url_ = url; + url_ = url; } public void setConnectTimeout(int timeout) { @@ -81,6 +83,14 @@ public class THttpClient extends TTransport { } inputStream_ = null; } + + if (connection != null) { + try { + connection.close(); + } catch (IOException ioe) { + } + connection = null; + } } public boolean isOpen() { @@ -106,62 +116,48 @@ public class THttpClient extends TTransport { requestBuffer_.write(buf, off, len); } - public void flush() throws TTransportException { + public void flush() throws TTransportException { // Extract request and reset buffer - byte[] data = requestBuffer_.toByteArray(); - requestBuffer_.reset(); - - try { - // Create connection object - HttpConnection connection = (HttpConnection)Connector.open(url_); - - // Timeouts, only if explicitly set - if (connectTimeout_ > 0) { - // XXX: not available - // connection.setConnectTimeout(connectTimeout_); - } - if (readTimeout_ > 0) { - // XXX: not available - // connection.setReadTimeout(readTimeout_); - } - - // Make the request - connection.setRequestMethod("POST"); - connection.setRequestProperty("Content-Type", "application/x-thrift"); - connection.setRequestProperty("Accept", "application/x-thrift"); - connection.setRequestProperty("User-Agent", "JavaME/THttpClient"); - - connection.setRequestProperty("Connection", "Keep-Alive"); - connection.setRequestProperty("Keep-Alive", "5000"); - connection.setRequestProperty("Http-version", "HTTP/1.1"); - connection.setRequestProperty("Cache-Control", "no-transform"); - - - if (customHeaders_ != null) { - for (Enumeration e = customHeaders_.keys() ; e.hasMoreElements() ;) { - String key = (String)e.nextElement(); - String value = (String)customHeaders_.get(key); - connection.setRequestProperty(key, value); - } - } - // connection.setDoOutput(true); - // connection.connect(); - - OutputStream os = connection.openOutputStream(); - os.write(data); - os.close(); - - int responseCode = connection.getResponseCode(); - if (responseCode != HttpConnection.HTTP_OK) { - throw new TTransportException("HTTP Response code: " + responseCode); - } - - // Read the responses - inputStream_ = connection.openInputStream(); - - } catch (IOException iox) { - System.out.println(iox.toString()); - throw new TTransportException(iox); + byte[] data = requestBuffer_.toByteArray(); + requestBuffer_.reset(); + + try { + // Create connection object + connection = (HttpConnection)Connector.open(url_); + + // Make the request + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/x-thrift"); + connection.setRequestProperty("Accept", "application/x-thrift"); + connection.setRequestProperty("User-Agent", "JavaME/THttpClient"); + + connection.setRequestProperty("Connection", "Keep-Alive"); + connection.setRequestProperty("Keep-Alive", "5000"); + connection.setRequestProperty("Http-version", "HTTP/1.1"); + connection.setRequestProperty("Cache-Control", "no-transform"); + + if (customHeaders_ != null) { + for (Enumeration e = customHeaders_.keys() ; e.hasMoreElements() ;) { + String key = (String)e.nextElement(); + String value = (String)customHeaders_.get(key); + connection.setRequestProperty(key, value); } + } + + OutputStream os = connection.openOutputStream(); + os.write(data); + os.close(); + + int responseCode = connection.getResponseCode(); + if (responseCode != HttpConnection.HTTP_OK) { + throw new TTransportException("HTTP Response code: " + responseCode); + } + + // Read the responses + inputStream_ = connection.openInputStream(); + } catch (IOException iox) { + System.out.println(iox.toString()); + throw new TTransportException(iox); } + } } -- 2.17.1