List of usage examples for org.apache.http.client ResponseHandler handleResponse
T handleResponse(HttpResponse response) throws ClientProtocolException, IOException;
From source file:cn.org.once.cstack.maven.plugin.utils.RestUtils.java
/** * sendGetCommand/*www .j av a 2s. c o m*/ * * @param url * @param log * @return * @throws MojoExecutionException * @throws CheckException */ public Map<String, String> sendGetCommand(String url, Log log) throws CheckException { Map<String, String> response = new HashMap<String, String>(); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet(url); try { CloseableHttpResponse httpResponse = httpclient.execute(httpget, localContext); ResponseHandler<String> handler = new ResponseErrorHandler(); String body = handler.handleResponse(httpResponse); response.put("body", body); httpResponse.close(); } catch (Exception e) { log.warn("GET request failed!"); throw new CheckException("Send GET to server failed!", e); } return response; }
From source file:cn.org.once.cstack.maven.plugin.utils.RestUtils.java
/** * @param url/*from w w w . ja va 2s . c om*/ * @param parameters * @return * @throws MojoExecutionException * @throws CheckException */ public Map<String, Object> sendPostCommand(String url, Map<String, String> parameters, Log log) throws CheckException { Map<String, Object> response = new HashMap<String, Object>(); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); try { ObjectMapper mapper = new ObjectMapper(); StringEntity entity = new StringEntity(mapper.writeValueAsString(parameters)); httpPost.setEntity(entity); CloseableHttpResponse httpResponse = httpclient.execute(httpPost, localContext); ResponseHandler<String> handler = new ResponseErrorHandler(); String body = handler.handleResponse(httpResponse); response.put("body", body); httpResponse.close(); } catch (Exception e) { log.warn("POST request failed!"); throw new CheckException("Send POST to server failed!", e); } return response; }
From source file:cn.org.once.cstack.maven.plugin.utils.RestUtils.java
/** * @param url/*w w w . j a va2 s. c o m*/ * @param parameters * @param log * @return * @throws MojoExecutionException */ public Map<String, String> connect(String url, Map<String, Object> parameters, Log log) throws MojoExecutionException { Map<String, String> response = new HashMap<String, String>(); CloseableHttpClient httpclient = HttpClients.createDefault(); List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("j_username", (String) parameters.get("login"))); nvps.add(new BasicNameValuePair("j_password", (String) parameters.get("password"))); localContext = HttpClientContext.create(); localContext.setCookieStore(new BasicCookieStore()); HttpPost httpPost = new HttpPost(url); try { httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse httpResponse = httpclient.execute(httpPost, localContext); ResponseHandler<String> handler = new ResponseErrorHandler(); String body = handler.handleResponse(httpResponse); response.put("body", body); httpResponse.close(); isConnected = true; log.info("Connection successful"); } catch (Exception e) { log.error("Connection failed! : " + e.getMessage()); isConnected = false; throw new MojoExecutionException( "Connection failed, please check your manager location or your credentials"); } return response; }
From source file:fr.treeptik.cloudunit.cli.rest.RestUtils.java
/** * sendGetCommand//w ww . j a v a2 s . com * * @param url * @param parameters * @return */ public Map<String, String> sendGetCommand(String url, Map<String, Object> parameters) throws ManagerResponseException { Map<String, String> response = new HashMap<String, String>(); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet(url); try { CloseableHttpResponse httpResponse = httpclient.execute(httpget, localContext); ResponseHandler<String> handler = new CustomResponseErrorHandler(); String body = handler.handleResponse(httpResponse); response.put("body", body); httpResponse.close(); } catch (Exception e) { throw new ManagerResponseException(e.getMessage(), e); } return response; }
From source file:fr.treeptik.cloudunit.cli.rest.RestUtils.java
/** * sendPostCommand//from ww w . j a v a 2 s . com * * @param url * @param parameters * @return * @throws ClientProtocolException */ public Map<String, Object> sendPostCommand(String url, Map<String, Object> credentials, Map<String, String> parameters) throws ManagerResponseException { Map<String, Object> response = new HashMap<String, Object>(); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); try { ObjectMapper mapper = new ObjectMapper(); StringEntity entity = new StringEntity(mapper.writeValueAsString(parameters)); httpPost.setEntity(entity); CloseableHttpResponse httpResponse = httpclient.execute(httpPost, localContext); ResponseHandler<String> handler = new CustomResponseErrorHandler(); String body = handler.handleResponse(httpResponse); response.put("body", body); httpResponse.close(); } catch (Exception e) { throw new ManagerResponseException(e.getMessage(), e); } return response; }
From source file:fr.treeptik.cloudunit.cli.rest.RestUtils.java
public Map<String, String> connect(String url, Map<String, Object> parameters) throws ManagerResponseException { Map<String, String> response = new HashMap<String, String>(); CloseableHttpClient httpclient = HttpClients.createDefault(); List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("j_username", (String) parameters.get("login"))); nvps.add(new BasicNameValuePair("j_password", (String) parameters.get("password"))); localContext = HttpClientContext.create(); localContext.setCookieStore(new BasicCookieStore()); HttpPost httpPost = new HttpPost(url); try {/*from w ww . j a v a 2 s . co m*/ httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse httpResponse = httpclient.execute(httpPost, localContext); ResponseHandler<String> handler = new CustomResponseErrorHandler(); String body = handler.handleResponse(httpResponse); response.put("body", body); httpResponse.close(); } catch (Exception e) { authentificationUtils.getMap().clear(); throw new ManagerResponseException(e.getMessage(), e); } return response; }
From source file:fr.treeptik.cloudunit.cli.rest.RestUtils.java
/** * sendPutCommand// w ww . jav a 2s . c o m * * @param url * @param parameters * @return * @throws ClientProtocolException */ public Map<String, Object> sendPutCommand(String url, Map<String, Object> credentials, Map<String, String> parameters) throws ManagerResponseException { Map<String, Object> response = new HashMap<String, Object>(); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPut httpPut = new HttpPut(url); httpPut.setHeader("Accept", "application/json"); httpPut.setHeader("Content-type", "application/json"); try { ObjectMapper mapper = new ObjectMapper(); StringEntity entity = new StringEntity(mapper.writeValueAsString(parameters)); httpPut.setEntity(entity); CloseableHttpResponse httpResponse = httpclient.execute(httpPut, localContext); ResponseHandler<String> handler = new CustomResponseErrorHandler(); String body = handler.handleResponse(httpResponse); response.put("body", body); httpResponse.close(); } catch (Exception e) { throw new ManagerResponseException(e.getMessage(), e); } return response; }
From source file:fr.ippon.wip.http.hc.HttpClientDecorator.java
/** * Calls #execute(HttpHost, HttpRequest, HttpContext) and pass response to responseHandler * Always consumes Entity from HttpResponse *///from ww w .j a v a2 s . c o m public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException { HttpResponse response = execute(target, request, context); try { return responseHandler.handleResponse(response); } finally { HttpEntity entity = response.getEntity(); if (entity != null) EntityUtils.consume(entity); } }
From source file:cn.org.once.cstack.cli.rest.RestUtils.java
/** * sendGetCommand/*from w w w .j a v a 2 s. c o m*/ * * @param url * @param parameters * @return */ public Map<String, String> sendGetCommand(String url, Map<String, Object> parameters) throws ManagerResponseException { Map<String, String> response = new HashMap<String, String>(); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet(url); try { CloseableHttpResponse httpResponse = httpclient.execute(httpget, localContext); ResponseHandler<String> handler = new CustomResponseErrorHandler(); String body = handler.handleResponse(httpResponse); response.put(BODY, body); httpResponse.close(); } catch (Exception e) { throw new ManagerResponseException(e.getMessage(), e); } return response; }
From source file:cn.org.once.cstack.cli.rest.RestUtils.java
/** * sendDeleteCommand//from w ww.j a v a 2 s . c o m * * @param url * @return */ public Map<String, String> sendDeleteCommand(String url, Map<String, Object> credentials) throws ManagerResponseException { Map<String, String> response = new HashMap<String, String>(); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpDelete httpDelete = new HttpDelete(url); CloseableHttpResponse httpResponse; try { httpResponse = httpclient.execute(httpDelete, localContext); ResponseHandler<String> handler = new CustomResponseErrorHandler(); String body = handler.handleResponse(httpResponse); response.put("body", body); httpResponse.close(); } catch (Exception e) { throw new ManagerResponseException(e.getMessage(), e); } return response; }