YoutrackUnmarshaller.java 3.1 KB
package pl.itcrowd.youtrack.api;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pl.itcrowd.youtrack.api.exceptions.YoutrackAPIException;
import pl.itcrowd.youtrack.api.rest.ErrorType;
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.Serializable;
import java.io.StringReader;

public final class YoutrackUnmarshaller {

    private static Log LOG = LogFactory.getLog(YoutrackUnmarshaller.class);

    private YoutrackUnmarshaller()
    {
    }

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

    public static String unmarshalError(Reader reader) throws JAXBException, IOException
    {
        String content = IOUtils.toString(reader);
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
            @SuppressWarnings("unchecked")
            final JAXBElement<ErrorType> element = (JAXBElement<ErrorType>) jaxbContext.createUnmarshaller().unmarshal(new StringReader(content));
            final ErrorType value = element.getValue();
            final int contentSize = value.getContent().size();
            if (contentSize == 0) {
                return "";
            } else if (contentSize == 1) {
                final Serializable serializable = value.getContent().get(0);
                if (serializable instanceof JAXBElement) {
                    if ("message".equals(((JAXBElement) serializable).getName().getLocalPart())) {
                        final Object messageValue = ((JAXBElement) serializable).getValue();
                        return messageValue == null ? "" : messageValue.toString();
                    }
                } else {
                    return serializable == null ? "" : serializable.toString();
                }
            } else if (contentSize == 2) {
                for (Serializable serializable : value.getContent()) {
                    if (serializable instanceof JAXBElement) {
                        if ("message".equals(((JAXBElement) serializable).getName().getLocalPart())) {
                            final Object messageValue = ((JAXBElement) serializable).getValue();
                            return messageValue == null ? "" : messageValue.toString();
                        }
                    }
                }
            }
            throw new YoutrackAPIException("Cannot unserialize error.\n" + content);
        } catch (JAXBException e) {
            LOG.error("Cannot unmarshal error.\n" + content, e);
            throw e;
        }
    }

    public static Object unmarshall(String string) throws JAXBException
    {
        return unmarshall(new StringReader(string));
    }

    public static Object unmarshall(Reader reader) throws JAXBException
    {
        return JAXBContext.newInstance(ObjectFactory.class).createUnmarshaller().unmarshal(reader);
    }
}