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    
019    package org.apache.hadoop.lib.wsrs;
020    
021    import com.sun.jersey.api.core.HttpContext;
022    import com.sun.jersey.core.spi.component.ComponentContext;
023    import com.sun.jersey.core.spi.component.ComponentScope;
024    import com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable;
025    import com.sun.jersey.spi.inject.Injectable;
026    import com.sun.jersey.spi.inject.InjectableProvider;
027    import org.apache.hadoop.classification.InterfaceAudience;
028    import org.slf4j.MDC;
029    
030    import javax.ws.rs.core.Context;
031    import javax.ws.rs.ext.Provider;
032    import java.lang.reflect.Type;
033    import java.security.Principal;
034    import java.util.regex.Pattern;
035    
036    @Provider
037    @InterfaceAudience.Private
038    public class UserProvider extends AbstractHttpContextInjectable<Principal> implements
039      InjectableProvider<Context, Type> {
040    
041      public static final String USER_NAME_PARAM = "user.name";
042    
043      public static final Pattern USER_PATTERN = Pattern.compile("[_a-zA-Z0-9]+");
044    
045      private static class UserParam extends StringParam {
046    
047        public UserParam(String user) {
048          super(USER_NAME_PARAM, user, USER_PATTERN);
049        }
050      }
051    
052      @Override
053      public Principal getValue(HttpContext httpContext) {
054        Principal principal = httpContext.getRequest().getUserPrincipal();
055        if (principal == null) {
056          final String user = httpContext.getRequest().getQueryParameters().getFirst(USER_NAME_PARAM);
057          if (user != null) {
058            principal = new Principal() {
059              @Override
060              public String getName() {
061                return new UserParam(user).value();
062              }
063            };
064          }
065        }
066        if (principal != null) {
067          MDC.put("user", principal.getName());
068        }
069        return principal;
070      }
071    
072      @Override
073      public ComponentScope getScope() {
074        return ComponentScope.PerRequest;
075      }
076    
077      @Override
078      public Injectable getInjectable(ComponentContext componentContext, Context context, Type type) {
079        return (type.equals(Principal.class)) ? this : null;
080      }
081    }