/** * Creates a map which decorates the given <code>map</code> and * creates the value collections using the supplied <code>collectionFactory</code>. * * @param <K> the key type * @param <V> the value type * @param <C> the collection class type * @param map the map to decorate * @param collectionFactory the collection factory (must return a Collection object). * @return a new multi-value map * @since 4.0 */ public static <K, V, C extends Collection<V>> MultiValueMap<K, V> multiValueMap(final Map<K, ? super C> map, final Factory<C> collectionFactory) { return new MultiValueMap<>(map, collectionFactory); }
/** * Creates a map which decorates the given <code>map</code> and * maps keys to collections of type <code>collectionClass</code>. * * @param <K> the key type * @param <V> the value type * @param <C> the collection class type * @param map the map to wrap * @param collectionClass the type of the collection class * @return a new multi-value map * @since 4.0 */ public static <K, V, C extends Collection<V>> MultiValueMap<K, V> multiValueMap(final Map<K, ? super C> map, final Class<C> collectionClass) { return new MultiValueMap<>(map, new ReflectionFactory<>(collectionClass)); }
public StructuralIndex() { this.valuesMap = new TreeMap<>(); this.map = new MultiValueMap<>(); }
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); headers = new MultiValueMap(); Iterator iter = serializableHeadersBackingList.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Entry) iter.next(); Collection c = (Collection) entry.getValue(); for (Iterator ic = c.iterator(); ic.hasNext();) { headers.put(entry.getKey(), ic.next()); } } serializableHeadersBackingList = null; }
public CMUdict(Reader reader) throws IOException, PronouncingDictionaryException { String line; int lineNumber = 0; final Pattern cmuPattern = Pattern.compile("(.+?)(\\(\\d+\\))? (.*)"); final BufferedReader bufRead = new BufferedReader(reader); dict = new MultiValueMap(); while ((line = bufRead.readLine()) != null) { lineNumber++; if (line.length() < 3) { throw new PronouncingDictionaryException( "syntax error on line " + lineNumber); } // Skip comment lines if (line.substring(0, 3).equals(";;;")) { continue; } Matcher m = cmuPattern.matcher(line); if (!m.find()) { throw new PronouncingDictionaryException( "syntax error on line " + lineNumber); } dict.put(m.group(1), m.group(3)); } bufRead.close(); }
@Test public void testJustSends304WithNoBodyIfRequestHeadersAskForIt() throws Exception { long timeStampInSeconds = System.currentTimeMillis() / 1000 * 1000; final InMemoryCachedEntry cachedPage = new InMemoryCachedEntry("dummy".getBytes(), "text/plain", "ASCII", 200, new MultiValueMap(), timeStampInSeconds, originalURL, -1); when(cachePolicy.shouldCache(cache, aggregationState, flushPolicy)).thenReturn(new CachePolicyResult(CachePolicyResult.useCache, "/test-page", cachedPage)); when(request.getDateHeader("If-Modified-Since")).thenReturn(timeStampInSeconds); // use some date in the future, so we're ahead of what cachedPage will say when(request.getHeader("If-None-Match")).thenReturn(null); when(response.isCommitted()).thenReturn(false); response.setStatus(304); // since we don't when response.getOuputStream(), we actually assert nothing is written to the body executeNoCacheFilterAndVerify(); }
SAXReader reader = new SAXReader(); Document document = reader.read(dictionaryFilename); dict = new MultiValueMap();
@Test public void testJustSends304WithNoBodyIfRequestHeadersAskForItEvenOnCommitedResponse() throws Exception { long timeStampInSeconds = System.currentTimeMillis() / 1000 * 1000; final InMemoryCachedEntry cachedPage = new InMemoryCachedEntry("dummy".getBytes(), "text/plain", "ASCII", 200, new MultiValueMap(), timeStampInSeconds, originalURL, -1); when(cachePolicy.shouldCache(cache, aggregationState, flushPolicy)).thenReturn(new CachePolicyResult(CachePolicyResult.useCache, "/test-page", cachedPage)); when(request.getDateHeader("If-Modified-Since")).thenReturn(timeStampInSeconds); // use some date in the future, so we're ahead of what cachedPage will say when(request.getHeader("If-None-Match")).thenReturn(null); // the main diff to the test above - response is already committed here and status (304) was set by another executed as indicated in preCacheStatusCode of the ContentCachedEntry when(response.isCommitted()).thenReturn(true); response.setStatus(304); // since the status is correct we don't when response.getOuputStream(), we actually assert nothing is written to the body when(webContext.getResponse()).thenReturn(response); when(request.getHeaders("Accept-Encoding")).thenReturn(enumeration("foo", "gzip", "bar")); when(response.containsHeader("Content-Encoding")).thenReturn(true); final ByteArrayOutputStream fakedOut = new ByteArrayOutputStream(); when(response.getOutputStream()).thenReturn(new SimpleServletOutputStream(fakedOut)); executeNoCacheFilterAndVerify(); }
@Test public void testPageShouldBeServedIfIfNoneMatchHeaderWasPassed() throws Exception { final String dummyContent = "i'm a dummy page that was cached earlier on"; final byte[] gzipped = GZipUtil.gzip(dummyContent.getBytes()); final InMemoryCachedEntry cachedPage = new InMemoryCachedEntry(dummyContent.getBytes(), "text/plain", "ASCII", 200, new MultiValueMap(), System.currentTimeMillis(), originalURL, -1); when(cachePolicy.shouldCache(cache, aggregationState, flushPolicy)).thenReturn(new CachePolicyResult(CachePolicyResult.useCache, "/test-page", cachedPage)); when(request.getDateHeader("If-Modified-Since")).thenReturn(System.currentTimeMillis() + 1000); // use some date in the future, so we're ahead of what cachedPage will say when(request.getHeader("If-None-Match")).thenReturn("Some value"); when(request.getHeader("User-Agent")).thenReturn("Mozilla/4.0 (MSIE 7.0; Windows NT 5.1)"); when(request.getHeaders("Accept-Encoding")).thenReturn(enumeration("foo", "gzip", "bar")); final ByteArrayOutputStream fakedOut = new ByteArrayOutputStream(); response.setStatus(200); response.setContentType("text/plain"); response.setCharacterEncoding("ASCII"); when(response.isCommitted()).thenReturn(false); when(response.containsHeader("Content-Encoding")).thenReturn(false); response.addHeader("Content-Encoding", "gzip"); when(response.containsHeader("Content-Encoding")).thenReturn(true); response.addHeader("Vary", "Accept-Encoding"); when(response.containsHeader("Vary")).thenReturn(true); response.setContentLength(gzipped.length); when(response.getOutputStream()).thenReturn(new SimpleServletOutputStream(fakedOut)); response.flushBuffer(); when(webContext.getResponse()).thenReturn(response); executeNoCacheFilterAndVerify(); assertTrue(Arrays.equals(gzipped, fakedOut.toByteArray())); }
@Test public void testDontJustSends304WithNoBodyIfRequestHeadersAskForItButResponseIsCommitted() throws Exception { long timeStampInSeconds = System.currentTimeMillis() / 1000 * 1000; final InMemoryCachedEntry cachedPage = new InMemoryCachedEntry("dummy".getBytes(), "text/plain", "ASCII", 200, new MultiValueMap(), timeStampInSeconds, originalURL, -1); final byte[] gzipped = GZipUtil.gzip("dummy".getBytes()); when(cachePolicy.shouldCache(cache, aggregationState, flushPolicy)).thenReturn(new CachePolicyResult(CachePolicyResult.useCache, "/test-page", cachedPage)); when(request.getDateHeader("If-Modified-Since")).thenReturn(timeStampInSeconds); // use some date in the future, so we're ahead of what cachedPage will say when(request.getHeader("If-None-Match")).thenReturn(null); when(request.getHeader("User-Agent")).thenReturn("Mozilla/4.0 (MSIE 7.0; Windows NT 5.1)"); when(response.isCommitted()).thenReturn(true); // we can't change status of the already committed response ... the only option left here is to proceed and send the data when(request.getHeaders("Accept-Encoding")).thenReturn(enumeration("foo", "gzip", "bar")); response.setStatus(200); response.setContentType("text/plain"); response.setCharacterEncoding("ASCII"); when(response.isCommitted()).thenReturn(false); when(response.containsHeader("Content-Encoding")).thenReturn(false); response.addHeader("Content-Encoding", "gzip"); when(response.containsHeader("Content-Encoding")).thenReturn(true); response.addHeader("Vary", "Accept-Encoding"); when(response.containsHeader("Vary")).thenReturn(true); response.setContentLength(gzipped.length); final ByteArrayOutputStream fakedOut = new ByteArrayOutputStream(); when(response.getOutputStream()).thenReturn(new SimpleServletOutputStream(fakedOut)); response.flushBuffer(); executeNoCacheFilterAndVerify(); }
@Test public void testGZipIfContentIsNotGZipped() throws IOException { final InMemoryCachedEntry p = new InMemoryCachedEntry("hello".getBytes(), "foo/bar", "cha", 1, new MultiValueMap(), System.currentTimeMillis(), "originalUrl", -1); assertEquals("hello", new String(GZipUtil.ungzip(p.getGzippedContent()))); assertEquals("hello", new String(p.getPlainContent())); } }
@Test public void testServesGZippedContentIfClientAcceptsGZipEncoding() throws Exception { final String dummyContent = "hello i'm a page that was cached"; final InMemoryCachedEntry cachedPage = new InMemoryCachedEntry(dummyContent.getBytes(), "text/plain", "ASCII", 123, new MultiValueMap(), System.currentTimeMillis(), originalURL, -1); when(cachePolicy.shouldCache(cache, aggregationState, flushPolicy)).thenReturn(new CachePolicyResult(CachePolicyResult.useCache, "/test-page", cachedPage)); when(request.getDateHeader("If-Modified-Since")).thenReturn(-1l); when(request.getHeader("User-Agent")).thenReturn("Mozilla/4.0 (MSIE 7.0; Windows NT 5.1)"); when(request.getHeaders("Accept-Encoding")).thenReturn(enumeration("foo", "gzip", "bar")); final ByteArrayOutputStream fakedOut = new ByteArrayOutputStream(); response.setStatus(123); response.setContentType("text/plain"); response.setCharacterEncoding("ASCII"); when(response.isCommitted()).thenReturn(false); when(response.containsHeader("Content-Encoding")).thenReturn(false); response.addHeader("Content-Encoding", "gzip"); when(response.containsHeader("Content-Encoding")).thenReturn(true); response.addHeader("Vary", "Accept-Encoding"); when(response.containsHeader("Vary")).thenReturn(true); byte[] gzipped = GZipUtil.gzip(dummyContent.getBytes()); response.setContentLength(gzipped.length); when(response.getOutputStream()).thenReturn(new SimpleServletOutputStream(fakedOut)); response.flushBuffer(); when(webContext.getResponse()).thenReturn(response); executeNoCacheFilterAndVerify(); assertTrue(Arrays.equals(gzipped, fakedOut.toByteArray())); }
@Test public void replay() throws Exception { // GIVEN MultiMap headers = new MultiValueMap(); headers.put(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_NO_CACHE); when(response.containsHeader(CacheConstants.HEADER_CACHE_CONTROL)).thenReturn(true); cachedEntry = new ContentCachedEntry("contentType", "characterEncoding", 0, headers, 0l, "originalUrl", 0) { @Override protected void writeContent(HttpServletRequest request, HttpServletResponse response, FilterChain chain, boolean acceptsGzipEncoding) throws IOException, ServletException { } @Override protected boolean canServeGzipContent() { return false; } }; // WHEN cachedEntry.replay(request, response, null); // THEN verify(response).setHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_NO_CACHE); } }
@Test public void testServesUnzippedContentAndRemovesGzipHeadersIfClientDoesNotAcceptGZipEncoding() throws Exception { final String dummyContent = "hello i'm a page that was cached"; final MultiValueMap headers = new MultiValueMap(); headers.put("Content-Encoding", "gzip"); headers.put("Vary", "Accept-Encoding"); headers.put("Dummy", "Some Value"); final InMemoryCachedEntry cachedPage = new InMemoryCachedEntry(dummyContent.getBytes(), "text/plain", "ASCII", 123, headers, System.currentTimeMillis(), originalURL, -1); when(cachePolicy.shouldCache(cache, aggregationState, flushPolicy)).thenReturn(new CachePolicyResult(CachePolicyResult.useCache, "/test-page", cachedPage)); when(request.getDateHeader("If-Modified-Since")).thenReturn(-1l); when(request.getHeader("User-Agent")).thenReturn("Mozilla/4.0 (MSIE 7.0; Windows NT 5.1)"); when(request.getHeaders("Accept-Encoding")).thenReturn(enumeration()); final ByteArrayOutputStream fakedOut = new ByteArrayOutputStream(); response.setStatus(123); when(response.containsHeader("Dummy")).thenReturn(false); response.addHeader("Dummy", "Some Value"); response.setContentType("text/plain"); response.setCharacterEncoding("ASCII"); response.setContentLength(dummyContent.length()); when(response.getOutputStream()).thenReturn(new SimpleServletOutputStream(fakedOut)); response.flushBuffer(); when(webContext.getResponse()).thenReturn(response); executeNoCacheFilterAndVerify(); assertEquals(dummyContent, fakedOut.toString()); }
@Ignore @Test public void testIgnoreEncodingAndServeContentFlatWhenUserAgentIsIE6() throws Exception { final String dummyContent = "hello i'm a page that was cached"; final MultiValueMap headers = new MultiValueMap(); headers.put("Last-Modified", 2000l); headers.put("Dummy", "dummy"); headers.put("Dummy", "dummy2"); final InMemoryCachedEntry cachedPage = new InMemoryCachedEntry(dummyContent.getBytes(), "text/plain", "ASCII", 123, headers, System.currentTimeMillis(), originalURL, -1); when(cachePolicy.shouldCache(cache, aggregationState, flushPolicy)).thenReturn(new CachePolicyResult(CachePolicyResult.useCache, "/test-page", cachedPage)); when(request.getDateHeader("If-Modified-Since")).thenReturn(-1l); when(request.getHeader("User-Agent")).thenReturn("Mozilla/4.0 (MSIE 6.0; Windows NT 5.1)"); final ByteArrayOutputStream fakedOut = new ByteArrayOutputStream(); response.setStatus(123); when(response.containsHeader("Last-Modified")).thenReturn(false); response.addDateHeader("Last-Modified", 2000l); when(response.containsHeader("Dummy")).thenReturn(false); response.addHeader("Dummy", "dummy"); response.addHeader("Dummy", "dummy2"); response.setContentType("text/plain"); response.setCharacterEncoding("ASCII"); response.setContentLength(32); when(response.getOutputStream()).thenReturn(new SimpleServletOutputStream(fakedOut)); response.flushBuffer(); when(webContext.getResponse()).thenReturn(response); when(request.getHeaders("Accept-Encoding")).thenReturn(enumeration("foo", "gzip", "bar")); when(response.containsHeader("Content-Encoding")).thenReturn(true); executeNoCacheFilterAndVerify(); assertTrue(Arrays.equals(dummyContent.getBytes(), fakedOut.toByteArray())); }
final String dummyContent = "hello i'm a page that was cached"; final MultiValueMap headers = new MultiValueMap(); headers.put("Last-Modified", 2000l); headers.put("Dummy", "dummy");
@Test public void testUnGZipIfContentIsGZipped() throws IOException { final String s = "hello"; final byte[] gzipped = GZipUtil.gzip(s.getBytes()); final MultiValueMap responseHeaders = new MultiValueMap(); responseHeaders.put("Content-Encoding", "gzip"); final InMemoryCachedEntry p = new InMemoryCachedEntry(gzipped, "text/plain", "cha", 1, responseHeaders, System.currentTimeMillis(), "originalUrl", -1); assertEquals(gzipped, p.getGzippedContent()); assertEquals("hello", new String(p.getPlainContent())); }