workaroud for snor issue
[vfc/nfvo/wfengine.git] / wso2 / rest-client / src / main / java / org / openo / baseservice / roa / util / restclient / RestfulOptions.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 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   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      * Get port.<br/>
39      * 
40      * @return port.
41      * @since  
42      */
43     public int getPort() {
44         final Object obj = this.getOption(RestfulClientConst.PORT_KEY_NAME);
45         if(null == obj) {
46             return 0;
47         }
48         return ((Integer)obj).intValue();
49     }
50
51     /**
52      * Set port.<br/>
53      * 
54      * @param port port to set.
55      * @return
56      * @since  
57      */
58     public boolean setPort(final int port) {
59         this.setOption(RestfulClientConst.PORT_KEY_NAME, port);
60         return true;
61     }
62
63     /**
64      * Get host.<br/>
65      * 
66      * @return the host.
67      * @since  
68      */
69     public String getHost() {
70         final Object obj = this.getOption(RestfulClientConst.HOST_KEY_NAME);
71         if(null == obj) {
72             return "";
73         }
74         return (String)obj;
75     }
76
77     /**
78      * Set host.<br/>
79      * 
80      * @param host host to set.
81      * @return
82      * @since  
83      */
84     public boolean setHost(final String host) {
85         this.setOption(RestfulClientConst.HOST_KEY_NAME, host);
86         return true;
87     }
88
89     /**
90      * Set rest time-out.<br/>
91      * 
92      * @param timeout time-out to set in seconds.
93      * @return
94      * @since  
95      */
96     public boolean setRestTimeout(final int timeout) {
97         if(0 < timeout && REST_OPTIONS_TIMEOUT_MAXTIMEOUT >= timeout) {
98             this.setOption(REST_OPTIONS_NAME_TIMEOUT, timeout);
99             return true;
100         }
101         return false;
102     }
103
104     /**
105      * Get time-out.<br/>
106      * 
107      * @return time-out in seconds.
108      * @since  
109      */
110     public int getRestTimeout() {
111         final Object obj = this.getOption(REST_OPTIONS_NAME_TIMEOUT);
112         if(null == obj) {
113             return 0;
114         }
115         return ((Integer)obj).intValue();
116     }
117
118     /**
119      * Get specified option.<br/>
120      * 
121      * @param optionName option name.
122      * @return option
123      * @since  
124      */
125     public Object getOption(final String optionName) {
126         return optionsMap.get(optionName);
127     }
128
129     /**
130      * Get option value as integer.<br/>
131      * 
132      * @param optionName option name.
133      * @return option value as int.
134      * @since  
135      */
136     public int getIntOption(final String optionName) {
137         final Object obj = this.getOption(optionName);
138         if(null == obj) {
139             return 0;
140         }
141         return ((Integer)obj).intValue();
142     }
143
144     /**
145      * Get option value as string.<br/>
146      * 
147      * @param optionName option name.
148      * @return option value as string.
149      * @since  
150      */
151     public String getStringOption(final String optionName) {
152         final Object obj = this.getOption(optionName);
153         if(null == obj) {
154             return "";
155         }
156         return (String)obj;
157     }
158
159     /**
160      * Set option.<br/>
161      * 
162      * @param option option name.
163      * @param optionsValue option value.
164      * @return
165      * @since  
166      */
167     public Object setOption(final String option, final Object optionsValue) {
168         return optionsMap.put(option, optionsValue);
169     }
170 }