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

com.paypal.http.serializer.Text Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package com.paypal.http.serializer;

import com.paypal.http.HttpRequest;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import static java.nio.charset.StandardCharsets.UTF_8;

public class Text implements Serializer {

	@Override
	public String contentType() {
		return "^text\\/.*";
	}

	@Override
	public byte[] encode(HttpRequest request) throws IOException {
		if (request.requestBody() instanceof String) {
			return ((String) request.requestBody()).getBytes(UTF_8);
		} else {
			return request.requestBody().toString().getBytes(UTF_8);
		}
	}

	@Override
	public  T decode(String source, Class cls) throws IOException {
		if (!cls.isAssignableFrom(String.class)) {
			throw new UnsupportedEncodingException("Text class unable to return types other than String");
		}

		return (T) source;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy