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 045 public static final String USER_PATTERN_KEY 046 = "httpfs.user.provider.user.pattern"; 047 048 public static final String USER_PATTERN_DEFAULT 049 = "^[A-Za-z_][A-Za-z0-9._-]*[$]?$"; 050 051 private static Pattern userPattern = Pattern.compile(USER_PATTERN_DEFAULT); 052 053 public static void setUserPattern(String pattern) { 054 userPattern = Pattern.compile(pattern); 055 } 056 057 public static Pattern getUserPattern() { 058 return userPattern; 059 } 060 061 static class UserParam extends StringParam { 062 063 public UserParam(String user) { 064 super(USER_NAME_PARAM, user, getUserPattern()); 065 } 066 067 @Override 068 public String parseParam(String str) { 069 if (str != null) { 070 int len = str.length(); 071 if (len < 1) { 072 throw new IllegalArgumentException(MessageFormat.format( 073 "Parameter [{0}], it's length must be at least 1", getName())); 074 } 075 } 076 return super.parseParam(str); 077 } 078 } 079 080 @Override 081 public Principal getValue(HttpContext httpContext) { 082 Principal principal = httpContext.getRequest().getUserPrincipal(); 083 if (principal == null) { 084 final String user = httpContext.getRequest().getQueryParameters().getFirst(USER_NAME_PARAM); 085 if (user != null) { 086 principal = new Principal() { 087 @Override 088 public String getName() { 089 return new UserParam(user).value(); 090 } 091 }; 092 } 093 } 094 if (principal != null) { 095 MDC.put("user", principal.getName()); 096 } 097 return principal; 098 } 099 100 @Override 101 public ComponentScope getScope() { 102 return ComponentScope.PerRequest; 103 } 104 105 @Override 106 public Injectable getInjectable(ComponentContext componentContext, Context context, Type type) { 107 return (type.equals(Principal.class)) ? this : null; 108 } 109 }