AMServlet extends HttpServlet { @Override protected void doGet( HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException { try { AMRequestHandler<?> amRequestHandler = _amRequestHandlerLocator.locateForPattern( _getRequestHandlerPattern(httpServletRequest)); if (amRequestHandler == null) { httpServletResponse.sendError( HttpServletResponse.SC_NOT_FOUND, httpServletRequest.getRequestURI()); return; } Optional<AdaptiveMedia<?>> adaptiveMediaOptional = (Optional<AdaptiveMedia<?>>)amRequestHandler.handleRequest( httpServletRequest); AdaptiveMedia<?> adaptiveMedia = adaptiveMediaOptional.orElseThrow( AMException.AMNotFound::new); Optional<Long> contentLengthOptional = adaptiveMedia.getValueOptional( AMAttribute.getContentLengthAMAttribute()); long contentLength = contentLengthOptional.orElse(0L); Optional<String> contentTypeOptional = adaptiveMedia.getValueOptional( AMAttribute.getContentTypeAMAttribute()); String contentType = contentTypeOptional.orElse( ContentTypes.APPLICATION_OCTET_STREAM); Optional<String> fileNameOptional = adaptiveMedia.getValueOptional( AMAttribute.getFileNameAMAttribute()); String fileName = fileNameOptional.orElse(null); boolean download = ParamUtil.getBoolean( httpServletRequest, "download"); if (download) { ServletResponseUtil.sendFile( httpServletRequest, httpServletResponse, fileName, adaptiveMedia.getInputStream(), contentLength, contentType, HttpHeaders.CONTENT_DISPOSITION_ATTACHMENT); } else { ServletResponseUtil.sendFile( httpServletRequest, httpServletResponse, fileName, adaptiveMedia.getInputStream(), contentLength, contentType); } } catch (AMException.AMNotFound amException) { httpServletResponse.sendError( HttpServletResponse.SC_NOT_FOUND, httpServletRequest.getRequestURI()); } catch (Exception exception) { if (_log.isWarnEnabled()) { _log.warn(exception, exception); } Throwable throwable = exception.getCause(); if (throwable instanceof PrincipalException) { httpServletResponse.sendError( HttpServletResponse.SC_FORBIDDEN, httpServletRequest.getRequestURI()); } else { httpServletResponse.sendError( HttpServletResponse.SC_BAD_REQUEST, httpServletRequest.getRequestURI()); } } }    }
@Test public void testMiscellaneousError() throws Exception { Mockito.when( _httpServletRequest.getPathInfo() ).thenReturn( RandomTestUtil.randomString() ); Mockito.when( (AMRequestHandler<Object>)_amRequestHandlerLocator.locateForPattern( Mockito.anyString()) ).thenReturn( (AMRequestHandler<Object>)_amRequestHandler ); Mockito.when( _amRequestHandler.handleRequest(_httpServletRequest) ).thenThrow( new IllegalArgumentException() ); _amServlet.doGet(_httpServletRequest, _httpServletResponse); Mockito.verify( _httpServletResponse ).sendError( Mockito.eq(HttpServletResponse.SC_BAD_REQUEST), Mockito.anyString() ); }
@Test public void testNoMediaFound() throws Exception { Mockito.when( _httpServletRequest.getPathInfo() ).thenReturn( RandomTestUtil.randomString() ); Mockito.when( (AMRequestHandler<Object>)_amRequestHandlerLocator.locateForPattern( Mockito.anyString()) ).thenReturn( (AMRequestHandler<Object>)_amRequestHandler ); Mockito.when( _amRequestHandler.handleRequest(_httpServletRequest) ).thenReturn( Optional.empty() ); _amServlet.doGet(_httpServletRequest, _httpServletResponse); Mockito.verify( _httpServletResponse ).sendError( Mockito.eq(HttpServletResponse.SC_NOT_FOUND), Mockito.anyString() ); }
@Test public void testNoMediaFoundWithException() throws Exception { Mockito.when( _httpServletRequest.getPathInfo() ).thenReturn( RandomTestUtil.randomString() ); Mockito.when( (AMRequestHandler<Object>)_amRequestHandlerLocator.locateForPattern( Mockito.anyString()) ).thenReturn( (AMRequestHandler<Object>)_amRequestHandler ); Mockito.when( _amRequestHandler.handleRequest(_httpServletRequest) ).thenThrow( AMException.AMNotFound.class ); _amServlet.doGet(_httpServletRequest, _httpServletResponse); Mockito.verify( _httpServletResponse ).sendError( Mockito.eq(HttpServletResponse.SC_NOT_FOUND), Mockito.anyString() ); }
@Test public void testNoPermissionError() throws Exception { Mockito.when( _httpServletRequest.getPathInfo() ).thenReturn( RandomTestUtil.randomString() ); Mockito.when( (AMRequestHandler<Object>)_amRequestHandlerLocator.locateForPattern( Mockito.anyString()) ).thenReturn( (AMRequestHandler<Object>)_amRequestHandler ); Mockito.when( _amRequestHandler.handleRequest(_httpServletRequest) ).thenThrow( new ServletException(new PrincipalException()) ); _amServlet.doGet(_httpServletRequest, _httpServletResponse); Mockito.verify( _httpServletResponse ).sendError( Mockito.eq(HttpServletResponse.SC_FORBIDDEN), Mockito.anyString() ); }
@Test public void testNoRequestHandlerFound() throws Exception { Mockito.when( _httpServletRequest.getPathInfo() ).thenReturn( RandomTestUtil.randomString() ); Mockito.when( _amRequestHandlerLocator.locateForPattern(Mockito.anyString()) ).thenReturn( null ); _amServlet.doGet(_httpServletRequest, _httpServletResponse); Mockito.verify( _httpServletResponse ).sendError( Mockito.eq(HttpServletResponse.SC_NOT_FOUND), Mockito.anyString() ); }