
uk.co.autotrader.traverson.http.entity.BodyFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of traverson4j-hc5 Show documentation
Show all versions of traverson4j-hc5 Show documentation
An Apache HttpComponents 5 client to power traverson4j-core
package uk.co.autotrader.traverson.http.entity;
import org.apache.hc.core5.http.HttpEntity;
import uk.co.autotrader.traverson.http.Body;
import uk.co.autotrader.traverson.http.FormDataBody;
import uk.co.autotrader.traverson.http.SimpleMultipartBody;
import uk.co.autotrader.traverson.http.TextBody;
import java.util.HashMap;
import java.util.Map;
public class BodyFactory {
private static Map, HttpEntityConverter> converters = new HashMap, HttpEntityConverter>();
//TODO: Look at a nicer way to register converters, maybe Reflections? Or ServiceLoaders
static {
register(SimpleMultipartBody.class, new MultipartEntityConverter());
register(TextBody.class, new TextEntityConverter());
register(FormDataBody.class, new FormDataEntityConverter());
}
public static void register(Class extends Body> type, HttpEntityConverter httpEntityConverter) {
converters.put(type, httpEntityConverter);
}
public HttpEntity toEntity(Body body) {
HttpEntityConverter converter = converters.get(body.getClass());
if (converter == null) {
throw new UnsupportedOperationException("Not supported Request body, the supported types are " + converters.keySet());
}
return converter.toEntity(body);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy