diff options
author | tian.ming@huawei.com <tian.ming@huawei.com> | 2016-08-25 21:13:33 +0800 |
---|---|---|
committer | tian.ming@huawei.com <tian.ming@huawei.com> | 2016-08-25 21:13:33 +0800 |
commit | 79d67955b160929ffacc6828014d3c5ce3110fb8 (patch) | |
tree | 011fb1952e2cfd8292d6ce247abdd745bfe1d700 /common-util/src/main/java/org/openo/baseservice/util | |
parent | 8b49886b96d5f7ebc91dbf1aa158b31ac4a72848 (diff) |
adjust the code
Change-Id: Id896bed917ba6730c6b5d96665ef9978cfb413de
Signed-off-by: tian.ming@huawei.com <tian.ming@huawei.com>
Diffstat (limited to 'common-util/src/main/java/org/openo/baseservice/util')
4 files changed, 229 insertions, 0 deletions
diff --git a/common-util/src/main/java/org/openo/baseservice/util/RestUtils.java b/common-util/src/main/java/org/openo/baseservice/util/RestUtils.java new file mode 100644 index 0000000..ee250b1 --- /dev/null +++ b/common-util/src/main/java/org/openo/baseservice/util/RestUtils.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.baseservice.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import javax.servlet.http.HttpServletRequest; + +/** + * Utility functions for ROA.<br/> + * <p> + * </p> + * + * @author + * @version SDNO 0.5 31-May-2016 + */ +public final class RestUtils { + + private static final Logger LOGGER = LoggerFactory.getLogger(RestUtils.class); + + private RestUtils() { + + } + + /** + * To get body from http request<br/> + * + * @param request : request object. + * @return Request body as string. + * @since SDNO 0.5 + */ + public static String getRequestBody(final HttpServletRequest request) { + String body = null; + final StringBuilder stringBuilder = new StringBuilder(); + BufferedReader bufferedReader = null; + + try { + final InputStream inputStream = request.getInputStream(); + if(inputStream != null) { + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + final char[] charBuffer = new char[128]; + int bytesRead = -1; + while((bytesRead = bufferedReader.read(charBuffer)) > 0) { + stringBuilder.append(charBuffer, 0, bytesRead); + } + } + } catch(final IOException ex) { + LOGGER.error("read inputStream buffer catch exception:", ex); + } finally { + if(bufferedReader != null) { + try { + bufferedReader.close(); + } catch(final IOException ex) { + LOGGER.error("close buffer catch exception:", ex); + } + } + } + + body = stringBuilder.toString(); + return body; + } + +} diff --git a/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesDefImpl.java b/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesDefImpl.java new file mode 100644 index 0000000..bd98fb9 --- /dev/null +++ b/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesDefImpl.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.baseservice.util.impl; + +import org.openo.baseservice.util.inf.SystemEnvVariables; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; + +/** + * System environment variable helper implementation.<br/> + * <p> + * </p> + * + * @author + * @version SDNO 0.5 24-Jun-2016 + */ +public class SystemEnvVariablesDefImpl extends SystemEnvVariables { + + private static final Logger LOG = LoggerFactory.getLogger(SystemEnvVariablesDefImpl.class); + + @Override + public String getAppRoot() { + String appRoot = null; + appRoot = System.getProperty("catalina.base"); + if(appRoot != null) { + appRoot = getCanonicalPath(appRoot); + } + return appRoot; + } + + /** + * Gets the canonical path<br/> + * + * @param inPath input path + * @return the canonical path. + * @since SDNO 0.5 + */ + private String getCanonicalPath(final String inPath) { + String path = null; + try { + if(inPath != null) { + final File file = new File(inPath); + path = file.getCanonicalPath(); + } + } catch(final IOException e) { + LOG.error("file.getCanonicalPath() IOException:", e); + } + return path; + } + +} diff --git a/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesFactory.java b/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesFactory.java new file mode 100644 index 0000000..e40ab53 --- /dev/null +++ b/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesFactory.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openo.baseservice.util.impl; + +import org.openo.baseservice.util.inf.SystemEnvVariables; + +/** + * SystemEnvVariables factory class + * <br/> + * <p> + * </p> + * + * @author + * @version SDNO 0.5 2016年8月13日 + */ +public class SystemEnvVariablesFactory { + + private static SystemEnvVariables systemEnvVariablesInstance = new SystemEnvVariablesDefImpl(); + + /** + * get the SystemEnvVariables instance + * <br/> + * + * @return SystemEnvVariables instance + * @since SDNO 0.5 + */ + public static SystemEnvVariables getInstance() { + return systemEnvVariablesInstance; + } +} diff --git a/common-util/src/main/java/org/openo/baseservice/util/inf/SystemEnvVariables.java b/common-util/src/main/java/org/openo/baseservice/util/inf/SystemEnvVariables.java new file mode 100644 index 0000000..014cdbd --- /dev/null +++ b/common-util/src/main/java/org/openo/baseservice/util/inf/SystemEnvVariables.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.baseservice.util.inf; + +/** + * Dependency the system env<br/> + * <p> + * </p> + * + * @author + * @version SDNO 0.5 31-May-2016 + */ +public abstract class SystemEnvVariables { + + /** + * Get application root path <br/> + * + * @return application root path. + * @since SDNO 0.5 + */ + public abstract String getAppRoot(); +} |