Java tutorial
/* The MIT License (MIT) Copyright (c) 2015 QAXML Pty Ltd Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.qatickets.web.common; import java.lang.annotation.Annotation; import java.util.HashSet; import java.util.Set; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.NumberUtils; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import org.springframework.core.MethodParameter; import org.springframework.stereotype.Component; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; import com.qatickets.service.TicketService; import com.qatickets.web.Attr; @Component public class SessionParamArgumentResolver implements HandlerMethodArgumentResolver { @Inject private TicketService ticketService; private static final String HTML5_DATE_PATTERN = "yyyy-MM-dd"; private static final DateTimeFormatter HTML5_DATE_PARSER = DateTimeFormat.forPattern(HTML5_DATE_PATTERN); public static final String UI_DATE_FORMAT = "EEEE, dd MMM yyyy"; private static final DateTimeFormatter UI_DATE_PARSER = DateTimeFormat.forPattern(UI_DATE_FORMAT); @SuppressWarnings({ "rawtypes", "serial" }) private static final Set<Class> supported = new HashSet<Class>() { { add(SessionUser.class); add(ParamTicket.class); add(HTML5Date.class); add(Checkbox.class); add(IntParam.class); } }; @SuppressWarnings("rawtypes") @Override public boolean supportsParameter(MethodParameter parameter) { final Annotation[] paramAnns = parameter.getParameterAnnotations(); for (final Annotation paramAnn : paramAnns) { for (Class c : supported) { if (c.isInstance(paramAnn)) { return true; } } } return false; } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { Annotation[] paramAnns = parameter.getParameterAnnotations(); HttpServletRequest req = (HttpServletRequest) webRequest.getNativeRequest(); HttpSession session = req.getSession(false); Object result = null; // parameter if (session != null) { for (Annotation paramAnn : paramAnns) { if (SessionUser.class.isInstance(paramAnn)) { result = session.getAttribute(UserHelper.USER); } else if (ParamTicket.class.isInstance(paramAnn)) { int ticketId = 0; if (NumberUtils.isDigits(req.getParameter(Attr.PARAM_TICKET_ID))) { ticketId = Integer.parseInt(req.getParameter(Attr.PARAM_TICKET_ID)); } result = this.ticketService.findById(ticketId); } else if (HTML5Date.class.isInstance(paramAnn)) { HTML5Date html5Date = (HTML5Date) paramAnn; String html5DateParameterName = null; if (StringUtils.isNotBlank(html5Date.value())) { html5DateParameterName = html5Date.value(); } else { html5DateParameterName = parameter.getParameterName(); } String date = req.getParameter(html5DateParameterName); if (StringUtils.isNotBlank(date)) { if (date.contains(",")) { // UI date result = DateTime.parse(date, UI_DATE_PARSER).toDate(); } else { // native picker date result = DateTime.parse(date, HTML5_DATE_PARSER).toDate(); } } } else if (Checkbox.class.isInstance(paramAnn)) { Checkbox checkbox = (Checkbox) paramAnn; String checkboxParameterName = null; if (StringUtils.isNotBlank(checkbox.value())) { checkboxParameterName = checkbox.value(); } else { checkboxParameterName = parameter.getParameterName(); } String checkboxValue = req.getParameter(checkboxParameterName); if (StringUtils.isNotBlank(checkboxValue)) { result = Boolean.TRUE; } else { result = Boolean.FALSE; } } else if (IntParam.class.isInstance(paramAnn)) { IntParam intParam = (IntParam) paramAnn; String parameterName = null; if (StringUtils.isNotBlank(intParam.value())) { parameterName = intParam.value(); } else { parameterName = parameter.getParameterName(); } String value = req.getParameter(parameterName); if (value != null && NumberUtils.isDigits(value)) { result = Integer.parseInt(value); } else { result = 0; } } } } return result; } }