1 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>HttpClientUtil.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">vfc-nfvo-driver-ems-ems-boco</a> > <a href="index.source.html" class="el_package">org.onap.vfc.nfvo.emsdriver.northbound.client</a> > <span class="el_source">HttpClientUtil.java</span></div><h1>HttpClientUtil.java</h1><pre class="source lang-java linenums">/**
2 * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.vfc.nfvo.emsdriver.northbound.client;
18 import java.io.IOException;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.http.HttpEntity;
23 import org.apache.http.auth.AuthScope;
24 import org.apache.http.auth.UsernamePasswordCredentials;
25 import org.apache.http.client.CredentialsProvider;
26 import org.apache.http.client.methods.CloseableHttpResponse;
27 import org.apache.http.client.methods.HttpDelete;
28 import org.apache.http.client.methods.HttpGet;
29 import org.apache.http.client.methods.HttpPost;
30 import org.apache.http.entity.StringEntity;
31 import org.apache.http.impl.client.BasicCredentialsProvider;
32 import org.apache.http.impl.client.CloseableHttpClient;
33 import org.apache.http.impl.client.HttpClientBuilder;
34 import org.apache.http.impl.client.HttpClients;
35 import org.apache.http.util.EntityUtils;
37 import org.apache.commons.codec.binary.Base64;
40 * HttpClient post request
42 <span class="nc" id="L42">public class HttpClientUtil {</span>
44 <span class="fc" id="L44"> private static Log log = LogFactory.getLog(HttpClientUtil.class);</span>
46 public static String doPost(String url,String json,String charset){
47 <span class="fc" id="L47"> CloseableHttpClient httpClient = null;</span>
48 <span class="fc" id="L48"> HttpPost httpPost = null;</span>
49 <span class="fc" id="L49"> String result = null;</span>
51 <span class="fc" id="L51"> httpClient = HttpClientFactory.getSSLClientFactory();</span>
52 <span class="fc" id="L52"> httpPost = new HttpPost(url);</span>
53 <span class="pc bpc" id="L53" title="1 of 2 branches missed."> if (null != json) {</span>
54 <span class="fc" id="L54"> StringEntity s = new StringEntity(json);</span>
55 <span class="fc" id="L55"> s.setContentEncoding("UTF-8");</span>
56 <span class="fc" id="L56"> s.setContentType("application/json"); // set contentType</span>
57 <span class="fc" id="L57"> httpPost.setEntity(s);</span>
59 <span class="nc" id="L59"> CloseableHttpResponse response = httpClient.execute(httpPost);</span>
61 <span class="nc bnc" id="L61" title="All 2 branches missed."> if(response != null){</span>
62 <span class="nc" id="L62"> HttpEntity resEntity = response.getEntity();</span>
63 <span class="nc bnc" id="L63" title="All 2 branches missed."> if(resEntity != null){</span>
64 <span class="nc" id="L64"> result = EntityUtils.toString(resEntity,charset);</span>
67 <span class="nc" id="L67"> } catch (Exception e) {</span>
68 <span class="nc" id="L68"> log.error("httpClient.execute(httpPost) is fail",e);</span>
70 <span class="nc bnc" id="L70" title="All 6 branches missed."> if(response != null){</span>
71 <span class="nc" id="L71"> response.close();</span>
73 <span class="nc" id="L73"> }</span>
74 <span class="fc" id="L74"> }catch(Exception e){</span>
75 <span class="fc" id="L75"> log.error("doPost is fail ",e);</span>
77 <span class="pc bpc" id="L77" title="5 of 6 branches missed."> if(httpClient != null){</span>
79 <span class="pc" id="L79"> httpClient.close();</span>
80 <span class="nc" id="L80"> } catch (IOException e) {</span>
81 <span class="pc" id="L81"> }</span>
84 <span class="nc" id="L84"> }</span>
85 <span class="fc" id="L85"> return result;</span>
88 public static String doDelete(String url ,String charset){
89 <span class="fc" id="L89"> CloseableHttpClient httpClient = null;</span>
90 <span class="fc" id="L90"> HttpDelete httpDelete = null;</span>
91 <span class="fc" id="L91"> String result = null;</span>
93 <span class="fc" id="L93"> httpClient = HttpClientFactory.getSSLClientFactory();</span>
94 <span class="fc" id="L94"> httpDelete = new HttpDelete(url);</span>
96 <span class="nc" id="L96"> CloseableHttpResponse response = httpClient.execute(httpDelete);</span>
99 <span class="nc bnc" id="L99" title="All 2 branches missed."> if(response != null){</span>
100 <span class="nc" id="L100"> HttpEntity resEntity = response.getEntity();</span>
101 <span class="nc bnc" id="L101" title="All 2 branches missed."> if(resEntity != null){</span>
102 <span class="nc" id="L102"> result = EntityUtils.toString(resEntity,charset);</span>
105 <span class="nc" id="L105"> } catch (Exception e) {</span>
106 <span class="nc" id="L106"> log.error("",e);</span>
108 <span class="nc bnc" id="L108" title="All 6 branches missed."> if(response != null){</span>
109 <span class="nc" id="L109"> response.close();</span>
111 <span class="nc" id="L111"> }</span>
112 <span class="fc" id="L112"> }catch(Exception e){</span>
113 <span class="fc" id="L113"> log.error("doDelete is fail ",e);</span>
115 <span class="pc bpc" id="L115" title="5 of 6 branches missed."> if(httpClient != null){</span>
117 <span class="pc" id="L117"> httpClient.close();</span>
118 <span class="nc" id="L118"> } catch (IOException e) {</span>
119 <span class="pc" id="L119"> }</span>
121 <span class="nc" id="L121"> }</span>
122 <span class="fc" id="L122"> return result;</span>
125 public static String doGet(String url, String charset){
126 <span class="fc" id="L126"> CloseableHttpClient httpClient = null;</span>
127 <span class="fc" id="L127"> HttpGet httpGet = null;</span>
128 <span class="fc" id="L128"> String result = null;</span>
130 <span class="fc" id="L130"> httpClient = HttpClients.createDefault();</span>
131 <span class="fc" id="L131"> httpGet = new HttpGet(url);</span>
132 <span class="fc" id="L132"> httpGet.setHeader("Content-Type", "application/json");</span>
133 <span class="fc" id="L133"> httpGet.setHeader("Accept", "application/json");</span>
134 <span class="fc" id="L134"> httpGet.setHeader("X-TransactionId", "111");</span>
135 <span class="fc" id="L135"> httpGet.setHeader("X-FromAppId", "ems-driver");</span>
136 <span class="fc" id="L136"> Base64 token = new Base64();</span>
137 <span class="fc" id="L137"> String authenticationEncoding = new String(token.encode(("AAI:AAI").getBytes()));</span>
139 <span class="fc" id="L139"> httpGet.setHeader("Authorization", "Basic " + authenticationEncoding);</span>
140 <span class="nc" id="L140"> CloseableHttpResponse response = httpClient.execute(httpGet);</span>
141 <span class="nc" id="L141"> log.info("1 doGet sucess url ="+url);</span>
143 <span class="nc bnc" id="L143" title="All 2 branches missed."> if(response != null){</span>
144 <span class="nc" id="L144"> HttpEntity resEntity = response.getEntity();</span>
145 <span class="nc bnc" id="L145" title="All 2 branches missed."> if(resEntity != null){</span>
146 <span class="nc" id="L146"> result = EntityUtils.toString(resEntity,charset);</span>
149 <span class="nc" id="L149"> } catch (Exception e) {</span>
150 <span class="nc" id="L150"> log.error("",e);</span>
152 <span class="nc bnc" id="L152" title="All 6 branches missed."> if(response != null){</span>
153 <span class="nc" id="L153"> response.close();</span>
155 <span class="nc" id="L155"> }</span>
156 <span class="fc" id="L156"> }catch(Exception e){</span>
157 <span class="fc" id="L157"> log.error("doGet is fail ",e);</span>
159 <span class="pc bpc" id="L159" title="5 of 6 branches missed."> if(httpClient != null){</span>
161 <span class="pc" id="L161"> httpClient.close();</span>
162 <span class="nc" id="L162"> } catch (IOException e) {</span>
163 <span class="pc" id="L163"> }</span>
165 <span class="nc" id="L165"> }</span>
166 <span class="fc" id="L166"> return result;</span>
169 </pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html>