001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.fs.http.server; 020 021import com.sun.jersey.api.container.ContainerException; 022import org.apache.hadoop.classification.InterfaceAudience; 023import org.apache.hadoop.lib.service.FileSystemAccessException; 024import org.apache.hadoop.lib.wsrs.ExceptionProvider; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027import org.slf4j.MDC; 028 029import javax.ws.rs.core.Response; 030import javax.ws.rs.ext.Provider; 031import java.io.FileNotFoundException; 032import java.io.IOException; 033 034/** 035 * JAX-RS <code>ExceptionMapper</code> implementation that maps HttpFSServer's 036 * exceptions to HTTP status codes. 037 */ 038@Provider 039@InterfaceAudience.Private 040public class HttpFSExceptionProvider extends ExceptionProvider { 041 private static Logger AUDIT_LOG = LoggerFactory.getLogger("httpfsaudit"); 042 private static Logger LOG = LoggerFactory.getLogger(HttpFSExceptionProvider.class); 043 044 /** 045 * Maps different exceptions thrown by HttpFSServer to HTTP status codes. 046 * <ul> 047 * <li>SecurityException : HTTP UNAUTHORIZED</li> 048 * <li>FileNotFoundException : HTTP NOT_FOUND</li> 049 * <li>IOException : INTERNAL_HTTP SERVER_ERROR</li> 050 * <li>UnsupporteOperationException : HTTP BAD_REQUEST</li> 051 * <li>all other exceptions : HTTP INTERNAL_SERVER_ERROR </li> 052 * </ul> 053 * 054 * @param throwable exception thrown. 055 * 056 * @return mapped HTTP status code 057 */ 058 @Override 059 public Response toResponse(Throwable throwable) { 060 Response.Status status; 061 if (throwable instanceof FileSystemAccessException) { 062 throwable = throwable.getCause(); 063 } 064 if (throwable instanceof ContainerException) { 065 throwable = throwable.getCause(); 066 } 067 if (throwable instanceof SecurityException) { 068 status = Response.Status.UNAUTHORIZED; 069 } else if (throwable instanceof FileNotFoundException) { 070 status = Response.Status.NOT_FOUND; 071 } else if (throwable instanceof IOException) { 072 status = Response.Status.INTERNAL_SERVER_ERROR; 073 } else if (throwable instanceof UnsupportedOperationException) { 074 status = Response.Status.BAD_REQUEST; 075 } else if (throwable instanceof IllegalArgumentException) { 076 status = Response.Status.BAD_REQUEST; 077 } else { 078 status = Response.Status.INTERNAL_SERVER_ERROR; 079 } 080 return createResponse(status, throwable); 081 } 082 083 /** 084 * Logs the HTTP status code and exception in HttpFSServer's log. 085 * 086 * @param status HTTP status code. 087 * @param throwable exception thrown. 088 */ 089 @Override 090 protected void log(Response.Status status, Throwable throwable) { 091 String method = MDC.get("method"); 092 String path = MDC.get("path"); 093 String message = getOneLineMessage(throwable); 094 AUDIT_LOG.warn("FAILED [{}:{}] response [{}] {}", new Object[]{method, path, status, message}); 095 LOG.warn("[{}:{}] response [{}] {}", new Object[]{method, path, status, message}, throwable); 096 } 097 098}