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

com.teamscale.commons.service.client.IDeserializationFormat Maven / Gradle / Ivy

There is a newer version: 2025.1.0-rc2
Show newest version
/*
 * Copyright (c) CQSE GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.teamscale.commons.service.client;

import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.Optional;

import org.conqat.engine.commons.util.JsonUtils;
import org.conqat.engine.core.core.ConQATException;

import com.fasterxml.jackson.databind.type.MapType;
import com.google.common.io.CharStreams;
import com.teamscale.commons.service.EMimeType;

/**
 * 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(Reader serializedData) throws IOException;

	/** 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(Reader serializedData) throws IOException {
				try {
					return JsonUtils.deserializeFromJson(serializedData, elementType);
				} catch (ConQATException e) {
					throw new IOException(e.getMessage(), e);
				}
			}
		};
	}

	/** Constructs a deserializer for the given map type from JSON. */
	static  IDeserializationFormat fromJson(MapType mapType) {
		return new IDeserializationFormat() {
			@Override
			public Optional getMimeType() {
				return Optional.of(EMimeType.JSON);
			}

			@Override
			public T deserialize(Reader serializedData) throws IOException {
				try {
					return JsonUtils.deserializeFromJson(serializedData, mapType);
				} 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(Reader 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(Reader serializedData) throws IOException {
				return CharStreams.toString(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(Reader serializedData) throws IOException {
				return CharStreams.toString(serializedData);
			}
		};
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy