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.IOException; 021 import java.lang.reflect.Type; 022 023 import javax.servlet.ServletContext; 024 import javax.servlet.http.HttpServletRequest; 025 import javax.ws.rs.core.Context; 026 import javax.ws.rs.ext.Provider; 027 028 import org.apache.hadoop.conf.Configuration; 029 import org.apache.hadoop.hdfs.server.common.JspHelper; 030 import org.apache.hadoop.security.UserGroupInformation; 031 import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod; 032 033 import com.sun.jersey.api.core.HttpContext; 034 import com.sun.jersey.core.spi.component.ComponentContext; 035 import com.sun.jersey.core.spi.component.ComponentScope; 036 import com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable; 037 import com.sun.jersey.spi.inject.Injectable; 038 import com.sun.jersey.spi.inject.InjectableProvider; 039 040 /** Inject user information to http operations. */ 041 @Provider 042 public class UserProvider 043 extends AbstractHttpContextInjectable<UserGroupInformation> 044 implements InjectableProvider<Context, Type> { 045 @Context HttpServletRequest request; 046 @Context ServletContext servletcontext; 047 048 @Override 049 public UserGroupInformation getValue(final HttpContext context) { 050 final Configuration conf = (Configuration) servletcontext 051 .getAttribute(JspHelper.CURRENT_CONF); 052 try { 053 return JspHelper.getUGI(servletcontext, request, conf, 054 AuthenticationMethod.KERBEROS, false); 055 } catch (IOException e) { 056 throw new SecurityException( 057 "Failed to obtain user group information: " + e, e); 058 } 059 } 060 061 @Override 062 public ComponentScope getScope() { 063 return ComponentScope.PerRequest; 064 } 065 066 @Override 067 public Injectable<UserGroupInformation> getInjectable( 068 final ComponentContext componentContext, final Context context, 069 final Type type) { 070 return type.equals(UserGroupInformation.class)? this : null; 071 } 072 }