001package co.codewizards.cloudstore.core.auth;
002
003import static co.codewizards.cloudstore.core.util.Util.*;
004
005import java.io.ByteArrayInputStream;
006import java.io.ByteArrayOutputStream;
007
008import javax.xml.bind.JAXBContext;
009import javax.xml.bind.JAXBException;
010import javax.xml.bind.Marshaller;
011import javax.xml.bind.Unmarshaller;
012
013public class SignedAuthTokenIO {
014        public byte[] serialise(SignedAuthToken signedAuthToken) {
015                assertNotNull("signedAuthToken", signedAuthToken);
016                try {
017                        JAXBContext context = createContext();
018                        Marshaller marshaller = context.createMarshaller();
019                        ByteArrayOutputStream os = new ByteArrayOutputStream();
020                        marshaller.marshal(signedAuthToken, os);
021                        return os.toByteArray();
022                } catch (JAXBException e) {
023                        throw new RuntimeException(e);
024                }
025        }
026
027        public SignedAuthToken deserialise(byte[] signedAuthTokenData) {
028                assertNotNull("signedAuthTokenData", signedAuthTokenData);
029                try {
030                        JAXBContext context = createContext();
031                        Unmarshaller unmarshaller = context.createUnmarshaller();
032                        Object object = unmarshaller.unmarshal(new ByteArrayInputStream(signedAuthTokenData));
033                        return (SignedAuthToken) object;
034                } catch (JAXBException e) {
035                        throw new RuntimeException(e);
036                }
037        }
038
039        private JAXBContext createContext() throws JAXBException {
040                return JAXBContext.newInstance(SignedAuthToken.class);
041        }
042}