/** * This methods adds all mappings found to the supplied {@link PrettyConfigBuilder}. It should be * called after all classes has been scanned via {@link #processClass(Class)}. * * @param builder The builder to add the mappings to */ public void build(final PrettyConfigBuilder builder) { // process all actions found for (ActionSpec actionSpec : urlActions) { // create an action for each referenced mapping for (String mappingId : actionSpec.getMappingIds()) { // Get the mapping references by the action UrlMapping mapping = urlMappings.get(mappingId); /* * Fail for unresolved mappings. This may happen when the user places * invalid mapping IDs in the mappingId attribute of * * @URLAction or @URLQueryParameter */ if (mapping == null) { throw new IllegalArgumentException( "Unable to find the mapping '" + mappingId + "' referenced at method '" + actionSpec.getMethod().getName() + "' in class '" + actionSpec.getMethod().getDeclaringClass().getName() + "'."); } // build UrlMapping UrlAction urlAction = new UrlAction(); urlAction.setPhaseId(actionSpec.getPhaseId()); urlAction.setOnPostback(actionSpec.isOnPostback()); urlAction.setInheritable(actionSpec.isInheritable()); // try to get bean name Class clazz = actionSpec.getMethod().getDeclaringClass(); // build expression PrettyExpression expression = buildPrettyExpression(clazz, actionSpec.getMethod().getName()); urlAction.setAction(expression); // trace if (log.isTraceEnabled()) { log.trace( "Adding action expression '" + urlAction.getAction() + "' to mapping: " + mapping.getId()); } // register this action mapping.addAction(urlAction); } } for (QueryParamSpec queryParamSpec : queryParamList) { // create a query param for each referenced mapping for (String mappingId : queryParamSpec.getMappingIds()) { // Get the mapping references by the query param UrlMapping mapping = urlMappings.get(mappingId); // fail for unresolved mappings if (mapping == null) { throw new IllegalArgumentException( "Unable to find the mapping '" + mappingId + "' referenced at field '" + queryParamSpec.getFieldName() + "' in class '" + queryParamSpec.getOwnerClass().getName() + "'."); } // build UrlMapping QueryParameter queryParam = new QueryParameter(); queryParam.setName(queryParamSpec.getName()); queryParam.setOnError(queryParamSpec.getOnError()); queryParam.setConverterId(StringUtils.trimToNull(queryParamSpec.getConverterId())); queryParam.setValidatorIds(join(queryParamSpec.getValidatorIds(), " ")); queryParam.setOnPostback(queryParamSpec.isOnPostback()); // optional validator method if (!isBlank(queryParamSpec.getValidator())) { queryParam.setValidatorExpression(new ConstantExpression(queryParamSpec.getValidator())); } // try to get bean name Class<?> clazz = queryParamSpec.getOwnerClass(); // build expression PrettyExpression expression = buildPrettyExpression(clazz, queryParamSpec.getFieldName()); queryParam.setExpression(expression); // trace if (log.isTraceEnabled()) { log.trace( "Registered query-param '" + queryParam.getName() + "' to '" + expression + "' in mapping: " + mapping.getId()); } // register this action mapping.addQueryParam(queryParam); } } // finally register all mappings for (UrlMapping mapping : urlMappings.values()) { builder.addMapping(mapping); } }
/** * Searches for {@link URLQueryParameter} annotations on a single field. * * @param field Field to scan * @param classMappingIds The mapping IDs of the class this method belongs to */ private void processFieldAnnotations(final Field field, final String[] classMappingIds) { // Is there a @URLQueryParameter annotation? URLQueryParameter queryParamAnnotation = field.getAnnotation(URLQueryParameter.class); if (queryParamAnnotation != null) { // create a QueryParamSpec from the annotation QueryParamSpec queryParam = new QueryParamSpec(); queryParam.setFieldName(field.getName()); queryParam.setOwnerClass(field.getDeclaringClass()); queryParam.setName(queryParamAnnotation.value()); queryParam.setOnPostback(queryParamAnnotation.onPostback()); // check which mapping the action belongs to if (!isBlank(queryParamAnnotation.mappingId())) { // action belongs to the mapping mentioned with mappingId attribute queryParam.setMappingIds(new String[] {queryParamAnnotation.mappingId().trim()}); } else if ((classMappingIds != null) && (classMappingIds.length > 0)) { // use the mappings found on the class queryParam.setMappingIds(classMappingIds); } else { throw new IllegalArgumentException( "Unable to find a suitable mapping " + "for the query-parameter definied on field '" + field.getName() + "' in class '" + field.getDeclaringClass().getName() + "'. Either place a @URLMapping annotation on the " + "class or reference a foreign mapping using the 'mappingId' attribute."); } // check if there is also a validation annotation placed on the field URLValidator validationAnnotation = field.getAnnotation(URLValidator.class); // check if annotation has been found if (validationAnnotation != null) { // set validation options on the QueryParamSpec object queryParam.setValidatorIds(validationAnnotation.validatorIds()); queryParam.setOnError(validationAnnotation.onError()); queryParam.setValidator(validationAnnotation.validator()); } // check if there is also a converter annotation placed on the field URLConverter converterAnnotation = field.getAnnotation(URLConverter.class); if (converterAnnotation != null) { queryParam.setConverterId(converterAnnotation.converterId().trim()); } // add the new spec object to the list of specs queryParamList.add(queryParam); } }