Fix for radio buttons
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / rest / api / RestResponseAsByteArray.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.rest.api;
22
23 import java.util.Arrays;
24
25 import org.apache.http.HttpStatus;
26
27 public class RestResponseAsByteArray {
28
29         private byte[] response;
30
31         private String statusDescription;
32
33         private int httpStatusCode = 0;
34
35         public RestResponseAsByteArray(byte[] response, String statusDescription, int httpStatusCode) {
36                 super();
37                 this.response = response;
38                 this.statusDescription = statusDescription;
39                 this.httpStatusCode = httpStatusCode;
40         }
41
42         /**
43          * @return the response
44          */
45         public byte[] getResponse() {
46                 return response;
47         }
48
49         /**
50          * @param response
51          *            the response to set
52          */
53         public void setResponse(byte[] response) {
54                 this.response = response;
55         }
56
57         /**
58          * @return the httpStatusCode
59          */
60         public int getHttpStatusCode() {
61                 return httpStatusCode;
62         }
63
64         /**
65          * @param httpStatusCode
66          *            the httpStatusCode to set
67          */
68         public void setHttpStatusCode(int httpStatusCode) {
69                 this.httpStatusCode = httpStatusCode;
70         }
71
72         /**
73          * @return the statusDescription
74          */
75         public String getStatusDescription() {
76                 return statusDescription;
77         }
78
79         /**
80          * @param statusDescription
81          *            the statusDescription to set
82          */
83         public void setStatusDescription(String statusDescription) {
84                 this.statusDescription = statusDescription;
85         }
86
87         public String toString() {
88
89                 StringBuilder stringBuilder = new StringBuilder();
90
91                 stringBuilder.append("Status: ");
92                 stringBuilder.append(httpStatusCode);
93                 stringBuilder.append("\n");
94                 stringBuilder.append("Message: ");
95                 stringBuilder.append(statusDescription);
96                 stringBuilder.append("\n");
97                 stringBuilder.append("Body length: ");
98                 stringBuilder.append(response == null ? 0 : response.length);
99
100                 return stringBuilder.toString();
101
102         }
103
104         public String toPrettyString() {
105
106                 int maxBytesToDisplay = 200;
107
108                 StringBuilder stringBuilder = new StringBuilder();
109
110                 stringBuilder.append("Status: ");
111                 stringBuilder.append(httpStatusCode);
112                 stringBuilder.append("\n");
113                 stringBuilder.append("Message: ");
114                 stringBuilder.append(statusDescription);
115                 stringBuilder.append("\n");
116                 if (httpStatusCode != HttpStatus.SC_OK) {
117                         stringBuilder.append("Body(maximum " + maxBytesToDisplay + " bytes): ");
118                         if (response != null) {
119                                 byte[] subArray = Arrays.copyOfRange(response, 0, Math.min(maxBytesToDisplay, response.length));
120                                 if (subArray != null && subArray.length > 0) {
121                                         String responseStr = new String(subArray);
122                                         stringBuilder.append(responseStr);
123                                 }
124                         }
125                 } else {
126                         stringBuilder.append("Body length: ");
127                         stringBuilder.append(response == null ? 0 : response.length);
128                 }
129
130                 return stringBuilder.toString();
131
132         }
133 }