Skip to content

Commit

Permalink
Merge pull request #29 from WURFL/1.11.0
Browse files Browse the repository at this point in the history
1.11.0: replaced jsoniter with gson library
  • Loading branch information
andreacastello authored Sep 27, 2021
2 parents d824c6f + b31906c commit b68d614
Show file tree
Hide file tree
Showing 14 changed files with 457 additions and 54 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
1.11.0
Fix and ehn: replaced jsoniter with Gson due to some jsoniter deserialization issue and an unfixed severe security alert (CVE-2021-23441).

1.0.15
Fix: Updated logback dependency due to discovered vulnerability.

Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ The WURFL Cloud Client for Java is released under the GNU GENERAL PUBLIC LICENSE
Contributors agree that any contributions are owned by the copyright holder and that contributors have absolutely no rights to their contributions.

To get started, sign the [Contributor License Agreement](https://www.clahub.com/agreements/WURFL/wurfl-cloud-client-java).

Full text of the GPL 2.0 and MIT licenses are available in the `LICENSE-*.txt` files
24 changes: 24 additions & 0 deletions LICENSE-MIT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(c) Copyright 2011 - 2021 Scientiamobile Inc.


Licensed under the MIT license:

http://www.opensource.org/licenses/mit-license.php

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
339 changes: 339 additions & 0 deletions LICENSE-gpl-2.0.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion code/client-java.iml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<orderEntry type="sourceFolder" forTests="false" />
<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.jsoniter:jsoniter:0.9.23" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.8" 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.2.0" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-classic:1.2.0" level="project" />
Expand Down
8 changes: 4 additions & 4 deletions code/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.scientiamobile.wurflcloud</groupId>
<artifactId>client-java</artifactId>
<version>1.0.15</version>
<version>1.11.0</version>
<packaging>jar</packaging>

<name>wurfl-cloud-client</name>
Expand Down Expand Up @@ -84,9 +84,9 @@
</dependency>
<!-- https://mvnrepository.com/artifact/com.jsoniter/jsoniter -->
<dependency>
<groupId>com.jsoniter</groupId>
<artifactId>jsoniter</artifactId>
<version>0.9.23</version>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version>
</dependency>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.scientiamobile.wurflcloud.cache.IWurflCloudCache;
import com.scientiamobile.wurflcloud.device.AbstractDevice;
import com.scientiamobile.wurflcloud.device.CacheDevice;
Expand All @@ -41,8 +42,6 @@
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 @@ -61,6 +60,7 @@ public class CloudClient extends Loggable implements ICloudClientRequest, Consta
private final Credentials credentials;
private final IWurflCloudCache cache;
private final Set<CloudListener> listeners = new HashSet<CloudListener>();
private final Gson gson = new Gson();

private static final int HTTP_ERROR_INVALID_KEY = 401;
private static final int HTTP_ERROR_MISSING_KEY = 402;
Expand Down Expand Up @@ -360,7 +360,7 @@ public boolean testCall(Encoding enc) {
*/
private CloudResponse processResponse(String rawData) {
try {
return deserialize(rawData, CloudResponse.class);
return gson.fromJson(rawData, CloudResponse.class);
} catch (Exception e) {
throw new WURFLCloudClientException("", HTTP_ERROR_JSON_KEY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsoniter.output.JsonStream;
import com.google.gson.Gson;
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 @@ -44,6 +42,8 @@ public class SimpleCookieCache extends AbstractWurflCloudCache {
*/
private static final String US_ASCII = "US-ASCII";

private final Gson gson = new Gson();

@Override
protected boolean purgeInternal() {
return true;
Expand All @@ -64,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 = deserialize(value, JsonCookie.class);
JsonCookie jc = gson.fromJson(value, JsonCookie.class);
long expiration = jc.getDate_set() + COOKIE_CACHE_EXPIRATION;
long nowSecs = System.currentTimeMillis() / 1000;
if (expiration < nowSecs) {
Expand Down Expand Up @@ -99,12 +99,10 @@ public boolean setDevice(HttpServletResponse response, String key, AbstractDevic
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(String.valueOf(nowSecs));
jsonCookie.setId(device.getId());
JsonCookie jsonCookie = new JsonCookie(capabilitiesMap, nowSecs, device.getId());
try {
cookieVal = JsonStream.serialize(jsonCookie);
//cookieVal = JsonStream.serialize(jsonCookie);
cookieVal = gson.toJson(jsonCookie);
logger.debug("cookie value: " + cookieVal);
cookieVal = URLEncoder.encode(cookieVal, US_ASCII);
logger.debug("encoded cookie value: " + cookieVal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,30 @@
*/
package com.scientiamobile.wurflcloud.device;

import com.scientiamobile.wurflcloud.utils.AuthorizationUtils;
import com.google.gson.annotations.SerializedName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
* Bean serialized from/to a cookie.
*/
public class JsonCookie {
private Map<String, Object> capabilities;
private String date_set;
private long ldate_set;
private String id;

@SerializedName("capabilities")
Map<String, Object> capabilities;
@SerializedName("date_set")
long date_set;
@SerializedName("id")
String id;

public JsonCookie(Map<String, Object> capabilities, long date, String devId)
{
this.capabilities = capabilities;
this.date_set = date;
this.id = devId;
}

/**
* Gets the capability map.
Expand All @@ -37,28 +46,20 @@ public Map<String, Object> getCapabilities() {

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

/**
* Sets the capability map
* @param capabilities The new capability map
*/
public void setCapabilities(HashMap<String, Object> capabilities) {
this.capabilities = capabilities;
}

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

public void setDate_set(String date_set) {
public void setDate_set(long 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");
}
}

public void setCapabilities(Map<String, Object> capabilities) {
this.capabilities = capabilities;
}

public void setId(String id) {
this.id = id;
}

/**
Expand All @@ -68,12 +69,4 @@ public void setDate_set(String date_set) {
public String getId() {
return id;
}

/**
* Sets the cookie identifier
* @param id The cookie identifier
*/
public void setId(String id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface Constants {
/**
* The version of this client
*/
String CLIENT_VERSION = "1.0.15";
String CLIENT_VERSION = "1.11.0";

/**
* Accepted encoding enum.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
*/
package com.scientiamobile.wurflcloud;

import com.google.gson.Gson;
import org.testng.annotations.Test;

import java.io.IOException;

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

/**
Expand All @@ -26,10 +25,11 @@
@Test
public class CloudResponseTest extends Loggable {

private final Gson gson = new Gson();

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 = deserialize(rawData, CloudResponse.class);
CloudResponse cloudResponse = gson.fromJson(rawData, CloudResponse.class);
assertNotNull(cloudResponse);
logger.info(cloudResponse.toString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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 static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertEquals;

import com.google.gson.Gson;
import com.scientiamobile.wurflcloud.device.JsonCookie;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Map;


@Test(groups = "unit")
public class JSONCookieSerdeTest {

@Test(groups = "unit")
public void testSerializationDeserializationTest() {

Gson gson = new Gson();
Map<String,Object> capabilities = new HashMap<>();
capabilities.put("brand_name", "Apple");
capabilities.put("model_name", "iPad Pro 11");

JsonCookie cookie = new JsonCookie(capabilities, 163265896L, "apple_ipad_ver1_sub13_7_subhwpro3110");
String jsonString = gson.toJson(cookie);
assertNotNull(jsonString);

JsonCookie deserializedCookie = gson.fromJson(jsonString, JsonCookie.class);
assertNotNull(deserializedCookie);
assertEquals(cookie.getDate_set(), deserializedCookie.getDate_set());
assertEquals(cookie.getCapabilities().size(), deserializedCookie.getCapabilities().size());
assertEquals(cookie.getId(), deserializedCookie.getId());
}
}
2 changes: 1 addition & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.scientiamobile.wurflcloud</groupId>
<artifactId>client-example</artifactId>
<version>1.0.15</version>
<version>1.11.0</version>
<packaging>war</packaging>

<name>WURFL Cloud Client example webapp</name>
Expand Down

0 comments on commit b68d614

Please sign in to comment.