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 package org.apache.hadoop.hdfs.web.resources;
019
020 import java.io.FileNotFoundException;
021 import java.io.IOException;
022
023 import javax.servlet.http.HttpServletResponse;
024 import javax.ws.rs.core.Context;
025 import javax.ws.rs.core.MediaType;
026 import javax.ws.rs.core.Response;
027 import javax.ws.rs.ext.ExceptionMapper;
028 import javax.ws.rs.ext.Provider;
029
030 import org.apache.commons.logging.Log;
031 import org.apache.commons.logging.LogFactory;
032 import org.apache.hadoop.hdfs.web.JsonUtil;
033 import org.apache.hadoop.ipc.RemoteException;
034 import org.apache.hadoop.security.authorize.AuthorizationException;
035
036 import com.sun.jersey.api.ParamException;
037 import com.sun.jersey.api.container.ContainerException;
038
039 /** Handle exceptions. */
040 @Provider
041 public class ExceptionHandler implements ExceptionMapper<Exception> {
042 public static final Log LOG = LogFactory.getLog(ExceptionHandler.class);
043
044 private static Exception toCause(Exception e) {
045 final Throwable t = e.getCause();
046 if (t != null && t instanceof Exception) {
047 e = (Exception)e.getCause();
048 }
049 return e;
050 }
051
052 private @Context HttpServletResponse response;
053
054 @Override
055 public Response toResponse(Exception e) {
056 if (LOG.isTraceEnabled()) {
057 LOG.trace("GOT EXCEPITION", e);
058 }
059
060 //clear content type
061 response.setContentType(null);
062
063 //Convert exception
064 if (e instanceof ParamException) {
065 final ParamException paramexception = (ParamException)e;
066 e = new IllegalArgumentException("Invalid value for webhdfs parameter \""
067 + paramexception.getParameterName() + "\": "
068 + e.getCause().getMessage(), e);
069 }
070 if (e instanceof ContainerException) {
071 e = toCause(e);
072 }
073 if (e instanceof RemoteException) {
074 e = ((RemoteException)e).unwrapRemoteException();
075 }
076
077 //Map response status
078 final Response.Status s;
079 if (e instanceof SecurityException) {
080 s = Response.Status.UNAUTHORIZED;
081 } else if (e instanceof AuthorizationException) {
082 s = Response.Status.UNAUTHORIZED;
083 } else if (e instanceof FileNotFoundException) {
084 s = Response.Status.NOT_FOUND;
085 } else if (e instanceof IOException) {
086 s = Response.Status.FORBIDDEN;
087 } else if (e instanceof UnsupportedOperationException) {
088 s = Response.Status.BAD_REQUEST;
089 } else if (e instanceof IllegalArgumentException) {
090 s = Response.Status.BAD_REQUEST;
091 } else {
092 LOG.warn("INTERNAL_SERVER_ERROR", e);
093 s = Response.Status.INTERNAL_SERVER_ERROR;
094 }
095
096 final String js = JsonUtil.toJsonString(e);
097 return Response.status(s).type(MediaType.APPLICATION_JSON).entity(js).build();
098 }
099 }