/** * Adds an entry to the {@link org.kuali.rice.krad.util.GlobalVariables#getMessageMap()} for the given * binding processing error * * @param ex exception that was thrown * @param bindingResult binding result containing the results of the binding process */ @Override public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) { // Create field error with the exceptions's code, e.g. "typeMismatch". super.processPropertyAccessException(ex, bindingResult); Object rejectedValue = ex.getValue(); if (!(rejectedValue == null || rejectedValue.equals(""))) { if (ex.getCause() instanceof FormatException) { GlobalVariables.getMessageMap().putError(ex.getPropertyName(), ((FormatException) ex.getCause()).getErrorKey(), new String[] {rejectedValue.toString()}); } else { GlobalVariables.getMessageMap().putError(ex.getPropertyName(), RiceKeyConstants.ERROR_CUSTOM, new String[] {"Invalid format"}); } } }
public void setValueSilently(Object value, PropertyChangeListener listenerToSkip) { try { if (logger.isDebugEnabled()) { Class valueClass = (value != null ? value.getClass() : null); logger.debug("Setting '" + formProperty + "' value to convert/validate '" + (UserMetadata.isFieldProtected(DefaultFormModel.this, formProperty) ? "***" : value) + "', class=" + valueClass); } super.setValueSilently(value, listenerToSkip); clearBindingError(this); } catch (ConversionException ce) { logger.warn("Conversion exception occurred setting value", ce); raiseBindingError(this, value, ce); } catch (PropertyAccessException pae) { logger.warn("Type Mismatch Exception occurred setting value", pae); raiseBindingError(this, value, pae); } }
protected String getMessageCodeForException(Exception e) { if (e instanceof PropertyAccessException) { return ((PropertyAccessException)e).getErrorCode(); } else if (e instanceof NullPointerException) { return "required"; } else if (e instanceof InvalidFormatException) { return "typeMismatch"; } else if (e instanceof IllegalArgumentException) { return "typeMismatch"; } else if (e.getCause() instanceof Exception) { return getMessageCodeForException((Exception)e.getCause()); } else { return "unknown"; } }
@Override public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) { // Create field error with the exceptions's code, e.g. "typeMismatch". String field = ex.getPropertyName(); String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field); Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field); Object rejectedValue = ex.getValue(); if (rejectedValue != null && rejectedValue.getClass().isArray()) { rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue)); } bindingResult.addError(new FieldError( bindingResult.getObjectName(), field, rejectedValue, true, codes, arguments, ex.getLocalizedMessage())); }
/** * Apply given property values to the target object. * <p>Default implementation applies all of the supplied property * values as bean property values. By default, unknown fields will * be ignored. * @param mpvs the property values to be bound (can be modified) * @see #getTarget * @see #getPropertyAccessor * @see #isIgnoreUnknownFields * @see #getBindingErrorProcessor * @see BindingErrorProcessor#processPropertyAccessException */ protected void applyPropertyValues(MutablePropertyValues mpvs) { try { // Bind request parameters onto target object. getPropertyAccessor().setPropertyValues(mpvs, isIgnoreUnknownFields(), isIgnoreInvalidFields()); } catch (PropertyBatchUpdateException ex) { // Use bind error processor to create FieldErrors. for (PropertyAccessException pae : ex.getPropertyAccessExceptions()) { getBindingErrorProcessor().processPropertyAccessException(pae, getInternalBindingResult()); } } }
public static void main(final String[] args) { final String rootContextDirectoryClassPath = "cool/pandora/modeller/ctx"; final String startupContextPath = rootContextDirectoryClassPath + "/common/richclient-startup-context.xml"; final String richclientApplicationContextPath = rootContextDirectoryClassPath + "/common/richclient-application-context.xml"; final String businessLayerContextPath = rootContextDirectoryClassPath + "/common/business-layer-context.xml"; try { new ApplicationLauncher(startupContextPath, new String[]{richclientApplicationContextPath, businessLayerContextPath}); } catch (final IllegalStateException ex1) { log.error("IllegalStateException during startup", ex1); JOptionPane.showMessageDialog(new JFrame(), "An illegal state error occured.\n", "Bagger startup error!", JOptionPane.ERROR_MESSAGE); System.exit(1); } catch (final PropertyAccessException ex) { log.error("PropertyAccessException during startup", ex); JOptionPane.showMessageDialog(new JFrame(), "An error occured loading properties.\n", "Bagger startup " + "error!", JOptionPane.ERROR_MESSAGE); System.exit(1); } catch (final RuntimeException e) { log.error("RuntimeException during startup", e); final String msg = e.getMessage(); if (msg.contains("SAXParseException")) { JOptionPane.showMessageDialog(new JFrame(), "An error occured parsing application context. You may " + "have no " + "internet access.\n", "Bagger startup error!", JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog(new JFrame(), "An error occured during startup.\n", "Bagger startup " + "error!", JOptionPane.ERROR_MESSAGE); } System.exit(1); } }
@Override public Object[] extract(BaseData item) { Object[] values = new Object[names.length]; Term extensionTerm = termFactory.findTerm(extension); Map<Term,String> propertyMap = DarwinCorePropertyMap.getPropertyMap(extensionTerm); BeanWrapper beanWrapper = new BeanWrapperImpl(item); for(int i = 0; i < names.length; i++) { String property = names[i]; Term propertyTerm = termFactory.findTerm(property); String propertyName = propertyMap.get(propertyTerm); try { String value = conversionService.convert(beanWrapper.getPropertyValue(propertyName), String.class); if(quoteCharacter == null) { values[i] = value; } else if(value != null) { values[i] = new StringBuilder().append(quoteCharacter).append(value).append(quoteCharacter).toString(); } else { values[i] = new StringBuilder().append(quoteCharacter).append(quoteCharacter).toString(); } } catch(PropertyAccessException pae) { if(quoteCharacter != null) { values[i] = new StringBuilder().append(quoteCharacter).append(quoteCharacter).toString(); } } catch(NullValueInNestedPathException nvinpe) { if(quoteCharacter != null) { values[i] = new StringBuilder().append(quoteCharacter).append(quoteCharacter).toString(); } } } return values; }
public static <T extends Enum<T>> String serializeEnum(ObjectMapper objMapper, Class<T> enumClass) throws BeansException, InvalidPropertyException, JsonProcessingException, PropertyAccessException { T[] enumItems = enumClass.getEnumConstants(); Map<String, Map<String, Object>> enumMap = new LinkedHashMap<>(enumItems.length); Map<String, Object> enumItemMap; Map<String, String> enumItemValueMap; String enumItemKey; BeanWrapper enumItemWrapper; PropertyDescriptor[] enumItemPropDescs; Method enumItemPropGetter; String enumItemPropName; for (T enumItem : enumItems) { enumMap.put((enumItemKey = enumItem.name()), (enumItemMap = new LinkedHashMap<>(2))); enumItemMap.put(KEY_PROP_NAME, enumItemKey); enumItemMap.put(VALUE_PROP_NAME, (enumItemValueMap = new LinkedHashMap<>((enumItemPropDescs = (enumItemWrapper = new BeanWrapperImpl(enumItem)).getPropertyDescriptors()).length))); for (PropertyDescriptor enumItemPropDesc : enumItemPropDescs) { if (((enumItemPropGetter = enumItemPropDesc.getReadMethod()) == null) || !enumItemPropGetter.isAnnotationPresent(JsonProperty.class)) { continue; } enumItemValueMap.put((enumItemPropName = enumItemPropDesc.getName()), enumItemWrapper.getPropertyValue(enumItemPropName).toString()); } } return objMapper.writeValueAsString(enumMap); }
public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) { // Create field error with the exceptions's code, e.g. "typeMismatch". String field = ex.getPropertyName(); String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field); Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field); Object rejectedValue = ex.getValue(); if (rejectedValue != null && rejectedValue.getClass().isArray()) { rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue)); } bindingResult.addError(new FieldError( bindingResult.getObjectName(), field, rejectedValue, true, codes, arguments, ex.getLocalizedMessage())); }
/** * {@inheritDoc} */ @Override public void processPropertyAccessException(PropertyAccessException e, BindingResult bindingResult) { TypeOrFormatMismatchException converted = null; if (Controllers.exceptionMatch(e.getCause().getClass(), PropertyEditingException.class)) { converted = new TypeOrFormatMismatchException(e, bindingResult); super.processPropertyAccessException(converted, bindingResult); } else { super.processPropertyAccessException(e, bindingResult); } }
public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) { // Create field error with the exceptions's code, e.g. "typeMismatch". super.processPropertyAccessException(ex, bindingResult); Object rejectedValue = ex.getValue(); if (!(rejectedValue == null || rejectedValue.equals(""))) { if (ex.getCause() instanceof FormatException) { GlobalVariables.getMessageMap().putError(ex.getPropertyName(), ((FormatException)ex.getCause()).getErrorKey(), new String[] {rejectedValue.toString()}); }else{ GlobalVariables.getMessageMap().putError(ex.getPropertyName(), RiceKeyConstants.ERROR_CUSTOM, new String[] {"Invalid format"}); } } }
public void execute(ControlAccessor controlAccessor, String name) { try { modelPropertyAccessor.setPropertyValue(name, controlAccessor.getControlValue()); } catch (PropertyAccessException pae) { errorProcessor.processPropertyAccessException(pae, bindingResult); } }
/** * Set value on binded object using the property name. * @param value the value to set */ protected void setValue(Object value) { BeanWrapper wrapper = getBeanWrapper(); Object convertedValue = convertIfNecessary(value, wrapper.getPropertyType(this.propertyName)); try { wrapper.setPropertyValue(propertyName, convertedValue); oldValue = value; } catch (PropertyAccessException pae) { log.error(pae); errorProcessor.processPropertyAccessException(component, pae, bindingResult); } }
/** * Add a ControlError instead FieldError to hold component that has failed. * @param control * @param ex * @param bindingResult */ public void processPropertyAccessException(Object control, PropertyAccessException ex, BindingResult bindingResult ) { // Create field error with the exceptions's code, e.g. "typeMismatch". String field = ex.getPropertyName(); String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field); Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field); Object rejectedValue = ex.getValue(); if (rejectedValue != null && rejectedValue.getClass().isArray()) { rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue)); } bindingResult.addError(new ControlError(control, bindingResult.getObjectName(), field, rejectedValue, true, codes, arguments, ex.getLocalizedMessage())); }
/** * Set property, without trowing exceptions on errors * @param bean bean name * @param name name * @param value value */ public static void setProperty(Object bean, String name, Object value) { try { BeanWrapper wrapper = new BeanWrapperImpl(bean); wrapper.setPropertyValue(new PropertyValue(name, value)); } catch (InvalidPropertyException ipe) { log.debug("Bean has no property: " + name); } catch (PropertyAccessException pae) { log.debug("Access Error on property: " + name); } }
protected static String generateMessage(PropertyAccessException e, BindingResult binding) { PropertyEditingException ex = (PropertyEditingException) e.getCause(); return ex.getMessage(); }
/** * Translate the given {@code PropertyAccessException} to an appropriate * error registered on the given {@code Errors} instance. * <p>Note that two error types are available: {@code FieldError} and * {@code ObjectError}. Usually, field errors are created, but in certain * situations one might want to create a global {@code ObjectError} instead. * @param ex the {@code PropertyAccessException} to translate * @param bindingResult the errors object to add the error(s) to. * You can add more than just one error or maybe even ignore it. * The {@code BindingResult} object features convenience utils such as * a {@code resolveMessageCodes} method to resolve an error code. * @see Errors * @see FieldError * @see ObjectError * @see MessageCodesResolver * @see BeanPropertyBindingResult#addError * @see BeanPropertyBindingResult#resolveMessageCodes */ void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult);
/** * コンストラクタです。 * @param e 例外 * @param binding バインディング結果 */ public TypeOrFormatMismatchException(PropertyAccessException e, BindingResult binding) { super(e.getPropertyChangeEvent(), generateMessage(e, binding), null); this.e = e; }