@Test public void testContentTypeSelection() throws Exception { Map<String, MediaType> map = new HashMap<>(); map.put("txt", MediaType.create("foo", "bar")); ContentTypeSelector selector = new ContentTypeSelector(map); RequestHeaders headers = mock(RequestHeaders.class); when(headers.isAccepted(any())).thenReturn(true); when(headers.acceptedTypes()).thenReturn(Collections.emptyList()); assertThat(selector.determine("foo.xml", headers), is(MediaType.APPLICATION_XML)); assertThat(selector.determine("foo.txt", headers), is(MediaType.create("foo", "bar"))); assertThat(selector.determine("foo.undefined", headers), is(MediaType.APPLICATION_OCTET_STREAM)); assertThat(selector.determine("undefined", headers), is(MediaType.APPLICATION_OCTET_STREAM)); headers = mock(RequestHeaders.class); when(headers.acceptedTypes()).thenReturn(Collections.singletonList(MediaType.TEXT_HTML)); assertThat(selector.determine("foo.undefined", headers), is(MediaType.TEXT_HTML)); }
MediaType determine(String filename, RequestHeaders requestHeaders) { MediaType mediaType = get(filename); List<MediaType> accepted = requestHeaders.acceptedTypes(); if (mediaType == null) { if (accepted.isEmpty()) { return MediaType.APPLICATION_OCTET_STREAM; } else { return accepted.get(0); } } else { if (requestHeaders.isAccepted(mediaType)) { return mediaType; } else { throw new HttpException("Not accepted media-type!", Http.Status.NOT_FOUND_404); } } }
ContentTypeSelector { MediaType determine(String filename, RequestHeaders requestHeaders) { MediaType mediaType = get(filename); List<MediaType> accepted = requestHeaders.acceptedTypes(); if (mediaType == null) { if (accepted.isEmpty()) { return MediaType.APPLICATION_OCTET_STREAM; } else { return accepted.get(0); } } else { if (requestHeaders.isAccepted(mediaType)) { return mediaType; } else { throw new HttpException("Not accepted media-type!", Http.Status.NOT_FOUND_404); } } } }
ContentTypeSelector { MediaType determine(String filename, RequestHeaders requestHeaders) { MediaType mediaType = get(filename); List<MediaType> accepted = requestHeaders.acceptedTypes(); if (mediaType == null) { if (accepted.isEmpty()) { return MediaType.APPLICATION_OCTET_STREAM; } else { return accepted.get(0); } } else { if (requestHeaders.isAccepted(mediaType)) { return mediaType; } else { throw new HttpException("Not accepted media-type!", Http.Status.NOT_FOUND_404); } } } ContentTypeSelector(Map<String, MediaType> specificContentTypes); }
ContentTypeSelector { MediaType determine(String filename, RequestHeaders requestHeaders) { MediaType mediaType = get(filename); List<MediaType> accepted = requestHeaders.acceptedTypes(); if (mediaType == null) { if (accepted.isEmpty()) { return MediaType.APPLICATION_OCTET_STREAM; } else { return accepted.get(0); } } else { if (requestHeaders.isAccepted(mediaType)) { return mediaType; } else { throw new HttpException("Not accepted media-type!", Http.Status.NOT_FOUND_404); } } } ContentTypeSelector(Map<String, MediaType> specificContentTypes);  }
ContentTypeSelector { MediaType determine(String filename, RequestHeaders requestHeaders) { MediaType mediaType = get(filename); List<MediaType> accepted = requestHeaders.acceptedTypes(); if (mediaType == null) { if (accepted.isEmpty()) { return MediaType.APPLICATION_OCTET_STREAM; } else { return accepted.get(0); } } else { if (requestHeaders.isAccepted(mediaType)) { return mediaType; } else { throw new HttpException("Not accepted media-type!", Http.Status.NOT_FOUND_404); } } } ContentTypeSelector(Map<String, MediaType> specificContentTypes);   }