Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

G11n java client - New caching workflow #425

Merged
merged 53 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
f4c1214
Loading configuration from the JSON configuration file
jessiejuachon Mar 11, 2020
1a4d95e
Removing code that is not yet ready to be used from the sample applic…
jessiejuachon Mar 11, 2020
5e72b73
fixing failing code scan
jessiejuachon Mar 11, 2020
73d85ca
Adding a cache properties map to hold cache details such as etag and …
jessiejuachon Mar 12, 2020
2a38107
Removing white space
jessiejuachon Mar 12, 2020
c7f5d4f
Fixing failing unit test
jessiejuachon Mar 12, 2020
056891a
Parse headers, response code, response message from an HTTP response …
jessiejuachon Mar 12, 2020
a2ba98b
Storing response headers in Map of cache properties
jessiejuachon Mar 17, 2020
17978bd
Fixing failing unit test due to NullPointerException when doing Map.p…
jessiejuachon Mar 17, 2020
6c69170
Changes after code review.
jessiejuachon Mar 17, 2020
41a8aa1
Removing exception handling for now
jessiejuachon Mar 18, 2020
dd2455d
Deprecating VIPCfg.initialize; cleaning up code
jessiejuachon Mar 19, 2020
e26d372
Storing the http response code in the cache
jessiejuachon Mar 20, 2020
01ab69e
Using cached etag as if-none-match request header value in the reques…
jessiejuachon Mar 25, 2020
750c9d2
Handling 404 response from Singleton service
jessiejuachon Mar 26, 2020
529e5cc
Using HTTP repsonse's Cache-Control max-age and timestamp to dertermi…
jessiejuachon Mar 26, 2020
b40fcf9
Moving constants to URLUtils
jessiejuachon Mar 26, 2020
fa123f9
Fixing failing test
jessiejuachon Mar 27, 2020
30b2054
Fixing failing code scan
jessiejuachon Mar 27, 2020
0c69787
Merge branch 'g11n-java-client' into g11n-java-client
Xiaochao8 Mar 27, 2020
ffc0a74
Cleaning up import in ComponentService,java
jessiejuachon Mar 30, 2020
d8450e4
Cleaning up code - caching/expiry
jessiejuachon Mar 30, 2020
d7f7641
Merge branch 'g11n-java-client' of https://github.com/jessiejuachon/s…
jessiejuachon Mar 30, 2020
0dcd827
Adding tests for new caching workflow; enabling old caching workflow …
jessiejuachon Mar 31, 2020
d06adb3
Fixing a failing test
jessiejuachon Mar 31, 2020
48c8558
Adding mock server response
jessiejuachon Mar 31, 2020
4e45bed
Removing unused file sampleconfig.json
jessiejuachon Mar 31, 2020
6a40dc8
Changes from code review
jessiejuachon Mar 31, 2020
4c3c12d
Value of VIPCfg.cacheExpiredTime is -1 when value is not set in confi…
jessiejuachon Mar 31, 2020
7e0d939
Changing a LinkedHashMap to a HashMap because insertion order does no…
jessiejuachon Apr 2, 2020
baef0d0
Adding CacheItem object to contain map of data and a another map for …
jessiejuachon Apr 3, 2020
02a96e9
Fixing failing unit test
jessiejuachon Apr 3, 2020
78d88f7
Fixing code scan issues
jessiejuachon Apr 3, 2020
655c223
Code cleanup
jessiejuachon Apr 3, 2020
8746915
Lazily instantiating cacheProps; code clean up and comments
jessiejuachon Apr 5, 2020
897a03c
Changes after code review
jessiejuachon Apr 7, 2020
3a4e183
Adding header to CacheItem.java
jessiejuachon Apr 7, 2020
b08feb4
Fixing code san issue. Synchronize on a final field
jessiejuachon Apr 7, 2020
8f57e24
Fixing code scan bug
jessiejuachon Apr 7, 2020
72c8799
Changes after code review
jessiejuachon Apr 7, 2020
92edfc8
Updating CasheService.isContainComponent to return false if cache key…
jessiejuachon Apr 7, 2020
2042554
HttpRequester if-none-match header and other customized headers are a…
jessiejuachon Apr 8, 2020
ae707ff
Code clean up; keep old caching expiration logic
jessiejuachon Apr 8, 2020
82818d5
Fixing code scan bug
jessiejuachon Apr 8, 2020
31d4ff6
Merge branch 'g11n-java-client' into g11n-java-client
jessiejuachon Apr 8, 2020
c9f83d7
code clean up
jessiejuachon Apr 9, 2020
78dcc1c
Removing response code and response msg from cache; Passing cacheItem…
jessiejuachon Apr 9, 2020
9281c64
Changes after code review
jessiejuachon Apr 10, 2020
2657762
Adding header to CacheItem.java file
jessiejuachon Apr 10, 2020
9e80f09
Fixing failing unit test
jessiejuachon Apr 10, 2020
68467f3
Changes after code review
jessiejuachon Apr 10, 2020
d67c72e
Fixing code scan bugs
jessiejuachon Apr 10, 2020
8591973
Not storing anything in cache if response is neither 200 nor 304
jessiejuachon Apr 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/main/java/com/vmware/vipclient/i18n/VIPCfg.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class VIPCfg {
private boolean collectSource;
private boolean cleanCache;
private long cacheExpiredTime;

private boolean machineTranslation;
private boolean initializeCache;
private int interalCleanCache;
Expand Down Expand Up @@ -117,17 +118,17 @@ public void initialize(String vipServer, String productName, String version) {
this.version = version;
this.vipServer = vipServer;
}

/**
* initialize the instance by a properties file
*
* @param cfg
*/
public void initialize(String cfg) throws VIPClientInitException {
ResourceBundle prop = ResourceBundle.getBundle(cfg);
if (prop == null) {
throw new VIPClientInitException("Can't not initialize VIPCfg, resource bundle is null.");
}
ResourceBundle prop = ResourceBundle.getBundle(cfg);
if (prop == null) {
throw new VIPClientInitException("Can't not initialize VIPCfg, resource bundle is null.");
}

if (prop.containsKey("productName"))
this.productName = prop.getString("productName");
Expand Down Expand Up @@ -158,7 +159,8 @@ public void initialize(String cfg) throws VIPClientInitException {
if (prop.containsKey("cacheExpiredTime"))
this.cacheExpiredTime = Long.parseLong(prop
.getString("cacheExpiredTime"));
}

}

/**
* initialize VIPService instances to provide HTTP requester
Expand All @@ -169,7 +171,7 @@ public void initializeVIPService() {
this.vipService.initializeVIPService(this.productName, this.version,
this.vipServer);
} catch (MalformedURLException e) {
logger.error("'vipServer' in configuration isn't a valid URL!");
logger.error("'vipServer' " + this.vipServer + " in configuration isn't a valid URL!");
}
}

Expand All @@ -195,7 +197,7 @@ public void setTranslationCache(Cache c) {
}
Cache createdCache = TranslationCacheManager
.getCache(VIPCfg.CACHE_L3);
if (createdCache != null && this.getCacheExpiredTime() > 0) {
if (createdCache != null && this.getCacheExpiredTime() != 0) {
c.setExpiredTime(this.getCacheExpiredTime());
Xiaochao8 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down Expand Up @@ -223,7 +225,7 @@ public synchronized Cache createTranslationCache(Class cacheClass) {
Task.startTaskOfCacheClean(VIPCfg.getInstance(), interalCleanCache);
}
Cache c = TranslationCacheManager.getCache(VIPCfg.CACHE_L3);
if (c != null && this.getCacheExpiredTime() > 0) {
if (c != null && this.getCacheExpiredTime() != 0) {
c.setExpiredTime(this.getCacheExpiredTime());
}
}
Expand Down Expand Up @@ -364,15 +366,17 @@ public boolean isInitializeCache() {
public void setInitializeCache(boolean initializeCache) {
this.initializeCache = initializeCache;
}


@Deprecated
public long getCacheExpiredTime() {
return cacheExpiredTime;
}


@Deprecated
public void setCacheExpiredTime(long cacheExpiredTime) {
this.cacheExpiredTime = cacheExpiredTime;
}

public CacheMode getCacheMode() {
return cacheMode;
}
Expand Down
91 changes: 64 additions & 27 deletions src/main/java/com/vmware/vipclient/i18n/base/HttpRequester.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -50,17 +51,18 @@ public class HttpRequester {
*/
private String baseURL;

public void setBaseURL(String baseURL) {
this.baseURL = baseURL;
}

/**
* The extra parameters to add to http header
* HTTP headers that are common to all HTTP requests
*/
private Map<String, String> customizedHeaderParams = null;
private Map<String, String> commonHeaderParams = null;

public void setCustomizedHeaderParams(Map<String, String> params) {
customizedHeaderParams = params;
commonHeaderParams = params;
}


public void setBaseURL(String baseURL) {
this.baseURL = baseURL;
}

/**
Expand Down Expand Up @@ -99,15 +101,33 @@ protected static boolean ping(String ipAddress) {
}
return status;
}

/**
* The get method of requesting a remote server.
* Send an HTTP request.
*
* @param url
* The remote server url.
* @return
* @param url The remote server url
* @param method HTTP method
* @param requestData HTTP URL parameters
*
* @return Map<String, Object> response
*/
public Map<String, Object> request(final String url, final String method, final Object requestData) {
return request(url, method, requestData, null);
}

/**
* Send an HTTP request.
*
* @param url The remote server url
* @param method HTTP method
* @param requestData HTTP URL parameters
* @param customizedHeaderParams customized HTTP request header for this request
*
* @return Map<String, Object> response
*/
public String request(final String url, final String method, final Object requestData) {
public Map<String, Object> request(final String url, final String method, final Object requestData,
final Map<String, String> customizedHeaderParams) {
Map <String, Object> response = new HashMap<String, Object>();
String r = "";
HttpURLConnection conn = null;
try {
Expand All @@ -121,18 +141,31 @@ public String request(final String url, final String method, final Object reques
urlStr.append(url);
}
logger.info("[" + method + "]" + urlStr.toString());
conn = createConnection(urlStr.toString());

conn = createConnection(urlStr.toString(), customizedHeaderParams);
if (conn != null) {
conn.setRequestMethod(method);
if (ConstantsKeys.POST.equalsIgnoreCase(method)) {
this.writeData(requestData, conn);
} else {
conn.connect();
}
if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {
r = this.handleResult(conn);
// logger.debug("The response from server is:\n"+r);
switch (conn.getResponseCode()) {
case HttpURLConnection.HTTP_NOT_MODIFIED :
break;
case HttpURLConnection.HTTP_OK :
r = this.handleResult(conn);
response.put(URLUtils.BODY, r);
// logger.debug("The response from server is:\n"+r);
break;
}
response.put(URLUtils.HEADERS, conn.getHeaderFields());
response.put(URLUtils.RESPONSE_CODE, conn.getResponseCode());
response.put(URLUtils.RESPONSE_MSG, conn.getResponseMessage());
response.put(URLUtils.RESPONSE_TIMESTAMP, System.currentTimeMillis());
Long maxAgeMillis = URLUtils.getMaxAgeMillis(conn.getHeaderFields());
if (maxAgeMillis != null)
response.put(URLUtils.MAX_AGE_MILLIS, maxAgeMillis);
}
} catch (IOException e) {
logger.info(e.getMessage());
Expand All @@ -142,9 +175,9 @@ public String request(final String url, final String method, final Object reques
conn = null;
}
}
return r;
return response;
}

public String getBaseURL() {
return this.baseURL;
}
Expand Down Expand Up @@ -210,7 +243,7 @@ private String getFormStr(Map<String, String> params) {
* The remote server url.
* @return
*/
private HttpURLConnection createConnection(String path) {
private HttpURLConnection createConnection(String path, Map<String, String> customizedHeaderParams) {
HttpURLConnection connection = null;
try {
URL url = new URL(path.trim());
Expand Down Expand Up @@ -239,7 +272,7 @@ public boolean verify(String hostname, SSLSession session) {
connection.setDoInput(true);
connection.setRequestProperty("accept", "*/*");

addHeaderParams(connection);
addHeaderParams(connection, customizedHeaderParams);
}
return connection;
}
Expand Down Expand Up @@ -283,13 +316,17 @@ public boolean isConnected() {
return HttpRequester.ping(ipAddress);
}

private void addHeaderParams(HttpURLConnection connection) {
if (null == customizedHeaderParams || null == connection) {
return;
private void addHeaderParams(HttpURLConnection connection, Map<String, String> customizedHeaderParams) {
if (customizedHeaderParams != null) {
for (final Entry<String, String> entry : customizedHeaderParams.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getValue());
}
}

for (final Entry<String, String> entry : customizedHeaderParams.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getValue());

if (commonHeaderParams != null) {
for (final Entry<String, String> entry : commonHeaderParams.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getValue());
}
}
}
}
19 changes: 9 additions & 10 deletions src/main/java/com/vmware/vipclient/i18n/base/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
*/
package com.vmware.vipclient.i18n.base.cache;
Xiaochao8 marked this conversation as resolved.
Show resolved Hide resolved

import java.util.Map;
import java.util.Set;
import com.vmware.vipclient.i18n.base.cache.CacheItem;

public interface Cache {

/**
* get a component's strings by key
* get a component's cached data by key
*
* @param key
* @return map of all strings under the component
* @return CacheItem object instance that holds the cached data (messages and associated properties)
*/
public Map<String, String> get(String key);

public CacheItem get(String key);
Xiaochao8 marked this conversation as resolved.
Show resolved Hide resolved
/**
* put strings to cache by key
*
* @param key
* @param map
* @param key cache key
* @param cacheItem item to be stored in the cache
* @return false if failed to put
*/
public boolean put(String key, Map<String, String> map);
public boolean put(String key, CacheItem cacheItem);

/**
* remove a component from cache by key
Expand Down Expand Up @@ -110,5 +110,4 @@ public interface Cache {
* @return a drop id
*/
public String getDropId();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright 2019 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vipclient.i18n.base.cache;

public interface CacheItem {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vipclient.i18n.base.cache;

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

public class FormatCacheItem implements CacheItem {
public FormatCacheItem() {

}

public FormatCacheItem (Map<String, String> dataMap) {
super();
this.addCachedData(dataMap);
}

public final Map<String, String> cachedData = new HashMap<String, String>();

public void addCachedData(Map<String, String> cachedData) {
if (cachedData != null)
this.cachedData.putAll(cachedData);
}

public Map<String, String> getCachedData() {
return cachedData;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ public class FormattingCache implements Cache {
private long expiredTime = 86400000; // 24hr
private long lastClean = System.currentTimeMillis();

private Map<String, Map<String, String>> formattingMap = new LinkedHashMap<String, Map<String, String>>();
private Map<String, FormatCacheItem> formattingMap = new LinkedHashMap<String, FormatCacheItem>();

public FormattingCache() {
super();
}

@SuppressWarnings("unchecked")
public Map<String, String> get(String cacheKey) {
Object cachedObject = formattingMap.get(cacheKey);
return cachedObject == null ? null : (Map<String, String>) cachedObject;
public FormatCacheItem get(String cacheKey) {
return formattingMap.get(cacheKey);
}

public synchronized boolean put(String cacheKey, Map<String, String> map) {
formattingMap.put(cacheKey, map);
@Override
public synchronized boolean put(String cacheKey, CacheItem itemToCache) {
formattingMap.put(cacheKey, (FormatCacheItem) itemToCache);
return formattingMap.get(cacheKey) != null;
}

Expand Down
Loading