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

com.abubusoft.kripton.retrofit2.KriptonRequestBodyConverter Maven / Gradle / Ivy

There is a newer version: 8.2.0-rc.4
Show newest version
/*******************************************************************************
 * Copyright 2018 Francesco Benincasa ([email protected])
 * 
 * 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.abubusoft.kripton.retrofit2;

import java.io.IOException;

import com.abubusoft.kripton.BinderContext;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import retrofit2.Converter;

// TODO: Auto-generated Javadoc
/**
 * The Class KriptonRequestBodyConverter.
 *
 * @param  the generic type
 */
final class KriptonRequestBodyConverter implements Converter {
	
	/** The Constant MEDIA_TYPE. */
	private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
	
	/** The binder context. */
	private BinderContext binderContext;

	/**
	 * Instantiates a new kripton request body converter.
	 *
	 * @param binderContext the binder context
	 * @param clazz the clazz
	 */
	KriptonRequestBodyConverter(BinderContext binderContext, Class clazz) {
		this.binderContext = binderContext;
	}

	/* (non-Javadoc)
	 * @see retrofit2.Converter#convert(java.lang.Object)
	 */
	@Override
	public RequestBody convert(T value) throws IOException {
		Buffer buffer = new Buffer();

		try {
			binderContext.serialize(value, buffer.outputStream());
			return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			buffer.close();
		}

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy