CommonLibrary(util/rest-client) code upload.
[vfc/nfvo/wfengine.git] / CommonLibrary / rest-client / src / main / java / org / openo / baseservice / roa / util / restclient / RestfulOptions.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 java.util.HashMap;
19 import java.util.Map;
20
21 /**
22  * Options for Rest communication.<br/>
23  * <p>
24  * </p>
25  * 
26  * @author
27  * @version SDNO 0.5 28-May-2016
28  */
29 public class RestfulOptions {
30
31     public static final String REST_OPTIONS_NAME_TIMEOUT = "timeout";
32
33     public static final int REST_OPTIONS_TIMEOUT_MAXTIMEOUT = 1800000;
34
35     private final Map<String, Object> optionsMap = new HashMap<>();
36
37     /**
38      * Set called service name.<br/>
39      * 
40      * @param serviceName service name.
41      * @return true.
42      * @since SDNO 0.5
43      */
44     public boolean setCalledServiceName(final String serviceName) {
45         this.setOption(RestfulClientConst.CALLED_SERVICE_NAME, serviceName);
46         return true;
47     }
48
49     /**
50      * Get called service name.<br/>
51      * 
52      * @return
53      * @since SDNO 0.5
54      */
55     public String getCalledServicName() {
56         final Object obj = this.getOption(RestfulClientConst.CALLED_SERVICE_NAME);
57         if(null == obj) {
58             return "";
59         }
60         return (String)obj;
61     }
62
63     /**
64      * Get port.<br/>
65      * 
66      * @return port.
67      * @since SDNO 0.5
68      */
69     public int getPort() {
70         final Object obj = this.getOption(RestfulClientConst.PORT_KEY_NAME);
71         if(null == obj) {
72             return 0;
73         }
74         return ((Integer)obj).intValue();
75     }
76
77     /**
78      * Set port.<br/>
79      * 
80      * @param port port to set.
81      * @return
82      * @since SDNO 0.5
83      */
84     public boolean setPort(final int port) {
85         this.setOption(RestfulClientConst.PORT_KEY_NAME, port);
86         return true;
87     }
88
89     /**
90      * Get host.<br/>
91      * 
92      * @return the host.
93      * @since SDNO 0.5
94      */
95     public String getHost() {
96         final Object obj = this.getOption(RestfulClientConst.HOST_KEY_NAME);
97         if(null == obj) {
98             return "";
99         }
100         return (String)obj;
101     }
102
103     /**
104      * Set host.<br/>
105      * 
106      * @param host host to set.
107      * @return
108      * @since SDNO 0.5
109      */
110     public boolean setHost(final String host) {
111         this.setOption(RestfulClientConst.HOST_KEY_NAME, host);
112         return true;
113     }
114
115     /**
116      * Set rest time-out.<br/>
117      * 
118      * @param timeout time-out to set in seconds.
119      * @return
120      * @since SDNO 0.5
121      */
122     public boolean setRestTimeout(final int timeout) {
123         if(0 < timeout && REST_OPTIONS_TIMEOUT_MAXTIMEOUT >= timeout) {
124             this.setOption(REST_OPTIONS_NAME_TIMEOUT, timeout);
125             return true;
126         }
127         return false;
128     }
129
130     /**
131      * Get time-out.<br/>
132      * 
133      * @return time-out in seconds.
134      * @since SDNO 0.5
135      */
136     public int getRestTimeout() {
137         final Object obj = this.getOption(REST_OPTIONS_NAME_TIMEOUT);
138         if(null == obj) {
139             return 0;
140         }
141         return ((Integer)obj).intValue();
142     }
143
144     /**
145      * Get specified option.<br/>
146      * 
147      * @param optionName option name.
148      * @return option
149      * @since SDNO 0.5
150      */
151     public Object getOption(final String optionName) {
152         return optionsMap.get(optionName);
153     }
154
155     /**
156      * Get option value as integer.<br/>
157      * 
158      * @param optionName option name.
159      * @return option value as int.
160      * @since SDNO 0.5
161      */
162     public int getIntOption(final String optionName) {
163         final Object obj = this.getOption(optionName);
164         if(null == obj) {
165             return 0;
166         }
167         return ((Integer)obj).intValue();
168     }
169
170     /**
171      * Get option value as string.<br/>
172      * 
173      * @param optionName option name.
174      * @return option value as string.
175      * @since SDNO 0.5
176      */
177     public String getStringOption(final String optionName) {
178         final Object obj = this.getOption(optionName);
179         if(null == obj) {
180             return "";
181         }
182         return (String)obj;
183     }
184
185     /**
186      * Set option.<br/>
187      * 
188      * @param option option name.
189      * @param optionsValue option value.
190      * @return
191      * @since SDNO 0.5
192      */
193     public Object setOption(final String option, final Object optionsValue) {
194         return optionsMap.put(option, optionsValue);
195     }
196 }