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