2 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openo.baseservice.roa.util.restclient;
18 import java.io.BufferedReader;
20 import java.io.FileReader;
21 import java.io.IOException;
23 import org.openo.baseservice.roa.util.ReaderHelper;
24 import org.openo.baseservice.util.impl.SystemEnvVariablesFactory;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import net.sf.json.JSONObject;
31 * Rest client options.<br/>
36 * @version SDNO 0.5 28-May-2016
38 public class RestfulConfigure {
40 private static final Logger LOG = LoggerFactory.getLogger(RestfulConfigure.class);
42 private RestfulOptions options = null;
47 * Use the default path initialization http Rest options.
52 public RestfulConfigure() {
53 final String config = "/etc/conf/restclient.json";
54 final String approot = SystemEnvVariablesFactory.getInstance().getAppRoot();
55 final StringBuilder extendSetting = new StringBuilder();
58 extendSetting.append(approot);
59 extendSetting.append(config);
61 extendSetting.append(System.getProperty("user.dir"));
62 extendSetting.append(config);
65 final String configfile = extendSetting.toString();
66 initRestConf(configfile);
72 * Use the specified file to initialize http Rest options.
78 public RestfulConfigure(final String configfile) {
79 initRestConf(configfile);
82 public RestfulOptions getOptions() {
86 private void initRestConf(final String configfile) {
87 options = getDefaultOptions();
89 final JSONObject obj = loadJsonFromFile(configfile);
91 if(obj.has(RestfulClientConst.SERVER_KEY_NAME)) {
92 final JSONObject server = obj.getJSONObject(RestfulClientConst.SERVER_KEY_NAME);
93 setStringOption(server, RestfulClientConst.HOST_KEY_NAME);
94 setIntOption(server, RestfulClientConst.PORT_KEY_NAME);
96 setIntOption(obj, RestfulClientConst.CONN_TIMEOUT_KEY_NAME);
97 setIntOption(obj, RestfulClientConst.THREAD_KEY_NAME);
98 setIntOption(obj, RestfulClientConst.IDLE_TIMEOUT_KEY_NAME);
99 setIntOption(obj, RestfulClientConst.TIMEOUT_KEY_NAME);
100 setIntOption(obj, RestfulClientConst.MAX_CONN_PER_ADDR_KEY_NAME);
101 setIntOption(obj, RestfulClientConst.MAX_RESPONSE_HEADER_SIZE);
102 setIntOption(obj, RestfulClientConst.MAX_REQUEST_HEADER_SIZE);
104 LOG.error("failed to load json from " + configfile);
108 private void setStringOption(final JSONObject json, final String key) {
110 options.setOption(key, json.getString(key));
114 private void setIntOption(final JSONObject json, final String key) {
116 options.setOption(key, json.getInt(key));
120 private RestfulOptions getDefaultOptions() {
121 options = new RestfulOptions();
122 options.setOption(RestfulClientConst.CONN_TIMEOUT_KEY_NAME, 3000);
123 options.setOption(RestfulClientConst.THREAD_KEY_NAME, 200);
124 options.setOption(RestfulClientConst.IDLE_TIMEOUT_KEY_NAME, 30000);
125 options.setOption(RestfulClientConst.TIMEOUT_KEY_NAME, 30000);
126 options.setOption(RestfulClientConst.MAX_CONN_PER_ADDR_KEY_NAME, 50);
127 options.setOption(RestfulClientConst.MAX_RESPONSE_HEADER_SIZE, 20 * 1024);
128 options.setOption(RestfulClientConst.MAX_REQUEST_HEADER_SIZE, 20 * 1024);
132 private JSONObject loadJsonFromFile(final String filePath) {
133 final File file = new File(filePath);
134 if((!file.exists()) || (!file.isFile())) {
135 LOG.error(filePath + "isn't exist.");
138 BufferedReader reader = null;
139 final StringBuilder jsonstr = new StringBuilder();
140 JSONObject jo = null;
142 reader = new BufferedReader(new FileReader(file));
143 final ReaderHelper rHelpper = new ReaderHelper(reader);
144 String tempString = null;
145 while((tempString = rHelpper.getLine()) != null) {
146 jsonstr.append(tempString);
148 jo = JSONObject.fromObject(jsonstr.toString());
149 } catch(final IOException e) {
150 LOG.error("load file exception:" + e);
155 } catch(final IOException e) {
156 LOG.error("close error.", e);