2 * Copyright 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.util;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
26 import javax.servlet.http.HttpServletRequest;
29 * Utility functions for ROA.<br/>
34 * @version 31-May-2016
36 public final class RestUtils {
38 private static final Logger LOGGER = LoggerFactory.getLogger(RestUtils.class);
45 * To get body from http request<br/>
47 * @param request : request object.
48 * @return Request body as string.
51 public static String getRequestBody(final HttpServletRequest request) {
53 final StringBuilder stringBuilder = new StringBuilder();
54 BufferedReader bufferedReader = null;
57 final InputStream inputStream = request.getInputStream();
58 if(inputStream != null) {
59 bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
60 final char[] charBuffer = new char[128];
62 while((bytesRead = bufferedReader.read(charBuffer)) > 0) {
63 stringBuilder.append(charBuffer, 0, bytesRead);
66 } catch(final IOException ex) {
67 LOGGER.error("read inputStream buffer catch exception:", ex);
69 if(bufferedReader != null) {
71 bufferedReader.close();
72 } catch(final IOException ex) {
73 LOGGER.error("close buffer catch exception:", ex);
78 body = stringBuilder.toString();