@Test public void noParameter() { RequestMapping mapping = new RequestMapping(null, "/test"); assertNull(mapping.matches("/girl")); assertNotNull(mapping.matches("/test")); assertNull(mapping.matches("/tester")); }
@Test public void negativeNumbers() { RequestMapping mapping = new RequestMapping(null, "/rank/{account}/{league}/{week}"); assertNull(mapping.matches("/rank")); assertNull(mapping.matches("/rank/CPA/Cool")); assertNotNull(mapping.matches("/rank/CPA/Cool/3")); assertNotNull(mapping.matches("/rank/CPA/Cool/-3")); }
@Test public void oneParameter() { RequestMapping mapping = new RequestMapping(null, "/test/{name}"); assertNull(mapping.matches("/test")); assertNull(mapping.matches("/test/john/doe")); PathParameters parameters = mapping.matches("/test/frank"); assertNotNull(parameters); assertEquals("frank", parameters.getString("name")); }
private RequestMappingInfo findMethodRequestMapping(FacesContext context, Bean<?> bean) { RequestMappingInfo result = null; Class clazz = bean.getBeanClass(); AnnotatedType annotatedType = beanManager.createAnnotatedType(clazz); Set<AnnotatedMethod> annotatedMethodSet = annotatedType.getMethods(); for (AnnotatedMethod method : annotatedMethodSet) { if (method.isAnnotationPresent(RequestMapping.class)) { RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); String[] mappings = requestMapping.value(); String mapping = null; for (String current : mappings) { String pathInfo = context.getExternalContext().getRequestPathInfo(); if (pathInfo.equals(current)) { result = new RequestMappingInfo(); result.setBean(bean); result.setMethod(method.getJavaMember()); result.setRequestMapping(mapping); result.setMappingType(RequestMappingInfo.MappingType.EXACT); break; } else if (current.endsWith("*")) { current = current.substring(0, current.length() - 1); if (pathInfo.startsWith(current)) { if (result == null) { result = new RequestMappingInfo(); result.setBean(bean); result.setMethod(method.getJavaMember()); result.setRequestMapping(current); result.setMappingType(RequestMappingInfo.MappingType.PREFIX); } else if (current.length() > result.getLength()) { result.setBean(bean); result.setMethod(method.getJavaMember()); result.setRequestMapping(current); result.setMappingType(RequestMappingInfo.MappingType.PREFIX); } } } else if (current.startsWith("*")) { current = current.substring(1); if (pathInfo.endsWith(current)) { result = new RequestMappingInfo(); result.setBean(bean); result.setMethod(method.getJavaMember()); result.setRequestMapping(current); result.setMappingType(RequestMappingInfo.MappingType.EXTENSION); break; } } } } if (result != null && (result.getMappingType().equals(RequestMappingInfo.MappingType.EXACT) || (result.getMappingType().equals(RequestMappingInfo.MappingType.EXTENSION)))) { break; } } return result; }
@Test public void twoParameters() { RequestMapping mapping = new RequestMapping(null, "/test/{name}/{id}"); assertNull(mapping.matches("/test")); assertNull(mapping.matches("/test/john")); PathParameters parameters = mapping.matches("/test/john/doe"); assertNotNull(parameters); assertEquals("john", parameters.getString("name")); assertEquals("doe", parameters.getString("id")); }
private HttpMethod getHttpMethod(RequestMapping classMapping, RequestMapping methodMapping) { HttpMethod httpMethod; if (methodMapping != null && isNotEmpty(methodMapping.method())) { httpMethod = getHttpMethod(methodMapping); } else if (classMapping != null && isNotEmpty(classMapping.method())) { httpMethod = getHttpMethod(classMapping); } else { // TODO add warning to the logs throw new SpringRestClientConfigurationException("Can't identify HTTP method!"); } return httpMethod; }
/** * Created a {@link RequestMappingInfo} from a 'Spring Integration HTTP Inbound Endpoint' {@link * RequestMapping}. * * @see RequestMappingHandlerMapping#getMappingForMethod */ private RequestMappingInfo getMappingForEndpoint(HttpRequestHandlingEndpointSupport endpoint) { final RequestMapping requestMapping = endpoint.getRequestMapping(); if (ObjectUtils.isEmpty(requestMapping.getPathPatterns())) { return null; } org.springframework.web.bind.annotation.RequestMapping requestMappingAnnotation = new org.springframework.web.bind.annotation.RequestMapping() { @Override public String[] value() { return requestMapping.getPathPatterns(); } @Override public RequestMethod[] method() { return requestMapping.getRequestMethods(); } @Override public String[] params() { return requestMapping.getParams(); } @Override public String[] headers() { return requestMapping.getHeaders(); } @Override public String[] consumes() { return requestMapping.getConsumes(); } @Override public String[] produces() { return requestMapping.getProduces(); } @Override public Class<? extends Annotation> annotationType() { return org.springframework.web.bind.annotation.RequestMapping.class; } }; Object[] createRequestMappingInfoParams = new Object[] {requestMappingAnnotation, this.getCustomTypeCondition(endpoint.getClass())}; return (RequestMappingInfo) ReflectionUtils.invokeMethod( CREATE_REQUEST_MAPPING_INFO_METHOD, this, createRequestMappingInfoParams); }
@Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String requestUri = req.getRequestURI(); logger.debug("Method : {}, Request URI : {}", req.getMethod(), requestUri); Controller controller = rm.findController(urlExceptParameter(req.getRequestURI())); String viewName; try { viewName = controller.execute(req, resp); } catch (Exception e) { throw new ServletException(e.getMessage()); } movePage(req, resp, viewName); }
private String getPath(RequestMapping classMapping) { if (classMapping != null && isNotEmpty(classMapping.value())) { return classMapping.value()[0]; } return null; }
private HttpMethod getHttpMethod(RequestMapping mapping) { return HttpMethod.valueOf(mapping.method()[0].name()); }