Skip to content

Commit 1fe6b04

Browse files
authored
[ZEPPELIN-6213] Add Cache-Control Header to API Calls
### What is this PR for? This pull request sets the no cache header for all Zeppelin API calls. ### What type of PR is it? * Improvement ### What is the Jira issue? * https://issues.apache.org/jira/browse/ZEPPELIN-6213 ### How should this be tested? * CI ### Screenshots Before - no cache header ![grafik](https://github.com/user-attachments/assets/e4bff5fb-686c-4944-9da1-21130214ac9d) After - with cache header ![grafik](https://github.com/user-attachments/assets/114cc66e-70fd-49fb-9bb1-f2b4026d4a86) ### Questions: * Does the license files need to update? No * Is there breaking changes for older versions? No * Does this needs documentation? No Closes #4956 from Reamer/cache_header. Signed-off-by: Philipp Dallig <philipp.dallig@gmail.com>
1 parent d774044 commit 1fe6b04

3 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.zeppelin.rest.filter;
18+
19+
import jakarta.ws.rs.HttpMethod;
20+
import jakarta.ws.rs.container.ContainerRequestContext;
21+
import jakarta.ws.rs.container.ContainerResponseContext;
22+
import jakarta.ws.rs.container.ContainerResponseFilter;
23+
import jakarta.ws.rs.core.CacheControl;
24+
import jakarta.ws.rs.core.HttpHeaders;
25+
26+
public class CacheControlFilter implements ContainerResponseFilter {
27+
28+
@Override
29+
public void filter(ContainerRequestContext req, ContainerResponseContext res) {
30+
if (req.getMethod().equals(HttpMethod.GET)) {
31+
CacheControl cc = new CacheControl();
32+
cc.setNoCache(true);
33+
res.getHeaders().add(HttpHeaders.CACHE_CONTROL, cc);
34+
}
35+
}
36+
}

zeppelin-server/src/main/java/org/apache/zeppelin/server/RestApiApplication.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.zeppelin.rest.SessionRestApi;
3636
import org.apache.zeppelin.rest.ZeppelinRestApi;
3737
import org.apache.zeppelin.rest.exception.WebApplicationExceptionMapper;
38+
import org.apache.zeppelin.rest.filter.CacheControlFilter;
3839
import org.glassfish.jersey.server.ServerProperties;
3940

4041
public class RestApiApplication extends Application {
@@ -57,6 +58,8 @@ public Set<Class<?>> getClasses() {
5758
s.add(WebApplicationExceptionMapper.class);
5859
// add JSON-Consumer and Producer
5960
s.add(GsonProvider.class);
61+
// Filter
62+
s.add(CacheControlFilter.class);
6063
return s;
6164
}
6265

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.zeppelin.rest.filter;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
22+
import java.io.IOException;
23+
24+
import org.apache.http.Header;
25+
import org.apache.http.client.methods.CloseableHttpResponse;
26+
import org.apache.zeppelin.MiniZeppelinServer;
27+
import org.apache.zeppelin.rest.AbstractTestRestApi;
28+
import org.junit.jupiter.api.AfterAll;
29+
import org.junit.jupiter.api.BeforeAll;
30+
import org.junit.jupiter.api.BeforeEach;
31+
import org.junit.jupiter.api.Test;
32+
33+
import jakarta.ws.rs.core.HttpHeaders;
34+
35+
class CacheContolFilterTest extends AbstractTestRestApi {
36+
37+
private static MiniZeppelinServer zepServer;
38+
39+
@BeforeAll
40+
static void init() throws Exception {
41+
zepServer = new MiniZeppelinServer(CacheContolFilterTest.class.getSimpleName());
42+
zepServer.start();
43+
}
44+
45+
@AfterAll
46+
static void destroy() throws Exception {
47+
zepServer.destroy();
48+
}
49+
50+
@BeforeEach
51+
void setUp() {
52+
zConf = zepServer.getZeppelinConfiguration();
53+
}
54+
55+
@Test
56+
void testCacheControl() throws IOException {
57+
try (CloseableHttpResponse get = httpGet("/version")) {
58+
Header[] headers = get.getHeaders(HttpHeaders.CACHE_CONTROL);
59+
assertEquals(1, headers.length);
60+
Header cacheHeader = headers[0];
61+
assertEquals(HttpHeaders.CACHE_CONTROL, cacheHeader.getName());
62+
assertTrue(cacheHeader.getValue().contains("no-cache"), () -> "Actual content:" + cacheHeader.getValue());
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)