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

org.conqat.engine.service.shared.client.ISerializationFormat Maven / Gradle / Ivy

package org.conqat.engine.service.shared.client;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.conqat.engine.commons.util.JsonUtils;
import org.conqat.engine.service.shared.EMimeType;
import org.conqat.engine.service.shared.XmlSerializationUtils;

/**
 * Provides a serialization strategy for service calls.
 */
public interface ISerializationFormat {

	/**
	 * Returns the MIME type that should be sent to the server as "Content-Type"
	 * header. May return empty to indicate that the Content-Type header should not
	 * be set.
	 */
	Optional getMimeType();

	/** Returns the serialized data. */
	String serialize(T data);

	/** Serializes the given data and wraps it into an HttpEntity. */
	default HttpEntity toHttpEntity(T data) {
		return new StringEntity(serialize(data), StandardCharsets.UTF_8);
	}

	/** Constructs a generic XML serializer. */
	static  ISerializationFormat toXml() {
		return new ISerializationFormat() {
			@Override
			public Optional getMimeType() {
				return Optional.of(EMimeType.XML);
			}

			@Override
			public String serialize(T data) {
				return XmlSerializationUtils.serializeToXML(data);
			}
		};
	}

	/** Constructs a generic JSON serializer. */
	static  ISerializationFormat toJson() {
		return new ISerializationFormat() {
			@Override
			public Optional getMimeType() {
				return Optional.of(EMimeType.JSON);
			}

			@Override
			public String serialize(T data) {
				return JsonUtils.serializeToJSON(data);
			}
		};
	}

	/**
	 * Constructs a serializer that just passes the given data without any
	 * serialization as plain text.
	 */
	static ISerializationFormat asPlainText() {
		return new ISerializationFormat() {
			@Override
			public Optional getMimeType() {
				return Optional.of(EMimeType.PLAIN);
			}

			@Override
			public String serialize(String data) {
				return data;
			}
		};
	}

	/**
	 * Constructs a serializer, which expects a List and sends them
	 * as url encoded form data.
	 */
	static ISerializationFormat> toUrlEncodedFormData() {
		return new ISerializationFormat>() {
			@Override
			public Optional getMimeType() {
				return Optional.of(EMimeType.URL_ENCODED_FORM_DATA);
			}

			@Override
			public String serialize(List data) {
				return URLEncodedUtils.format(data, StandardCharsets.UTF_8);
			}
		};
	}

	/**
	 * Constructs a serializer, which expects a raw HttpEntity that should be sent
	 * as multipart form data.
	 */
	static ISerializationFormat toMultiPart() {
		return new ISerializationFormat() {
			@Override
			public Optional getMimeType() {
				return Optional.empty();
			}

			@Override
			public String serialize(HttpEntity data) {
				throw new IllegalStateException("This should never be called directly. Call via toHttpEntity().");
			}

			@Override
			public HttpEntity toHttpEntity(HttpEntity data) {
				return data;
			}
		};
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy