org.conqat.engine.service.shared.client.IDeserializationFormat Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-commons Show documentation
Show all versions of teamscale-commons Show documentation
Provides common DTOs for Teamscale
package org.conqat.engine.service.shared.client;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.conqat.engine.commons.util.JsonUtils;
import org.conqat.engine.core.core.ConQATException;
import org.conqat.engine.service.shared.EMimeType;
import org.conqat.engine.service.shared.XmlSerializationUtils;
/**
* Provides a deserialization strategy for service calls.
*/
public interface IDeserializationFormat {
/**
* Returns the MIME type that should be sent to the server as "Accept" header.
* May return empty to indicate that the Accept header should not be set.
*/
Optional getMimeType();
/** Deserializes the given data to an object. */
T deserialize(String serializedData) throws IOException;
/** Constructs a deserializer for the given data type from XML. */
static IDeserializationFormat fromXml(Class type) {
return new IDeserializationFormat() {
@Override
public Optional getMimeType() {
return Optional.of(EMimeType.XML);
}
@Override
public T deserialize(String serializedData) throws IOException {
return XmlSerializationUtils.deserializeFromXML(serializedData, type);
}
};
}
/**
* Constructs a deserializer for the given data type from XML using the aliases
* for deserialization.
*/
static IDeserializationFormat fromXml(Class type, Map> aliases) {
return new IDeserializationFormat() {
@Override
public Optional getMimeType() {
return Optional.of(EMimeType.XML);
}
@Override
public T deserialize(String serializedData) throws IOException {
return XmlSerializationUtils.deserializeFromXMLWithAliases(serializedData, type, aliases);
}
};
}
/** Constructs a deserializer for the given data type from JSON. */
static IDeserializationFormat fromJson(Class elementType) {
return new IDeserializationFormat() {
@Override
public Optional getMimeType() {
return Optional.of(EMimeType.JSON);
}
@Override
public T deserialize(String serializedData) throws IOException {
try {
return JsonUtils.deserializeFromJson(serializedData, elementType);
} catch (ConQATException e) {
throw new IOException(e.getMessage(), e);
}
}
};
}
/** Constructs a deserializer for a list of the given data type from JSON. */
static IDeserializationFormat> fromJsonList(Class elementType) {
return new IDeserializationFormat>() {
@Override
public Optional getMimeType() {
return Optional.of(EMimeType.JSON);
}
@Override
public List deserialize(String serializedData) throws IOException {
try {
return JsonUtils.deserializeFromJson(serializedData, JsonUtils.getJavaListType(elementType));
} catch (ConQATException e) {
throw new IOException(e.getMessage(), e);
}
}
};
}
/** Constructs a deserializer for plain text responses. */
static IDeserializationFormat asPlainText() {
return new IDeserializationFormat() {
@Override
public Optional getMimeType() {
return Optional.of(EMimeType.PLAIN);
}
@Override
public String deserialize(String serializedData) {
return serializedData;
}
};
}
/**
* Constructs a deserializer for a request without setting the Accept header.
* The response is returned as a plain string.
*/
static IDeserializationFormat asUnspecifiedPlainText() {
return new IDeserializationFormat() {
@Override
public Optional getMimeType() {
return Optional.empty();
}
@Override
public String deserialize(String serializedData) {
return serializedData;
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy