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

com.liferay.headless.batch.engine.internal.jaxrs.application.ObjectMessageBodyReader Maven / Gradle / Ivy

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.headless.batch.engine.internal.jaxrs.application;

import java.io.InputStream;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import java.nio.charset.StandardCharsets;

import java.util.Scanner;

import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;

import org.osgi.service.component.annotations.Component;

/**
 * @author Javier Gamarra
 */
@Component(
	property = {
		"osgi.jaxrs.application.select=(osgi.jaxrs.name=Liferay.Headless.Batch.Engine)",
		"osgi.jaxrs.extension=true",
		"osgi.jaxrs.name=Liferay.Headless.Batch.Engine.ObjectMessageBodyReader"
	},
	service = MessageBodyReader.class
)
@Consumes({"application/x-ndjson", "text/csv"})
@Provider
public class ObjectMessageBodyReader implements MessageBodyReader {

	@Override
	public boolean isReadable(
		Class clazz, Type type, Annotation[] annotations, MediaType mediaType) {

		if (mediaType.equals(MediaType.valueOf("application/x-ndjson")) ||
			mediaType.equals(MediaType.valueOf("text/csv"))) {

			return true;
		}

		return false;
	}

	@Override
	public Object readFrom(
			Class clazz, Type type, Annotation[] annotations,
			MediaType mediaType, MultivaluedMap multivaluedMap,
			InputStream inputStream)
		throws WebApplicationException {

		String text = null;

		try (Scanner scanner = new Scanner(
				inputStream, StandardCharsets.UTF_8.name())) {

			scanner.useDelimiter("\\A");

			text = scanner.next();
		}

		return text;
	}

}