fixing warnings from checkstyle in common-app-api
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / util / HttpUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.common.util;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import fj.data.Either;
26
27 import javax.servlet.ServletInputStream;
28 import javax.servlet.http.HttpServletRequest;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31
32 public class HttpUtil {
33     public static Either<String, IOException> readJsonStringFromRequest(HttpServletRequest request) {
34         Either<String, IOException> eitherResult;
35         try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
36             ServletInputStream reader = request.getInputStream();
37             int value;
38             while ((value = reader.read()) != -1) {
39                 stream.write(value);
40             }
41             eitherResult = Either.left(new String(stream.toByteArray()));
42         } catch (IOException e) {
43             eitherResult = Either.right(e);
44         }
45         return eitherResult;
46
47     }
48
49     /**
50      * Builds an object from a JSON in the POST Body of the request.
51      */
52     public static <T> Either<T, Exception> getObjectFromJson(HttpServletRequest request, Class<T> classOfT) {
53         Either<T, Exception> eitherResult;
54         try {
55             Either<String, IOException> eitherReadJson = readJsonStringFromRequest(request);
56             if (eitherReadJson.isLeft()) {
57                 eitherResult = convertJsonStringToObject(eitherReadJson.left().value(), classOfT);
58             } else {
59                 eitherResult = Either.right((Exception) eitherReadJson.right().value());
60             }
61         } catch (Exception e) {
62             eitherResult = Either.right(e);
63         }
64
65         return eitherResult;
66     }
67
68     public static <T> Either<T, Exception> convertJsonStringToObject(String sentJson, Class<T> classOfT) {
69         Either<T, Exception> eitherResult;
70         try {
71             Gson gson = new GsonBuilder().setPrettyPrinting().create();
72             T object = gson.fromJson(sentJson, classOfT);
73             eitherResult = Either.left(object);
74         } catch (Exception e) {
75             eitherResult = Either.right(e);
76         }
77         return eitherResult;
78     }
79 }