15
15
import io .clientcore .core .serialization .json .JsonSerializer ;
16
16
import io .clientcore .core .serialization .xml .XmlSerializer ;
17
17
import io .clientcore .core .http .models .HttpResponseException ;
18
+ import io .clientcore .core .utils .CoreUtils ;
19
+ import java .lang .reflect .ParameterizedType ;
20
+ import java .io .IOException ;
21
+ import java .nio .charset .StandardCharsets ;
18
22
19
23
/**
20
24
* Initializes a new instance of the SpecialReturnBodiesServiceImpl type.
@@ -54,9 +58,16 @@ public BinaryData getBinaryData(String url) {
54
58
int responseCode = networkResponse .getStatusCode ();
55
59
boolean expectedResponse = responseCode == 200 ;
56
60
if (!expectedResponse ) {
57
- String errorMessage = networkResponse .getValue ().toString ();
58
- networkResponse .close ();
59
- throw new HttpResponseException (errorMessage , networkResponse , null );
61
+ BinaryData value = networkResponse .getValue ();
62
+ if (value == null || value .toBytes ().length == 0 ) {
63
+ networkResponse .close ();
64
+ throw instantiateUnexpectedException (responseCode , networkResponse , null , null );
65
+ } else {
66
+ ParameterizedType returnType = CoreUtils .createParameterizedType (io .clientcore .core .models .binarydata .BinaryData .class );
67
+ Object decoded = CoreUtils .decodeNetworkResponse (value , jsonSerializer , returnType );
68
+ networkResponse .close ();
69
+ throw instantiateUnexpectedException (responseCode , networkResponse , value , decoded );
70
+ }
60
71
}
61
72
return networkResponse .getValue ();
62
73
}
@@ -71,9 +82,16 @@ public Response<BinaryData> getBinaryDataWithResponse(String url) {
71
82
int responseCode = networkResponse .getStatusCode ();
72
83
boolean expectedResponse = responseCode == 200 ;
73
84
if (!expectedResponse ) {
74
- String errorMessage = networkResponse .getValue ().toString ();
75
- networkResponse .close ();
76
- throw new HttpResponseException (errorMessage , networkResponse , null );
85
+ BinaryData value = networkResponse .getValue ();
86
+ if (value == null || value .toBytes ().length == 0 ) {
87
+ networkResponse .close ();
88
+ throw instantiateUnexpectedException (responseCode , networkResponse , null , null );
89
+ } else {
90
+ ParameterizedType returnType = CoreUtils .createParameterizedType (io .clientcore .core .http .models .Response .class , BinaryData .class );
91
+ Object decoded = CoreUtils .decodeNetworkResponse (value , jsonSerializer , returnType );
92
+ networkResponse .close ();
93
+ throw instantiateUnexpectedException (responseCode , networkResponse , value , decoded );
94
+ }
77
95
}
78
96
return networkResponse ;
79
97
}
@@ -88,8 +106,14 @@ public byte[] getByteArray(String url) {
88
106
int responseCode = networkResponse .getStatusCode ();
89
107
boolean expectedResponse = responseCode == 200 ;
90
108
if (!expectedResponse ) {
91
- String errorMessage = networkResponse .getValue ().toString ();
92
- throw new HttpResponseException (errorMessage , networkResponse , null );
109
+ BinaryData value = networkResponse .getValue ();
110
+ if (value == null || value .toBytes ().length == 0 ) {
111
+ throw instantiateUnexpectedException (responseCode , networkResponse , null , null );
112
+ } else {
113
+ ParameterizedType returnType = null ;
114
+ Object decoded = CoreUtils .decodeNetworkResponse (value , jsonSerializer , returnType );
115
+ throw instantiateUnexpectedException (responseCode , networkResponse , value , decoded );
116
+ }
93
117
}
94
118
BinaryData responseBody = networkResponse .getValue ();
95
119
return responseBody != null ? responseBody .toBytes () : null ;
@@ -106,8 +130,14 @@ public Response<byte[]> getByteArrayWithResponse(String url) {
106
130
int responseCode = networkResponse .getStatusCode ();
107
131
boolean expectedResponse = responseCode == 200 ;
108
132
if (!expectedResponse ) {
109
- String errorMessage = networkResponse .getValue ().toString ();
110
- throw new HttpResponseException (errorMessage , networkResponse , null );
133
+ BinaryData value = networkResponse .getValue ();
134
+ if (value == null || value .toBytes ().length == 0 ) {
135
+ throw instantiateUnexpectedException (responseCode , networkResponse , null , null );
136
+ } else {
137
+ ParameterizedType returnType = CoreUtils .createParameterizedType (io .clientcore .core .http .models .Response .class );
138
+ Object decoded = CoreUtils .decodeNetworkResponse (value , jsonSerializer , returnType );
139
+ throw instantiateUnexpectedException (responseCode , networkResponse , value , decoded );
140
+ }
111
141
}
112
142
BinaryData responseBody = networkResponse .getValue ();
113
143
return new Response <>(networkResponse .getRequest (), responseCode , networkResponse .getHeaders (), responseBody != null ? responseBody .toBytes () : null );
@@ -124,9 +154,16 @@ public InputStream getInputStream(String url) {
124
154
int responseCode = networkResponse .getStatusCode ();
125
155
boolean expectedResponse = responseCode == 200 ;
126
156
if (!expectedResponse ) {
127
- String errorMessage = networkResponse .getValue ().toString ();
128
- networkResponse .close ();
129
- throw new HttpResponseException (errorMessage , networkResponse , null );
157
+ BinaryData value = networkResponse .getValue ();
158
+ if (value == null || value .toBytes ().length == 0 ) {
159
+ networkResponse .close ();
160
+ throw instantiateUnexpectedException (responseCode , networkResponse , null , null );
161
+ } else {
162
+ ParameterizedType returnType = CoreUtils .createParameterizedType (java .io .InputStream .class );
163
+ Object decoded = CoreUtils .decodeNetworkResponse (value , jsonSerializer , returnType );
164
+ networkResponse .close ();
165
+ throw instantiateUnexpectedException (responseCode , networkResponse , value , decoded );
166
+ }
130
167
}
131
168
return networkResponse .getValue ().toStream ();
132
169
}
@@ -141,10 +178,34 @@ public Response<InputStream> getInputStreamWithResponse(String url) {
141
178
int responseCode = networkResponse .getStatusCode ();
142
179
boolean expectedResponse = responseCode == 200 ;
143
180
if (!expectedResponse ) {
144
- String errorMessage = networkResponse .getValue ().toString ();
145
- networkResponse .close ();
146
- throw new HttpResponseException (errorMessage , networkResponse , null );
181
+ BinaryData value = networkResponse .getValue ();
182
+ if (value == null || value .toBytes ().length == 0 ) {
183
+ networkResponse .close ();
184
+ throw instantiateUnexpectedException (responseCode , networkResponse , null , null );
185
+ } else {
186
+ ParameterizedType returnType = CoreUtils .createParameterizedType (io .clientcore .core .http .models .Response .class , InputStream .class );
187
+ Object decoded = CoreUtils .decodeNetworkResponse (value , jsonSerializer , returnType );
188
+ networkResponse .close ();
189
+ throw instantiateUnexpectedException (responseCode , networkResponse , value , decoded );
190
+ }
147
191
}
148
192
return new Response <>(networkResponse .getRequest (), responseCode , networkResponse .getHeaders (), networkResponse .getValue ().toStream ());
149
193
}
194
+
195
+ private static HttpResponseException instantiateUnexpectedException (int responseCode , Response <BinaryData > response , BinaryData data , Object decodedValue ) {
196
+ StringBuilder exceptionMessage = new StringBuilder ("Status code " ).append (responseCode ).append (", " );
197
+ String contentType = response .getHeaders ().getValue (HttpHeaderName .CONTENT_TYPE );
198
+ if ("application/octet-stream" .equalsIgnoreCase (contentType )) {
199
+ String contentLength = response .getHeaders ().getValue (HttpHeaderName .CONTENT_LENGTH );
200
+ exceptionMessage .append ("(" ).append (contentLength ).append ("-byte body)" );
201
+ } else if (data == null || data .toBytes ().length == 0 ) {
202
+ exceptionMessage .append ("(empty body)" );
203
+ } else {
204
+ exceptionMessage .append ('"' ).append (new String (data .toBytes (), StandardCharsets .UTF_8 )).append ('"' );
205
+ }
206
+ if (decodedValue instanceof IOException || decodedValue instanceof IllegalStateException ) {
207
+ return new HttpResponseException (exceptionMessage .toString (), response , (Throwable ) decodedValue );
208
+ }
209
+ return new HttpResponseException (exceptionMessage .toString (), response , decodedValue );
210
+ }
150
211
}
0 commit comments