×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Seiji Sogabe
Added: Aug 3, 2015 4:36 AM
Views: 1914
Tags: no tags
  1.     @Test
  2.     public void testGetAsString() throws Exception {
  3.         ClientRequest req = new ClientRequest("http://localhost:9999/api/p");
  4.         req.accept(MediaType.APPLICATION_XML);
  5.  
  6.         ClientResponse<String> response = req.get(String.class);
  7.         if (response.getStatus() == 200) {
  8.             LOG.info(response.getEntity());
  9.         }
  10.         response.releaseConnection();
  11.     }
  12.  
  13.     @Test
  14.     public void testGetAsObject() throws Exception {
  15.         ClientConnectionManager cm = new ThreadSafeClientConnManager();
  16.         HttpClient httpClient = new DefaultHttpClient(cm);
  17.         ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);
  18.  
  19.         ClientRequest req = new ClientRequest("http://localhost:9999/api/p", executor);
  20.         req.accept(MediaType.APPLICATION_JSON);
  21.         ClientResponse<List<Plugin>> response = req.get(new GenericType<List<Plugin>>() {
  22.         });
  23.         if (response.getStatus() == 200) {
  24.             List<Plugin> plugins = response.getEntity();
  25.             for (Plugin plugin : plugins) {
  26.                 LOG.info(plugin.toString());
  27.             }
  28.         } else {
  29.             LOG.info(String.valueOf(response.getStatus()));
  30.         }
  31.         response.releaseConnection();
  32.     }
  33.  
  34.     @Test
  35.     public void testGetOneAsObject() throws Exception {
  36.         ClientConnectionManager cm = new ThreadSafeClientConnManager();
  37.         HttpClient httpClient = new DefaultHttpClient(cm);
  38.         ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);
  39.  
  40.         ClientRequest req = new ClientRequest("http://localhost:9999/api/p/{id}", executor);
  41.         req.pathParameter("id", 1);
  42.         req.accept(MediaType.APPLICATION_JSON);
  43.         ClientResponse<Plugin> response = req.get(Plugin.class);
  44.         if (response.getStatus() == 200) {
  45.             Plugin plugin = response.getEntity();
  46.             LOG.info(plugin.toString());
  47.         } else {
  48.             LOG.info(String.valueOf(response.getStatus()));
  49.         }
  50.         response.releaseConnection();
  51.     }
  52.  
  53.     @Test
  54.     public void testJenkinsApi() throws Exception {
  55.         ClientConnectionManager cm = new ThreadSafeClientConnManager();
  56.         HttpClient httpClient = new DefaultHttpClient(cm);
  57.         ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);
  58.  
  59.         HttpHost proxy = new HttpHost("132.222.121.98", 8080);
  60.         httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
  61.  
  62.         ClientRequest req = new ClientRequest("http://ci.buildria.com/job/aqutils/api/json", executor);
  63.         req.accept(MediaType.APPLICATION_JSON);
  64.  
  65.         ClientResponse<String> response = null;
  66.         try {
  67.             response = req.get(String.class);
  68.             if (response.getStatus() == 200) {
  69.                 LOG.info(response.getEntity());
  70.             } else {
  71.                 LOG.info(String.valueOf(response.getStatus()));
  72.             }
  73.         } finally {
  74.             if (response != null) {
  75.                 response.releaseConnection();
  76.             }
  77.         }
  78.     }
  79.  
  80.     @Test
  81.     public void testRegisterOne() throws Exception {
  82.         Plugin plugin = new Plugin();
  83.         plugin.setName("JaCoCo Plugin");
  84.         plugin.setDescription("Coverage Tool");
  85.  
  86.         ClientRequest req = new ClientRequest("http://localhost:9999/api/p");
  87.         req.body(MediaType.APPLICATION_JSON, plugin);
  88.         req.accept(MediaType.APPLICATION_JSON);
  89.         req.getExecutionInterceptorList().add(new LogInterceptor());
  90.  
  91.         ClientResponse<Plugin> response = req.post(Plugin.class);
  92.         if (response.getStatus() == 201) {
  93.             LOG.info(response.getEntity().toString());
  94.         } else {
  95.             LOG.warn("faied to post");
  96.         }
  97.         response.releaseConnection();
  98.  
  99.     }
  100.  
  101.     @Test
  102.     public void testMultiThreads() throws Exception {
  103.         final int SIZE = 30;
  104.  
  105.         List<Future<?>> list = new ArrayList<>();
  106.  
  107.         long start = System.currentTimeMillis();
  108.  
  109.         ExecutorService es = Executors.newFixedThreadPool(SIZE);
  110.         for (int j = 0; j < 100; j++) {
  111.             for (int i = 0; i < SIZE; i++) {
  112.                 Future<?> future = es.submit(new ClientRunnable());
  113.                 list.add(future);
  114.             }
  115.  
  116.             for (Future<?> f : list) {
  117.                 f.get();
  118.             }
  119.         }
  120.  
  121.         LOG.info("### it took {} ms.", System.currentTimeMillis() - start);
  122.     }
  123.  
  124.     private static class ClientRunnable implements Runnable {
  125.  
  126.         @Override
  127.         public void run() {
  128.             ClientConnectionManager cm = new ThreadSafeClientConnManager();
  129.             HttpClient httpClient = new DefaultHttpClient(cm);
  130.             ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);
  131.  
  132. //            HttpHost proxy = new HttpHost("132.222.121.98", 8080);
  133. //            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
  134.             URLConnectionClientExecutor executor2 = new URLConnectionClientExecutor() {
  135.  
  136.                 @Override
  137.                 protected HttpURLConnection createConnection(ClientRequest request) throws Exception {
  138.                     String uri = request.getUri();
  139.                     String httpMethod = request.getHttpMethod();
  140.  
  141.                     Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("132.222.121.98", 8080));
  142.  
  143.                     HttpURLConnection connection = (HttpURLConnection) new URL(uri).openConnection(proxy);
  144.                     connection.setRequestMethod(httpMethod);
  145.                     return connection;
  146.                 }
  147.  
  148.             };
  149.  
  150.             ClientRequest req = new ClientRequest("http://10.1.1.120:9999/api/p", executor);
  151. //            ClientRequest req = new ClientRequest("http://10.1.1.120:9999/api/p");
  152.             req.accept(MediaType.APPLICATION_JSON);
  153.  
  154.             ClientResponse<String> response = null;
  155.             try {
  156.                 response = req.get(String.class);
  157.                 if (response.getStatus() == 200) {
  158.                     LOG.info(response.getEntity());
  159.                 } else {
  160.                     LOG.info(String.valueOf(response.getStatus()));
  161.                 }
  162.             } catch (Exception ex) {
  163.                 //
  164.             } finally {
  165.                 if (response != null) {
  166.                     response.releaseConnection();
  167.                 }
  168.             }
  169.  
  170.         }
  171.  
  172.     }
  173.