001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.ws.server.provider; 015 016import org.gbif.api.util.VocabularyUtils; 017import org.gbif.api.vocabulary.Country; 018 019import org.springframework.core.MethodParameter; 020import org.springframework.web.bind.support.WebDataBinderFactory; 021import org.springframework.web.context.request.NativeWebRequest; 022import org.springframework.web.method.support.HandlerMethodArgumentResolver; 023import org.springframework.web.method.support.ModelAndViewContainer; 024 025public class CountryHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver { 026 027 @Override 028 public boolean supportsParameter(MethodParameter parameter) { 029 return Country.class.equals(parameter.getParameterType()); 030 } 031 032 @Override 033 public Object resolveArgument( 034 MethodParameter parameter, 035 ModelAndViewContainer mavContainer, 036 NativeWebRequest webRequest, 037 WebDataBinderFactory binderFactory) { 038 final String paramName = parameter.getParameterName(); 039 final String countryCode = paramName != null ? webRequest.getParameter(paramName) : null; 040 Country result = Country.fromIsoCode(countryCode); 041 042 if (result == null) { 043 // if nothing found also try by enum name 044 result = VocabularyUtils.lookupEnum(countryCode, Country.class); 045 } 046 047 return result; 048 } 049}