Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. /**
  2. * Copy response body data (the entity) from the proxy to the servlet client.
  3. * TODO: CACHE entity here for retrieval in filter
  4. */
  5. protected void copyResponseEntity( HttpResponse proxyResponse, HttpServletResponse servletResponse,
  6. HttpRequest proxyRequest, HttpServletRequest servletRequest ) throws IOException
  7. {
  8. HttpEntity entity = proxyResponse.getEntity();
  9. if ( entity != null )
  10. {
  11. String key = getCurrentUrlFromRequest( servletRequest ); // 1
  12. basicCache.getCache().put( key, proxyResponse.getEntity() ); // 2
  13. OutputStream servletOutputStream = servletResponse.getOutputStream();
  14. entity.writeTo( servletOutputStream );
  15. }
  16. }
  17.  
  18. private FilterConfig filterConfig;
  19.  
  20. @Autowired BasicCache basicCache;
  21.  
  22.  
  23. @Override
  24. public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain )
  25. throws IOException, ServletException
  26. {
  27. String url = getCurrentUrlFromRequest( servletRequest ); // 1
  28. HttpEntity page = (HttpEntity) basicCache.getCache().get( url ); //2
  29. if ( null != page ) // 3
  30. {
  31. OutputStream servletOutputStream = servletResponse.getOutputStream(); // 4
  32. page.writeTo( servletOutputStream ); // 5 stream closed :(
  33. }
  34. else
  35. {
  36. filterChain.doFilter( servletRequest, servletResponse );
  37. }
  38.  
  39. }
  40.  
  41.  
  42. public String getCurrentUrlFromRequest( ServletRequest request )
  43. {
  44. if ( !( request instanceof HttpServletRequest ) ) return null;
  45.  
  46. return getCurrentUrlFromRequest( (HttpServletRequest) request );
  47. }
  48.  
  49. public String getCurrentUrlFromRequest( HttpServletRequest request )
  50. {
  51. StringBuffer requestURL = request.getRequestURL();
  52. String queryString = request.getQueryString();
  53.  
  54. if ( queryString == null ) return requestURL.toString();
  55.  
  56. return requestURL.append( '?' ).append( queryString ).toString();
  57. }
  58.  
  59. @Override
  60. public void destroy()
  61. {
  62. }
  63.  
  64. @Override
  65. public void init( FilterConfig filterConfig ) throws ServletException
  66. {
  67. this.filterConfig = filterConfig;
  68. }
  69.  
  70. private UserManagedCache<String, HttpEntity> userManagedCache;
  71.  
  72. public BasicCache()
  73. {
  74. userManagedCache = UserManagedCacheBuilder.newUserManagedCacheBuilder( String.class, HttpEntity.class )
  75. .build( true );
  76. }
  77.  
  78. public UserManagedCache getCache()
  79. {
  80. return userManagedCache;
  81. }
  82.  
  83. public void destroy()
  84. {
  85. if ( null != userManagedCache )
  86. {
  87. userManagedCache.close();
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement