@Test public void keyNotFound() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader( "Accept", MediaType.APPLICATION_JSON ); request.setServletPath( "cache" ); request.setPathInfo( "/foo" ); MockHttpServletResponse response = new MockHttpServletResponse(); directMemoryServlet.doGet( request, response ); assertEquals( HttpServletResponse.SC_NO_CONTENT, response.getStatus() ); }
@Override protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { String path = req.getPathInfo(); String servletPath = req.getServletPath(); String key = retrieveKeyFromPath( path ); if ( StringUtils.isEmpty( key ) ) { resp.sendError( HttpServletResponse.SC_BAD_REQUEST, "key missing in path" ); return; } String acceptContentType = req.getHeader( "Accept" ); if ( StringUtils.isEmpty( acceptContentType ) ) { resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "you must specify Accept with Content-Type you want in the response" ); return; } ContentTypeHandler contentTypeHandler = findGetCacheContentTypeHandler( req, resp ); if ( contentTypeHandler == null ) { String contentType = req.getContentType(); log.error( "No content type handler for content type {}", acceptContentType ); resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Content-Type: " + acceptContentType + " not supported" ); return; } byte[] bytes = cacheService.retrieveByteArray( key ); log.debug( "return content size {} for key {}", ( bytes == null ? "null" : bytes.length ), key ); if ( bytes == null || bytes.length == 0 ) { resp.sendError( HttpServletResponse.SC_NO_CONTENT, "No content for key: " + key ); return; } try { byte[] respBytes = contentTypeHandler.handleGet( new DirectMemoryRequest().setKey( key ), bytes, resp, req ); resp.getOutputStream().write( respBytes ); } catch ( DirectMemoryException e ) { resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage() ); } }
DirectMemoryServlet extends HttpServlet { @Override protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { String path = req.getPathInfo(); String servletPath = req.getServletPath(); String key = retrieveKeyFromPath( path ); if ( StringUtils.isEmpty( key ) ) { resp.sendError( HttpServletResponse.SC_BAD_REQUEST, "key missing in path" ); return; } String acceptContentType = req.getHeader( "Accept" ); if ( StringUtils.isEmpty( acceptContentType ) ) { resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "you must specify Accept with Content-Type you want in the response" ); return; } ContentTypeHandler contentTypeHandler = findGetCacheContentTypeHandler( req, resp ); if ( contentTypeHandler == null ) { String contentType = req.getContentType(); log.error( "No content type handler for content type {}", acceptContentType ); resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Content-Type: " + acceptContentType + " not supported" ); return; } byte[] bytes = cacheService.retrieveByteArray( key ); log.debug( "return content size {} for key {}", ( bytes == null ? "null" : bytes.length ), key ); if ( bytes == null || bytes.length == 0 ) { resp.sendError( HttpServletResponse.SC_NO_CONTENT, "No content for key: " + key ); return; } try { byte[] respBytes = contentTypeHandler.handleGet( new DirectMemoryRequest().setKey( key ), bytes, resp, req ); resp.getOutputStream().write( respBytes ); } catch ( DirectMemoryException e ) { resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage() ); } } }
DirectMemoryServlet extends HttpServlet { @Override protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { String path = req.getPathInfo(); String servletPath = req.getServletPath(); String key = retrieveKeyFromPath( path ); if ( StringUtils.isEmpty( key ) ) { resp.sendError( HttpServletResponse.SC_BAD_REQUEST, "key missing in path" ); return; } String acceptContentType = req.getHeader( "Accept" ); if ( StringUtils.isEmpty( acceptContentType ) ) { resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "you must specify Accept with Content-Type you want in the response" ); return; } ContentTypeHandler contentTypeHandler = findGetCacheContentTypeHandler( req, resp ); if ( contentTypeHandler == null ) { String contentType = req.getContentType(); log.error( "No content type handler for content type {}", acceptContentType ); resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Content-Type: " + acceptContentType + " not supported" ); return; } byte[] bytes = cacheService.retrieveByteArray( key ); log.debug( "return content size {} for key {}", ( bytes == null ? "null" : bytes.length ), key ); if ( bytes == null || bytes.length == 0 ) { resp.sendError( HttpServletResponse.SC_NO_CONTENT, "No content for key: " + key ); return; } try { byte[] respBytes = contentTypeHandler.handleGet( new DirectMemoryRequest().setKey( key ), bytes, resp, req ); resp.getOutputStream().write( respBytes ); } catch ( DirectMemoryException e ) { resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage() ); } }  }
DirectMemoryServlet extends HttpServlet { @Override protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { String path = req.getPathInfo(); String servletPath = req.getServletPath(); String key = retrieveKeyFromPath( path ); if ( StringUtils.isEmpty( key ) ) { resp.sendError( HttpServletResponse.SC_BAD_REQUEST, "key missing in path" ); return; } String acceptContentType = req.getHeader( "Accept" ); if ( StringUtils.isEmpty( acceptContentType ) ) { resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "you must specify Accept with Content-Type you want in the response" ); return; } ContentTypeHandler contentTypeHandler = findGetCacheContentTypeHandler( req, resp ); if ( contentTypeHandler == null ) { String contentType = req.getContentType(); log.error( "No content type handler for content type {}", acceptContentType ); resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Content-Type: " + acceptContentType + " not supported" ); return; } byte[] bytes = cacheService.retrieveByteArray( key ); log.debug( "return content size {} for key {}", ( bytes == null ? "null" : bytes.length ), key ); if ( bytes == null || bytes.length == 0 ) { resp.sendError( HttpServletResponse.SC_NO_CONTENT, "No content for key: " + key ); return; } try { byte[] respBytes = contentTypeHandler.handleGet( new DirectMemoryRequest().setKey( key ), bytes, resp, req ); resp.getOutputStream().write( respBytes ); } catch ( DirectMemoryException e ) { resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage() ); } }  @Override void init( ServletConfig config ); @Override void destroy(); }
DirectMemoryServlet extends HttpServlet { @Override protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { String path = req.getPathInfo(); String servletPath = req.getServletPath(); String key = retrieveKeyFromPath( path ); if ( StringUtils.isEmpty( key ) ) { resp.sendError( HttpServletResponse.SC_BAD_REQUEST, "key missing in path" ); return; } String acceptContentType = req.getHeader( "Accept" ); if ( StringUtils.isEmpty( acceptContentType ) ) { resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "you must specify Accept with Content-Type you want in the response" ); return; } ContentTypeHandler contentTypeHandler = findGetCacheContentTypeHandler( req, resp ); if ( contentTypeHandler == null ) { String contentType = req.getContentType(); log.error( "No content type handler for content type {}", acceptContentType ); resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Content-Type: " + acceptContentType + " not supported" ); return; } byte[] bytes = cacheService.retrieveByteArray( key ); log.debug( "return content size {} for key {}", ( bytes == null ? "null" : bytes.length ), key ); if ( bytes == null || bytes.length == 0 ) { resp.sendError( HttpServletResponse.SC_NO_CONTENT, "No content for key: " + key ); return; } try { byte[] respBytes = contentTypeHandler.handleGet( new DirectMemoryRequest().setKey( key ), bytes, resp, req ); resp.getOutputStream().write( respBytes ); } catch ( DirectMemoryException e ) { resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage() ); } }  @Override void init( ServletConfig config ); @Override void destroy();  }