@@ -70,31 +70,36 @@ public void requestProxyService(HttpServletRequest request, DeferredResult<Respo
70
70
@ Override
71
71
public void onComplete (Result result ) {
72
72
if (result .isSucceeded ()) {
73
- ResponseEntity <JsonNode > responseEntity ;
74
73
75
- responseInfo .setProxyElapsedTime (System .currentTimeMillis () - proxyStartTime );
74
+ try {
75
+ ResponseEntity <JsonNode > responseEntity ;
76
76
77
- if (log .isDebugEnabled ()){
78
- log .debug ("Http Proxy ElapsedTime " + responseInfo .getProxyElapsedTime ());
79
- }
77
+ responseInfo .setProxyElapsedTime (System .currentTimeMillis () - proxyStartTime );
78
+
79
+ if (log .isDebugEnabled ()){
80
+ log .debug ("Http Proxy ElapsedTime " + responseInfo .getProxyElapsedTime ());
81
+ }
80
82
81
- HttpFields responseHeaders = result .getResponse ().getHeaders ();
82
- if (responseHeaders .contains (HttpHeader .CONTENT_TYPE )) {
83
- String contentTypeValue = responseHeaders .get (HttpHeader .CONTENT_TYPE );
84
- if ( contentTypeValue .split (Constant .CONTENT_TYPE_EXTRACT_DELIMITER )[0 ]
85
- .equals (responseInfo .getRequestAccept ().split (Constant .CONTENT_TYPE_EXTRACT_DELIMITER )[0 ])){
83
+ HttpFields responseHeaders = result .getResponse ().getHeaders ();
84
+ if (checkContentType (responseHeaders )) {
86
85
87
86
JsonNode responseInJsonNode = InputStreamToJsonObj (getContentAsInputStream ());
88
87
responseEntity =
89
88
new ResponseEntity <>(responseInJsonNode , HttpStatus .valueOf (result .getResponse ().getStatus ()));
90
89
91
90
deferredResult .setResult (responseEntity );
92
- return ;
91
+
92
+ } else {
93
+ log .error (getContentAsString ());
94
+ deferredResult .setErrorResult (new ProxyServiceFailException (ERROR_MESSAGE_WRONG_CONTENT_TYPE ));
93
95
}
96
+
97
+ } catch (IOException ex ) {
98
+
99
+ log .error (getContentAsString ());
100
+ deferredResult .setErrorResult (new ProxyServiceFailException (HttpStatus .BAD_GATEWAY .getReasonPhrase ()));
94
101
}
95
102
96
- log .error (getContentAsString ());
97
- deferredResult .setErrorResult (new ProxyServiceFailException (ERROR_MESSAGE_WRONG_CONTENT_TYPE ));
98
103
}
99
104
}
100
105
@@ -105,6 +110,16 @@ public void onFailure(Response response, Throwable failure) {
105
110
});
106
111
}
107
112
113
+ private boolean checkContentType (HttpFields responseHeaders ) {
114
+ if (responseHeaders .contains (HttpHeader .CONTENT_TYPE )) {
115
+ String contentTypeValue = responseHeaders .get (HttpHeader .CONTENT_TYPE );
116
+ return contentTypeValue .split (Constant .CONTENT_TYPE_EXTRACT_DELIMITER )[0 ]
117
+ .equals (responseInfo .getRequestAccept ().split (Constant .CONTENT_TYPE_EXTRACT_DELIMITER )[0 ])
118
+ }
119
+
120
+ return false ;
121
+ }
122
+
108
123
/**
109
124
* Create a new request object based on the variables created in prepareProxyInterceptor.
110
125
*
@@ -144,13 +159,11 @@ private static Request setHeaderAndQueryInfo(Request request, ResponseInfo respo
144
159
* @param responseInput An inputStream containing the response body received by the outbound service.
145
160
* @return The jsonNode object with the response body changed to json format.
146
161
*/
147
- private static JsonNode InputStreamToJsonObj (InputStream responseInput ) {
148
- try {
149
- InputStreamReader responseInputStreamReader = new InputStreamReader (responseInput , Charset .forName (Constant .SERVER_DEFAULT_ENCODING_TYPE ));
150
- return JsonUtil .getObjectMapper ().readTree (responseInputStreamReader );
151
- } catch (IOException ex ) {
152
- throw new ProxyServiceFailException (ERROR_MESSAGE_WRONG_CONTENT_TYPE );
153
- }
162
+ private static JsonNode InputStreamToJsonObj (InputStream responseInput ) throws IOException {
163
+
164
+ InputStreamReader responseInputStreamReader = new InputStreamReader (responseInput , Charset .forName (Constant .SERVER_DEFAULT_ENCODING_TYPE ));
165
+ return JsonUtil .getObjectMapper ().readTree (responseInputStreamReader );
166
+
154
167
}
155
168
156
169
}
0 commit comments