Update vfc svnfm driver pom
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / csm / connect / SslCertificateSocket.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
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
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 "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.
15  */
16 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect;
17
18 import org.apache.commons.httpclient.ConnectTimeoutException;
19 import org.apache.commons.httpclient.params.HttpConnectionParams;
20 import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
21 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
22 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmException;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import javax.net.ssl.SSLSocketFactory;
27 import java.io.IOException;
28 import java.net.InetAddress;
29 import java.net.Socket;
30 import java.security.GeneralSecurityException;
31
32 /**
33  * Created by QuanZhong on 2017/3/6.
34  */
35 public class SslCertificateSocket extends AbstractSslContext implements SecureProtocolSocketFactory {
36
37     private static final Logger LOG = LoggerFactory.getLogger(SslAnonymousSocket.class);
38
39     private SSLSocketFactory sslSocketFactory = null;
40
41     /**
42      * Initialize
43      * <br>
44      *
45      * @throws VnfmException
46      * @since  NFVO 0.5
47      */
48     public void init() throws VnfmException {
49         try {
50             sslSocketFactory = getCertificateSSLContext().getSocketFactory();
51         } catch(GeneralSecurityException e) {
52             LOG.error("function=init, get Anonymous SSLContext exception, exceptioninfo", e);
53             throw (VnfmException)new VnfmException().initCause(e);
54         }
55     }
56
57     @Override
58     public Socket createSocket(String host, int port) throws IOException {
59         return sslSocketFactory.createSocket(host, port);
60     }
61
62     @Override
63     public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException {
64         return sslSocketFactory.createSocket(host, port, clientHost, clientPort);
65     }
66
67     @Override
68     public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
69                                HttpConnectionParams params) throws IOException, ConnectTimeoutException {
70         if(params == null) {
71             throw new IOException("Illegal socket parameters!");
72         } else {
73             int timeout = params.getConnectionTimeout();
74
75             if(timeout == 0) {
76                 return createSocket(host, port, localAddress, localPort);
77             } else {
78                 return ControllerThreadSocketFactory.createSocket(this, host, port, localAddress, localPort, timeout);
79             }
80         }
81     }
82
83     @Override
84     public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
85         return sslSocketFactory.createSocket(socket, host, port, autoClose);
86     }
87 }