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

io.antmedia.valves.DataTransferValve Maven / Gradle / Ivy

Go to download

Ant Media Server supports RTMP, RTSP, MP4, HLS, WebRTC, Adaptive Streaming, etc.

There is a newer version: 2.13.2
Show newest version
package io.antmedia.valves;

import java.io.IOException;

import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.WebApplicationContext;

import io.antmedia.AntMediaApplicationAdapter;
import io.antmedia.AppSettings;
import io.antmedia.analytic.model.PlayerStatsEvent;
import io.antmedia.filter.TokenFilterManager;
import io.antmedia.logger.LoggerUtils;
import io.antmedia.rest.RestServiceBase;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.ws.rs.HttpMethod;

/**
 * This class just logs the data transfered for http requests to 
 * @author mekya
 *
 */
public class DataTransferValve extends ValveBase {

	@Override
	public void invoke(Request request, Response response) throws IOException, ServletException {

		getNext().invoke(request, response);
		
		String streamId = TokenFilterManager.getStreamId(request.getRequestURI());
		String method = request.getMethod();

		if (StringUtils.isNotBlank(streamId) && (HttpMethod.GET.equals(method) || HttpMethod.HEAD.equals(method))) 
		{
			String subscriberId = ((HttpServletRequest) request).getParameter("subscriberId");

			if (subscriberId != null) {
				subscriberId = subscriberId.replaceAll(RestServiceBase.REPLACE_CHARS, "_");
			}
			
			String clientIP = request.getRemoteAddr().replaceAll(RestServiceBase.REPLACE_CHARS, "_");

			long bytesWritten = response.getBytesWritten(false);
			
			ConfigurableWebApplicationContext context = (ConfigurableWebApplicationContext) request.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

			PlayerStatsEvent playerStatsEvent = new PlayerStatsEvent();
			playerStatsEvent.setStreamId(streamId);
			playerStatsEvent.setUri(request.getRequestURI());
			playerStatsEvent.setSubscriberId(subscriberId);
			playerStatsEvent.setApp(((AntMediaApplicationAdapter)context.getBean(AntMediaApplicationAdapter.BEAN_NAME)).getScope().getName());
			playerStatsEvent.setByteTransferred(bytesWritten);
			playerStatsEvent.setClientIP(clientIP);
			
			
			log(playerStatsEvent);
			
		}
	}
	
	public void log(PlayerStatsEvent playerStatsEvent) {
		LoggerUtils.logAnalyticsFromServer(playerStatsEvent);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy