ErrorUnmarshaller.java 1.26 KB
package pl.itcrowd.youtrack.api;

import org.apache.commons.io.IOUtils;
import pl.itcrowd.youtrack.api.rest.ObjectFactory;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

//TODO methods from this class should be probably merged with YoutrackUnmarshaller
public final class ErrorUnmarshaller {

    public static String unmarshal(String string) throws JAXBException, IOException
    {
        return unmarshal(new StringReader(string));
    }

    public static String unmarshal(Reader reader) throws JAXBException, IOException
    {
        String content = IOUtils.toString(reader);
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
            @SuppressWarnings("unchecked")
            final JAXBElement<String> element = (JAXBElement<String>) jaxbContext.createUnmarshaller().unmarshal(new StringReader(content));
            return element.getValue();
        } catch (JAXBException e) {
//            TODO we need logging here
            System.err.println("Cannot unmarshal input stream.\n" + content + e);
            throw e;
        }
    }

    private ErrorUnmarshaller()
    {
    }
}