| 30 |
ashish |
1 |
/*
|
|
|
2 |
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
* or more contributor license agreements. See the NOTICE file
|
|
|
4 |
* distributed with this work for additional information
|
|
|
5 |
* regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
* to you under the Apache License, Version 2.0 (the
|
|
|
7 |
* "License"); you may not use this file except in compliance
|
|
|
8 |
* with the License. You may obtain a copy of the License at
|
|
|
9 |
*
|
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
*
|
|
|
12 |
* Unless required by applicable law or agreed to in writing,
|
|
|
13 |
* software distributed under the License is distributed on an
|
|
|
14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
* KIND, either express or implied. See the License for the
|
|
|
16 |
* specific language governing permissions and limitations
|
|
|
17 |
* under the License.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
package org.apache.thrift.transport;
|
|
|
21 |
|
|
|
22 |
import java.io.ByteArrayOutputStream;
|
|
|
23 |
import java.io.InputStream;
|
|
|
24 |
import java.io.IOException;
|
|
|
25 |
|
|
|
26 |
import java.net.URL;
|
|
|
27 |
import java.net.HttpURLConnection;
|
|
|
28 |
import java.util.HashMap;
|
|
|
29 |
import java.util.Map;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* HTTP implementation of the TTransport interface. Used for working with a
|
|
|
33 |
* Thrift web services implementation.
|
|
|
34 |
*
|
|
|
35 |
*/
|
|
|
36 |
public class THttpClient extends TTransport {
|
|
|
37 |
|
|
|
38 |
private URL url_ = null;
|
|
|
39 |
|
|
|
40 |
private final ByteArrayOutputStream requestBuffer_ =
|
|
|
41 |
new ByteArrayOutputStream();
|
|
|
42 |
|
|
|
43 |
private InputStream inputStream_ = null;
|
|
|
44 |
|
|
|
45 |
private int connectTimeout_ = 0;
|
|
|
46 |
|
|
|
47 |
private int readTimeout_ = 0;
|
|
|
48 |
|
|
|
49 |
private Map<String,String> customHeaders_ = null;
|
|
|
50 |
|
|
|
51 |
public THttpClient(String url) throws TTransportException {
|
|
|
52 |
try {
|
|
|
53 |
url_ = new URL(url);
|
|
|
54 |
} catch (IOException iox) {
|
|
|
55 |
throw new TTransportException(iox);
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public void setConnectTimeout(int timeout) {
|
|
|
60 |
connectTimeout_ = timeout;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
public void setReadTimeout(int timeout) {
|
|
|
64 |
readTimeout_ = timeout;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
public void setCustomHeaders(Map<String,String> headers) {
|
|
|
68 |
customHeaders_ = headers;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public void setCustomHeader(String key, String value) {
|
|
|
72 |
if (customHeaders_ == null) {
|
|
|
73 |
customHeaders_ = new HashMap<String, String>();
|
|
|
74 |
}
|
|
|
75 |
customHeaders_.put(key, value);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
public void open() {}
|
|
|
79 |
|
|
|
80 |
public void close() {
|
|
|
81 |
if (null != inputStream_) {
|
|
|
82 |
try {
|
|
|
83 |
inputStream_.close();
|
|
|
84 |
} catch (IOException ioe) {
|
|
|
85 |
;
|
|
|
86 |
}
|
|
|
87 |
inputStream_ = null;
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public boolean isOpen() {
|
|
|
92 |
return true;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
public int read(byte[] buf, int off, int len) throws TTransportException {
|
|
|
96 |
if (inputStream_ == null) {
|
|
|
97 |
throw new TTransportException("Response buffer is empty, no request.");
|
|
|
98 |
}
|
|
|
99 |
try {
|
|
|
100 |
int ret = inputStream_.read(buf, off, len);
|
|
|
101 |
if (ret == -1) {
|
|
|
102 |
throw new TTransportException("No more data available.");
|
|
|
103 |
}
|
|
|
104 |
return ret;
|
|
|
105 |
} catch (IOException iox) {
|
|
|
106 |
throw new TTransportException(iox);
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public void write(byte[] buf, int off, int len) {
|
|
|
111 |
requestBuffer_.write(buf, off, len);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
public void flush() throws TTransportException {
|
|
|
115 |
// Extract request and reset buffer
|
|
|
116 |
byte[] data = requestBuffer_.toByteArray();
|
|
|
117 |
requestBuffer_.reset();
|
|
|
118 |
|
|
|
119 |
try {
|
|
|
120 |
// Create connection object
|
|
|
121 |
HttpURLConnection connection = (HttpURLConnection)url_.openConnection();
|
|
|
122 |
|
|
|
123 |
// Timeouts, only if explicitly set
|
|
|
124 |
if (connectTimeout_ > 0) {
|
|
|
125 |
connection.setConnectTimeout(connectTimeout_);
|
|
|
126 |
}
|
|
|
127 |
if (readTimeout_ > 0) {
|
|
|
128 |
connection.setReadTimeout(readTimeout_);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
// Make the request
|
|
|
132 |
connection.setRequestMethod("POST");
|
|
|
133 |
connection.setRequestProperty("Content-Type", "application/x-thrift");
|
|
|
134 |
connection.setRequestProperty("Accept", "application/x-thrift");
|
|
|
135 |
connection.setRequestProperty("User-Agent", "Java/THttpClient");
|
|
|
136 |
if (customHeaders_ != null) {
|
|
|
137 |
for (Map.Entry<String, String> header : customHeaders_.entrySet()) {
|
|
|
138 |
connection.setRequestProperty(header.getKey(), header.getValue());
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
connection.setDoOutput(true);
|
|
|
142 |
connection.connect();
|
|
|
143 |
connection.getOutputStream().write(data);
|
|
|
144 |
|
|
|
145 |
int responseCode = connection.getResponseCode();
|
|
|
146 |
if (responseCode != HttpURLConnection.HTTP_OK) {
|
|
|
147 |
throw new TTransportException("HTTP Response code: " + responseCode);
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
// Read the responses
|
|
|
151 |
inputStream_ = connection.getInputStream();
|
|
|
152 |
|
|
|
153 |
} catch (IOException iox) {
|
|
|
154 |
throw new TTransportException(iox);
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
}
|