Guest User

Untitled

a guest
Jun 21st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. //
  2. // デフォルトサーブレット
  3. //
  4.  
  5. import java.util.regex.*;
  6. import javax.servlet.*;
  7. import javax.servlet.http.*;
  8.  
  9.  
  10. public class MyStaticResourceServlet extends HttpServlet {
  11.  
  12. static String BR = System.getProperty('line.separator')
  13.  
  14. def regex_endswith_slash = { '^/(.*)/$' }
  15. def path_endswith_slash = { "/view.gsp?id=${it.group(1)}/index" }
  16.  
  17.  
  18. @Override
  19. public void doPost( HttpServletRequest request, HttpServletResponse response){ doGet(request,response) }
  20. @Override
  21. public void doGet( HttpServletRequest request, HttpServletResponse response){
  22.  
  23. String spath = getPathInfo(request)
  24. Matcher m = null
  25.  
  26. // chk only slash
  27. m = Pattern.compile(regex_endswith_slash()).matcher(spath)
  28. if( m.matches() ){
  29. def f_spath = path_endswith_slash(m)
  30.  
  31. //println 'Called HTML --------------------------'
  32. //println f_spath
  33.  
  34. request.getRequestDispatcher(f_spath).forward( request,response )
  35. return
  36. }
  37.  
  38. String rPath = getServletContext().getRealPath("/");
  39. def file = new File( new File(rPath), spath.replaceAll('^/','') )
  40. //println "----------- ${file.absolutePath}"
  41.  
  42.  
  43. if( ['js','css'].find{ file.name.endsWith(it) } ){
  44.  
  45. if( file.name.endsWith('css') )
  46. //response.contentType = 'text/css'
  47. response.contentType = 'text/css;charset=UTF-8'
  48. if( file.name.endsWith('js') ){
  49. //response.contentType = 'application/x-javascript'
  50. response.contentType = 'application/x-javascript;charset=UTF-8'
  51. }
  52.  
  53. def w = response.getWriter()
  54. file.newReader('UTF-8').each{ w << it << BR }
  55. w.close()
  56. return
  57. }
  58. else if( ['ico'].find{ file.name.endsWith(it) } ){
  59. response.contentType = 'image/x-icon'
  60. outputAsBinary( response,file )
  61. return
  62. }
  63. else{
  64. outputAsBinary( response,file )
  65. return
  66. }
  67. }
  68.  
  69. private void outputAsBinary( def response , def file ){
  70. def out = response.outputStream
  71.  
  72. def is = file.newInputStream()
  73. while( true ){
  74. def data = is.read()
  75. if( data==-1 ){ break }
  76.  
  77. out.write( data )
  78. }
  79. is.close()
  80.  
  81. out.close()
  82. }
  83.  
  84. static private String getPathInfo(HttpServletRequest request){
  85.  
  86. String spath=(String)request.getAttribute("javax.servlet.include.servlet_path");
  87. if (spath == null) {
  88. spath = ((HttpServletRequest) request).getServletPath();
  89. if (spath == null || spath.length() == 0) {
  90. // Servlet 2.1 puts the path of an extension-matched
  91. // servlet in PathInfo.
  92. spath = ((HttpServletRequest) request).getPathInfo();
  93. }
  94. }
  95.  
  96. return spath;
  97. }
  98. }
Add Comment
Please, Sign In to add comment