blob: aa0f0a3a9199bf12960062659f38d53f9f5deef3 [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
53 public void close() {}
54
55 public boolean isOpen() {
56 return true;
57 }
58
59 public int read(byte[] buf, int off, int len) throws TTransportException {
60 if (inputStream_ == null) {
61 throw new TTransportException("Response buffer is empty, no request.");
62 }
63 try {
64 int ret = inputStream_.read(buf, off, len);
65 if (ret == -1) {
66 throw new TTransportException("No more data available.");
67 }
68 return ret;
69 } catch (IOException iox) {
70 throw new TTransportException(iox);
71 }
72 }
73
74 public void write(byte[] buf, int off, int len) {
75 requestBuffer_.write(buf, off, len);
76 }
77
78 public void flush() throws TTransportException {
79 // Extract request and reset buffer
80 byte[] data = requestBuffer_.toByteArray();
81 requestBuffer_.reset();
82
83 try {
84 // Create connection object
85 HttpURLConnection connection = (HttpURLConnection)url_.openConnection();
86
87 // Timeouts, only if explicitly set
88 if (connectTimeout_ > 0) {
89 connection.setConnectTimeout(connectTimeout_);
90 }
91 if (readTimeout_ > 0) {
92 connection.setReadTimeout(readTimeout_);
93 }
94
95 // Make the request
96 connection.setRequestMethod("POST");
97 connection.setRequestProperty("Content-Type", "application/x-thrift");
98 connection.setRequestProperty("Accept", "application/x-thrift");
99 connection.setRequestProperty("User-Agent", "Java/THttpClient");
100 connection.setDoOutput(true);
101 connection.connect();
102 connection.getOutputStream().write(data);
103
104 int responseCode = connection.getResponseCode();
105 if (responseCode != HttpURLConnection.HTTP_OK) {
106 throw new TTransportException("HTTP Response code: " + responseCode);
107 }
108
109 // Read the responses
110 inputStream_ = connection.getInputStream();
111
112 } catch (IOException iox) {
113 throw new TTransportException(iox);
114 }
115 }
116}