Search in sources :

Example 1 with Client

use of ua_parser.Client in project keycloak by keycloak.

the class DeviceActivityManager method getDeviceFromUserAgent.

private static DeviceRepresentation getDeviceFromUserAgent(KeycloakSession session) {
    KeycloakContext context = session.getContext();
    if (context.getRequestHeaders() == null) {
        return null;
    }
    String userAgent = context.getRequestHeaders().getHeaderString(HttpHeaders.USER_AGENT);
    if (userAgent == null) {
        return null;
    }
    if (userAgent.length() > USER_AGENT_MAX_LENGTH) {
        logger.warn("Ignoring User-Agent header. Length is above the permitted: " + USER_AGENT_MAX_LENGTH);
        return null;
    }
    DeviceRepresentation current;
    try {
        Client client = UA_PARSER.parse(userAgent);
        current = new DeviceRepresentation();
        current.setDevice(client.device.family);
        String browserVersion = client.userAgent.major;
        if (client.userAgent.minor != null) {
            browserVersion += "." + client.userAgent.minor;
        }
        if (client.userAgent.patch != null) {
            browserVersion += "." + client.userAgent.patch;
        }
        if (browserVersion == null) {
            browserVersion = DeviceRepresentation.UNKNOWN;
        }
        current.setBrowser(client.userAgent.family, browserVersion);
        current.setOs(client.os.family);
        String osVersion = client.os.major;
        if (client.os.minor != null) {
            osVersion += "." + client.os.minor;
        }
        if (client.os.patch != null) {
            osVersion += "." + client.os.patch;
        }
        if (client.os.patchMinor != null) {
            osVersion += "." + client.os.patchMinor;
        }
        current.setOsVersion(osVersion);
        current.setIpAddress(context.getConnection().getRemoteAddr());
        current.setMobile(userAgent.toLowerCase().contains("mobile"));
    } catch (Exception cause) {
        logger.error("Failed to create device info from user agent header", cause);
        return null;
    }
    return current;
}
Also used : DeviceRepresentation(org.keycloak.representations.account.DeviceRepresentation) KeycloakContext(org.keycloak.models.KeycloakContext) Client(ua_parser.Client) IOException(java.io.IOException)

Example 2 with Client

use of ua_parser.Client in project blueocean-plugin by jenkinsci.

the class BrowserAndOperatingSystemAnalyticsProperties method properties.

@Override
public Map<String, Object> properties(TrackRequest req) {
    StaplerRequest httpReq = Stapler.getCurrentRequest();
    if (httpReq == null) {
        return null;
    }
    String userAgent = httpReq.getHeader("User-Agent");
    if (userAgent == null) {
        return null;
    }
    Client client = PARSER.parse(userAgent);
    String browserFamily = client.userAgent.family;
    // If we can't find the browser family then we shouldn't record anything
    if (browserFamily == null) {
        return null;
    }
    Map<String, Object> props = new HashMap<>();
    props.put("browserFamily", browserFamily);
    String browserVersionMajor = client.userAgent.major;
    // Versions are useful if they are available
    if (isNotEmpty(browserVersionMajor)) {
        props.put("browserVersionMajor", browserVersionMajor);
    }
    String browserVersionMinor = client.userAgent.minor;
    if (isNotEmpty(browserVersionMinor)) {
        props.put("browserVersionMinor", browserVersionMinor);
    }
    // If the operating system is available lets use that
    String operatingSystemFamily = client.os.family;
    if (isNotEmpty(operatingSystemFamily)) {
        props.put("osFamily", operatingSystemFamily);
        String osVersionMajor = client.os.major;
        if (isNotEmpty(osVersionMajor)) {
            props.put("osVersionMajor", osVersionMajor);
        }
        String osVersionMinor = client.os.minor;
        if (isNotEmpty(osVersionMinor)) {
            props.put("osVersionMinor", osVersionMinor);
        }
    }
    return props;
}
Also used : HashMap(java.util.HashMap) StaplerRequest(org.kohsuke.stapler.StaplerRequest) Client(ua_parser.Client)

Example 3 with Client

use of ua_parser.Client in project LogHub by fbacchella.

the class UserAgent method processMessage.

@Override
public boolean processMessage(Event event, String field, String destination) {
    if (event.get(field) == null) {
        return false;
    }
    String userAgent = event.get(field).toString();
    Client c = uaParser.parse(userAgent);
    Map<String, Object> ua = new HashMap<>(3);
    Helpers.putNotEmpty(ua, "device", c.device.family);
    Map<String, Object> os = new HashMap<>(5);
    Helpers.putNotEmpty(os, "family", c.os.family);
    Helpers.putNotEmpty(os, "major", c.os.major);
    Helpers.putNotEmpty(os, "minor", c.os.minor);
    Helpers.putNotEmpty(os, "patch", c.os.patch);
    Helpers.putNotEmpty(os, "patchMinor", c.os.patchMinor);
    if (os.size() > 0) {
        ua.put("os", os);
    }
    Map<String, Object> agent = new HashMap<>(4);
    Helpers.putNotEmpty(agent, "family", c.userAgent.family);
    Helpers.putNotEmpty(agent, "major", c.userAgent.major);
    Helpers.putNotEmpty(agent, "minor", c.userAgent.minor);
    Helpers.putNotEmpty(agent, "patch", c.userAgent.patch);
    if (agent.size() > 0) {
        ua.put("userAgent", agent);
    }
    event.put(destination, ua);
    return true;
}
Also used : HashMap(java.util.HashMap) Client(ua_parser.Client)

Aggregations

Client (ua_parser.Client)3 HashMap (java.util.HashMap)2 IOException (java.io.IOException)1 KeycloakContext (org.keycloak.models.KeycloakContext)1 DeviceRepresentation (org.keycloak.representations.account.DeviceRepresentation)1 StaplerRequest (org.kohsuke.stapler.StaplerRequest)1