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; 019 020import java.net.URI; 021import java.util.List; 022import java.util.Map; 023 024import javax.ws.rs.core.MultivaluedMap; 025import javax.ws.rs.core.UriBuilder; 026 027import com.sun.jersey.spi.container.ContainerRequest; 028import com.sun.jersey.spi.container.ContainerRequestFilter; 029import com.sun.jersey.spi.container.ContainerResponseFilter; 030import com.sun.jersey.spi.container.ResourceFilter; 031import org.apache.hadoop.util.StringUtils; 032 033/** 034 * A filter to change parameter names to lower cases 035 * so that parameter names are considered as case insensitive. 036 */ 037public class ParamFilter implements ResourceFilter { 038 private static final ContainerRequestFilter LOWER_CASE 039 = new ContainerRequestFilter() { 040 @Override 041 public ContainerRequest filter(final ContainerRequest request) { 042 final MultivaluedMap<String, String> parameters = request.getQueryParameters(); 043 if (containsUpperCase(parameters.keySet())) { 044 //rebuild URI 045 final URI lower = rebuildQuery(request.getRequestUri(), parameters); 046 request.setUris(request.getBaseUri(), lower); 047 } 048 return request; 049 } 050 }; 051 052 @Override 053 public ContainerRequestFilter getRequestFilter() { 054 return LOWER_CASE; 055 } 056 057 @Override 058 public ContainerResponseFilter getResponseFilter() { 059 return null; 060 } 061 062 /** Do the strings contain upper case letters? */ 063 static boolean containsUpperCase(final Iterable<String> strings) { 064 for(String s : strings) { 065 for(int i = 0; i < s.length(); i++) { 066 if (Character.isUpperCase(s.charAt(i))) { 067 return true; 068 } 069 } 070 } 071 return false; 072 } 073 074 /** Rebuild the URI query with lower case parameter names. */ 075 private static URI rebuildQuery(final URI uri, 076 final MultivaluedMap<String, String> parameters) { 077 UriBuilder b = UriBuilder.fromUri(uri).replaceQuery(""); 078 for(Map.Entry<String, List<String>> e : parameters.entrySet()) { 079 final String key = StringUtils.toLowerCase(e.getKey()); 080 for(String v : e.getValue()) { 081 b = b.queryParam(key, v); 082 } 083 } 084 return b.build(); 085 } 086}