Commit e8648480 authored by Yong Liu's avatar Yong Liu
Browse files

when timestamp changes, reloading nesstar objects.

parent b164384c
......@@ -60,9 +60,6 @@ public class NesstarDropService extends Service<NesstarDropConfiguration> {
environment.addHealthCheck(new NesstarHealthCheck(serverHandler));
TimedCacheCleanerFilter timedCacheCleanerFilter = new TimedCacheCleanerFilter(configuration.getExpirationTimeout());
environment.addFilter(timedCacheCleanerFilter, "/*");
environment.addFilter(CrossOriginFilter.class, "/*")
.setInitParam(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin");
......
......@@ -194,4 +194,9 @@ public class CommonEntityTagFilter extends EntityTagFilter {
// No implementation necessary
}
@Override
protected void reloadNesstarObject(final HttpServletRequest request) {
}
}
package com.nesstar.rest.filters;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.api.cube.Cube;
import com.nesstar.api.cube.Dimension;
import com.nesstar.api.cube.Measure;
import com.nesstar.rest.common.CubeClassVariables;
import com.nesstar.rest.common.ETagData;
import com.nesstar.rest.common.MissingResourceException;
import com.nesstar.rest.common.ResourceExtractor;
import com.nesstar.rest.common.ServerHandler;
import com.nesstar.rest.request_objects.CubeRequest;
import org.slf4j.Logger;
......@@ -9,6 +16,9 @@ import org.slf4j.LoggerFactory;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
public class CubeEntityTagFilter extends OperationEntityTagFilter {
......@@ -18,16 +28,40 @@ public class CubeEntityTagFilter extends OperationEntityTagFilter {
super(serverHandler);
}
private Cube getCube(final HttpServletRequest request) {
String cubeId = getCubeId(request);
Cube cube = null;
try {
cube = ResourceExtractor.getCube(cubeId, this.serverHandler.getServer());
} catch (MissingResourceException e) {
LOG.warn("Missing cube {}", cubeId);
} catch (IOException e) {
LOG.warn("IOException when getting study: {}", cubeId);
}
return cube;
}
@Override
protected ETagData generateETagData(final HttpServletRequest request) {
CubeRequest cubeRequest = getCubeRequest(request);
String url = request.getRequestURL().toString();
String cubeId = getCubeId(request);
return generateETagData(cubeRequest, url, cubeId);
Date date = null;
try {
Cube cube = this.getCube(request);
if (cube != null) {
date = cube.getTimeStamp();
}
} catch (NotAuthorizedException e) {
date = new Date();
} catch (IOException e) {
date = new Date();
}
return generateETagData(cubeRequest, url, cubeId, date);
}
public static ETagData generateETagData(final CubeRequest cubeRequest, final String url, final String cubeId) {
public static ETagData generateETagData(final CubeRequest cubeRequest, final String url, final String cubeId, Date date) {
ETagData data = new ETagData();
data.add(url);
......@@ -42,7 +76,7 @@ public class CubeEntityTagFilter extends OperationEntityTagFilter {
data.add(measure);
}
}
data.add(TimedCacheCleanerFilter.getLatestTimeStamp());
data.add(Integer.toString(date.hashCode()));
return data;
}
......@@ -84,4 +118,41 @@ public class CubeEntityTagFilter extends OperationEntityTagFilter {
public void destroy() {
// No implementation necessary
}
public static ETagData generateETagData(CubeClassVariables classVariables, String url, String cubeId) {
ETagData data = new ETagData();
data.add(url);
if (classVariables != null) {
for (final Dimension dimension : classVariables.getDimensions()) {
data.add(dimension.getId());
}
for (final Measure measure : classVariables.getMeasures()) {
data.add(measure.getId());
}
try {
data.add(Integer.toString(classVariables.getCube().getTimeStamp().hashCode()));
} catch (NotAuthorizedException e) {
data.add(Integer.toString(new Date().hashCode()));
} catch (IOException e) {
data.add(Integer.toString(new Date().hashCode()));
}
}
return data;
}
@Override
protected void reloadNesstarObject(final HttpServletRequest request) {
try {
Cube cube = this.getCube(request);
if (cube != null) {
cube.reload();
}
} catch (IOException e) {
LOG.warn("Missing cube {}", getCubeId(request));
} catch (NotAuthorizedException e) {
LOG.warn("Missing cube {}", getCubeId(request));
}
}
}
......@@ -44,6 +44,7 @@ public abstract class EntityTagFilter implements Filter {
}
protected abstract boolean checkForMatchingETag(HttpServletRequest request);
protected abstract void reloadNesstarObject(final HttpServletRequest request);
protected String[] getUriPartsFromRequest(HttpServletRequest request) {
String uri = request.getRequestURI();
......
......@@ -26,7 +26,11 @@ public abstract class OperationEntityTagFilter extends EntityTagFilter {
@Override
protected boolean checkForMatchingETag(HttpServletRequest request) {
ETag etag = getEtagFromRequest(request);
return etagsMatch(request, etag);
boolean ret = etagsMatch(request, etag);
if (!ret) {
this.reloadNesstarObject(request);
}
return ret;
}
protected ETag getEtagFromRequest(HttpServletRequest request) {
......@@ -79,4 +83,10 @@ public abstract class OperationEntityTagFilter extends EntityTagFilter {
@Override
public abstract void destroy();
@Override
protected void reloadNesstarObject(final HttpServletRequest request) {
}
}
......@@ -75,7 +75,7 @@ public final class CubeResource extends AbstractResource {
classVariables.setDimensions(classVariables.getCube().getDimensions());
classVariables.setMeasures(classVariables.getCube().getMeasures());
performCube(cubeRequest, classVariables);
ETag etag = ETag.generateEtagForCube(cubeRequest, request.getRequestURL().toString(), cubeId);
ETag etag = ETag.generateEtagForCube(classVariables, request.getRequestURL().toString(), cubeId);
setETagHeader(etag, response);
return getCubeObject(classVariables);
}
......
......@@ -24,7 +24,7 @@ public class CubeEntityTagFilterTest extends EntityTagFilterTest{
public void testFilteringOnCube() throws Exception {
when(request.getRequestURL()).thenReturn(new StringBuffer("http://example.com/cube/cube1234"));
when(request.getRequestURI()).thenReturn("/cube/cube1234");
when(request.getHeader("If-None-Match")).thenReturn("80c3eeab70149ec9fdec22ce0c8fd7f8");
when(request.getHeader("If-None-Match")).thenReturn("59ae0a089517fd0fb7c91341dd564e0d");
EntityTagFilter filter = new CubeEntityTagFilter(serverHandler);
filter.doFilter(request, response, chain);
......@@ -38,7 +38,7 @@ public class CubeEntityTagFilterTest extends EntityTagFilterTest{
new StringBuffer("http://example.com/cube/cube1234?q={\"dimensions\":[{\"id\":\"dimensionID1\",\"members\":[\"memberID1\",\"memberID2\"]}],\"measures\":[\"measureID1\",\"measureID2\"]}"));
when(request.getRequestURI()).thenReturn("/cube/cube1234");
when(request.getParameter("q")).thenReturn("{\"dimensions\":[{\"id\":\"dimensionID1\",\"members\":[\"memberID1\",\"memberID2\"]}],\"measures\":[\"measureID1\",\"measureID2\"]}");
when(request.getHeader("If-None-Match")).thenReturn("8f7cf31c8d3cd8cc6903c795e7c0d696");
when(request.getHeader("If-None-Match")).thenReturn("7547c289642243a0c6e0fe6b39fdf6ae");
EntityTagFilter filter = new CubeEntityTagFilter(serverHandler);
filter.doFilter(request, response, chain);
......
......@@ -25,14 +25,15 @@ public class EntityTagFilterTest {
request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class);
chain = mock(FilterChain.class);
serverHandler = mock(ServerHandler.class);
Server server = mock(Server.class);
bank = mock(Bank.class);
when(serverHandler.getServer()).thenReturn(server);
when(server.getBank(any(Class.class))).thenReturn(bank);
when(server.getTimeStamp()).thenReturn(createDate());
}
Date createDate() throws Exception {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment