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