Blame view

src/main/java/pl/itcrowd/youtrack/api/ErrorUnmarshaller.java 1.4 KB
bernard authored
1
package pl.itcrowd.youtrack.api;
bernard authored
2 3

import org.apache.commons.io.IOUtils;
bernard authored
4
import pl.itcrowd.youtrack.api.rest.ObjectFactory;
bernard authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

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 {
// -------------------------- STATIC METHODS --------------------------

    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;
        }
    }

// --------------------------- CONSTRUCTORS ---------------------------

    private ErrorUnmarshaller()
    {
    }
}