1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
<?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">/**
* Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onap.vfc.nfvo.emsdriver.northbound.client;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.commons.codec.binary.Base64;
/*
* HttpClient post request
*/
<span class="nc" id="L42">public class HttpClientUtil {</span>
<span class="fc" id="L44"> private static Log log = LogFactory.getLog(HttpClientUtil.class);</span>
public static String doPost(String url,String json,String charset){
<span class="fc" id="L47"> CloseableHttpClient httpClient = null;</span>
<span class="fc" id="L48"> HttpPost httpPost = null;</span>
<span class="fc" id="L49"> String result = null;</span>
try{
<span class="fc" id="L51"> httpClient = HttpClientFactory.getSSLClientFactory();</span>
<span class="fc" id="L52"> httpPost = new HttpPost(url);</span>
<span class="pc bpc" id="L53" title="1 of 2 branches missed."> if (null != json) {</span>
<span class="fc" id="L54"> StringEntity s = new StringEntity(json);</span>
<span class="fc" id="L55"> s.setContentEncoding("UTF-8");</span>
<span class="fc" id="L56"> s.setContentType("application/json"); // set contentType</span>
<span class="fc" id="L57"> httpPost.setEntity(s);</span>
}
<span class="nc" id="L59"> CloseableHttpResponse response = httpClient.execute(httpPost);</span>
try {
<span class="nc bnc" id="L61" title="All 2 branches missed."> if(response != null){</span>
<span class="nc" id="L62"> HttpEntity resEntity = response.getEntity();</span>
<span class="nc bnc" id="L63" title="All 2 branches missed."> if(resEntity != null){</span>
<span class="nc" id="L64"> result = EntityUtils.toString(resEntity,charset);</span>
}
}
<span class="nc" id="L67"> } catch (Exception e) {</span>
<span class="nc" id="L68"> log.error("httpClient.execute(httpPost) is fail",e);</span>
}finally{
<span class="nc bnc" id="L70" title="All 6 branches missed."> if(response != null){</span>
<span class="nc" id="L71"> response.close();</span>
}
<span class="nc" id="L73"> }</span>
<span class="fc" id="L74"> }catch(Exception e){</span>
<span class="fc" id="L75"> log.error("doPost is fail ",e);</span>
}finally{
<span class="pc bpc" id="L77" title="5 of 6 branches missed."> if(httpClient != null){</span>
try {
<span class="pc" id="L79"> httpClient.close();</span>
<span class="nc" id="L80"> } catch (IOException e) {</span>
<span class="pc" id="L81"> }</span>
}
<span class="nc" id="L84"> }</span>
<span class="fc" id="L85"> return result;</span>
}
public static String doDelete(String url ,String charset){
<span class="fc" id="L89"> CloseableHttpClient httpClient = null;</span>
<span class="fc" id="L90"> HttpDelete httpDelete = null;</span>
<span class="fc" id="L91"> String result = null;</span>
try{
<span class="fc" id="L93"> httpClient = HttpClientFactory.getSSLClientFactory();</span>
<span class="fc" id="L94"> httpDelete = new HttpDelete(url);</span>
<span class="nc" id="L96"> CloseableHttpResponse response = httpClient.execute(httpDelete);</span>
try {
<span class="nc bnc" id="L99" title="All 2 branches missed."> if(response != null){</span>
<span class="nc" id="L100"> HttpEntity resEntity = response.getEntity();</span>
<span class="nc bnc" id="L101" title="All 2 branches missed."> if(resEntity != null){</span>
<span class="nc" id="L102"> result = EntityUtils.toString(resEntity,charset);</span>
}
}
<span class="nc" id="L105"> } catch (Exception e) {</span>
<span class="nc" id="L106"> log.error("",e);</span>
}finally{
<span class="nc bnc" id="L108" title="All 6 branches missed."> if(response != null){</span>
<span class="nc" id="L109"> response.close();</span>
}
<span class="nc" id="L111"> }</span>
<span class="fc" id="L112"> }catch(Exception e){</span>
<span class="fc" id="L113"> log.error("doDelete is fail ",e);</span>
}finally{
<span class="pc bpc" id="L115" title="5 of 6 branches missed."> if(httpClient != null){</span>
try {
<span class="pc" id="L117"> httpClient.close();</span>
<span class="nc" id="L118"> } catch (IOException e) {</span>
<span class="pc" id="L119"> }</span>
}
<span class="nc" id="L121"> }</span>
<span class="fc" id="L122"> return result;</span>
}
public static String doGet(String url, String charset){
<span class="fc" id="L126"> CloseableHttpClient httpClient = null;</span>
<span class="fc" id="L127"> HttpGet httpGet = null;</span>
<span class="fc" id="L128"> String result = null;</span>
try{
<span class="fc" id="L130"> httpClient = HttpClients.createDefault();</span>
<span class="fc" id="L131"> httpGet = new HttpGet(url);</span>
<span class="fc" id="L132"> httpGet.setHeader("Content-Type", "application/json");</span>
<span class="fc" id="L133"> httpGet.setHeader("Accept", "application/json");</span>
<span class="fc" id="L134"> httpGet.setHeader("X-TransactionId", "111");</span>
<span class="fc" id="L135"> httpGet.setHeader("X-FromAppId", "ems-driver");</span>
<span class="fc" id="L136"> Base64 token = new Base64();</span>
<span class="fc" id="L137"> String authenticationEncoding = new String(token.encode(("AAI:AAI").getBytes()));</span>
<span class="fc" id="L139"> httpGet.setHeader("Authorization", "Basic " + authenticationEncoding);</span>
<span class="nc" id="L140"> CloseableHttpResponse response = httpClient.execute(httpGet);</span>
<span class="nc" id="L141"> log.info("1 doGet sucess url ="+url);</span>
try {
<span class="nc bnc" id="L143" title="All 2 branches missed."> if(response != null){</span>
<span class="nc" id="L144"> HttpEntity resEntity = response.getEntity();</span>
<span class="nc bnc" id="L145" title="All 2 branches missed."> if(resEntity != null){</span>
<span class="nc" id="L146"> result = EntityUtils.toString(resEntity,charset);</span>
}
}
<span class="nc" id="L149"> } catch (Exception e) {</span>
<span class="nc" id="L150"> log.error("",e);</span>
}finally{
<span class="nc bnc" id="L152" title="All 6 branches missed."> if(response != null){</span>
<span class="nc" id="L153"> response.close();</span>
}
<span class="nc" id="L155"> }</span>
<span class="fc" id="L156"> }catch(Exception e){</span>
<span class="fc" id="L157"> log.error("doGet is fail ",e);</span>
}finally{
<span class="pc bpc" id="L159" title="5 of 6 branches missed."> if(httpClient != null){</span>
try {
<span class="pc" id="L161"> httpClient.close();</span>
<span class="nc" id="L162"> } catch (IOException e) {</span>
<span class="pc" id="L163"> }</span>
}
<span class="nc" id="L165"> }</span>
<span class="fc" id="L166"> return result;</span>
}
}
</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>
|