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.UserGroupInformation;
031import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod;
032
033import com.sun.jersey.api.core.HttpContext;
034import com.sun.jersey.core.spi.component.ComponentContext;
035import com.sun.jersey.core.spi.component.ComponentScope;
036import com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable;
037import com.sun.jersey.spi.inject.Injectable;
038import com.sun.jersey.spi.inject.InjectableProvider;
039
040/** Inject user information to http operations. */
041@Provider
042public 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}