@@ -142,13 +142,13 @@ public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Excep
142142 } catch (InvalidParameterValueException ex ) {
143143 logger .error ("post request validation failed" , ex );
144144 responseContent .append (ex .getMessage ());
145- writeResponse (ctx .channel ());
145+ writeResponse (ctx .channel (), HttpResponseStatus . BAD_REQUEST );
146146 return ;
147147 }
148148 if (uploadEntity == null ) {
149149 logger .error ("Unable to create upload entity. An exception occurred." );
150150 responseContent .append ("Internal Server Error" );
151- writeResponse (ctx .channel ());
151+ writeResponse (ctx .channel (), HttpResponseStatus . INTERNAL_SERVER_ERROR );
152152 return ;
153153 }
154154 //set the base directory to download the file
@@ -160,13 +160,13 @@ public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Excep
160160 } catch (ErrorDataDecoderException | IncompatibleDataDecoderException e ) {
161161 logger .error ("exception while initialising the decoder" , e );
162162 responseContent .append (e .getMessage ());
163- writeResponse (ctx .channel ());
163+ writeResponse (ctx .channel (), HttpResponseStatus . INTERNAL_SERVER_ERROR );
164164 return ;
165165 }
166166 } else {
167167 logger .warn ("received a get request" );
168168 responseContent .append ("only post requests are allowed" );
169- writeResponse (ctx .channel ());
169+ writeResponse (ctx .channel (), HttpResponseStatus . BAD_REQUEST );
170170 return ;
171171 }
172172
@@ -181,12 +181,12 @@ public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Excep
181181 } catch (ErrorDataDecoderException e ) {
182182 logger .error ("data decoding exception" , e );
183183 responseContent .append (e .getMessage ());
184- writeResponse (ctx .channel ());
184+ writeResponse (ctx .channel (), HttpResponseStatus . INTERNAL_SERVER_ERROR );
185185 return ;
186186 }
187187 if (chunk instanceof LastHttpContent ) {
188188 readFileUploadData ();
189- writeResponse (ctx .channel ());
189+ writeResponse (ctx .channel (), HttpResponseStatus . OK );
190190 reset ();
191191 }
192192 }
@@ -221,15 +221,15 @@ private void readFileUploadData() throws IOException {
221221 }
222222 }
223223
224- private void writeResponse (Channel channel ) {
224+ private void writeResponse (Channel channel , HttpResponseStatus statusCode ) {
225225 // Convert the response content to a ChannelBuffer.
226226 ByteBuf buf = copiedBuffer (responseContent .toString (), CharsetUtil .UTF_8 );
227227 responseContent .setLength (0 );
228228 // Decide whether to close the connection or not.
229229 boolean close = HttpHeaders .Values .CLOSE .equalsIgnoreCase (request .headers ().get (CONNECTION )) ||
230230 request .getProtocolVersion ().equals (HttpVersion .HTTP_1_0 ) && !HttpHeaders .Values .KEEP_ALIVE .equalsIgnoreCase (request .headers ().get (CONNECTION ));
231231 // Build the response object.
232- FullHttpResponse response = new DefaultFullHttpResponse (HttpVersion .HTTP_1_1 , HttpResponseStatus . OK , buf );
232+ FullHttpResponse response = new DefaultFullHttpResponse (HttpVersion .HTTP_1_1 , statusCode , buf );
233233 response .headers ().set (CONTENT_TYPE , "text/plain; charset=UTF-8" );
234234 if (!close ) {
235235 // There's no need to add 'Content-Length' header if this is the last response.
@@ -247,7 +247,7 @@ private void writeResponse(Channel channel) {
247247 public void exceptionCaught (ChannelHandlerContext ctx , Throwable cause ) throws Exception {
248248 logger .warn (responseContent .toString (), cause );
249249 responseContent .append ("\r \n Exception occurred: " ).append (cause .getMessage ());
250- writeResponse (ctx .channel ());
250+ writeResponse (ctx .channel (), HttpResponseStatus . INTERNAL_SERVER_ERROR );
251251 ctx .channel ().close ();
252252 }
253253}
0 commit comments