RequestBuilder { public T mappingRequestParam(HttpServletRequest request, Class<T> clazz) throws Exception { T returnObject = clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); BeanWrapperImpl beanWrapperImpl = new BeanWrapperImpl(returnObject); for(Field field : fields) { RequestParamName requestParamName = field.getAnnotation(RequestParamName.class); String paramName = null; String paramValue = null; if(requestParamName != null) { paramName = requestParamName.name(); paramValue = request.getParameter(paramName); if(paramValue == null) { paramValue = requestParamName.defaultValue(); } paramValue = URLDecoder.decode(paramValue, "utf-8"); } logger.debug("field name : {}, paramName : {}, defaultValue : {}", new Object[]{field.getName(), paramName, paramValue}); beanWrapperImpl.setPropertyValue(field.getName(), paramValue); } beanWrapperImpl.setPropertyValue("userIp", request.getRemoteAddr()); return returnObject; }  T mappingRequestParam(HttpServletRequest request, Class<T> clazz);  }
@Test public void keyword() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setParameter("keyword", "nike"); RequestBuilder<SearchRequest> builder = new RequestBuilder<SearchRequest>(); SearchRequest searchRequest = builder.mappingRequestParam(request, SearchRequest.class); Assert.assertEquals("nike", searchRequest.getKeyword()); }
@Test public void collectionName() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setParameter("col_name", "test"); RequestBuilder<SearchRequest> builder = new RequestBuilder<SearchRequest>(); SearchRequest searchRequest = builder.mappingRequestParam(request, SearchRequest.class); Assert.assertEquals("test", searchRequest.getCollectionName()); }
@Test public void pageSize() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setParameter("page_size", "50"); RequestBuilder<SearchRequest> builder = new RequestBuilder<SearchRequest>(); SearchRequest searchRequest = builder.mappingRequestParam(request, SearchRequest.class); Assert.assertEquals("50", searchRequest.getPageSize()); }
@Test public void sort() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setParameter("sort", "name desc"); RequestBuilder<SearchRequest> builder = new RequestBuilder<SearchRequest>(); SearchRequest searchRequest = builder.mappingRequestParam(request, SearchRequest.class); Assert.assertEquals("name desc", searchRequest.getSort()); }
@Test public void searchField() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setParameter("search_field", "title, contents"); RequestBuilder<SearchRequest> builder = new RequestBuilder<SearchRequest>(); SearchRequest searchRequest = builder.mappingRequestParam(request, SearchRequest.class); Assert.assertEquals("title, contents", searchRequest.getSearchField()); }
@Test public void customQuery() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setParameter("cq", "title:\"jang\""); RequestBuilder<SearchRequest> builder = new RequestBuilder<SearchRequest>(); SearchRequest searchRequest = builder.mappingRequestParam(request, SearchRequest.class); Assert.assertEquals("title:\"jang\"", searchRequest.getCustomQuery()); }