6eb404a73ff18cd191dbaa4a2420db9509c6b2e3
[vfc/nfvo/driver/ems.git] /
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> &gt; <a href="index.source.html" class="el_package">org.onap.vfc.nfvo.emsdriver.northbound.client</a> &gt; <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
3  *
4  * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an &quot;AS IS&quot; 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.
15  */
16 package org.onap.vfc.nfvo.emsdriver.northbound.client;
17
18 import java.io.IOException;
19
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;
36
37 import org.apache.commons.codec.binary.Base64;
38
39 /*
40  * HttpClient post request
41  */
42 <span class="nc" id="L42">public class HttpClientUtil {</span>
43         
44 <span class="fc" id="L44">      private static Log log = LogFactory.getLog(HttpClientUtil.class);</span>
45         
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>
50         try{
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(&quot;UTF-8&quot;);</span>
56 <span class="fc" id="L56">                              s.setContentType(&quot;application/json&quot;); // set contentType</span>
57 <span class="fc" id="L57">                              httpPost.setEntity(s);</span>
58                         }
59 <span class="nc" id="L59">            CloseableHttpResponse response = httpClient.execute(httpPost);</span>
60             try {
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>
65                                     }
66                                 }
67 <span class="nc" id="L67">                      } catch (Exception e) {</span>
68 <span class="nc" id="L68">                              log.error(&quot;httpClient.execute(httpPost) is fail&quot;,e);</span>
69                         }finally{
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>
72                                 }
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(&quot;doPost is fail &quot;,e);</span>
76         }finally{
77 <span class="pc bpc" id="L77" title="5 of 6 branches missed.">          if(httpClient != null){</span>
78                         try {
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>
82                 }
83                 
84 <span class="nc" id="L84">              }</span>
85 <span class="fc" id="L85">        return result;</span>
86     }
87     
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>
92         try{
93 <span class="fc" id="L93">            httpClient = HttpClientFactory.getSSLClientFactory();</span>
94 <span class="fc" id="L94">            httpDelete = new HttpDelete(url);</span>
95             
96 <span class="nc" id="L96">            CloseableHttpResponse response = httpClient.execute(httpDelete);</span>
97             
98             try {
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>
103                                     }
104                                 }
105 <span class="nc" id="L105">                     } catch (Exception e) {</span>
106 <span class="nc" id="L106">                             log.error(&quot;&quot;,e);</span>
107                         }finally{
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>
110                                 }
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(&quot;doDelete is fail &quot;,e);</span>
114         }finally{
115 <span class="pc bpc" id="L115" title="5 of 6 branches missed.">         if(httpClient != null){</span>
116                         try {
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>
120                         }
121 <span class="nc" id="L121">        }</span>
122 <span class="fc" id="L122">        return result;</span>
123     }
124     
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>
129         try{
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(&quot;Content-Type&quot;, &quot;application/json&quot;);</span>
133 <span class="fc" id="L133">            httpGet.setHeader(&quot;Accept&quot;, &quot;application/json&quot;);</span>
134 <span class="fc" id="L134">            httpGet.setHeader(&quot;X-TransactionId&quot;, &quot;111&quot;);</span>
135 <span class="fc" id="L135">            httpGet.setHeader(&quot;X-FromAppId&quot;, &quot;ems-driver&quot;);</span>
136 <span class="fc" id="L136">            Base64 token = new Base64();</span>
137 <span class="fc" id="L137">            String authenticationEncoding = new String(token.encode((&quot;AAI:AAI&quot;).getBytes()));</span>
138
139 <span class="fc" id="L139">            httpGet.setHeader(&quot;Authorization&quot;, &quot;Basic &quot; + authenticationEncoding);</span>
140 <span class="nc" id="L140">            CloseableHttpResponse response = httpClient.execute(httpGet);</span>
141 <span class="nc" id="L141">            log.info(&quot;1 doGet sucess url =&quot;+url);</span>
142             try {
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>
147                                     }
148                                 }
149 <span class="nc" id="L149">                     } catch (Exception e) {</span>
150 <span class="nc" id="L150">                             log.error(&quot;&quot;,e);</span>
151                         }finally{
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>
154                                 }
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(&quot;doGet is fail &quot;,e);</span>
158         }finally{
159 <span class="pc bpc" id="L159" title="5 of 6 branches missed.">         if(httpClient != null){</span>
160                         try {
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>
164                         }
165 <span class="nc" id="L165">        }</span>
166 <span class="fc" id="L166">        return result;</span>
167     }
168 }
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>