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