-
Notifications
You must be signed in to change notification settings - Fork 27
/
HttpProxy.java
214 lines (182 loc) · 6.34 KB
/
HttpProxy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright 2012-2015, the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.flipkart.phantom.http.impl;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ByteArrayEntity;
import com.flipkart.phantom.task.spi.AbstractHandler;
import com.flipkart.phantom.task.spi.TaskContext;
/**
* Abstract class for handling HTTP proxy requests
*
* @author kartikbu
* @created 16/7/13 1:54 AM
* @version 1.0
*/
public abstract class HttpProxy extends AbstractHandler {
/** The default thread pool size*/
public static final int DEFAULT_THREAD_POOL_SIZE = 500;
/** Name of the proxy */
private String name;
/** The connection pool implementation instance */
private HttpConnectionPool pool;
/** The thread pool size for this proxy*/
private int threadPoolSize = HttpProxy.DEFAULT_THREAD_POOL_SIZE;
/**
* Init hook provided by the HttpProxy
*/
public void init(TaskContext context) throws Exception {
if (pool == null) {
throw new AssertionError("HttpConnectionPool object 'pool' must be given");
} else {
pool.initConnectionPool();
}
}
/**
* Shutdown hooks provided by the HttpProxy
*/
public void shutdown(TaskContext context) throws Exception {
pool.shutdown();
}
/**
* Abstract method implementation. Returns the host created for the Http connection pool
* @see com.flipkart.phantom.task.spi.AbstractHandler#getHost()
*/
public String getHost() {
return this.getPool().getHost();
}
/**
* Abstract method implementation. Returns the port of the Http connection pool
* @see com.flipkart.phantom.task.spi.AbstractHandler#getPort()
*/
public int getPort() {
return this.getPool().getPort();
}
/**
* The main method which makes the HTTP request
*/
public HttpResponse doRequest(HttpRequestWrapper httpRequestWrapper) throws Exception {
/** get necessary data required for the output */
return pool.execute(createRequest(httpRequestWrapper.getMethod(),httpRequestWrapper.getUri(),
httpRequestWrapper.getData()), httpRequestWrapper.getHeaders().get());
}
/**
* Creates a HttpRequestBase object understood by the apache http library
* @param method HTTP request method
* @param uri HTTP request URI
* @param data HTTP request data
* @return
* @throws Exception
*/
private HttpRequestBase createRequest(String method, String uri, byte[] data) throws Exception {
// get
if ("GET".equals(method)) {
HttpGet r = new HttpGet(pool.constructUrl(uri));
return r;
// put
} else if ("PUT".equals(method)) {
HttpPut r = new HttpPut(pool.constructUrl(uri));
r.setEntity(new ByteArrayEntity(data));
return r;
// post
} else if ("POST".equals(method)) {
HttpPost r = new HttpPost(pool.constructUrl(uri));
r.setEntity(new ByteArrayEntity(data));
return r;
// delete
} else if ("DELETE".equals(method)) {
HttpDelete r = new HttpDelete(pool.constructUrl(uri));
return r;
// invalid
} else {
return null;
}
}
/**
* Abstract fallback request method
* @param httpRequestWrapper the http Request Wrapper object
* @return HttpResponse response after executing the fallback
*/
public abstract HttpResponse fallbackRequest(HttpRequestWrapper httpRequestWrapper, Map<String,Object> controlParams);
/**
* Abstract method which gives group key
* @return String group key
*/
public abstract String getGroupKey();
/**
* Abstract method which gives command name
* @return String command name
*/
public abstract String getCommandKey();
/**
* Abstract method which gives the thread pool name
* @return String thread pool name
*/
public abstract String getThreadPoolKey();
/**
* Returns the thread pool size
* @return thread pool size
*/
public int getThreadPoolSize() {
return this.threadPoolSize;
}
/**
* Abstract method implementation
* @see com.flipkart.phantom.task.spi.AbstractHandler#getDetails()
*/
public String getDetails() {
if (pool != null) {
String details = "Endpoint: ";
details += (pool.getSecure() ? "https://" : "http://") + pool.getHost() + ":" + pool.getPort() + "\n";
details += "Connection Timeout: " + pool.getConnectionTimeout() + "ms\n";
details += "Operation Timeout: " + pool.getOperationTimeout() + "ms\n";
details += "Max Connections: " + pool.getMaxConnections() + "\n";
details += "Request Queue Size: " + pool.getRequestQueueSize() + "\n";
return details;
}
return "No endpoint configured";
}
/**
* Abstract method implementation
* @see AbstractHandler#getType()
*/
@Override
public String getType() {
return "HttpProxy";
}
/** getters / setters */
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public HttpConnectionPool getPool() {
return pool;
}
public void setPool(HttpConnectionPool pool) {
this.pool = pool;
}
public void setThreadPoolSize(int threadPoolSize) {
this.threadPoolSize = threadPoolSize;
}
/** getters / setters */
}