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