/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.hdfs.web.resources;

import com.sun.jersey.api.ParamException;
import com.sun.jersey.api.container.ContainerException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hdfs.web.JsonUtil;
import org.apache.hadoop.ipc.RemoteException;
import org.apache.hadoop.security.authorize.AuthorizationException;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Handle exceptions.
 */
@Provider
public class ExceptionHandler implements ExceptionMapper<Exception> {
  public static final Log LOG = LogFactory.getLog(ExceptionHandler.class);

  private static Exception toCause(Exception e) {
    final Throwable t = e.getCause();
    if (t != null && t instanceof Exception) {
      e = (Exception) e.getCause();
    }
    return e;
  }

  private
  @Context
  HttpServletResponse response;

  @Override
  public Response toResponse(Exception e) {
    if (LOG.isTraceEnabled()) {
      LOG.trace("GOT EXCEPITION", e);
    }

    //clear content type
    response.setContentType(null);

    //Convert exception
    if (e instanceof ParamException) {
      final ParamException paramexception = (ParamException) e;
      e = new IllegalArgumentException(
          "Invalid value for webhdfs parameter \"" +
              paramexception.getParameterName() + "\": " +
              e.getCause().getMessage(), e);
    }
    if (e instanceof ContainerException) {
      e = toCause(e);
    }
    if (e instanceof RemoteException) {
      e = ((RemoteException) e).unwrapRemoteException();
    }

    //Map response status
    final Response.Status s;
    if (e instanceof SecurityException) {
      s = Response.Status.FORBIDDEN;
    } else if (e instanceof AuthorizationException) {
      s = Response.Status.FORBIDDEN;
    } else if (e instanceof FileNotFoundException) {
      s = Response.Status.NOT_FOUND;
    } else if (e instanceof IOException) {
      s = Response.Status.FORBIDDEN;
    } else if (e instanceof UnsupportedOperationException) {
      s = Response.Status.BAD_REQUEST;
    } else if (e instanceof IllegalArgumentException) {
      s = Response.Status.BAD_REQUEST;
    } else {
      LOG.warn("INTERNAL_SERVER_ERROR", e);
      s = Response.Status.INTERNAL_SERVER_ERROR;
    }

    final String js = JsonUtil.toJsonString(e);
    return Response.status(s).type(MediaType.APPLICATION_JSON).entity(js)
        .build();
  }
}

@@ -847,16 +847,72 @@
 urces;%0A%0A
+import com.google.common.annotations.VisibleForTesting;%0A
 import c
@@ -941,24 +941,24 @@
 mException;%0A
-
 import com.s
@@ -1243,16 +1243,84 @@
 ception;
+%0Aimport org.apache.hadoop.security.token.SecretManager.InvalidToken;
 %0A%0Aimport
@@ -1600,16 +1600,63 @@
 ception;
+%0Aimport org.apache.hadoop.ipc.StandbyException;
 %0A%0A/**%0A *
@@ -1918,24 +1918,28 @@
 .getCause();
+    
 %0A    if (t !
@@ -1939,34 +1939,465 @@
 if (
-t != null && t instanceof 
+e instanceof SecurityException) %7B%0A      // For the issue reported in HDFS-6475, if SecurityException's cause%0A      // is InvalidToken, and the InvalidToken's cause is StandbyException,%0A      // return StandbyException; Otherwise, leave the exception as is,%0A      // since they are handled elsewhere. See HDFS-6588.%0A      if (t != null && t instanceof InvalidToken) %7B%0A        final Throwable t1 = t.getCause();%0A        if (t1 != null && t1 instanceof Standby
 Exce
@@ -2411,21 +2411,32 @@
 %7B%0A      
+ 
+   
 e = (
+Standby
 Exceptio
@@ -2437,30 +2437,133 @@
 ception)
- e.getCause();
+t1;%0A        %7D%0A      %7D%0A    %7D else %7B%0A      if (t != null && t instanceof Exception) %7B%0A        e = (Exception)t;%0A      %7D
 %0A    %7D%0A 
@@ -3329,24 +3329,103 @@
 n();%0A    %7D%0A%0A
+    if (e instanceof SecurityException) %7B%0A      e = toCause(e);%0A    %7D%0A        %0A
     //Map re
@@ -4237,16 +4237,16 @@
 ity(js)%0A
-
         
@@ -4259,10 +4259,127 @@
 ();%0A  %7D%0A
+  %0A  @VisibleForTesting%0A  public void initResponse(HttpServletResponse response) %7B%0A    this.response = response;%0A  %7D%0A
 %7D%0A

apply HDFS-6475