e9ccc53b3b21e0005af5026803371ca15ff713fc
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk feature sdnr wt
4  *  ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  ******************************************************************************/
21 package org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeConnector.test;
22
23 import java.io.BufferedReader;
24 import java.io.InputStreamReader;
25 import java.io.Reader;
26 import java.net.URL;
27 import java.net.URLConnection;
28 import java.security.cert.X509Certificate;
29 import javax.net.ssl.HostnameVerifier;
30 import javax.net.ssl.HttpsURLConnection;
31 import javax.net.ssl.SSLContext;
32 import javax.net.ssl.TrustManager;
33 import javax.net.ssl.X509TrustManager;
34
35 public class DcaePrivateTester {
36
37
38     public static void test(URL url, boolean readFromServer) throws Exception {
39         // Create a trust manager that does not validate certificate chains
40         TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
41             @Override
42             public java.security.cert.X509Certificate[] getAcceptedIssuers() {
43                 return null;
44             }
45             @Override
46             public void checkClientTrusted(X509Certificate[] certs, String authType) {
47             }
48             @Override
49             public void checkServerTrusted(X509Certificate[] certs, String authType) {
50             }
51         } };
52         // Install the all-trusting trust manager
53         final SSLContext sc = SSLContext.getInstance("TLS");
54         sc.init(null, trustAllCerts, new java.security.SecureRandom());
55         HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
56         // Create all-trusting host name verifier
57         HostnameVerifier allHostsValid = (hostname, session) -> true;
58
59         // Install the all-trusting host verifier
60         HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
61
62         //URL url = new URL("https://www.google.com");
63         URLConnection con = url.openConnection();
64         System.out.println("Connection background: "+con.getClass().getName()+" "+url.getHost());
65
66         if (readFromServer) {
67             final Reader reader = new InputStreamReader(con.getInputStream());
68             final BufferedReader br = new BufferedReader(reader);
69             String line = "";
70             while ((line = br.readLine()) != null) {
71                 System.out.println(line);
72             }
73             br.close();
74         } /**/
75     } // End of main
76
77
78
79 //    httpTestUrl=https://plan.fritz.box:9092/ux/#
80 //    keyStore=etc/clientkeystore
81 //    keyStorePassword=daylight2016
82     public static void main(String[] args) {
83
84         String urlString = "https://www.google.de";
85         //String urlString = "https://plan.fritz.box:9092/ux/#";
86         //String urlString = "http://plan.fritz.box:9091/ux/#";
87         //String urlString = "http://127.0.0.1:30000/eventListener/v3";
88
89         try {
90             test(new URL(urlString), true);
91         } catch (Exception e) {
92             System.out.println("(..something..) failed");
93             e.printStackTrace();
94         }
95         /*
96         System.out.println("Test HTTPS");
97
98         final HttpsClient httpTestClient;
99         httpTestClient = new HttpsClient();
100
101         httpTestClient.testIt(
102                 //"https://plan.fritz.box:9092/ux/#",
103                 "https://www.google.de",
104                 "/home/herbert/odl/distribution-karaf-0.5.1-Boron-SR1/etc/clientkeystore",
105                 "daylight2016"
106         );/**/
107
108     }
109
110 }