All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tukeof.common.rest.xstream.XStreamXmlRequestBodyConverter Maven / Gradle / Ivy

The newest version!
package com.tukeof.common.rest.xstream;

import com.thoughtworks.xstream.XStream;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import retrofit2.Converter;

import java.io.IOException;
import java.io.OutputStreamWriter;

final class XStreamXmlRequestBodyConverter implements Converter {

    private static final MediaType MEDIA_TYPE = MediaType.parse("application/xml; charset=UTF-8");
    private static final String CHARSET = "UTF-8";

    private final XStream xStream;

    XStreamXmlRequestBodyConverter(XStream xStream) {
        this.xStream = xStream;
    }

    @Override
    public RequestBody convert(T value) throws IOException {

        Buffer buffer = new Buffer();

        try {
            OutputStreamWriter osw = new OutputStreamWriter(buffer.outputStream(), CHARSET);
            xStream.toXML(value, osw);
            osw.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy