0

I want to register a custom property editor for one of the properties of a domain class, the class is something like this

class Accessory{
  String name
  byte[] image
}

From the client side i am sending a base64 encoded string for the image, now i want this string to automatically convert to byte array at the time of binding

My property editor class looks like this

import java.beans.PropertyEditorSupport
import org.apache.commons.codec.binary.Base64
class CustomAccessoryImageEditor extends PropertyEditorSupport{


    String getAsText() {
        value.toString()
    }

    void setAsText(String text) {
        String encodedImage = text?:""
        byte[] imageBytes = decodeImageToBytes(encodedImage)
        if(imageBytes.size()){
            value = imageBytes
        }


    }

    byte[] decodeImageToBytes(String encodedImage){
            return Base64.decodeBase64(encodedImage)
        }
}

I am not able to find a way to register this editor properly.

Right now i have something like this in my registrar class

registry.registerCustomEditor(byte, Accessory.image, new CustomAccessoryImageEditor())

but when i run this, i get an error message saying cannot find property image on class Accessory

I have two questions, 1. Is it possible to have a property editor for a specific property of a class ? 2. If yes then how to specify the property path ?

vijay tyagi
  • 2,226
  • 3
  • 20
  • 31
  • `registerCustomEditor(byte[], 'image', new Custom....)` should register an editor for all properties named _image_ of type _byte[]_, but I'm not sure there's any way to limit it to one particular class. – Ian Roberts Dec 12 '12 at 12:03
  • yeah, i know that, actually it's not taking the property path - 'image' into consideration, while binding it says cannot find property image on class CustomAccessoryImageEditor . It would be good if i can limit this to property with name image – vijay tyagi Dec 12 '12 at 12:17

1 Answers1

2

I don't think it is possible to have a property editor for a specific property of a class. But if the image property was of type Image (a wrapper for byte[]) then you could register an editor for that and Spring would bind an encoded text representation to the custom wrapper.

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • thanks for replying dave, is there any workaround for achieving this ? i cannot have a wrapper, also want to know if there is a slightly more detailed documentation for this data binding, springsource's doc is not enough – vijay tyagi Dec 12 '12 at 10:26
  • PropertyEditor is a standard JDK feature, so Spring doesn't document it. There are alternatives in Spring though (see Chapter 6 or the user guide http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html). You can probably use a Converter for String to byte[] - the question then will be whether in your use case it can be injected in the right place. We don't know enough about your application (and I don't know enough about Grails) to give you any more pointers than that. – Dave Syer Dec 12 '12 at 13:52