com.tukeof.common.rest.xstream.XStreamXmlResponseBodyConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-rest Show documentation
Show all versions of common-rest Show documentation
a encapsulated restful java library
The newest version!
package com.tukeof.common.rest.xstream;
import com.thoughtworks.xstream.XStream;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import java.io.IOException;
final class XStreamXmlResponseBodyConverter implements Converter {
private final Class cls;
private final XStream xStream;
XStreamXmlResponseBodyConverter(Class cls, XStream xStream) {
this.cls = cls;
this.xStream = xStream;
}
@Override
@SuppressWarnings("unchecked")
public T convert(ResponseBody value) throws IOException {
try {
this.xStream.processAnnotations(cls);
T instance = cls.newInstance();
this.xStream.fromXML(value.byteStream(), instance);
return instance;
} catch (IllegalAccessException | InstantiationException e) {
throw new RuntimeException(e);
} finally {
value.close();
}
}
}