2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.common.rest.api;
23 import java.util.Arrays;
25 import org.apache.http.HttpStatus;
27 public class RestResponseAsByteArray {
29 private byte[] response;
31 private String statusDescription;
33 private int httpStatusCode = 0;
35 public RestResponseAsByteArray(byte[] response, String statusDescription, int httpStatusCode) {
37 this.response = response;
38 this.statusDescription = statusDescription;
39 this.httpStatusCode = httpStatusCode;
43 * @return the response
45 public byte[] getResponse() {
53 public void setResponse(byte[] response) {
54 this.response = response;
58 * @return the httpStatusCode
60 public int getHttpStatusCode() {
61 return httpStatusCode;
65 * @param httpStatusCode
66 * the httpStatusCode to set
68 public void setHttpStatusCode(int httpStatusCode) {
69 this.httpStatusCode = httpStatusCode;
73 * @return the statusDescription
75 public String getStatusDescription() {
76 return statusDescription;
80 * @param statusDescription
81 * the statusDescription to set
83 public void setStatusDescription(String statusDescription) {
84 this.statusDescription = statusDescription;
87 public String toString() {
89 StringBuilder stringBuilder = new StringBuilder();
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);
100 return stringBuilder.toString();
104 public String toPrettyString() {
106 int maxBytesToDisplay = 200;
108 StringBuilder stringBuilder = new StringBuilder();
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);
126 stringBuilder.append("Body length: ");
127 stringBuilder.append(response == null ? 0 : response.length);
130 return stringBuilder.toString();