@Test public void testInvalidFile(){ ContentTypeSelector selector = new ContentTypeSelector(Collections.emptyMap()); RequestHeaders headers = mock(RequestHeaders.class); when(headers.isAccepted(any())).thenReturn(false); when(headers.acceptedTypes()).thenReturn(Collections.singletonList(MediaType.TEXT_HTML)); HttpException ex = assertThrows(HttpException.class, () -> { selector.determine("foo.xml", headers); }); assertThat(ex.getMessage(), is("Not accepted media-type!")); }
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);   }