Marek
Marek

Reputation: 457

jaxb base64binary encoding / decoding

I needed to include MimeMessage object in service response. So basicly i decided to use base64binary type. Message is prepared by code:

//message is a MimeMessage type
ByteArrayOutputStream baos = new ByteArrayOutputStream();
message.writeTo(baos);
byte[] bytes = baos.toByteArray();
content.setMessageContent(bytes);

and in schema it is represented:

  <xs:simpleType name="MessageContent">
    <xs:restriction base="xs:base64Binary">
      <xs:whiteSpace value="collapse" fixed="true"/>
    </xs:restriction>   </xs:simpleType>

I am getting response with characters that are not encoding properly. for example %3D is transformd to =3D, but should be just =.

I think it may be related to coding, but both marhaller and unmarshaller are using UTF-8 as default.

Any tip will help, Thanks Marek.

Upvotes: 3

Views: 4433

Answers (1)

Petru Gardea
Petru Gardea

Reputation: 21658

I quickly tried this on my end and it works; there's something else you didn't post that might cause your issue. I've used NetBeans 7.1 to generate the classes (all is out of the box); try it too, and see if you get the same results. Then please let me know...

XSD:

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Message">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="content" type="MessageContent"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:simpleType name="MessageContent">
        <xsd:restriction base="xsd:base64Binary">
            <xsd:whiteSpace fixed="true" value="collapse"/>
        </xsd:restriction> 
    </xsd:simpleType>
</xsd:schema>

The Java code snippet:

JAXBContext jc = JAXBContext.newInstance(("org.tempuri.xmlschema"));
Marshaller m = jc.createMarshaller();   
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
ObjectFactory o = new ObjectFactory();
Message msg = o.createMessage();
byte[] data = new byte[] {0x45, 0x31};
msg.setContent(data);
m.marshal(msg, System.out);

The result:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Message xmlns="http://tempuri.org/XMLSchema.xsd">
    <content>RTE=</content>
</Message>

Upvotes: 1

Related Questions