Skip to content

Commit

Permalink
Merge pull request #23 from WURFL/develop
Browse files Browse the repository at this point in the history
WURFL cloud client Java 1.0.14
  • Loading branch information
andreacastello committed Mar 25, 2020
2 parents f58a426 + d455b55 commit 22e4cc4
Show file tree
Hide file tree
Showing 12 changed files with 1,100 additions and 46 deletions.
18 changes: 11 additions & 7 deletions code/client-java.iml
Expand Up @@ -12,19 +12,23 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: org.testng:testng:6.9.8" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.beust:jcommander:1.48" level="project" />
<orderEntry type="library" name="Maven: com.jsoniter:jsoniter:0.9.23" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.testng:testng:6.9.8" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.beust:jcommander:1.48" level="project" />
<orderEntry type="library" name="Maven: com.jsoniter:jsoniter:0.9.23" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.testng:testng:6.9.8" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.beust:jcommander:1.48" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet:servlet-api:2.4" level="project" />
<orderEntry type="library" name="Maven: net.sf.ehcache:ehcache-core:2.5.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.10.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.10.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.10" level="project" />
<orderEntry type="library" name="Maven: com.jsoniter:jsoniter:0.9.23" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.6.4" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-core:1.0.0" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-classic:1.0.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.testng:testng:5.14.10" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:3.8.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.testng:testng:6.9.8" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.beanshell:bsh:2.0b4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.beust:jcommander:1.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.yaml:snakeyaml:1.6" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.beust:jcommander:1.48" level="project" />
<orderEntry type="library" name="Maven: org.mockito:mockito-core:1.9.0" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="library" name="Maven: org.objenesis:objenesis:1.0" level="project" />
Expand Down
19 changes: 8 additions & 11 deletions code/pom.xml
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.scientiamobile.wurflcloud</groupId>
<artifactId>client-java</artifactId>
<version>1.0.13</version>
<version>1.0.14</version>
<packaging>jar</packaging>

<name>wurfl-cloud-client</name>
Expand Down Expand Up @@ -58,7 +58,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<testng.version>5.14.10</testng.version>
<testng.version>6.9.8</testng.version>
<logback.version>1.0.0</logback.version>
<slf4j.version>1.6.4</slf4j.version>
<jackson.version>2.10.2</jackson.version>
Expand All @@ -80,18 +80,14 @@
<version>2.5.1</version>
<optional>true</optional>
</dependency>
<!-- Jackson 2 -->
<!-- https://mvnrepository.com/artifact/com.jsoniter/jsoniter -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.10.3</version>
<groupId>com.jsoniter</groupId>
<artifactId>jsoniter</artifactId>
<version>0.9.23</version>
</dependency>


<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down Expand Up @@ -149,6 +145,7 @@
<exclude>**/*.bz2</exclude>
<exclude>**/.*</exclude>
<exclude>**/*LICENSE*.txt</exclude>
<exclude>**/*_ua*.txt</exclude>
<exclude>**/CHANGELOG*</exclude>
<exclude>logs/**.*</exclude>
</excludes>
Expand Down
Expand Up @@ -31,7 +31,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.scientiamobile.wurflcloud.cache.IWurflCloudCache;
import com.scientiamobile.wurflcloud.device.AbstractDevice;
import com.scientiamobile.wurflcloud.device.CacheDevice;
Expand All @@ -42,6 +41,8 @@
import com.scientiamobile.wurflcloud.utils.Constants;
import com.scientiamobile.wurflcloud.utils.Credentials;

import static com.jsoniter.JsonIterator.deserialize;


/**
* Cloud thin client, associated to every request.
Expand All @@ -59,7 +60,6 @@ public class CloudClient extends Loggable implements ICloudClientRequest, Consta
private final String[] searchCapabilities;
private final Credentials credentials;
private final IWurflCloudCache cache;
private final ObjectMapper mapper = new ObjectMapper();
private final Set<CloudListener> listeners = new HashSet<CloudListener>();

private static final int HTTP_ERROR_INVALID_KEY = 401;
Expand Down Expand Up @@ -362,8 +362,8 @@ public boolean testCall(Encoding enc) {
*/
private CloudResponse processResponse(String rawData) {
try {
return mapper.readValue(rawData, CloudResponse.class);
} catch (IOException e) {
return deserialize(rawData, CloudResponse.class);
} catch (Exception e) {
throw new WURFLCloudClientException("", HTTP_ERROR_JSON_KEY);
}
}
Expand Down
Expand Up @@ -18,5 +18,5 @@
* Facility logging base class
*/
public abstract class Loggable {
protected final Logger logger = LoggerFactory.getLogger(getClass());
protected final transient Logger logger = LoggerFactory.getLogger(getClass());
}
Expand Up @@ -20,13 +20,14 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.jsoniter.output.JsonStream;
import com.scientiamobile.wurflcloud.ICloudClientRequest;
import com.scientiamobile.wurflcloud.device.AbstractDevice;
import com.scientiamobile.wurflcloud.device.CookieDevice;
import com.scientiamobile.wurflcloud.device.JsonCookie;

import static com.jsoniter.JsonIterator.deserialize;

/**
* Cache implementation using Cookies.
*
Expand All @@ -38,8 +39,6 @@ public class SimpleCookieCache extends AbstractWurflCloudCache {
*/
public static final int COOKIE_CACHE_EXPIRATION = 86400;

private final ObjectMapper mapper = new ObjectMapper();

/**
* Cookie encoding charset
*/
Expand All @@ -65,7 +64,7 @@ public AbstractDevice getDevice(HttpServletRequest request, ICloudClientRequest
try {
value = URLDecoder.decode(value, US_ASCII);
if (logger.isDebugEnabled()) logger.debug("decoded cookie value: " + value);
JsonCookie jc = mapper.readValue(value, JsonCookie.class);
JsonCookie jc = deserialize(value, JsonCookie.class);
long expiration = jc.getDate_set() + COOKIE_CACHE_EXPIRATION;
long nowSecs = System.currentTimeMillis() / 1000;
if (expiration < nowSecs) {
Expand Down Expand Up @@ -95,18 +94,17 @@ public AbstractDevice getDeviceFromID(String key) {
* @param key
* @param device
* @return
* @see COOKIE_CACHE_EXPIRATION
*/
public boolean setDevice(HttpServletResponse response, String key, AbstractDevice device) {
String cookieVal;
HashMap<String, Object> capabilitiesMap = new HashMap<String, Object>(device.getCapabilities());
long nowSecs = System.currentTimeMillis() / 1000;
JsonCookie jsonCookie = new JsonCookie();
jsonCookie.setCapabilities(capabilitiesMap);
jsonCookie.setDate_set(nowSecs);
jsonCookie.setDate_set(String.valueOf(nowSecs));
jsonCookie.setId(device.getId());
try {
cookieVal = mapper.writeValueAsString(jsonCookie);
cookieVal = JsonStream.serialize(jsonCookie);
logger.debug("cookie value: " + cookieVal);
cookieVal = URLEncoder.encode(cookieVal, US_ASCII);
logger.debug("encoded cookie value: " + cookieVal);
Expand Down
Expand Up @@ -11,6 +11,10 @@
*/
package com.scientiamobile.wurflcloud.device;

import com.scientiamobile.wurflcloud.utils.AuthorizationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

Expand All @@ -19,7 +23,8 @@
*/
public class JsonCookie {
private Map<String, Object> capabilities;
private long date_set;
private String date_set;
private long ldate_set;
private String id;

/**
Expand All @@ -30,6 +35,8 @@ public Map<String, Object> getCapabilities() {
return capabilities;
}

private static final Logger logger = LoggerFactory.getLogger(JsonCookie.class);

/**
* Sets the capability map
* @param capabilities The new capability map
Expand All @@ -39,11 +46,19 @@ public void setCapabilities(HashMap<String, Object> capabilities) {
}

public long getDate_set() {
return date_set;
return ldate_set;
}

public void setDate_set(long date_set) {
public void setDate_set(String date_set) {
this.date_set = date_set;
try {
// json iter may have problems parsing long values that arrive as strings, so we do it explicitly
this.ldate_set = Long.parseLong(this.date_set);
}
catch (Exception e){
logger.error("Unable to parse date_set field value " + this.date_set + " : it is not a number");
}

}

/**
Expand Down
Expand Up @@ -24,7 +24,7 @@ public interface Constants {
/**
* The version of this client
*/
String CLIENT_VERSION = "1.0.13";
String CLIENT_VERSION = "1.0.14";

/**
* Accepted encoding enum.
Expand Down
Expand Up @@ -43,7 +43,7 @@ public void setup() throws Exception {
cfg = new CloudClientConfig();
cfg.clearServers();
cfg.addCloudServer("timeout_server", "www.google.com:81", 10);
cfg.apiKey = "XXXXXX:YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
cfg.apiKey = "369735:mkl1fRdMqhsD4EJr5NQITp6Ob8CUgKaX";
cfg.connectionTimeout = 2000;
am = new AuthenticationManager(cfg);
ccm = new CloudClientManager(am, cfg, new WurflCloudCache_Null(), null);
Expand Down
Expand Up @@ -11,12 +11,11 @@
*/
package com.scientiamobile.wurflcloud;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;

import static com.jsoniter.JsonIterator.deserialize;
import static org.testng.Assert.assertNotNull;

/**
Expand All @@ -26,16 +25,11 @@
*/
@Test
public class CloudResponseTest extends Loggable {
ObjectMapper mapper;

@BeforeClass
public void setup() {
mapper = new ObjectMapper();
}

public void testRawData() throws IOException {
String rawData = "{\"apiVersion\":\"WurflCloud 2.1.6\",\"mtime\":1310581109,\"id\":\"mozilla_ver5\",\"capabilities\":{\"resolution_height\":600,\"resolution_width\":800},\"errors\":{}}";
CloudResponse cloudResponse = mapper.readValue(rawData, CloudResponse.class);
CloudResponse cloudResponse = deserialize(rawData, CloudResponse.class);
assertNotNull(cloudResponse);
logger.info(cloudResponse.toString());
}
Expand Down
@@ -0,0 +1,44 @@
/**
* Copyright (c) 2015 ScientiaMobile Inc.
*
* The WURFL Cloud Client is intended to be used in both open-source and
* commercial environments. To allow its use in as many situations as possible,
* the WURFL Cloud Client is dual-licensed. You may choose to use the WURFL
* Cloud Client under either the GNU GENERAL PUBLIC LICENSE, Version 2.0, or
* the MIT License.
*
* Refer to the COPYING.txt file distributed with this package.
*/
package com.scientiamobile.wurflcloud;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.*;

public class DetectionPerformanceTest {

private ICloudClientManager cloudClient;

@BeforeClass
public void setup() throws Exception {
CloudClientLoader loader = new CloudClientLoader(null, "/CloudClientManagerTest.properties");
cloudClient = loader.getClientManager();
}

@Test
public void performanceTest() throws IOException {
long elapsed = 0;
int c = 0;
BufferedReader br = new BufferedReader(new FileReader("src/test/resources/1000_ua.txt"));
String line = null;
long st = System.nanoTime();
while ((line = br.readLine()) != null && c < 1000){
cloudClient.getDeviceFromUserAgent(line, null);
c++;
}
elapsed = System.nanoTime() - st;
System.out.println("-- Detection time: " + elapsed + " nanoseconds -- ");
}

}
Expand Up @@ -11,12 +11,13 @@
*/
package com.scientiamobile.wurflcloud;

import junit.framework.Assert;

import org.testng.annotations.Test;

import com.scientiamobile.wurflcloud.utils.Constants;

import static org.testng.Assert.assertEquals;

@Test(groups = "unit")
public class VersionTest extends Loggable {

Expand All @@ -29,7 +30,7 @@ public void versionReadTest() {
String versionStr = System.getProperty("wurflcloudversion");
logger.debug("Version read from pom.xml: " + versionStr);
logger.debug("Version read from Constants.java: " + Constants.CLIENT_VERSION);
Assert.assertEquals(versionStr, Constants.CLIENT_VERSION);
assertEquals(versionStr, Constants.CLIENT_VERSION);
}

}

0 comments on commit 22e4cc4

Please sign in to comment.