blob: 8b42ba3d5fd3d0279e7fc546bc7fdd18ccf59fc8 [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;
15
16/**
17 * HTTP implementation of the TTransport interface. Used for working with a
18 * Thrift web services implementation.
19 *
20 * @author Mark Slee <mcslee@facebook.com>
21 */
22public class THttpClient extends TTransport {
23
24 private URL url_ = null;
25
26 private final ByteArrayOutputStream requestBuffer_ =
27 new ByteArrayOutputStream();
28
29 private InputStream inputStream_ = null;
30
31 private int connectTimeout_ = 0;
32
33 private int readTimeout_ = 0;
34
35 public THttpClient(String url) throws TTransportException {
36 try {
37 url_ = new URL(url);
38 } catch (IOException iox) {
39 throw new TTransportException(iox);
40 }
41 }
42
43 public void setConnectTimeout(int timeout) {
44 connectTimeout_ = timeout;
45 }
46
47 public void setReadTimeout(int timeout) {
48 readTimeout_ = timeout;
49 }
50
51 public void open() {}
52
eletuchy66ccee22007-06-25 18:29:28 +000053 public void close() {
54 if (null != inputStream_) {
55 try {
56 inputStream_.close();
57 } catch (IOException ioe) {
58 ;
59 }
60 inputStream_ = null;
61 }
62 }
Mark Slee7d5da162006-10-10 01:38:25 +000063
64 public boolean isOpen() {
65 return true;
66 }
67
68 public int read(byte[] buf, int off, int len) throws TTransportException {
69 if (inputStream_ == null) {
70 throw new TTransportException("Response buffer is empty, no request.");
71 }
72 try {
73 int ret = inputStream_.read(buf, off, len);
74 if (ret == -1) {
75 throw new TTransportException("No more data available.");
76 }
77 return ret;
78 } catch (IOException iox) {
79 throw new TTransportException(iox);
80 }
81 }
82
83 public void write(byte[] buf, int off, int len) {
84 requestBuffer_.write(buf, off, len);
85 }
86
87 public void flush() throws TTransportException {
88 // Extract request and reset buffer
89 byte[] data = requestBuffer_.toByteArray();
90 requestBuffer_.reset();
91
92 try {
93 // Create connection object
94 HttpURLConnection connection = (HttpURLConnection)url_.openConnection();
95
96 // Timeouts, only if explicitly set
97 if (connectTimeout_ > 0) {
98 connection.setConnectTimeout(connectTimeout_);
99 }
100 if (readTimeout_ > 0) {
101 connection.setReadTimeout(readTimeout_);
102 }
103
104 // Make the request
105 connection.setRequestMethod("POST");
106 connection.setRequestProperty("Content-Type", "application/x-thrift");
107 connection.setRequestProperty("Accept", "application/x-thrift");
108 connection.setRequestProperty("User-Agent", "Java/THttpClient");
109 connection.setDoOutput(true);
110 connection.connect();
111 connection.getOutputStream().write(data);
112
113 int responseCode = connection.getResponseCode();
114 if (responseCode != HttpURLConnection.HTTP_OK) {
115 throw new TTransportException("HTTP Response code: " + responseCode);
116 }
117
118 // Read the responses
119 inputStream_ = connection.getInputStream();
120
121 } catch (IOException iox) {
122 throw new TTransportException(iox);
123 }
124 }
125}