@Test public void testDeriveSearchRequest() throws Exception { request.setUri("/myapp", null); testException("Search topic not provided"); request.setUri("/myapp", "/crap"); testException("Topic not configured"); testException("crap"); request.setUri("/myapp", "/Environment/one/too/too/many"); testException("Search key in request uri not configured"); testException("many"); request.setUri("/myapp", "/Environment/one/two/three"); SearchRequest searchRequest = factory.deriveSearchRequest(request); Assert.assertTrue(searchRequest != null); Assert.assertTrue("Environment".equals(searchRequest.getTopic())); CompositeCriteria searchCriteria = searchRequest.getSearchCriteria(); Assert.assertTrue(searchCriteria != null); Assert.assertTrue(CompositeCriteria.Operator.AND.equals(searchCriteria.getOperator())); Assert.assertTrue(searchCriteria.getSearchCriteria() != null); Assert.assertTrue(searchCriteria.getSearchCriteria().length==3); testCriteria(searchCriteria.getSearchCriteria()[0], "TABLE_CAT", FilterCriteria.Operation.EQUAL, "one"); testCriteria(searchCriteria.getSearchCriteria()[1], "TABLE_SCHEM", FilterCriteria.Operation.EQUAL, "two"); testCriteria(searchCriteria.getSearchCriteria()[2], "TABLE_NAME", FilterCriteria.Operation.EQUAL, "three"); request.setUri("/myapp", "/Environments/one"); searchRequest = factory.deriveSearchRequest(request); Assert.assertTrue(searchRequest != null); Assert.assertTrue("Environment".equals(searchRequest.getTopic())); request.setUri("/myapp", "/Environments/one"); request.getParameterMap().put(RequestConstants.PARM_START_ROW, "2"); searchRequest = factory.deriveSearchRequest(request); Assert.assertTrue(searchRequest != null); Assert.assertTrue(searchRequest.getStartRow().equals(2L)); request.getParameterMap().put(RequestConstants.PARM_START_ROW, "invalid"); testException("Invalid start row request parm"); request.getParameterMap().remove(RequestConstants.PARM_START_ROW); request.setUri("/myapp", "/Environments/one"); request.getParameterMap().put(RequestConstants.PARM_MAX_ROWS, "2"); searchRequest = factory.deriveSearchRequest(request); Assert.assertTrue(searchRequest != null); Assert.assertTrue(searchRequest.getMaxRows().equals(2L)); request.getParameterMap().put(RequestConstants.PARM_MAX_ROWS, "invalid"); testException("Invalid max rows request parm"); request.getParameterMap().remove(RequestConstants.PARM_MAX_ROWS); }
public SearchRequest deriveSearchRequest(HttpServletRequest request) { String[] uriNodes = deriveSearchNodes(request); if (ArrayUtils.isEmpty(uriNodes) ) { throw new MonetaException("Search topic not provided in request uri") .addContextValue("request path info", request.getPathInfo()) .addContextValue("request context path", request.getContextPath()) .addContextValue("request uri", request.getRequestURI()); } SearchRequest searchRequest = new SearchRequest(); String topicRequested=uriNodes[0]; Topic searchTopic = findSearchTopic(topicRequested); searchRequest.setTopic(searchTopic.getTopicName()); CompositeCriteria baseCriteria = new CompositeCriteria(); searchRequest.setSearchCriteria(baseCriteria); baseCriteria.setOperator(CompositeCriteria.Operator.AND); baseCriteria.setSearchCriteria(new FilterCriteria[0]); FilterCriteria filterCriteria = null; TopicKeyField keyField = null; String parmStr=request.getParameter(RequestConstants.PARM_START_ROW); if (StringUtils.isNotEmpty(parmStr)) { try{searchRequest.setStartRow(Long.valueOf(parmStr));} catch (Exception e) { throw new MonetaException("Invalid start row request parm", e) .addContextValue(RequestConstants.PARM_START_ROW, parmStr); } } parmStr=request.getParameter(RequestConstants.PARM_MAX_ROWS); if (StringUtils.isNotEmpty(parmStr)) { try{searchRequest.setMaxRows(Long.valueOf(parmStr));} catch (Exception e) { throw new MonetaException("Invalid max rows request parm", e) .addContextValue(RequestConstants.PARM_MAX_ROWS, parmStr); } } for (int pathParamOffset = 1; pathParamOffset < uriNodes.length; pathParamOffset++) { if (searchTopic.getKeyFieldList().size() < pathParamOffset) { throw new MonetaException("Search key in request uri not configured for topic") .addContextValue("search key", uriNodes[pathParamOffset]) .addContextValue("topic", searchRequest.getTopic()) .addContextValue("request path info", request.getPathInfo()) .addContextValue("request context path", request.getContextPath()) .addContextValue("request uri", request.getRequestURI()); } keyField=searchTopic.getKeyFieldList().get(pathParamOffset-1); filterCriteria = new FilterCriteria(); filterCriteria.setFieldName(keyField.getColumnName()); filterCriteria.setOperation(FilterCriteria.Operation.EQUAL); if (TopicKeyField.DataType.STRING.equals(keyField.getDataType())) { filterCriteria.setValue(uriNodes[pathParamOffset]); } else { filterCriteria.setValue(Long.valueOf(uriNodes[pathParamOffset])); } baseCriteria.setSearchCriteria( (Criteria[])ArrayUtils.add(baseCriteria.getSearchCriteria(), filterCriteria)); } return searchRequest; }
SearchRequestFactory { public SearchRequest deriveSearchRequest(HttpServletRequest request) { String[] uriNodes = deriveSearchNodes(request); if (ArrayUtils.isEmpty(uriNodes) ) { throw new MonetaException("Search topic not provided in request uri") .addContextValue("request path info", request.getPathInfo()) .addContextValue("request context path", request.getContextPath()) .addContextValue("request uri", request.getRequestURI()); } SearchRequest searchRequest = new SearchRequest(); String topicRequested=uriNodes[0]; Topic searchTopic = findSearchTopic(topicRequested); searchRequest.setTopic(searchTopic.getTopicName()); CompositeCriteria baseCriteria = new CompositeCriteria(); searchRequest.setSearchCriteria(baseCriteria); baseCriteria.setOperator(CompositeCriteria.Operator.AND); baseCriteria.setSearchCriteria(new FilterCriteria[0]); FilterCriteria filterCriteria = null; TopicKeyField keyField = null; String parmStr=request.getParameter(RequestConstants.PARM_START_ROW); if (StringUtils.isNotEmpty(parmStr)) { try{searchRequest.setStartRow(Long.valueOf(parmStr));} catch (Exception e) { throw new MonetaException("Invalid start row request parm", e) .addContextValue(RequestConstants.PARM_START_ROW, parmStr); } } parmStr=request.getParameter(RequestConstants.PARM_MAX_ROWS); if (StringUtils.isNotEmpty(parmStr)) { try{searchRequest.setMaxRows(Long.valueOf(parmStr));} catch (Exception e) { throw new MonetaException("Invalid max rows request parm", e) .addContextValue(RequestConstants.PARM_MAX_ROWS, parmStr); } } for (int pathParamOffset = 1; pathParamOffset < uriNodes.length; pathParamOffset++) { if (searchTopic.getKeyFieldList().size() < pathParamOffset) { throw new MonetaException("Search key in request uri not configured for topic") .addContextValue("search key", uriNodes[pathParamOffset]) .addContextValue("topic", searchRequest.getTopic()) .addContextValue("request path info", request.getPathInfo()) .addContextValue("request context path", request.getContextPath()) .addContextValue("request uri", request.getRequestURI()); } keyField=searchTopic.getKeyFieldList().get(pathParamOffset-1); filterCriteria = new FilterCriteria(); filterCriteria.setFieldName(keyField.getColumnName()); filterCriteria.setOperation(FilterCriteria.Operation.EQUAL); if (TopicKeyField.DataType.STRING.equals(keyField.getDataType())) { filterCriteria.setValue(uriNodes[pathParamOffset]); } else { filterCriteria.setValue(Long.valueOf(uriNodes[pathParamOffset])); } baseCriteria.setSearchCriteria( (Criteria[])ArrayUtils.add(baseCriteria.getSearchCriteria(), filterCriteria)); } return searchRequest; } }
SearchRequestFactory { public SearchRequest deriveSearchRequest(HttpServletRequest request) { String[] uriNodes = deriveSearchNodes(request); if (ArrayUtils.isEmpty(uriNodes) ) { throw new MonetaException("Search topic not provided in request uri") .addContextValue("request path info", request.getPathInfo()) .addContextValue("request context path", request.getContextPath()) .addContextValue("request uri", request.getRequestURI()); } SearchRequest searchRequest = new SearchRequest(); String topicRequested=uriNodes[0]; Topic searchTopic = findSearchTopic(topicRequested); searchRequest.setTopic(searchTopic.getTopicName()); CompositeCriteria baseCriteria = new CompositeCriteria(); searchRequest.setSearchCriteria(baseCriteria); baseCriteria.setOperator(CompositeCriteria.Operator.AND); baseCriteria.setSearchCriteria(new FilterCriteria[0]); FilterCriteria filterCriteria = null; TopicKeyField keyField = null; String parmStr=request.getParameter(RequestConstants.PARM_START_ROW); if (StringUtils.isNotEmpty(parmStr)) { try{searchRequest.setStartRow(Long.valueOf(parmStr));} catch (Exception e) { throw new MonetaException("Invalid start row request parm", e) .addContextValue(RequestConstants.PARM_START_ROW, parmStr); } } parmStr=request.getParameter(RequestConstants.PARM_MAX_ROWS); if (StringUtils.isNotEmpty(parmStr)) { try{searchRequest.setMaxRows(Long.valueOf(parmStr));} catch (Exception e) { throw new MonetaException("Invalid max rows request parm", e) .addContextValue(RequestConstants.PARM_MAX_ROWS, parmStr); } } for (int pathParamOffset = 1; pathParamOffset < uriNodes.length; pathParamOffset++) { if (searchTopic.getKeyFieldList().size() < pathParamOffset) { throw new MonetaException("Search key in request uri not configured for topic") .addContextValue("search key", uriNodes[pathParamOffset]) .addContextValue("topic", searchRequest.getTopic()) .addContextValue("request path info", request.getPathInfo()) .addContextValue("request context path", request.getContextPath()) .addContextValue("request uri", request.getRequestURI()); } keyField=searchTopic.getKeyFieldList().get(pathParamOffset-1); filterCriteria = new FilterCriteria(); filterCriteria.setFieldName(keyField.getColumnName()); filterCriteria.setOperation(FilterCriteria.Operation.EQUAL); if (TopicKeyField.DataType.STRING.equals(keyField.getDataType())) { filterCriteria.setValue(uriNodes[pathParamOffset]); } else { filterCriteria.setValue(Long.valueOf(uriNodes[pathParamOffset])); } baseCriteria.setSearchCriteria( (Criteria[])ArrayUtils.add(baseCriteria.getSearchCriteria(), filterCriteria)); } return searchRequest; }  }
SearchRequestFactory { public SearchRequest deriveSearchRequest(HttpServletRequest request) { String[] uriNodes = deriveSearchNodes(request); if (ArrayUtils.isEmpty(uriNodes) ) { throw new MonetaException("Search topic not provided in request uri") .addContextValue("request path info", request.getPathInfo()) .addContextValue("request context path", request.getContextPath()) .addContextValue("request uri", request.getRequestURI()); } SearchRequest searchRequest = new SearchRequest(); String topicRequested=uriNodes[0]; Topic searchTopic = findSearchTopic(topicRequested); searchRequest.setTopic(searchTopic.getTopicName()); CompositeCriteria baseCriteria = new CompositeCriteria(); searchRequest.setSearchCriteria(baseCriteria); baseCriteria.setOperator(CompositeCriteria.Operator.AND); baseCriteria.setSearchCriteria(new FilterCriteria[0]); FilterCriteria filterCriteria = null; TopicKeyField keyField = null; String parmStr=request.getParameter(RequestConstants.PARM_START_ROW); if (StringUtils.isNotEmpty(parmStr)) { try{searchRequest.setStartRow(Long.valueOf(parmStr));} catch (Exception e) { throw new MonetaException("Invalid start row request parm", e) .addContextValue(RequestConstants.PARM_START_ROW, parmStr); } } parmStr=request.getParameter(RequestConstants.PARM_MAX_ROWS); if (StringUtils.isNotEmpty(parmStr)) { try{searchRequest.setMaxRows(Long.valueOf(parmStr));} catch (Exception e) { throw new MonetaException("Invalid max rows request parm", e) .addContextValue(RequestConstants.PARM_MAX_ROWS, parmStr); } } for (int pathParamOffset = 1; pathParamOffset < uriNodes.length; pathParamOffset++) { if (searchTopic.getKeyFieldList().size() < pathParamOffset) { throw new MonetaException("Search key in request uri not configured for topic") .addContextValue("search key", uriNodes[pathParamOffset]) .addContextValue("topic", searchRequest.getTopic()) .addContextValue("request path info", request.getPathInfo()) .addContextValue("request context path", request.getContextPath()) .addContextValue("request uri", request.getRequestURI()); } keyField=searchTopic.getKeyFieldList().get(pathParamOffset-1); filterCriteria = new FilterCriteria(); filterCriteria.setFieldName(keyField.getColumnName()); filterCriteria.setOperation(FilterCriteria.Operation.EQUAL); if (TopicKeyField.DataType.STRING.equals(keyField.getDataType())) { filterCriteria.setValue(uriNodes[pathParamOffset]); } else { filterCriteria.setValue(Long.valueOf(uriNodes[pathParamOffset])); } baseCriteria.setSearchCriteria( (Criteria[])ArrayUtils.add(baseCriteria.getSearchCriteria(), filterCriteria)); } return searchRequest; }  SearchRequest deriveSearchRequest(HttpServletRequest request); }
SearchRequestFactory { public SearchRequest deriveSearchRequest(HttpServletRequest request) { String[] uriNodes = deriveSearchNodes(request); if (ArrayUtils.isEmpty(uriNodes) ) { throw new MonetaException("Search topic not provided in request uri") .addContextValue("request path info", request.getPathInfo()) .addContextValue("request context path", request.getContextPath()) .addContextValue("request uri", request.getRequestURI()); } SearchRequest searchRequest = new SearchRequest(); String topicRequested=uriNodes[0]; Topic searchTopic = findSearchTopic(topicRequested); searchRequest.setTopic(searchTopic.getTopicName()); CompositeCriteria baseCriteria = new CompositeCriteria(); searchRequest.setSearchCriteria(baseCriteria); baseCriteria.setOperator(CompositeCriteria.Operator.AND); baseCriteria.setSearchCriteria(new FilterCriteria[0]); FilterCriteria filterCriteria = null; TopicKeyField keyField = null; String parmStr=request.getParameter(RequestConstants.PARM_START_ROW); if (StringUtils.isNotEmpty(parmStr)) { try{searchRequest.setStartRow(Long.valueOf(parmStr));} catch (Exception e) { throw new MonetaException("Invalid start row request parm", e) .addContextValue(RequestConstants.PARM_START_ROW, parmStr); } } parmStr=request.getParameter(RequestConstants.PARM_MAX_ROWS); if (StringUtils.isNotEmpty(parmStr)) { try{searchRequest.setMaxRows(Long.valueOf(parmStr));} catch (Exception e) { throw new MonetaException("Invalid max rows request parm", e) .addContextValue(RequestConstants.PARM_MAX_ROWS, parmStr); } } for (int pathParamOffset = 1; pathParamOffset < uriNodes.length; pathParamOffset++) { if (searchTopic.getKeyFieldList().size() < pathParamOffset) { throw new MonetaException("Search key in request uri not configured for topic") .addContextValue("search key", uriNodes[pathParamOffset]) .addContextValue("topic", searchRequest.getTopic()) .addContextValue("request path info", request.getPathInfo()) .addContextValue("request context path", request.getContextPath()) .addContextValue("request uri", request.getRequestURI()); } keyField=searchTopic.getKeyFieldList().get(pathParamOffset-1); filterCriteria = new FilterCriteria(); filterCriteria.setFieldName(keyField.getColumnName()); filterCriteria.setOperation(FilterCriteria.Operation.EQUAL); if (TopicKeyField.DataType.STRING.equals(keyField.getDataType())) { filterCriteria.setValue(uriNodes[pathParamOffset]); } else { filterCriteria.setValue(Long.valueOf(uriNodes[pathParamOffset])); } baseCriteria.setSearchCriteria( (Criteria[])ArrayUtils.add(baseCriteria.getSearchCriteria(), filterCriteria)); } return searchRequest; }  SearchRequest deriveSearchRequest(HttpServletRequest request);  }