e721d6a4f74639d4fbcb9494cd8af212a152d5e6
[vfc/nfvo/resmanagement.git] /
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.onap.vfc.nfvo.resmanagement.common.util.restclient;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24
25 /**
26  * RestFul instance factory. <br/>
27  * <p>
28  * </p>
29  * 
30  * @author
31  * @version 28-May-2016
32  */
33 public class RestfulFactory {
34
35     /**
36      * https protocol.
37      */
38     public static final String PROTO_HTTPS = "https";
39
40     /**
41      * http protocol.
42      */
43     public static final String PROTO_HTTP = "http";
44
45     private static final Logger LOG = LogManager.getLogger(RestfulFactory.class);
46
47     private static final Map<String, Restful> INSTANCES = new HashMap<>(2);
48
49     private RestfulFactory() {
50
51     }
52
53     /**
54      * Get RESTful instance. This method returns a singleton instance.
55      * <br/>
56      * 
57      * @param protocol protocol. currently only support 'http'.
58      * @return restful instance.
59      * @since
60      */
61     public static synchronized Restful getRestInstance(final String protocol) {
62         Restful rest = INSTANCES.get(protocol);
63         if(rest != null) {
64             return rest;
65         }
66         if(PROTO_HTTP.equals(protocol)) {
67             rest = createHttpRest();
68             INSTANCES.put(protocol, rest);
69         }
70
71         if (PROTO_HTTPS.equals(protocol)) {
72             rest = createHttpsRest();
73             INSTANCES.put(protocol, rest);
74         }
75         return rest;
76     }
77
78     private static Restful createHttpRest() {
79         final HttpRest rest = new HttpRest();
80         setRestOption(rest, null);
81         return rest;
82     }
83
84     private static Restful createHttpsRest() {
85         final HttpsRest rest = new HttpsRest();
86         rest.initHttpsRest();
87         return rest;
88     }
89
90     private static void setRestOption(final HttpRest rest, final String restoptionfile) {
91         try {
92             RestfulConfigure config;
93             if(restoptionfile == null || restoptionfile.isEmpty()) {
94                 config = new RestfulConfigure();
95             } else {
96                 config = new RestfulConfigure(restoptionfile);
97             }
98
99             final RestfulOptions option = config.getOptions();
100             rest.initHttpRest(option);
101         } catch(final ServiceException e) {
102             LOG.error("init http client exception: ", e);
103         }
104     }
105 }