FixedCacheControlOutInterceptor extends AbstractPhaseInterceptor<Message> { @SuppressWarnings("unchecked") public final void handleMessage(Message message) { OutputStream os = message.getContent(OutputStream.class); if (os == null) { return; } MultivaluedMap<String, Object> headers = (MetadataMap<String, Object>) message.get(Message.PROTOCOL_HEADERS); if (headers == null) { headers = new MetadataMap<String, Object>(); } headers.add("Cache-Control", new String("no-cache")); headers.add("Expires", new String("-1")); message.put(Message.PROTOCOL_HEADERS, headers); } FixedCacheControlOutInterceptor(); @SuppressWarnings("unchecked") final void handleMessage(Message message);  }
@Test public void testCacheControlHeader() { Message message = new MessageImpl(); ByteArrayOutputStream sw = new ByteArrayOutputStream(); message.setContent(OutputStream.class , sw); FixedCacheControlOutInterceptor interceptor = new FixedCacheControlOutInterceptor(); interceptor.handleMessage(message); @SuppressWarnings("unchecked") MetadataMap<String, Object> headers = (MetadataMap<String, Object>) message.get(Message.PROTOCOL_HEADERS); assertEquals(((List)headers.get("Expires")).get(0), "-1"); assertEquals(((List)headers.get("Cache-Control")).get(0), "no-cache"); }
@SuppressWarnings("unchecked") @Test public void testCacheControlPreserveExisting() { Message message = new MessageImpl(); ByteArrayOutputStream sw = new ByteArrayOutputStream(); message.setContent(OutputStream.class , sw); MetadataMap<String, Object> headers = (MetadataMap<String, Object>) message.get(Message.PROTOCOL_HEADERS); if (headers == null) { headers = new MetadataMap<String, Object>(); } headers.add("Test", new String("Test")); message.put(Message.PROTOCOL_HEADERS, headers); FixedCacheControlOutInterceptor interceptor = new FixedCacheControlOutInterceptor(); interceptor.handleMessage(message); headers = (MetadataMap<String, Object>) message.get(Message.PROTOCOL_HEADERS); ((List)headers.get("Cache-Control")).get(0); assertEquals(((List)headers.get("Cache-Control")).get(0), "no-cache"); assertEquals(((List)headers.get("Expires")).get(0), "-1"); assertEquals(((List)headers.get("Test")).get(0), new String("Test")); }