bernard
authored
|
1
|
package pl.itcrowd.youtrack.api;
|
bernard
authored
|
2
|
|
bernard
authored
|
3
4
5
6
7
|
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;
|
bernard
authored
|
8
|
import pl.itcrowd.youtrack.api.rest.ObjectFactory;
|
bernard
authored
|
9
10
|
import javax.xml.bind.JAXBContext;
|
bernard
authored
|
11
|
import javax.xml.bind.JAXBElement;
|
bernard
authored
|
12
|
import javax.xml.bind.JAXBException;
|
bernard
authored
|
13
|
import java.io.IOException;
|
bernard
authored
|
14
|
import java.io.Reader;
|
bernard
authored
|
15
|
import java.io.Serializable;
|
bernard
authored
|
16
17
18
19
|
import java.io.StringReader;
public final class YoutrackUnmarshaller {
|
bernard
authored
|
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
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;
}
}
|
bernard
authored
|
69
70
71
72
73
74
75
76
77
78
|
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);
}
}
|