2  * Copyright 2016-2017, Nokia Corporation
 
   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.
 
  17 package com.nokia.vfcadaptor.vnfmdriver.controller;
 
  19 import java.io.IOException;
 
  20 import java.io.UnsupportedEncodingException;
 
  22 import javax.net.ssl.SSLContext;
 
  23 import javax.net.ssl.TrustManager;
 
  24 import javax.net.ssl.X509TrustManager;
 
  26 import org.apache.http.HttpEntity;
 
  27 import org.apache.http.HttpResponse;
 
  28 import org.apache.http.client.ClientProtocolException;
 
  29 import org.apache.http.client.HttpClient;
 
  30 import org.apache.http.client.methods.HttpGet;
 
  31 import org.apache.http.client.methods.HttpPost;
 
  32 import org.apache.http.conn.scheme.Scheme;
 
  33 import org.apache.http.conn.scheme.SchemeRegistry;
 
  34 import org.apache.http.conn.ssl.SSLSocketFactory;
 
  35 import org.apache.http.entity.StringEntity;
 
  36 import org.apache.http.impl.client.DefaultHttpClient;
 
  37 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
 
  38 import org.apache.http.util.EntityUtils;
 
  39 import org.junit.BeforeClass;
 
  40 import org.slf4j.Logger;
 
  41 import org.slf4j.LoggerFactory;
 
  42 import org.springframework.http.MediaType;
 
  44 public class BaseControllerTestCase {
 
  46         private static final String CONTENT_TYPE = "Content-Type";
 
  47         private static final String AUTH = "auth";
 
  49         private static final String UTF_8 = "utf-8";
 
  51         protected static String serviceUrl = "http://127.0.0.1:8080/AppSenseAnalysisSystem";
 
  53         protected static String baseUrl;
 
  55         protected static String pictureServerRootUrl = "http://localhost";
 
  57         protected Logger log = LoggerFactory.getLogger(this.getClass());
 
  58         protected boolean isHttpsProtocol = false;
 
  61         public static void beforeClass() throws Exception {
 
  65         public static org.apache.http.client.HttpClient wrapClient(org.apache.http.client.HttpClient base) {  
 
  67             SSLContext ctx = SSLContext.getInstance("TLS");  
 
  68             X509TrustManager tm = new X509TrustManager() {  
 
  69                 public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
 
  72                 public void checkClientTrusted(  
 
  73                         java.security.cert.X509Certificate[] chain,  
 
  75                         throws java.security.cert.CertificateException {  
 
  76                     // TODO Auto-generated method stub  
 
  79                 public void checkServerTrusted(  
 
  80                         java.security.cert.X509Certificate[] chain,  
 
  82                         throws java.security.cert.CertificateException {  
 
  83                     // TODO Auto-generated method stub  
 
  87             ctx.init(null, new TrustManager[] { tm }, null);  
 
  88             SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
 
  89             SchemeRegistry registry = new SchemeRegistry();  
 
  90             registry.register(new Scheme("https", 8089, ssf));  
 
  91             ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(registry);  
 
  92             return new DefaultHttpClient(mgr, base.getParams());  
 
  93         } catch (Exception ex) {  
 
  99         protected String sendPostMsg(String message, String url) throws UnsupportedEncodingException,
 
 100                         IOException, ClientProtocolException {
 
 102                 HttpClient httpclient = new DefaultHttpClient();
 
 105                     httpclient = wrapClient(httpclient);
 
 107                 HttpPost httppost = new HttpPost(url);
 
 108                 StringEntity myEntity = new StringEntity(message, UTF_8);
 
 110                 httppost.addHeader(AUTH, auth);
 
 111                 httppost.addHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
 
 112 //              httppost.addHeader(CONTENT_TYPE, MediaType.TEXT_XML_VALUE);
 
 113                 httppost.setEntity(myEntity);
 
 114                 HttpResponse response = httpclient.execute(httppost);
 
 115                 HttpEntity resEntity = response.getEntity();
 
 116                 String responseContent = "";
 
 117                 if (resEntity != null) {
 
 118                         responseContent = EntityUtils.toString(resEntity, "UTF-8");
 
 119                         EntityUtils.consume(resEntity);
 
 121                 httpclient.getConnectionManager().shutdown();
 
 122                 return responseContent;
 
 125         protected String sendGetMsg(String message, String url) throws UnsupportedEncodingException,
 
 126         IOException, ClientProtocolException {
 
 128       HttpClient httpclient = new DefaultHttpClient();
 
 131      httpclient = wrapClient(httpclient);
 
 133      HttpGet  httpGet = new  HttpGet(url);
 
 134      StringEntity myEntity = new StringEntity(message, UTF_8);
 
 136      httpGet.addHeader(AUTH, auth);
 
 137      httpGet.addHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
 
 138         //httppost.addHeader(CONTENT_TYPE, MediaType.TEXT_XML_VALUE);
 
 139      //((HttpResponse) httpGet).setEntity(myEntity);
 
 140   HttpResponse response = httpclient.execute(httpGet);
 
 141   HttpEntity resEntity = response.getEntity();
 
 142    String responseContent = "";
 
 143    if (resEntity != null) {
 
 144         responseContent = EntityUtils.toString(resEntity, "UTF-8");
 
 145         responseContent.replaceAll("\r", "");//
 
 146         EntityUtils.consume(resEntity);
 
 148    httpclient.getConnectionManager().shutdown();
 
 149    return responseContent;