Mark Slee | 7eb0d63 | 2007-03-01 00:00:27 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | 7d5da16 | 2006-10-10 01:38:25 +0000 | [diff] [blame] | 7 | package com.facebook.thrift.transport; |
| 8 | |
| 9 | import java.io.ByteArrayOutputStream; |
| 10 | import java.io.InputStream; |
| 11 | import java.io.IOException; |
| 12 | |
| 13 | import java.net.URL; |
| 14 | import java.net.HttpURLConnection; |
Mark Slee | f60467e | 2007-11-29 02:57:34 +0000 | [diff] [blame] | 15 | import java.util.HashMap; |
| 16 | import java.util.Map; |
Mark Slee | 7d5da16 | 2006-10-10 01:38:25 +0000 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * HTTP implementation of the TTransport interface. Used for working with a |
| 20 | * Thrift web services implementation. |
| 21 | * |
| 22 | * @author Mark Slee <mcslee@facebook.com> |
| 23 | */ |
| 24 | public class THttpClient extends TTransport { |
| 25 | |
| 26 | private URL url_ = null; |
| 27 | |
| 28 | private final ByteArrayOutputStream requestBuffer_ = |
| 29 | new ByteArrayOutputStream(); |
| 30 | |
| 31 | private InputStream inputStream_ = null; |
| 32 | |
| 33 | private int connectTimeout_ = 0; |
| 34 | |
| 35 | private int readTimeout_ = 0; |
| 36 | |
Mark Slee | f60467e | 2007-11-29 02:57:34 +0000 | [diff] [blame] | 37 | private Map<String,String> customHeaders_ = null; |
| 38 | |
Mark Slee | 7d5da16 | 2006-10-10 01:38:25 +0000 | [diff] [blame] | 39 | public THttpClient(String url) throws TTransportException { |
| 40 | try { |
| 41 | url_ = new URL(url); |
| 42 | } catch (IOException iox) { |
| 43 | throw new TTransportException(iox); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public void setConnectTimeout(int timeout) { |
| 48 | connectTimeout_ = timeout; |
| 49 | } |
| 50 | |
| 51 | public void setReadTimeout(int timeout) { |
| 52 | readTimeout_ = timeout; |
| 53 | } |
| 54 | |
Mark Slee | f60467e | 2007-11-29 02:57:34 +0000 | [diff] [blame] | 55 | public void setCustomHeaders(Map<String,String> headers) { |
| 56 | customHeaders_ = headers; |
| 57 | } |
| 58 | |
| 59 | public void setCustomHeader(String key, String value) { |
| 60 | if (customHeaders_ == null) { |
| 61 | customHeaders_ = new HashMap<String, String>(); |
| 62 | } |
| 63 | customHeaders_.put(key, value); |
| 64 | } |
| 65 | |
Mark Slee | 7d5da16 | 2006-10-10 01:38:25 +0000 | [diff] [blame] | 66 | public void open() {} |
| 67 | |
eletuchy | 66ccee2 | 2007-06-25 18:29:28 +0000 | [diff] [blame] | 68 | public void close() { |
| 69 | if (null != inputStream_) { |
| 70 | try { |
| 71 | inputStream_.close(); |
| 72 | } catch (IOException ioe) { |
| 73 | ; |
| 74 | } |
| 75 | inputStream_ = null; |
| 76 | } |
| 77 | } |
Mark Slee | 7d5da16 | 2006-10-10 01:38:25 +0000 | [diff] [blame] | 78 | |
| 79 | public boolean isOpen() { |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | public int read(byte[] buf, int off, int len) throws TTransportException { |
| 84 | if (inputStream_ == null) { |
| 85 | throw new TTransportException("Response buffer is empty, no request."); |
| 86 | } |
| 87 | try { |
| 88 | int ret = inputStream_.read(buf, off, len); |
| 89 | if (ret == -1) { |
| 90 | throw new TTransportException("No more data available."); |
| 91 | } |
| 92 | return ret; |
| 93 | } catch (IOException iox) { |
| 94 | throw new TTransportException(iox); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public void write(byte[] buf, int off, int len) { |
| 99 | requestBuffer_.write(buf, off, len); |
| 100 | } |
| 101 | |
| 102 | public void flush() throws TTransportException { |
| 103 | // Extract request and reset buffer |
Mark Slee | f60467e | 2007-11-29 02:57:34 +0000 | [diff] [blame] | 104 | byte[] data = requestBuffer_.toByteArray(); |
Mark Slee | 7d5da16 | 2006-10-10 01:38:25 +0000 | [diff] [blame] | 105 | requestBuffer_.reset(); |
| 106 | |
| 107 | try { |
| 108 | // Create connection object |
| 109 | HttpURLConnection connection = (HttpURLConnection)url_.openConnection(); |
| 110 | |
| 111 | // Timeouts, only if explicitly set |
| 112 | if (connectTimeout_ > 0) { |
| 113 | connection.setConnectTimeout(connectTimeout_); |
| 114 | } |
| 115 | if (readTimeout_ > 0) { |
| 116 | connection.setReadTimeout(readTimeout_); |
| 117 | } |
| 118 | |
| 119 | // Make the request |
| 120 | connection.setRequestMethod("POST"); |
| 121 | connection.setRequestProperty("Content-Type", "application/x-thrift"); |
| 122 | connection.setRequestProperty("Accept", "application/x-thrift"); |
| 123 | connection.setRequestProperty("User-Agent", "Java/THttpClient"); |
Mark Slee | f60467e | 2007-11-29 02:57:34 +0000 | [diff] [blame] | 124 | if (customHeaders_ != null) { |
| 125 | for (Map.Entry<String, String> header : customHeaders_.entrySet()) { |
| 126 | connection.setRequestProperty(header.getKey(), header.getValue()); |
| 127 | } |
| 128 | } |
Mark Slee | 7d5da16 | 2006-10-10 01:38:25 +0000 | [diff] [blame] | 129 | connection.setDoOutput(true); |
| 130 | connection.connect(); |
| 131 | connection.getOutputStream().write(data); |
| 132 | |
| 133 | int responseCode = connection.getResponseCode(); |
| 134 | if (responseCode != HttpURLConnection.HTTP_OK) { |
| 135 | throw new TTransportException("HTTP Response code: " + responseCode); |
| 136 | } |
| 137 | |
| 138 | // Read the responses |
| 139 | inputStream_ = connection.getInputStream(); |
| 140 | |
| 141 | } catch (IOException iox) { |
| 142 | throw new TTransportException(iox); |
Mark Slee | f60467e | 2007-11-29 02:57:34 +0000 | [diff] [blame] | 143 | } |
Mark Slee | 7d5da16 | 2006-10-10 01:38:25 +0000 | [diff] [blame] | 144 | } |
| 145 | } |