blob: ab3893db7f0f6924987a9703296f7f8a10e7c483 [file] [log] [blame]
Mark Slee7eb0d632007-03-01 00:00:27 +00001// 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 Slee7d5da162006-10-10 01:38:25 +00007package com.facebook.thrift.transport;
8
9import java.io.ByteArrayOutputStream;
10import java.io.InputStream;
11import java.io.IOException;
12
13import java.net.URL;
14import java.net.HttpURLConnection;
Mark Sleef60467e2007-11-29 02:57:34 +000015import java.util.HashMap;
16import java.util.Map;
Mark Slee7d5da162006-10-10 01:38:25 +000017
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 */
24public 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 Sleef60467e2007-11-29 02:57:34 +000037 private Map<String,String> customHeaders_ = null;
38
Mark Slee7d5da162006-10-10 01:38:25 +000039 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 Sleef60467e2007-11-29 02:57:34 +000055 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 Slee7d5da162006-10-10 01:38:25 +000066 public void open() {}
67
eletuchy66ccee22007-06-25 18:29:28 +000068 public void close() {
69 if (null != inputStream_) {
70 try {
71 inputStream_.close();
72 } catch (IOException ioe) {
73 ;
74 }
75 inputStream_ = null;
76 }
77 }
Mark Slee7d5da162006-10-10 01:38:25 +000078
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 Sleef60467e2007-11-29 02:57:34 +0000104 byte[] data = requestBuffer_.toByteArray();
Mark Slee7d5da162006-10-10 01:38:25 +0000105 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 Sleef60467e2007-11-29 02:57:34 +0000124 if (customHeaders_ != null) {
125 for (Map.Entry<String, String> header : customHeaders_.entrySet()) {
126 connection.setRequestProperty(header.getKey(), header.getValue());
127 }
128 }
Mark Slee7d5da162006-10-10 01:38:25 +0000129 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 Sleef60467e2007-11-29 02:57:34 +0000143 }
Mark Slee7d5da162006-10-10 01:38:25 +0000144 }
145}