@Test
public void testGetAsString
() throws Exception {
ClientRequest req = new ClientRequest("http://localhost:9999/api/p");
req.accept(MediaType.APPLICATION_XML);
ClientResponse
<String
> response
= req.
get(String.
class);
if (response.getStatus() == 200) {
LOG.info(response.getEntity());
}
response.releaseConnection();
}
@Test
public void testGetAsObject
() throws Exception {
ClientConnectionManager cm = new ThreadSafeClientConnManager();
HttpClient httpClient = new DefaultHttpClient(cm);
ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);
ClientRequest req = new ClientRequest("http://localhost:9999/api/p", executor);
req.accept(MediaType.APPLICATION_JSON);
ClientResponse<List<Plugin>> response = req.get(new GenericType<List<Plugin>>() {
});
if (response.getStatus() == 200) {
List<Plugin> plugins = response.getEntity();
for (Plugin plugin : plugins) {
LOG.info(plugin.toString());
}
} else {
LOG.
info(String.
valueOf(response.
getStatus()));
}
response.releaseConnection();
}
@Test
public void testGetOneAsObject
() throws Exception {
ClientConnectionManager cm = new ThreadSafeClientConnManager();
HttpClient httpClient = new DefaultHttpClient(cm);
ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);
ClientRequest req = new ClientRequest("http://localhost:9999/api/p/{id}", executor);
req.pathParameter("id", 1);
req.accept(MediaType.APPLICATION_JSON);
ClientResponse<Plugin> response = req.get(Plugin.class);
if (response.getStatus() == 200) {
Plugin plugin = response.getEntity();
LOG.info(plugin.toString());
} else {
LOG.
info(String.
valueOf(response.
getStatus()));
}
response.releaseConnection();
}
@Test
public void testJenkinsApi
() throws Exception {
ClientConnectionManager cm = new ThreadSafeClientConnManager();
HttpClient httpClient = new DefaultHttpClient(cm);
ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);
HttpHost proxy = new HttpHost("132.222.121.98", 8080);
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
ClientRequest req = new ClientRequest("http://ci.buildria.com/job/aqutils/api/json", executor);
req.accept(MediaType.APPLICATION_JSON);
ClientResponse<String> response = null;
try {
response
= req.
get(String.
class);
if (response.getStatus() == 200) {
LOG.info(response.getEntity());
} else {
LOG.
info(String.
valueOf(response.
getStatus()));
}
} finally {
if (response != null) {
response.releaseConnection();
}
}
}
@Test
public void testRegisterOne
() throws Exception {
Plugin plugin = new Plugin();
plugin.setName("JaCoCo Plugin");
plugin.setDescription("Coverage Tool");
ClientRequest req = new ClientRequest("http://localhost:9999/api/p");
req.body(MediaType.APPLICATION_JSON, plugin);
req.accept(MediaType.APPLICATION_JSON);
req.getExecutionInterceptorList().add(new LogInterceptor());
ClientResponse<Plugin> response = req.post(Plugin.class);
if (response.getStatus() == 201) {
LOG.info(response.getEntity().toString());
} else {
LOG.warn("faied to post");
}
response.releaseConnection();
}
@Test
public void testMultiThreads
() throws Exception {
final int SIZE = 30;
List<Future<?>> list = new ArrayList<>();
long start
= System.
currentTimeMillis();
ExecutorService es = Executors.newFixedThreadPool(SIZE);
for (int j = 0; j < 100; j++) {
for (int i = 0; i < SIZE; i++) {
Future<?> future = es.submit(new ClientRunnable());
list.add(future);
}
for (Future<?> f : list) {
f.get();
}
}
LOG.
info("### it took {} ms.",
System.
currentTimeMillis() - start
);
}
private static class ClientRunnable
implements Runnable {
@Override
public void run() {
ClientConnectionManager cm = new ThreadSafeClientConnManager();
HttpClient httpClient = new DefaultHttpClient(cm);
ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);
// HttpHost proxy = new HttpHost("132.222.121.98", 8080);
// httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
URLConnectionClientExecutor executor2 = new URLConnectionClientExecutor() {
@Override
String uri
= request.
getUri();
String httpMethod
= request.
getHttpMethod();
Proxy proxy
= new Proxy(Proxy.
Type.
HTTP,
new InetSocketAddress
("132.222.121.98",
8080));
connection.setRequestMethod(httpMethod);
return connection;
}
};
ClientRequest req = new ClientRequest("http://10.1.1.120:9999/api/p", executor);
// ClientRequest req = new ClientRequest("http://10.1.1.120:9999/api/p");
req.accept(MediaType.APPLICATION_JSON);
ClientResponse<String> response = null;
try {
response
= req.
get(String.
class);
if (response.getStatus() == 200) {
LOG.info(response.getEntity());
} else {
LOG.
info(String.
valueOf(response.
getStatus()));
}
//
} finally {
if (response != null) {
response.releaseConnection();
}
}
}
}