001 /* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016 package org.opengion.hayabusa.filter; 017 018 import org.opengion.hayabusa.common.HybsSystem; 019 020 import java.util.Locale; 021 022 import java.io.File; 023 import java.io.IOException; 024 import java.io.UnsupportedEncodingException; 025 import javax.servlet.ServletRequest; 026 import javax.servlet.ServletResponse; 027 import javax.servlet.ServletContext; 028 import javax.servlet.Filter; 029 import javax.servlet.FilterChain; 030 import javax.servlet.FilterConfig; 031 import javax.servlet.ServletException; 032 import javax.servlet.http.HttpServletRequest; 033 import javax.servlet.http.HttpServletResponse; 034 035 /** 036 * Filter インターフェースを継承した HTML?画面を作?するフィルタクラスです? 037 * web.xml で filter 設定することにより、使用できます? 038 * こ?フィルターでは??常の画面アクセスを行うと、指定?フォル?対して 039 * JSPをHTMLに変換した形で、ファイルをセーブして?ます?こ?HTMLは? 040 * ?サンプル画面として、使用できます? 041 * 出来る限り??画面として使えるように、画面間リンク??ボタン制御? 042 * JavaScript を挿入する事で実現して?す? 043 * 044 * フィルターに対してweb.xml でパラメータを設定します? 045 * ・saveDir :ファイルをセーブするディレクトリ 046 * 047 * パラメータがな??合?、G:/webapps/作番/filetemp/DIR/ 以下に自動設定されます? 048 * また??レクトリが?相対パスの場合?、G:/webapps/作番/ 以下に、絶対パスの 049 * 場合?、そのパスの下に作?されます? * 050 * 051 * 【WEB-INF/web.xml? 052 * <filter> 053 * <filter-name>FileFilter</filter-name> 054 * <filter-class>org.opengion.hayabusa.filter.FileFilter</filter-class> 055 * <init-param> 056 * <param-name>saveDir</param-name> 057 * <param-value>filetemp/DIR/</param-value> 058 * </init-param> 059 * </filter> 060 * 061 * <filter-mapping> 062 * <filter-name>FileFilter</filter-name> 063 * <url-pattern>/jsp/*</url-pattern> 064 * </filter-mapping> 065 * 066 * @og.group フィルター処? 067 * 068 * @version 4.0 069 * @author Kazuhiko Hasegawa 070 * @since JDK5.0, 071 */ 072 public class FileFilter implements Filter { 073 private String saveDir = null; // "G:/webapps/gf/filetemp/DIR/" など 074 075 /** 076 * Filter インターフェースの doFilter メソ? 077 * 078 * Filter クラスの doFilter メソ?はコン?により呼び出され???チェーンにおけ? 079 * リソースへのクライアントリクエスト?ために?毎回リクエスト?レスポンスのペアが? 080 * チェーンを?して渡されます? こ?メソ?に渡され?FilterChain を利用して、Filter ? 081 * リクエストやレスポンスをチェーン??次のエン??(Filter)にリクエストとレスポンス? 082 * 渡す事ができます? 083 * こ?メソ?の典型的な実??以下?ようなパターンとなるでしょ?? 084 * 1. リクエスト?検査 085 * 2. オプションとして、?力フィルタリング用にコン??しくはヘッ?フィルタリング 086 * するためにカスタ??よるリクエストオブジェクト?ラ?? 087 * 3. オプションとして、?力フィルタリング用にコン??しくはヘッ?フィルタリング 088 * するためにカスタ??よるレスポンスオブジェクトラ?? 089 * 4. 以下? a)、b) のどちらか 090 * a) FileterChain オブジェク?chain.doFilter()) を利用してチェーンの次のエン??を呼び出? 091 * b) リクエスト??止めるために、リクエスト?レスポンスのペアをフィルタチェーンの次の 092 * エン??に渡さな? 093 * 5. フィルタチェーンの次のエン??の呼び出した後?直接レスポンスのヘッ?セ? 094 * 095 * @param req ServletRequestオブジェク? 096 * @param res ServletResponseオブジェク? 097 * @param chain FilterChainオブジェク? 098 * @throws IOException 入出力エラーが発生したと? 099 * @throws ServletException サーブレ?関係?エラーが発生した?合?throw されます? 100 */ 101 public void doFilter( final ServletRequest req, 102 final ServletResponse res, 103 final FilterChain chain ) 104 throws IOException, ServletException { 105 if(req instanceof HttpServletRequest && res instanceof HttpServletResponse) { 106 HttpServletRequest request = (HttpServletRequest) req; 107 HttpServletResponse response = (HttpServletResponse) res; 108 109 try { 110 request.setCharacterEncoding( "UTF-8" ); 111 } 112 catch ( UnsupportedEncodingException ex ) { 113 throw new RuntimeException( ex ); 114 } 115 116 String filename = makeFileName( request ); 117 if( filename != null ) { 118 FileResponseWrapper wrappedResponse = new FileResponseWrapper(response,filename); 119 chain.doFilter(req, wrappedResponse); 120 wrappedResponse.finishResponse(); 121 } 122 else { 123 chain.doFilter(req, res); 124 } 125 } 126 } 127 128 /** 129 * フィルターの初期処?ソ?です? 130 * 131 * フィルターに対してweb.xml で初期パラメータを設定します? 132 * ・startTime:停止開始時刻 133 * ・stopTime :停止終?刻 134 * ・filename :停止時メ?ージ表示ファイル? 135 * 136 * @og.rev 5.7.3.2 (2014/02/28) Tomcat8 対応?getRealPath( "/" ) の互換性のための修正? 137 * 138 * @param filterConfig FilterConfigオブジェク? 139 */ 140 public void init(final FilterConfig filterConfig) { 141 ServletContext context = filterConfig.getServletContext(); 142 // String realPath = context.getRealPath( "/" ); 143 String realPath = context.getRealPath( "" ) + File.separator; // 5.7.3.2 (2014/02/28) Tomcat8 対? 144 145 String dir = filterConfig.getInitParameter("saveDir"); 146 if( dir != null && dir.length() > 1 ) { 147 dir = dir.replace( '\\','/' ); 148 if( dir.charAt(0) == '/' || dir.charAt(1) == ':' ) { 149 saveDir = dir; 150 } 151 else { 152 saveDir = realPath + dir ; 153 } 154 155 if( dir.charAt(dir.length()-1) != '/' ) { 156 saveDir = saveDir + "/" ; 157 } 158 } 159 else { 160 saveDir = realPath + "filetemp/DIR/" ; 161 } 162 } 163 164 /** 165 * Filter インターフェースの destroy メソ? (何もしません)? 166 * 167 * サービス状態を終えた事を Filter に伝えるために Web コン?が呼び出します? 168 * Filter の doFilter メソ?が終?たか、タイ?ウトに達した?てのスレ?において? 169 * こ?メソ?を??呼び出されます? Web コン?がこのメソ?を呼び出した後?? 170 * Filter のこ?インスタンスにおいて二度と doFilter メソ?を呼び出す事?ありません? 171 * 172 * こ?メソ?は、フィルタに保持されて?(例えば、メモリ、ファイルハンドル、スレ?) 173 * 様?なリソースを開放する機会を与え?あら?永続?の状態が、メモリ上におけ?Filter 174 * の現在の状態と同期して?ように注意してください? 175 */ 176 public void destroy() { 177 // noop 178 } 179 180 /** 181 * セーブするファイル名を、リクエスト情報より取得します? 182 * 183 * リクエストされたファイル(.jsp)を?HTMLファイル(.htm)にする?でなく? 184 * 呼び出されたとき? command を?に、ファイル名を作?します? 185 * command="NEW" + forward.jsp ? "forward.htm" 186 * command="RENEW" + forward.jsp ? "renew.htm" 187 * command="日本語名+ forward.jsp ? "コマンド名.htm" 188 * command="日本語名+ update.jsp ? "コマンド名.htm" 189 * command="NEW" + index.jsp ? "indexNW.htm" 190 * command="RENEW" + index.jsp ? "indexRNW.htm" 191 * command="NEW" + query.jsp ? "queryNW.htm" 192 * command="NEW" + resultXX.jsp ? "forwardXX.htm" 5.6.3.4 (2013/04/26) result.jsp にフレー?使?ターン(??イン) 193 * matrixMenu対? 194 * URI? URI? request取? 195 * ?gamenId="jsp" + index.jsp + GAMENID=XXXX ?saveDir + "jsp/indexXXXX.htm" Matrixメニューからの画面呼出し? 196 * ② gamenId="jsp" + result.jsp + GAMENID=XXXX ?saveDir + "XXXX/index.htm" 画面QUERYのヘッ??メニュー 197 * ③ gamenId="jsp" + index.jsp + group=YYYY ?saveDir + "jsp/indexYYYY.htm" ?存在しな??ず? 198 * ④ gamenId="menu" + multiMenu.jsp + GAMENID=XXXX ?saveDir + "jsp/menuXXXX.htm" ?存在しな??ず? 199 * ⑤ gamenId="menu" + multiMenu.jsp + group=YYYY ?saveDir + "jsp/menuYYYY.htm" 通常メニューのグループ選? 200 * ⑥ gamenId="menu" + matrixMenu.jsp + group=YYYY ?saveDir + "menu/matrixMenuYYYY.htm" Matrixメニューのグループ選? 201 * そ?? xxxx.jsp ? "xxxx.htm" 202 * 203 * こ?メソ?は、フィルタに保持されて?(例えば、メモリ、ファイルハンドル、スレ?) 204 * 様?なリソースを開放する機会を与え?あら?永続?の状態が、メモリ上におけ?Filter 205 * の現在の状態と同期して?ように注意してください? 206 * 207 * @og.rev 4.0.0.0 (2007/11/28) メソ?の戻り?をチェ?します? 208 * @og.rev 4.3.3.0 (2008/10/01) Matrixメニュー対? 209 * @og.rev 5.5.2.5 (2012/05/21) update.jsp に出力されるファイルを?コマンド名.htm に出力するよ?機?追? 210 * @og.rev 5.6.3.4 (2013/04/26) 5.6.3.4 (2013/04/26) command="NEW" + resultXX.jsp ? "forwardXX.htm"?result.jsp にフレー?使?ターン(??イン) 211 * @og.rev 5.6.4.2 (2013/05/17) Matrixメニュー buttonRequest ?対? 212 * 213 * @param request ServletRequestオブジェク? 214 * 215 * @return セーブするファイル? 216 */ 217 private String makeFileName( final ServletRequest request ) { 218 HttpServletRequest myReq = (HttpServletRequest) request; 219 String requestURI = myReq.getRequestURI(); 220 221 int index2 = requestURI.lastIndexOf( '/' ); 222 String jspID = requestURI.substring( index2+1 ); 223 int index1 = requestURI.lastIndexOf( '/',index2-1 ); 224 String gamenId = requestURI.substring( index1+1,index2 ); 225 226 String file = null; 227 228 if( jspID != null && jspID.endsWith( ".jsp" ) ) { 229 String cmd = request.getParameter( "command" ); 230 if( cmd != null && jspID.equals( "forward.jsp" ) ) { 231 if( "NEW".equals( cmd ) ) { file = "forward.htm"; } 232 else if( "RENEW".equals( cmd ) || "REVIEW".equals( cmd ) ) { file = "renew.htm"; } 233 else { 234 String xferVal = request.getParameter( HybsSystem.NO_XFER_KEY + cmd ); 235 // 5.5.2.5 (2012/05/21) update.jsp に出力されるファイルを?コマンド名.htm に出力するよ?機?追? 236 if( "update.jsp".equals( xferVal ) ) { 237 file = cmd + ".htm" ; 238 } 239 else if( xferVal != null && xferVal.endsWith( "jsp" ) ) { 240 file = xferVal.toLowerCase(Locale.JAPAN).replace( "jsp","htm" ); 241 } 242 else { 243 String xferCmd = request.getParameter( HybsSystem.NO_XFER_KEY + cmd + "CMD" ); 244 if( xferCmd != null ) { 245 file = xferCmd.toLowerCase(Locale.JAPAN) + ".htm"; 246 } 247 else { 248 file = cmd.toLowerCase(Locale.JAPAN) + ".htm"; 249 } 250 } 251 } 252 } 253 else if( "index.jsp".equals( jspID ) && ( "RENEW".equals( cmd ) || "REVIEW".equals( cmd ) ) ) { 254 file = "indexRNW.htm"; 255 } 256 else if( "index.jsp".equals( jspID ) && "NEW".equals( cmd ) ) { 257 file = "indexNW.htm"; 258 } 259 else if( "query.jsp".equals( jspID ) && "NEW".equals( cmd ) ) { 260 file = "queryNW.htm"; 261 } 262 // 5.6.3.4 (2013/04/26) command="NEW" + resultXX.jsp ? "forwardXX.htm"?result.jsp にフレー?使?ターン(??イン) 263 // else if( jspID.startsWith( "result" ) && jspID.endsWith( ".jsp" ) && "NEW".equals( cmd ) ) { 264 else if( jspID.startsWith( "result" ) && "NEW".equals( cmd ) ) { 265 file = "forward" + jspID.substring( 6,jspID.length()-4 ) + ".htm" ; 266 } 267 // 5.6.4.2 (2013/05/17) fileDownload.jsp の対? 268 else if( "fileDownload.jsp".equals( jspID ) ) { 269 gamenId = request.getParameter( "GAMENID" ); // gamenId(??フォル?抽出)をリクエスト変数から取得する? 270 file = request.getParameter( "filename" ); // 日本語ファイル名で抽出する場合? 271 file = "fileDownload:" + file ; // ただし?セーブ時は、UnicodeLittle なので?fileDownload:" でマ?カーする? 272 } 273 else { 274 file = jspID.substring( 0,jspID.length()-4 ) + ".htm" ; 275 } 276 277 // 5.6.4.2 (2013/05/17) Matrixメニュー 対? 278 // URI? URI? request取? 279 // ?gamenId="jsp" + index.jsp + GAMENID=XXXX ?saveDir + "jsp/indexXXXX.htm" Matrixメニューからの画面呼出し? 280 // ② gamenId="jsp" + result.jsp + GAMENID=XXXX ?saveDir + "jsp/indexXXXX.htm" 画面QUERYのヘッ??メニュー 281 // ③ gamenId="menu" + multiMenu.jsp + group=YYYY ?saveDir + "menu/menuYYYY.htm" 通常メニューのグループ選? 282 // ④ gamenId="menu" + matrixMenu.jsp + group=YYYY ?saveDir + "menu/matrixMenuYYYY.htm" Matrixメニューのグループ選? 283 String guiKey = request.getParameter( "GAMENID" ); 284 String group = request.getParameter( "group" ); 285 286 if( "jsp".equals( gamenId ) && guiKey != null ) { 287 if( "index.jsp".equals( jspID ) || "result.jsp".equals( jspID ) ) { 288 file = "jsp/index" + guiKey + ".htm"; // ?② 289 } 290 } 291 else if( group != null ) { 292 if( "multiMenu.jsp".equals( jspID ) ) { 293 file = gamenId + "/menu" + group + ".htm"; // ③ 294 } 295 else if( "matrixMenu.jsp".equals( jspID ) ) { 296 file = gamenId + "/matrixMenu" + group + ".htm"; // ④ 297 } 298 gamenId = "jsp" ; // トリ?ー 299 } 300 301 // 5.6.4.2 (2013/05/17) Matrixメニュー buttonRequest ? 302 // // 4.3.3.0 (2008/10/01) Matrixメニュー対? 303 // // GAMENID=XXXX&buttonRequest=true + index.jsp ?"jsp/indexXXXX.htm" 304 // // GAMENID=XXXX&buttonRequest=true + multiMenu.jsp ?"jsp/menuXXXX.htm" 305 // // group=YYYY&buttonRequest=true + matrixMenu.jsp ?"menu/matrixMenuYYYY.htm" 306 // String guiKey = request.getParameter( "GAMENID" ); 307 // String btnReq = request.getParameter( "buttonRequest" ); 308 // if( "true".equals( btnReq ) ) { 309 // if( guiKey != null ) { 310 // if( "index.jsp".equals( jspID ) ) { 311 // file = "jsp/index" + guiKey + ".htm"; 312 // } 313 // else if( "multiMenu.jsp".equals( jspID ) ) { 314 // file = "jsp/menu" + guiKey + ".htm"; 315 // gamenId = "jsp" ; // トリ?ー 316 // } 317 // } 318 // else { 319 // 320 // String group = request.getParameter( "group" ); 321 // if( group != null ) { 322 // if( "matrixMenu.jsp".equals( jspID ) ) { 323 // file = "menu/matrixMenu" + group + ".htm"; 324 // gamenId = "jsp" ; // トリ?ー 325 // } 326 // } 327 // } 328 // } 329 330 // int index1 = requestURI.lastIndexOf( '/',index2-1 ); 331 // String gamenId = requestURI.substring( index1+1,index2 ); 332 333 if( "jsp".equals( gamenId ) ) { file = saveDir + file; } 334 else { file = saveDir + gamenId + "/" + file; } 335 336 // File fl = new File( saveDir + gamenId ); 337 // if( !fl.exists() && !fl.mkdirs() ) { 338 File fl = new File( file ).getParentFile(); 339 if( fl != null && !fl.exists() && !fl.mkdirs() ) { 340 String errMsg = "??フォル?作?できませんでした?" + fl + "]" ; 341 throw new RuntimeException( errMsg ); 342 } 343 } 344 345 return file; 346 } 347 }