4343import org .apache .hadoop .security .SecurityUtil ;
4444import org .apache .commons .logging .Log ;
4545import org .apache .commons .logging .LogFactory ;
46+ import org .eclipse .jetty .server .Response ;
4647import org .apache .hadoop .classification .InterfaceAudience ;
4748import org .apache .hadoop .conf .Configuration ;
4849import org .apache .hadoop .hdfs .DFSUtil ;
@@ -118,7 +119,7 @@ private FSImage getAndValidateFSImage(ServletContext context,
118119 if (nnImage == null ) {
119120 String errorMsg = "NameNode initialization not yet complete. "
120121 + "FSImage has not been set in the NameNode." ;
121- response . sendError (HttpServletResponse .SC_FORBIDDEN , errorMsg );
122+ sendError (response , HttpServletResponse .SC_FORBIDDEN , errorMsg );
122123 throw new IOException (errorMsg );
123124 }
124125 return nnImage ;
@@ -207,7 +208,7 @@ private void serveFile(File file) throws IOException {
207208
208209 } catch (Throwable t ) {
209210 String errMsg = "GetImage failed. " + StringUtils .stringifyException (t );
210- response . sendError (HttpServletResponse .SC_GONE , errMsg );
211+ sendError (response , HttpServletResponse .SC_GONE , errMsg );
211212 throw new IOException (errMsg );
212213 } finally {
213214 response .getOutputStream ().close ();
@@ -223,7 +224,7 @@ private void validateRequest(ServletContext context, Configuration conf,
223224 conf )) {
224225 String errorMsg = "Only Namenode, Secondary Namenode, and administrators may access "
225226 + "this servlet" ;
226- response . sendError (HttpServletResponse .SC_FORBIDDEN , errorMsg );
227+ sendError (response , HttpServletResponse .SC_FORBIDDEN , errorMsg );
227228 LOG .warn ("Received non-NN/SNN/administrator request for image or edits from "
228229 + request .getUserPrincipal ().getName ()
229230 + " at "
@@ -236,7 +237,7 @@ private void validateRequest(ServletContext context, Configuration conf,
236237 && !myStorageInfoString .equals (theirStorageInfoString )) {
237238 String errorMsg = "This namenode has storage info " + myStorageInfoString
238239 + " but the secondary expected " + theirStorageInfoString ;
239- response . sendError (HttpServletResponse .SC_FORBIDDEN , errorMsg );
240+ sendError (response , HttpServletResponse .SC_FORBIDDEN , errorMsg );
240241 LOG .warn ("Received an invalid request file transfer request "
241242 + "from a secondary with storage info " + theirStorageInfoString );
242243 throw new IOException (errorMsg );
@@ -552,7 +553,7 @@ public Void run() throws Exception {
552553 // we need a different response type here so the client can differentiate this
553554 // from the failure to upload due to (1) security, or (2) other checkpoints already
554555 // present
555- response . sendError (HttpServletResponse .SC_EXPECTATION_FAILED ,
556+ sendError (response , HttpServletResponse .SC_EXPECTATION_FAILED ,
556557 "Nameode " +request .getLocalAddr ()+" is currently not in a state which can "
557558 + "accept uploads of new fsimages. State: " +state );
558559 return null ;
@@ -567,15 +568,15 @@ public Void run() throws Exception {
567568 // if the node is attempting to upload an older transaction, we ignore it
568569 SortedSet <ImageUploadRequest > larger = currentlyDownloadingCheckpoints .tailSet (imageRequest );
569570 if (larger .size () > 0 ) {
570- response . sendError (HttpServletResponse .SC_CONFLICT ,
571+ sendError (response , HttpServletResponse .SC_CONFLICT ,
571572 "Another checkpointer is already in the process of uploading a" +
572573 " checkpoint made up to transaction ID " + larger .last ());
573574 return null ;
574575 }
575576
576577 //make sure no one else has started uploading one
577578 if (!currentlyDownloadingCheckpoints .add (imageRequest )) {
578- response . sendError (HttpServletResponse .SC_CONFLICT ,
579+ sendError (response , HttpServletResponse .SC_CONFLICT ,
579580 "Either current namenode is checkpointing or another"
580581 + " checkpointer is already in the process of "
581582 + "uploading a checkpoint made at transaction ID "
@@ -622,7 +623,7 @@ public Void run() throws Exception {
622623 (txid - lastCheckpointTxid ) + " expecting at least "
623624 + checkpointTxnCount ;
624625 LOG .info (message );
625- response . sendError (HttpServletResponse .SC_CONFLICT , message );
626+ sendError (response , HttpServletResponse .SC_CONFLICT , message );
626627 return null ;
627628 }
628629
@@ -632,7 +633,7 @@ public Void run() throws Exception {
632633 + "another checkpointer already uploaded an "
633634 + "checkpoint for txid " + txid ;
634635 LOG .info (message );
635- response . sendError (HttpServletResponse .SC_CONFLICT , message );
636+ sendError (response , HttpServletResponse .SC_CONFLICT , message );
636637 return null ;
637638 }
638639
@@ -669,11 +670,20 @@ public Void run() throws Exception {
669670 });
670671 } catch (Throwable t ) {
671672 String errMsg = "PutImage failed. " + StringUtils .stringifyException (t );
672- response . sendError (HttpServletResponse .SC_GONE , errMsg );
673+ sendError (response , HttpServletResponse .SC_GONE , errMsg );
673674 throw new IOException (errMsg );
674675 }
675676 }
676677
678+ private void sendError (HttpServletResponse response , int code , String message )
679+ throws IOException {
680+ if (response instanceof Response ) {
681+ ((Response )response ).setStatusWithReason (code , message );
682+ }
683+
684+ response .sendError (code , message );
685+ }
686+
677687 /*
678688 * Params required to handle put image request
679689 */
0 commit comments