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

io.github.mngsk.devicedetector.client.AbstractClientParser Maven / Gradle / Ivy

Go to download

The Universal Device Detection library that parses User Agents and detects devices (desktop, tablet, mobile, tv, cars, console, etc.), clients (browsers, feed readers, media players, PIMs, ...), operating systems, brands and models.

There is a newer version: 1.0.10
Show newest version
package io.github.mngsk.devicedetector.client;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import io.github.mngsk.devicedetector.util.AbstractParser;

public abstract class AbstractClientParser
		extends AbstractParser {

	protected String type;
	protected List regexes;

	public AbstractClientParser(String type, String fixtureFile) {
		this(type, fixtureFile, new ObjectMapper(new YAMLFactory()));
	}

	public AbstractClientParser(String type, String fixtureFile,
			ObjectMapper objectMapper) {
		this.type = type;

		InputStream inputStream = getClass().getClassLoader()
				.getResourceAsStream(fixtureFile);
		Class regexClass = (Class) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
		CollectionType listType = objectMapper.getTypeFactory()
				.constructCollectionType(List.class, regexClass);
		try {
			this.regexes = objectMapper.readValue(inputStream, listType);
		} catch (IOException e) {
			throw new RuntimeException("Could not load " + fixtureFile, e);
		}
	}

	@Override
	public Optional parse(String userAgent) {
		for (T regex : this.regexes) {
			Matcher matcher = regex.getPattern().matcher(userAgent);
			if (matcher.find()) {
				String name = super.buildByMatch(regex.getName(), matcher);

				String version = super.buildVersion(regex.getVersion(),
						matcher);

				return Optional.of(new Client(this.type, name, version));
			}
		}

		return Optional.empty();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy