I have to answer the question myself =)
I solved this by using a custom XmlAdapter that will tranlsate the binary data only in one direction. This is still a hack and we are not using this any more. Reasons below.
Here is the adapter:
public class DuplexBase64MarshallAdapter extends XmlAdapter<String, byte[]> {
/**
* running the adapter in half duplex mode means, the incoming data is marshaled but the
* outgoing data not.
*/
public static final boolean HALF_DUPLEX = false;
/**
* Running the adapter in full duplex means, the incoming and outgoing data is marshalled.
*/
public static final boolean FULL_DUPLEX = true;
private boolean isFullDuplexMode;
public DuplexBase64MarshallAdapter() {
this.isFullDuplexMode = HALF_DUPLEX;
}
/**
* Constructor
*
* @param fullDuplex
* use {@link #HALF_DUPLEX} or {@link #FULL_DUPLEX}
*/
public DuplexBase64MarshallAdapter( boolean fullDuplex ) {
this.isFullDuplexMode = fullDuplex;
}
@Override
public byte[] unmarshal( String v ) throws Exception {
return Base64.decode( v );
}
/**
* Return always an empty string. We do not want to deliver binary content here.
*/
@Override
public String marshal( byte[] v ) throws Exception {
if( isFullDuplexMode ) {
return Base64.encodeBytes( v );
}
return "";
}
}
The entity needs to be annotated with this adapter:
@Entity
@XmlRootElement
public class Attachment {
private String name;
private String mimeType;
private byte[] dataPart;
public String getName() {
return name;
}
public void setName( String name ) {
this.name = name;
}
public String getMimeType() {
return mimeType;
}
public void setMimeType( String mimeType ) {
this.mimeType = mimeType;
}
@XmlJavaTypeAdapter( DuplexBase64MarshallAdapter.class )
public byte[] getDataPart() {
return dataPart.clone();
}
public void setDataPart( byte[] dataPart ) {
this.dataPart = dataPart.clone();
}
}
This solution works as exepcted. But there is a drawback: One of the intentions was to not let hibernate load the binary data while processing/loading attachment data. But thats not how it works. The binary data is loaded by hibernate in this case because it is send to the XMLAdapter but not translated into base64 :(