/** * We have inbound data and a type that we need to convert it into, this method performs the * conversion. */ protected Object convert( String val, Class<?> propType, InboundContext inboundContext, Property property) { String[] split = ConvertUtil.splitInbound(val); String splitValue = split[ConvertUtil.INBOUND_INDEX_VALUE]; String splitType = split[ConvertUtil.INBOUND_INDEX_TYPE]; InboundVariable nested = new InboundVariable(inboundContext, null, splitType, splitValue); nested.dereference(); Property incc = createTypeHintContext(inboundContext, property); return converterManager.convertInbound(propType, nested, incc); }
/* (non-Javadoc) * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext) */ public Object convertInbound(Class<?> paramType, InboundVariable data) throws ConversionException { if (data.isNull()) { return null; } String uriString = LocalUtil.urlDecode(data.getValue()); try { return new URI(uriString); } catch (URISyntaxException ex) { log.warn("Failed to create URL from string '" + uriString + "'. Returning null"); return null; } }
/** We're using setter injection, create and populate a bean using property setters */ private Object createUsingSetterInjection( Class<?> paramType, InboundVariable data, Map<String, String> tokens) throws InstantiationException, IllegalAccessException { Object bean; if (instanceType != null) { bean = instanceType.newInstance(); } else { bean = paramType.newInstance(); } // We should put the new object into the working map in case it // is referenced later nested down in the conversion process. if (instanceType != null) { data.getContext().addConverted(data, instanceType, bean); } else { data.getContext().addConverted(data, paramType, bean); } Map<String, Property> properties = getPropertyMapFromObject(bean, false, true); for (Entry<String, String> entry : tokens.entrySet()) { String key = entry.getKey(); // TODO: We don't URL decode method names we probably should. This is $dwr // TODO: We should probably have stripped these out already if (key.startsWith("%24dwr")) { continue; } Property property = properties.get(key); if (property == null) { log.warn( "Missing setter: " + paramType.getName() + ".set" + Character.toTitleCase(key.charAt(0)) + key.substring(1) + "() to match javascript property: " + key + ". Check include/exclude rules and overloaded methods."); continue; } Object output = convert(entry.getValue(), property.getPropertyType(), data.getContext(), property); property.setValue(bean, output); } return bean; }
/* (non-Javadoc) * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext) */ public Object convertInbound(Class<?> paramType, InboundVariable data) throws ConversionException { if (data.isNull()) { return null; } String value = data.getValue(); if (value == null) { throw new NullPointerException(data.toString()); } // If the text is null then the whole bean is null if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) { return null; } if (!value.startsWith(ProtocolConstants.INBOUND_MAP_START) || !value.endsWith(ProtocolConstants.INBOUND_MAP_END)) { log.warn( "Expected object while converting data for " + paramType.getName() + " in " + data.getContext().getCurrentProperty() + ". Passed: " + value); throw new ConversionException(paramType, "Data conversion error. See logs for more details."); } value = value.substring(1, value.length() - 1); try { // Loop through the properties passed in Map<String, String> tokens = extractInboundTokens(paramType, value); if (paramsString == null) { return createUsingSetterInjection(paramType, data, tokens); } else { return createUsingConstructorInjection(paramType, data, tokens); } } catch (InvocationTargetException ex) { throw new ConversionException(paramType, ex.getTargetException()); } catch (ConversionException ex) { throw ex; } catch (Exception ex) { throw new ConversionException(paramType, ex); } }
/** We're using constructor injection, create and populate a bean using a constructor */ private Object createUsingConstructorInjection( Class<?> paramType, InboundVariable data, Map<String, String> tokens) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?> type; if (instanceType != null) { type = instanceType; } else { type = paramType; } // Find a constructor to match List<Class<?>> parameterTypes = new ArrayList<Class<?>>(); for (Pair<Class<?>, String> parameter : parameters) { parameterTypes.add(parameter.left); } Class<?>[] paramTypeArray = parameterTypes.toArray(new Class<?>[parameterTypes.size()]); Constructor<?> constructor; try { constructor = type.getConstructor(paramTypeArray); } catch (NoSuchMethodException ex) { log.error( "Can't find a constructor for " + type.getName() + " with params " + parameterTypes); throw ex; } List<Object> arguments = new ArrayList<Object>(); int paramNum = 0; for (Pair<Class<?>, String> parameter : parameters) { String argument = tokens.get(parameter.right); ConstructorProperty property = new ConstructorProperty(constructor, parameter.right, paramNum); Object output = convert(argument, parameter.left, data.getContext(), property); arguments.add(output); paramNum++; } // log.debug("Using constructor injection for: " + constructor); Object[] argArray = arguments.toArray(new Object[arguments.size()]); try { return constructor.newInstance(argArray); } catch (InstantiationException ex) { log.error( "Error building using constructor " + constructor.getName() + " with arguments " + argArray); throw ex; } catch (IllegalAccessException ex) { log.error( "Error building using constructor " + constructor.getName() + " with arguments " + argArray); throw ex; } catch (InvocationTargetException ex) { log.error( "Error building using constructor " + constructor.getName() + " with arguments " + argArray); throw ex; } }