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.servlet;
017
018 import java.io.FileInputStream;
019 import java.io.IOException;
020
021 import javax.mail.internet.MimeUtility;
022 import javax.servlet.ServletException;
023 import javax.servlet.ServletOutputStream;
024 import javax.servlet.http.HttpServlet;
025 import javax.servlet.http.HttpServletRequest;
026 import javax.servlet.http.HttpServletResponse;
027
028 import org.opengion.fukurou.security.HybsCryptography;
029 import org.opengion.fukurou.util.Closer;
030 import org.opengion.fukurou.util.KanaFilter;
031 import org.opengion.fukurou.util.StringUtil;
032 import org.opengion.hayabusa.common.HybsSystem;
033 import org.opengion.hayabusa.common.HybsSystemException;
034
035 /**
036 * サーバã?管ç�?ƒ•ァイルをダウンロードするå?合に使用する、サーブレãƒ?ƒˆですã?
037 *
038 * 引数(URL)にæŒ?®šã?ファイルをサーバã?からクライアントにãƒ?‚¦ンロードさせますã?
039 * file には、サーバã?ファイルの物ç�?‚¢ドレスを指定しますã?相対パスを使用する場合ã?ã€?
040 * コンãƒ?‚ストルーãƒ?通常、Tomcatでは、G:\webapps\dbdef2\ など)からのパスと判断しますã?
041 * name には、クライアントに送信するファイル名をæŒ?®šしますã?ファイル名をæŒ?®šしなã�??合ã?ã€?
042 * サーバã?の物ç�?ƒ•ァイルのファイル名が代わりに使用されますã?
043 * 日本語ファイル名ã?、すべて UTF-8化して処ç�?�—ますã?æŒ?®šするファイルに日本語が含まれる
044 * 場合ã?、URLエンコードを行ってくださいã€?
045 * <del>ファイル名ã?æŒ?®šに、Content-disposition 属æ?を使用してã�?�¾すが、Internet Explorer の
046 * 仕様で、attachment でæŒ?®šすると開くボタンが使えませんã€?"ファイルå�? が見つかりませんã€?
047 * こã?実è£?�§は、inline を使用するため、新たな画面で開きたい場合ã?、リンクにターゲãƒ?ƒˆ属æ?ã‚?
048 * æŒ?®šしてくださいã€?
049 * http://support.microsoft.com/?scid=kb;ja;436605&spid=2073&sid=269</del>
050 * 基本çš?�«はContent-disposition属æ?として"attachment"が指定されますã?
051 * ä½?�—、引数に inline=true を指定することで、Content-disposition属æ?に"inline"が指定されますã?
052 * またã?シスãƒ?ƒ リソースのUSE_FILEDOWNLOAD_CHECKKEYをtrueにæŒ?®šすることで、簡易的なチェãƒ?‚¯ã‚?
053 * 行うことができますã?
054 * 具体的には、これを有効にすると、file属æ?の値から計算されるMD5チェãƒ?‚¯サãƒ?�¨ã€?key"とã�?�†
055 * パラメーターにæŒ?®šされた値がä¸??した場合ã?みãƒ?‚¦ンロードが許可され、keyが指定されてã�?�ªã�??
056 * またã?値が異なるå?合ã?ãƒ?‚¦ンロードエラーとなりますã?
057 *
058 * ä¸?ˆ¬çš?�ªサーブレãƒ?ƒˆと同様に、デプロイメントã?ãƒ?‚£スクリプタ WEB-INF/web.xml にã€?
059 * servlet 要ç´?�¨ そã?マッピング(servlet-mapping)を定義するå¿?¦�がありますã?
060 *
061 * <servlet>
062 * <servlet-name>fileDownload</servlet-name>
063 * <servlet-class>org.opengion.hayabusa.servlet.FileDownload</servlet-class>
064 * </servlet>
065 *
066 * <servlet-mapping>
067 * <servlet-name>fileDownload</servlet-name>
068 * <url-pattern>/jsp/fileDownload</url-pattern>
069 * </servlet-mapping>
070 *
071 * ä¸?ˆ¬には、http://:ポã?ãƒ?シスãƒ?ƒ ID/jsp/fileDownload?file=サーバã?物ç�?ƒ•ァイル&name=ファイルå�?
072 * 形式ã?URL でアクセスしますã?
073 *
074 * @og.rev 3.8.1.1 (2005/11/21) 新規追åŠ?
075 * @og.group そã?他機è?
076 *
077 * @version 0.9.0 2000/10/17
078 * @author Kazuhiko Hasegawa
079 * @since JDK1.1,
080 */
081 public class FileDownload extends HttpServlet {
082 private static final long serialVersionUID = 539020110901L ;
083
084 // 拡張子contentType対応テーブル
085 private static final String CONTENT_TYPE_TABLE[][] = {
086 {"jpg", "image/pjpeg" },
087 {"gif", "image/gif" },
088 {"txt", "text/plain" },
089 // {"xls", "application/vnd.ms-excel"}
090 // OpenDocument追åŠ?
091 {"xls", "application/vnd.ms-excel"},
092 {"odp", "application/vnd.oasis.opendocument.presentation"}, // 4.3.5.5 (2008/03/08)
093 {"ods", "application/vnd.oasis.opendocument.spreadsheet"}, // 4.3.5.5 (2008/03/08)
094 {"odt", "application/vnd.oasis.opendocument.text"} // 4.3.5.5 (2008/03/08)
095 };
096 private static final int EXTENTION = 0;
097 private static final int CONTENT_TYPE= 1;
098
099 /**
100 * GET メソãƒ?ƒ‰が呼ばれたときに実行しますã?
101 *
102 * 処ç�??、doPost へ振りなおしてã�?�¾すã?
103 *
104 * @param request HttpServletRequestオブジェクãƒ?
105 * @param response HttpServletResponseオブジェクãƒ?
106 *
107 * @og.rev 3.8.1.2 (2005/12/19) 半角カãƒ?全角カナ変換機è?の追åŠ?
108 *
109 * @throws ServletException
110 * @throws IOException
111 */
112 @Override
113 public void doGet( final HttpServletRequest request, final HttpServletResponse response )
114 throws ServletException, IOException {
115 doPost( request,response );
116 }
117
118 /**
119 * POST メソãƒ?ƒ‰が呼ばれたときに実行しますã?
120 *
121 * file 引数の サーバã?物ç�?ƒ•ァイルをã?クライアントにストリーãƒ?Œ–して返しますã?
122 * name 引数があれã?、その名前のファイル名でクライアントがファイルセーブできるように
123 * しますã?name 引数がなければ、そのまま物ç�?ƒ•ァイル名が使用されますã?
124 * サーバã?物ç�?ƒ•ァイル名が、相対パスの場合ã?コンãƒ?‚ストルートに対する相対パスになりますã?
125 * (例:G:\webapps\dbdef2\ など)
126 *
127 * @og.rev 5.3.2.0 (2011/02/01) 日本語ファイル名が正しく処ç�?�§きなã�?ƒ�グを修正
128 * @og.rev 5.3.4.0 (2011/04/01) IEでファイルが正しくãƒ?‚¦ンロードできなã�?ƒ�グを修正
129 * @og.rev 5.3.5.0 (2011/05/01) ファイルãƒ?‚¦ンロードチェãƒ?‚¯キー対å¿?
130 * @og.rev 5.3.6.0 (2011/06/01) ファイルãƒ?‚¦ンロードã?attachmentに変更(ãƒ?‚¦ンロードダイアログをå?ã�?
131 * @og.rev 5.3.8.0 (2011/08/01) ファイル名指定でIEの場合ã?URLエンコードすると途中でåˆ?‚Œるたã‚?IE7のバグ)、Shift_JIS(WIndows-31J)で直接æŒ?®šするã?
132 * @og.rev 5.3.9.0 (2011/09/01) 引数にinline=trueを指定することで、インライン表示がå?来るよã�?�«対å¿?
133 *
134 * @param request HttpServletRequestオブジェクãƒ?
135 * @param response HttpServletResponseオブジェクãƒ?
136 *
137 * @throws ServletException
138 * @throws IOException
139 */
140 @Override
141 public void doPost( final HttpServletRequest request, final HttpServletResponse response )
142 throws ServletException, IOException {
143
144 // 3.8.1.2 (2005/12/19) 半角カãƒ?全角カナ変換機è?の追åŠ?
145 boolean hanzenFlag = HybsSystem.sysBool( "USE_FILEDOWNLOAD_HAN_ZEN" );
146
147 String reqFilename = request.getParameter( "file" );
148 String newFilename = request.getParameter( "name" );
149
150 // 5.3.9.0 (2011/09/01) 引数にinline=trueを指定することで、インライン表示がå?来るよã�?�«対å¿?
151 boolean inline = StringUtil.nval( request.getParameter( "inline" ), false );
152 String dipositionType = inline ? "inline" : "attachment";
153
154 // クライアントå?のæ–?—エンコーãƒ?‚£ングをUTF-8に変換
155 reqFilename = new String( reqFilename.getBytes("ISO-8859-1"), "UTF-8" );
156
157 // 5.3.5.0 (2011/05/01) ファイルãƒ?‚¦ンロードチェãƒ?‚¯キー対å¿?
158 boolean useCheck = HybsSystem.sysBool( "USE_FILEDOWNLOAD_CHECKKEY" );
159 if( useCheck ) {
160 String checkKey = request.getParameter( "key" );
161 if( checkKey == null || !checkKey.equals( HybsCryptography.getMD5( reqFilename ) ) ) {
162 String msg = "アクセスが拒否されましたã€?URLチェãƒ?‚¯)";
163 throw new HybsSystemException( msg );
164 }
165 }
166
167 // 相対パスを絶対パスに変換。ファイルセパレータも正規化されてã�?�¾すã?
168 reqFilename = HybsSystem.url2dir( reqFilename );
169
170 // 拡張子からcontentTypeを獲å¾?
171 String contentType = getContentType( reqFilename );
172 // contentTypeをå?åŠ?
173 response.setContentType( contentType );
174
175 // 表示ファイル名ã?æŒ?®?
176 if( newFilename == null || newFilename.length() == 0 ) {
177 newFilename = getFileName( reqFilename );
178 }
179 else {
180 newFilename = new String( newFilename.getBytes("ISO-8859-1"), "UTF-8" );
181 }
182
183 // 3.8.1.2 (2005/12/19) 半角カナを全角カナに置き換えますã?ファイルãƒ?‚¤アログのæ–?—化け仮対å¿?
184 if( hanzenFlag ) {
185 newFilename = KanaFilter.han2zen( newFilename );
186 }
187
188 // 5.3.2.0 (2011/02/01) 日本語ファイル名が正しく処ç�?�§きなã�?ƒ�グを修正
189 // newFilename = StringUtil.urlEncode( newFilename );
190 if( request.getHeader( "User-Agent" ).indexOf( "MSIE" ) == -1 ) {
191 newFilename = MimeUtility.encodeWord( newFilename, "UTF-8", "B" );
192 }
193 else {
194 // 5.3.8.0 (2011/08/01) IEの場合ã?URLエンコードすると途中でåˆ?‚Œるたã‚?IE7のバグ)、Shift_JIS(WIndows-31J)で直接æŒ?®šするã?
195 // newFilename = StringUtil.urlEncode( newFilename );
196 newFilename = new String( newFilename.getBytes("Windows-31J"), "ISO-8859-1" );
197 }
198
199 // ファイル名ã?送信( attachment部åˆ?‚’inlineに変更すればインライン表示 )
200 // 5.3.9.0 (2011/09/01) 引数にinline=trueを指定することで、インライン表示がå?来るよã�?�«対å¿?
201 // response.setHeader( "Content-disposition", "inline; filename=\"" + newFilename + "\"" );
202 // response.setHeader( "Content-disposition", "attachment; filename=\"" + newFilename + "\"" );
203 response.setHeader( "Content-disposition", dipositionType + "; filename=\"" + newFilename + "\"" );
204
205 // 5.3.4.0 (2011/04/01) IEでファイルが正しくãƒ?‚¦ンロードできなã�?ƒ�グを修正
206 response.setHeader( "Cache-Control", "public" );
207
208 // ファイルå†?®¹の出åŠ?
209 FileInputStream fin = null;
210 ServletOutputStream out = null;
211 try {
212 fin = new FileInputStream( reqFilename );
213 out = response.getOutputStream();
214
215 // ファイル読み込み用バッファ
216 byte buffer[] = new byte[4096];
217 int size;
218 while((size = fin.read(buffer))!=-1) {
219 out.write(buffer,0, size);
220 out.flush();
221 }
222 }
223 finally {
224 Closer.ioClose( fin ); // 4.0.0 (2006/01/31) close 処ç�?™‚の IOException を無è¦?
225 Closer.ioClose( out ); // 4.0.0 (2006/01/31) close 処ç�?™‚の IOException を無è¦?
226 }
227 }
228
229 /**
230 * アドレス名から拡張子を取り出しますã?
231 *
232 * アドレス名ã?後ろからã€?." 以降を拡張子としてåˆ?‚Š取りますã?
233 * 拡張子が存在しなã�??å�?æŒ?®šã?ファイル名に "." が含まれなã�??å�?は
234 * ゼロæ–?—å?("")を返しますã?
235 *
236 * @param fileAddress アドレスå�?
237 *
238 * @return 拡張å?
239 */
240 private String getExtention( final String fileAddress ) {
241 int idx = fileAddress.lastIndexOf('.');
242 if( idx!=-1 ) { return fileAddress.substring( idx+1 ); }
243 return "";
244 }
245
246 /**
247 * アドレス名からファイル名を取り出しますã?
248 *
249 * アドレス名ã?後ろから、ファイルセパレータ以降をファイル名としてåˆ?‚Š取りますã?
250 * ファイルセパレータが存在しなã�??合ã?アドレス名をそã?まま返しますã?
251 * ここでは、OS毎に異なるファイルセパレータを統ä¸?¾Œに処ç�?�—てくださいã€?
252 *
253 * @param fileAddress アドレスå�?
254 *
255 * @return ファイルå�?
256 */
257 private String getFileName( final String fileAddress ) {
258 int idx = fileAddress.lastIndexOf( HybsSystem.FS );
259 if( idx!=-1 ) { return fileAddress.substring( idx+1 ); }
260 return fileAddress;
261 }
262
263 /**
264 * アドレス名から対応するコンãƒ?ƒ³ãƒ?‚¿イプを取り出しますã?
265 *
266 * アドレス名からã?ファイル拡張子を取り出しã?対応するコンãƒ?ƒ³ãƒ?‚¿イプを返しますã?
267 * コンãƒ?ƒ³ãƒ?‚¿イプã?、CONTENT_TYPE_TABLE 配å?に定義してã�?‚‹中から検索して返しますã?
268 * 存在しなã�??合ã?ã€?application/octet-stream" を返しますã?
269 *
270 * @param fileAddress アドレスå�?
271 *
272 * @return コンãƒ?ƒ³ãƒ?‚¿イãƒ?
273 */
274 private String getContentType( final String fileAddress ) {
275 String extention = getExtention( fileAddress );
276 for( int j=0; j<CONTENT_TYPE_TABLE.length; j++ ) {
277 if( CONTENT_TYPE_TABLE[j][EXTENTION].equalsIgnoreCase( extention ) ) {
278 return CONTENT_TYPE_TABLE[j][CONTENT_TYPE];
279 }
280 }
281 return "application/octet-stream";
282 }
283 }