Skip to content

Commit f7a601e

Browse files
committed
Use Response.setStatusWithReason() for RestCsrfPreventionFilter and image servlet.
Change-Id: Id4aaf799a481902a2ca32f25399d454d376066f1
1 parent 48c0a5d commit f7a601e

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/http/RestCsrfPreventionFilter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.apache.hadoop.classification.InterfaceStability;
3838
import org.apache.hadoop.conf.Configuration;
3939

40+
import org.eclipse.jetty.server.Response;
4041
import org.slf4j.Logger;
4142
import org.slf4j.LoggerFactory;
4243

@@ -271,6 +272,10 @@ public void proceed() throws IOException, ServletException {
271272

272273
@Override
273274
public void sendError(int code, String message) throws IOException {
275+
if (httpResponse instanceof Response) {
276+
((Response)httpResponse).setStatusWithReason(code, message);
277+
}
278+
274279
httpResponse.sendError(code, message);
275280
}
276281
}

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ImageServlet.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.hadoop.hdfs.DFSConfigKeys;
4343
import org.apache.hadoop.hdfs.DFSUtilClient;
4444
import org.apache.hadoop.security.SecurityUtil;
45+
import org.eclipse.jetty.server.Response;
4546
import org.slf4j.Logger;
4647
import org.slf4j.LoggerFactory;
4748
import org.apache.hadoop.classification.InterfaceAudience;
@@ -119,7 +120,7 @@ private FSImage getAndValidateFSImage(ServletContext context,
119120
if (nnImage == null) {
120121
String errorMsg = "NameNode initialization not yet complete. "
121122
+ "FSImage has not been set in the NameNode.";
122-
response.sendError(HttpServletResponse.SC_FORBIDDEN, errorMsg);
123+
sendError(response, HttpServletResponse.SC_FORBIDDEN, errorMsg);
123124
throw new IOException(errorMsg);
124125
}
125126
return nnImage;
@@ -218,7 +219,7 @@ private void serveFile(File file) throws IOException {
218219

219220
} catch (Throwable t) {
220221
String errMsg = "GetImage failed. " + StringUtils.stringifyException(t);
221-
response.sendError(HttpServletResponse.SC_GONE, errMsg);
222+
sendError(response, HttpServletResponse.SC_GONE, errMsg);
222223
throw new IOException(errMsg);
223224
} finally {
224225
response.getOutputStream().close();
@@ -234,7 +235,7 @@ private void validateRequest(ServletContext context, Configuration conf,
234235
conf)) {
235236
String errorMsg = "Only Namenode, Secondary Namenode, and administrators may access "
236237
+ "this servlet";
237-
response.sendError(HttpServletResponse.SC_FORBIDDEN, errorMsg);
238+
sendError(response, HttpServletResponse.SC_FORBIDDEN, errorMsg);
238239
LOG.warn("Received non-NN/SNN/administrator request for image or edits from "
239240
+ request.getUserPrincipal().getName()
240241
+ " at "
@@ -247,7 +248,7 @@ private void validateRequest(ServletContext context, Configuration conf,
247248
&& !myStorageInfoString.equals(theirStorageInfoString)) {
248249
String errorMsg = "This namenode has storage info " + myStorageInfoString
249250
+ " but the secondary expected " + theirStorageInfoString;
250-
response.sendError(HttpServletResponse.SC_FORBIDDEN, errorMsg);
251+
sendError(response, HttpServletResponse.SC_FORBIDDEN, errorMsg);
251252
LOG.warn("Received an invalid request file transfer request "
252253
+ "from a secondary with storage info " + theirStorageInfoString);
253254
throw new IOException(errorMsg);
@@ -578,7 +579,7 @@ public Void run() throws Exception {
578579
// we need a different response type here so the client can differentiate this
579580
// from the failure to upload due to (1) security, or (2) other checkpoints already
580581
// present
581-
response.sendError(HttpServletResponse.SC_EXPECTATION_FAILED,
582+
sendError(response, HttpServletResponse.SC_EXPECTATION_FAILED,
582583
"Nameode "+request.getLocalAddr()+" is currently not in a state which can "
583584
+ "accept uploads of new fsimages. State: "+state);
584585
return null;
@@ -593,15 +594,15 @@ public Void run() throws Exception {
593594
// if the node is attempting to upload an older transaction, we ignore it
594595
SortedSet<ImageUploadRequest> larger = currentlyDownloadingCheckpoints.tailSet(imageRequest);
595596
if (larger.size() > 0) {
596-
response.sendError(HttpServletResponse.SC_CONFLICT,
597+
sendError(response, HttpServletResponse.SC_CONFLICT,
597598
"Another checkpointer is already in the process of uploading a" +
598599
" checkpoint made up to transaction ID " + larger.last());
599600
return null;
600601
}
601602

602603
//make sure no one else has started uploading one
603604
if (!currentlyDownloadingCheckpoints.add(imageRequest)) {
604-
response.sendError(HttpServletResponse.SC_CONFLICT,
605+
sendError(response, HttpServletResponse.SC_CONFLICT,
605606
"Either current namenode is checkpointing or another"
606607
+ " checkpointer is already in the process of "
607608
+ "uploading a checkpoint made at transaction ID "
@@ -648,7 +649,7 @@ public Void run() throws Exception {
648649
(txid - lastCheckpointTxid) + " expecting at least "
649650
+ checkpointTxnCount;
650651
LOG.info(message);
651-
response.sendError(HttpServletResponse.SC_CONFLICT, message);
652+
sendError(response, HttpServletResponse.SC_CONFLICT, message);
652653
return null;
653654
}
654655

@@ -658,7 +659,7 @@ public Void run() throws Exception {
658659
+ "another checkpointer already uploaded an "
659660
+ "checkpoint for txid " + txid;
660661
LOG.info(message);
661-
response.sendError(HttpServletResponse.SC_CONFLICT, message);
662+
sendError(response, HttpServletResponse.SC_CONFLICT, message);
662663
return null;
663664
}
664665

@@ -695,11 +696,20 @@ public Void run() throws Exception {
695696
});
696697
} catch (Throwable t) {
697698
String errMsg = "PutImage failed. " + StringUtils.stringifyException(t);
698-
response.sendError(HttpServletResponse.SC_GONE, errMsg);
699+
sendError(response, HttpServletResponse.SC_GONE, errMsg);
699700
throw new IOException(errMsg);
700701
}
701702
}
702703

704+
private void sendError(HttpServletResponse response, int code, String message)
705+
throws IOException {
706+
if (response instanceof Response) {
707+
((Response)response).setStatusWithReason(code, message);
708+
}
709+
710+
response.sendError(code, message);
711+
}
712+
703713
/*
704714
* Params required to handle put image request
705715
*/

0 commit comments

Comments
 (0)