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.text.MessageFormat;
035    import java.util.regex.Pattern;
036    
037    @Provider
038    @InterfaceAudience.Private
039    public class UserProvider extends AbstractHttpContextInjectable<Principal> implements
040      InjectableProvider<Context, Type> {
041    
042      public static final String USER_NAME_PARAM = "user.name";
043    
044      public static final Pattern USER_PATTERN = Pattern.compile("^[A-Za-z_][A-Za-z0-9._-]*[$]?$");
045    
046      static class UserParam extends StringParam {
047    
048        public UserParam(String user) {
049          super(USER_NAME_PARAM, user, USER_PATTERN);
050        }
051    
052        @Override
053        public String parseParam(String str) {
054          if (str != null) {
055            int len = str.length();
056            if (len < 1) {
057              throw new IllegalArgumentException(MessageFormat.format(
058                "Parameter [{0}], it's length must be at least 1", getName()));
059            }
060          }
061          return super.parseParam(str);
062        }
063      }
064    
065      @Override
066      public Principal getValue(HttpContext httpContext) {
067        Principal principal = httpContext.getRequest().getUserPrincipal();
068        if (principal == null) {
069          final String user = httpContext.getRequest().getQueryParameters().getFirst(USER_NAME_PARAM);
070          if (user != null) {
071            principal = new Principal() {
072              @Override
073              public String getName() {
074                return new UserParam(user).value();
075              }
076            };
077          }
078        }
079        if (principal != null) {
080          MDC.put("user", principal.getName());
081        }
082        return principal;
083      }
084    
085      @Override
086      public ComponentScope getScope() {
087        return ComponentScope.PerRequest;
088      }
089    
090      @Override
091      public Injectable getInjectable(ComponentContext componentContext, Context context, Type type) {
092        return (type.equals(Principal.class)) ? this : null;
093      }
094    }