Search in sources :

Example 1 with ReadableUserAgent

use of net.sf.uadetector.ReadableUserAgent in project ART-TIME by Artezio.

the class CheckSupportedBrowsersFilterTest method testParseUserAgent_ifHeaderParamUserAgentIsEmpty.

@Test
public void testParseUserAgent_ifHeaderParamUserAgentIsEmpty() {
    expect(request.getHeader("User-Agent")).andReturn("");
    replay(request);
    ReadableUserAgent actual = filter.parseUserAgent(request);
    verify(request);
    assertSame(UserAgent.EMPTY, actual);
}
Also used : ReadableUserAgent(net.sf.uadetector.ReadableUserAgent) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with ReadableUserAgent

use of net.sf.uadetector.ReadableUserAgent in project apex-malhar by apache.

the class UserAgentExtractor method extractInformation.

@Override
public Map<String, Object> extractInformation(Object value) {
    Map<String, Object> m = new HashMap<String, Object>();
    ReadableUserAgent agent = parser.parse(value.toString());
    m.put("browser", agent.getName());
    m.put("os", agent.getOperatingSystem().getName());
    return m;
}
Also used : ReadableUserAgent(net.sf.uadetector.ReadableUserAgent) HashMap(java.util.HashMap)

Example 3 with ReadableUserAgent

use of net.sf.uadetector.ReadableUserAgent in project cryptonomica by Cryptonomica.

the class UserTools method registerLogin.

// end of ensureGoogleAuth method
public static Login registerLogin(final HttpServletRequest httpServletRequest, final User googleUser) {
    // Register login of this Cryptonomica User:
    Login login = new Login();
    // IP
    String userIP = httpServletRequest.getHeader("X-FORWARDED-FOR");
    if (userIP == null) {
        userIP = httpServletRequest.getRemoteAddr();
    }
    // Get an UserAgentStringParser and analyze the requesting client
    // see example on: http://uadetector.sourceforge.net/usage.html
    UserAgentStringParser parser = UADetectorServiceFactory.getResourceModuleParser();
    ReadableUserAgent agent = parser.parse(httpServletRequest.getHeader("User-Agent"));
    String userBrowser = agent.getName();
    String userOS = agent.getOperatingSystem().getName();
    // 
    String userProviderOrg = null;
    String userProviderHost = null;
    String country = null;
    String region = null;
    String city = null;
    JSONObject ipInfoIoJSON = GetJSONfromURL.getIpInfoIo(userIP);
    if (ipInfoIoJSON != null) {
        try {
            userProviderOrg = ipInfoIoJSON.getString("org");
        } catch (JSONException e) {
            userProviderOrg = e.getMessage();
        }
        try {
            userProviderHost = ipInfoIoJSON.getString("hostname");
        } catch (JSONException e) {
            userProviderHost = e.getMessage();
        }
        try {
            country = ipInfoIoJSON.getString("country");
        } catch (JSONException e) {
            country = e.getMessage();
        }
        try {
            region = ipInfoIoJSON.getString("region");
        } catch (JSONException e) {
            region = e.getMessage();
        }
        try {
            city = ipInfoIoJSON.getString("city");
        } catch (JSONException e) {
            city = e.getMessage();
        }
    }
    login.setCryptonomicaUserKey(Key.create(CryptonomicaUser.class, googleUser.getUserId()));
    login.setLoginEmail(new Email(googleUser.getEmail()));
    login.setLoginDate(new Date());
    login.setIP(userIP);
    login.setUserBrowser(userBrowser);
    login.setUserOS(userOS);
    login.setHostname(userProviderHost);
    login.setCity(city);
    login.setRegion(region);
    login.setCountry(country);
    login.setProvider(userProviderOrg);
    // --- save Login:
    Key<Login> loginKey = ofy().save().entity(login).now();
    login = ofy().load().key(loginKey).now();
    return login;
}
Also used : ReadableUserAgent(net.sf.uadetector.ReadableUserAgent) Email(com.google.appengine.api.datastore.Email) JSONObject(org.json.JSONObject) UserAgentStringParser(net.sf.uadetector.UserAgentStringParser) JSONException(org.json.JSONException) Login(net.cryptonomica.entities.Login) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) Date(java.util.Date)

Example 4 with ReadableUserAgent

use of net.sf.uadetector.ReadableUserAgent in project ART-TIME by Artezio.

the class CheckSupportedBrowsersFilterTest method testParseUserAgent.

@Test
public void testParseUserAgent() {
    UserAgentStringParser parser = createMock(UserAgentStringParser.class);
    PowerMock.mockStatic(UADetectorServiceFactory.class);
    expect(request.getHeader("User-Agent")).andReturn("Mozilla/5.0");
    expect(UADetectorServiceFactory.getResourceModuleParser()).andReturn(parser);
    expect(parser.parse("Mozilla/5.0")).andReturn(userAgent);
    PowerMock.replayAll(UADetectorServiceFactory.class, parser, request);
    ReadableUserAgent actual = filter.parseUserAgent(request);
    PowerMock.verifyAll();
    assertSame(userAgent, actual);
}
Also used : ReadableUserAgent(net.sf.uadetector.ReadableUserAgent) UserAgentStringParser(net.sf.uadetector.UserAgentStringParser) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ReadableUserAgent (net.sf.uadetector.ReadableUserAgent)4 UserAgentStringParser (net.sf.uadetector.UserAgentStringParser)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Email (com.google.appengine.api.datastore.Email)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 CryptonomicaUser (net.cryptonomica.entities.CryptonomicaUser)1 Login (net.cryptonomica.entities.Login)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1