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