Skip to content

Add Cache Control Headers for Angular Generator Rest Services. #276

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions common/src/main/java/com/genexus/ClientInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,12 @@ static public String getPlatformName()
platformName = SpecificImplementation.Application.getModelContext().getHttpContext().getHeader("DevicePlatform");
return platformName;
}

public final class DeviceTypeEnum {
public static final int iOS = 0;
public static final int Android = 1;
public static final int Blackberry = 2;
public static final int Windows = 3;
public static final int Web = 4;
}
}
2 changes: 1 addition & 1 deletion common/src/main/java/com/genexus/GXSmartCacheProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void discardUpdates()
// <param name="dateLastModified"></param>
// <param name="dateUpdated"></param>
// <returns>Unknown/Invalid/UpToDate</returns>
static public DataUpdateStatus CheckDataStatus(String queryId, Date dateLastModified, Date[] dateUpdated_arr)
static public DataUpdateStatus checkDataStatus(String queryId, Date dateLastModified, Date[] dateUpdated_arr)
{
return (DataUpdateStatus) provider.CheckDataStatus(queryId, dateLastModified, dateUpdated_arr);
}
Expand Down
49 changes: 39 additions & 10 deletions java/src/main/java/com/genexus/GxRestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,41 +258,68 @@ public boolean processHeaders(String queryId, IHttpServletRequest myServletReque
{
setTheme(theme);
}
String etag = myServletRequest.getMethod().equalsIgnoreCase(POST) ? null : myServletRequest.getHeader("If-Modified-Since");
String eTag = isPostRequest(myServletRequest) ? null : myServletRequest.getHeader("If-Modified-Since");
Date dt = Application.getStartDateTime();
Date newDt = new Date();
GXSmartCacheProvider.DataUpdateStatus status;
Date[] newDt_arr = new Date[] { newDt };
if (etag == null)
if (eTag == null)
{
status = GXSmartCacheProvider.DataUpdateStatus.Invalid;
GXSmartCacheProvider.CheckDataStatus(queryId, dt, newDt_arr);
GXSmartCacheProvider.checkDataStatus(queryId, dt, newDt_arr);
}
else
{
dt = HTMLDateToDatetime(etag);
status = GXSmartCacheProvider.CheckDataStatus(queryId, dt, newDt_arr);
dt = HTMLDateToDatetime(eTag);
status = GXSmartCacheProvider.checkDataStatus(queryId, dt, newDt_arr);
}
newDt = newDt_arr[0];
if (myServletResponse != null) // Temporary: Jersey Service called through AWS Lambda where HttpResponse is null.
myServletResponse.addHeader("Last-Modified", DateTimeToHTMLDate(newDt));
addHeader(myServletResponse, "Last-Modified", DateTimeToHTMLDate(newDt));
addCacheHeaders(myServletResponse);
if (status == GXSmartCacheProvider.DataUpdateStatus.UpToDate)
{
return false;
}
return true;
}


private void addCacheHeaders(HttpServletResponse myServletResponse) {
/*
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
* Specifying no-cache or max-age=0 indicates that
* clients can cache a resource and must revalidate each time before using it.
* This means HTTP request occurs each time, but it can skip downloading HTTP body if the content is valid.
*/
if (ClientInformation.getDeviceType() == ClientInformation.DeviceTypeEnum.Web) {
addHeader(myServletResponse, "Cache-Control", "no-cache, max-age=0");
}
}

boolean isPostRequest(HttpServletRequest request)
{
return request.getMethod().equalsIgnoreCase(POST);
}

void addHeader(HttpServletResponse response, String headerName, String headerValue)
{
if (response != null) {
// Temporary: Jersey Service called through AWS Lambda where HttpResponse is null.
response.addHeader(headerName, headerValue);
}
else {
logger.warn("Could add HttpHeader to Response");
}
}

Date HTMLDateToDatetime(String s)
{
// Formato fecha: RFC 1123
// Date Format: RFC 1123
try
{
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", java.util.Locale.US);
java.util.TimeZone tz = java.util.TimeZone.getTimeZone("GMT");
sdf.setTimeZone(tz);
return sdf.parse(s);

}
catch(ParseException p)
{
Expand All @@ -308,4 +335,6 @@ String DateTimeToHTMLDate(Date dt)
sdf.setTimeZone(tz);
return sdf.format(dt);
}


}