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 org.opengion.hayabusa.common.HybsSystem;
019    import org.opengion.hayabusa.servlet.multipart.MultipartParser;
020    import org.opengion.hayabusa.servlet.multipart.Part;
021    import org.opengion.hayabusa.servlet.multipart.FilePart;
022    import org.opengion.hayabusa.servlet.multipart.ParamPart;
023    
024    import java.io.File;
025    import java.io.IOException;
026    import java.util.Map;
027    // import java.util.HashMap;
028    import java.util.TreeMap;
029    import java.util.List;
030    import java.util.ArrayList;
031    import java.util.Set;
032    import java.util.Random ;
033    import java.util.concurrent.atomic.AtomicInteger;       // 5.5.2.6 (2012/05/25) findbugs対�
034    import javax.servlet.http.HttpServletRequest;
035    
036    /**
037     * ファイルをサーãƒã?ã«ã‚¢ãƒ??ロードã™ã‚‹å?åˆã«ä½¿ç”¨ã•れるã?ルãƒãƒ‘ートå?ç?‚µãƒ¼ãƒ–レãƒ?ƒˆã§ã™ã?
038     *
039     * 通常ã®ãƒ•ァイルアãƒ??ロード時ã®ã€form ã§ä½¿ç”¨ã™ã‚‹ã€enctype="multipart/form-data"
040     * を指定ã—ãŸå?åˆã?ã€ä»–ã?リクエスト情報もã?å–り出ã™ã“ã¨ãŒå¯èƒ½ã§ã™ã?
041     *
042     * ファイルをアãƒ??ロード後ã«ã€æŒ‡å®šã?ファイルåã«å¤‰æ›´ã™ã‚‹æ©Ÿè?ãŒã‚りã¾ã™ã?
043     * file 登録ãƒ?‚¤ã‚¢ãƒ­ã‚°ã§æŒ?®šã—ã?name ã«ã€?_NEW" ã¨ã?†å称を付ã‘ãŸãƒªã‚¯ã‚¨ã‚¹ãƒˆå?ã‚?
044     * ファイルã®ã‚¢ãƒ??ロードã¨åŒæ™‚ã«é€ä¿¡ã™ã‚‹ã“ã¨ã§ã€ã“ã®åå‰ã«ãƒ•ã‚¡ã‚¤ãƒ«ã‚’ä»˜ã‘æ›¿ãˆã¾ã™ã?
045     * ã¾ãŸã?ã‚¢ãƒ??ロード後ã?ファイルåã?ã€name æŒ?®šã?åç§°ã§ã€å–りå?ã›ã¾ã™ã?
046     * クライアントã‹ã‚‰ç™»éŒ²ã—ãŸã‚ªãƒªã‚¸ãƒŠãƒ«ã®ãƒ•ァイルåã?ã€name ã«ã€?_ORG" ã¨ã?†åç§°
047     * ã§å–り出ã™ã“ã¨ãŒå¯èƒ½ã§ã™ã?
048     *
049     * maxPostSize : æœ?¤§è»¢é€ã‚µã‚¤ã‚º(Byte)を指定ã—ã¾ã™ã? 0,ã¾ãŸã?マイナスã§ç„¡åˆ¶é™ã§ã™ã?
050     * useBackup   : ファイルアãƒ??ロード時ã«ã€ã™ã§ã«åŒåã®ãƒ•ァイルãŒå­˜åœ¨ã—ãŸå ´åˆã«ã€?
051     *               ãƒãƒƒã‚¯ã‚¢ãƒ??処ç?renameTo)ã™ã‚‹ã‹ã©ã?‹[true/false]を指定ã—ã¾ã?åˆæœŸå€¤:false)
052     *
053     * ファイルアãƒ??ロード時ã«ã€ã‚¢ãƒ??ロードå?ã«ã€åŒåã?ファイルãŒå­˜åœ¨ã—ãŸå ´åˆã?ã€æ—¢å­˜æ©Ÿè?ã¯ã€ãã®ã¾ã¾
054     * ç½®ãæ›ãˆã¦ã?¾ã—ãŸãŒã?簡易ãƒãƒ¼ã‚¸ãƒ§ãƒ³ã‚¢ãƒ??機è?ã¨ã—ã¦ã€useBackup="true" を指定ã™ã‚‹ã¨ã€æ—¢å­˜ã?ファイルã‚?
055     * リãƒã?ãƒ?—ã¦ã€ãƒãƒ?‚¯ã‚¢ãƒ??ファイルを作æ?ã—ã¾ã™ã?
056     * ãƒãƒƒã‚¯ã‚¢ãƒ??ファイルã¯ã€ã‚¢ãƒ??ロードフォルãƒ?‚’基準ã¨ã—ã¦ã€_backup/ファイルå?æ‹¡å¼µå­_処ç?™‚刻ã®long値.æ‹¡å¼µå­?ã«ãªã‚Šã¾ã™ã?
057     * オリジナルã®ãƒ•ァイルå(拡張å­ä»˜ï¼‰ã‚’残ã—ãŸã¾ã¾ã€?_処ç?™‚刻ã®long値" を追åŠ?—ã€ã•らã«ã€ã‚ªãƒªã‚¸ãƒŠãƒ«ã®æ‹¡å¼µå­ã‚’追åŠ?—ã¾ã™ã?
058     * ãƒãƒƒã‚¯ã‚¢ãƒ??ファイルã®å½¢å¼ã?æŒ?®šã§ãã¾ã›ã‚“ã€?
059     *
060     * @og.group ãã?他機è?
061     *
062     * @version  4.0
063     * @author       Kazuhiko Hasegawa
064     * @since    JDK5.0,
065     */
066    public final class MultipartRequest {
067    //      private static volatile int dumyNewFileCnt = 1 ;        // 3.8.1.4 (2006/03/17)
068            private static AtomicInteger dumyNewFileCnt = new AtomicInteger(1);             // 5.5.2.6 (2012/05/25) findbugs対�
069    
070            private static String RANDOM_KEY = new Random().nextInt() + "_" ;               // 5.6.5.3 (2013/06/28) ã‚¢ãƒ??ロード時ã®ãƒ?ƒŸãƒ¼ãƒ•ァイルåã‚’ã‚‚ã†å°‘ã—ã?‘ランãƒ?ƒ ã«ã™ã‚‹ã€?
071    
072    //      private final Map<String,List<String>> parameters   = new HashMap<String,List<String>>();
073    //      private final Map<String,UploadedFile> files              = new HashMap<String,UploadedFile>();
074            private final Map<String,List<String>> parameters   = new TreeMap<String,List<String>>();               // 5.6.5.2 (2013/06/21) ソートã—ã¾ã™ã?
075            private final Map<String,UploadedFile> files              = new TreeMap<String,UploadedFile>();             // 5.6.5.2 (2013/06/21) ソートã—ã¾ã™ã?
076    
077            /**
078             * MultipartRequest オブジェクトを構築ã—ã¾ã™ã?
079             *
080             * 引数ã¨ã—ã¦ã€ãƒ•ァイルアãƒ??ロード時ã®ä¿å­˜ãƒ•ォルãƒ??æœ?¤§ã‚µã‚¤ã‚ºã€ã‚¨ãƒ³ã‚³ãƒ¼ãƒ‰ã?
081             * æ–°ã—ã„ファイルåãªã©ã‚’指定ã§ãã¾ã™ã?æ–°ã—ã„ファイルåã?ã€ã‚¢ãƒ??ロードã•れる
082             * ファイルãŒä¸?¤ã?‘ã®å ´åˆã«ä½¿ç”¨ã§ãã¾ã™ã?è¤?•°ã®ãƒ•ã‚¡ã‚¤ãƒ«ã‚’åŒæ™‚ã«å¤‰æ›´ã—ãŸã?
083             * å ´åˆã?ã€ã‚¢ãƒ??ロードルールã«ã®ã£ã¨ã‚Šã?ãƒªã‚¯ã‚¨ã‚¹ãƒˆãƒ‘ãƒ©ãƒ¡ãƒ¼ã‚¿ã§æŒ?®šã—ã¦ãã ã•ã„ã€?
084             *
085             * @og.rev 3.8.1.3A (2006/01/30) 新ファイルåã«ã‚ªãƒªã‚¸ãƒŠãƒ«ãƒ•ァイルåã?æ‹¡å¼µå­ã‚’ã‚»ãƒ?ƒˆã—ã¾ã?
086             * @og.rev 4.0.0.0 (2007/11/28) メソãƒ?ƒ‰ã®æˆ»ã‚Šå?ã‚’ãƒã‚§ãƒ?‚¯ã—ã¾ã™ã?
087             * @og.rev 5.5.2.6 (2012/05/25) findbugs対応ã?staticフィールドã¸ã®æ›¸ãè¾¼ã¿ã«ã€AtomicInteger を利用ã—ã¾ã™ã?
088             * @og.rev 5.6.5.3 (2013/06/28) useBackup引数追�
089             *
090             * @param       request HttpServletRequestオブジェク�
091             * @param       saveDirectory   ファイルアãƒ??ロードãŒã‚ã£ãŸå?åˆã?ä¿å­˜ãƒ•ォルãƒ?
092             * @param       maxPostSize     ファイルアãƒ??ãƒ­ãƒ¼ãƒ‰æ™‚ã®æœ?¤§ãƒ•ァイルサイズ(Byte)0,ã¾ãŸã?マイナスã§ç„¡åˆ¶é™?
093             * @param       encoding        ファイルã®ã‚¨ãƒ³ã‚³ãƒ¼ãƒ?
094             * @param       inputFilename   ã‚¢ãƒ??ロードã•れãŸãƒ•ã‚¡ã‚¤ãƒ«ã®æ–°ã—ã„åå‰
095             * @param       useBackup               ファイルアãƒ??ロード時ã«ã€ãƒãƒ?‚¯ã‚¢ãƒ??処ç?™ã‚‹ã‹ã©ã?‹[true/false]を指å®?
096             * @throws IOException
097             */
098            public MultipartRequest(final HttpServletRequest request,
099                                                            final String saveDirectory,
100                                                            final int maxPostSize,
101                                                            final String  encoding,
102    //                                                      final String  inputFilename ) throws IOException {
103                                                            final String  inputFilename,
104                                                            final boolean useBackup ) throws IOException {                  // 5.6.5.3 (2013/06/28) 追�
105    
106                    if(request == null) {
107                            throw new IllegalArgumentException("request cannot be null");
108                    }
109                    if(saveDirectory == null) {
110                            throw new IllegalArgumentException("saveDirectory cannot be null");
111                    }
112                    // 5.5.2.6 (2012/05/25) 0,ã¾ãŸã?マイナスã§ç„¡åˆ¶é™?
113    //              if(maxPostSize <= 0) {
114    //                      throw new IllegalArgumentException("maxPostSize must be positive");
115    //              }
116    
117                    // Save the dir
118                    File dir = new File(saveDirectory);
119    
120                    // Check saveDirectory is truly a directory
121                    if(!dir.isDirectory()) {
122                            throw new IllegalArgumentException("Not a directory: " + saveDirectory);
123                    }
124    
125                    // Check saveDirectory is writable
126                    if(!dir.canWrite()) {
127                            throw new IllegalArgumentException("Not writable: " + saveDirectory);
128                    }
129    
130                    // Parse the incoming multipart, storing files in the dir provided,
131                    // and populate the meta objects which describe what we found
132                    MultipartParser parser = new MultipartParser(request, maxPostSize);
133                    if(encoding != null) {
134                            parser.setEncoding(encoding);
135                    }
136    
137                    List<String> list = new ArrayList<String>();
138    
139                    Part part;
140                    while ((part = parser.readNextPart()) != null) {
141                            String name = part.getName();
142                            if( part.isParam() && part instanceof ParamPart ) {
143                                    ParamPart paramPart = (ParamPart)part;
144                                    String value = paramPart.getStringValue();
145                                    List<String> existingValues = parameters.get(name);
146                                    if(existingValues == null) {
147                                            existingValues = new ArrayList<String>();
148                                            parameters.put(name, existingValues);
149                                    }
150                                    existingValues.add(value);
151                            }
152                            else if( part.isFile() && part instanceof FilePart ) {
153                                    FilePart filePart = (FilePart)part;
154                                    String fileName = filePart.getFilename();
155                                    if(fileName != null) {
156                                            list.add( name );               // 3.5.6.5 (2004/08/09)
157                                            // 3.8.1.2 (2005/12/19) 仮ファイルã§ã‚»ãƒ¼ãƒ–ã™ã‚‹ã?
158    //                                      String newName = String.valueOf( dumyNewFileCnt++ ) ;   // 3.8.1.4 (2006/03/17)
159    //                                      String newName = String.valueOf( dumyNewFileCnt.getAndIncrement() ) ;   // 5.5.2.6 (2012/05/25) findbugs対�
160                                            String tempName = RANDOM_KEY + dumyNewFileCnt.getAndIncrement() ;               // 5.6.5.3 (2013/06/28) ã‚¢ãƒ??ロード時ã®ãƒ?ƒŸãƒ¼ãƒ•ァイルåã‚’ã‚‚ã†å°‘ã—ã?‘ランãƒ?ƒ ã«ã™ã‚‹ã€?
161    //                                      filePart.setFilename( newName );                        // 5.6.5.3 (2013/06/28) newName ã¯ã‚?‚„ã“ã—ã??ã§ã€å¤‰æ›´
162                                            filePart.setFilename( tempName );
163                                            filePart.writeTo(dir);
164                                            files.put(name,
165                                                              new UploadedFile( dir.toString(),
166    //                                                                                              newName,                // 3.8.1.2 (2005/12/19)
167                                                                                                    tempName,               // 3.8.1.2 (2005/12/19)
168                                                                                                    fileName,
169                                                                                                    filePart.getContentType()));
170                                    }
171                                    else {
172                                            files.put(name, new UploadedFile(null, null, null, null));
173                                    }
174                            }
175                            else {
176                                    String errMsg = "PartオブジェクトãŒã€ParamPartã§ã‚?ilePartã§ã‚‚ã‚りã¾ã›ã‚“ã€?
177                                                            + " class=[" + part.getClass() + "]";
178                                    throw new RuntimeException( errMsg );
179                            }
180                    }
181    
182                    // 3.5.6.5 (2004/08/09) 登録後ã«ãƒ•ァイルをリãƒã?ãƒ?—ã¾ã™ã?
183                    int size = list.size();
184                    for( int i=0; i<size; i++ ) {
185                            String name = list.get(i);
186                            File file = getFile( name );
187                            String newName = inputFilename;
188                            if( newName == null && name != null ) {
189                                    int adrs = name.lastIndexOf( HybsSystem.JOINT_STRING ); // カラãƒ?_è¡Œç•ªå· ã® __ ã®ä½ç½®
190                                    if( adrs < 0 ) {
191                                            newName = getParameter( name + "_NEW" );
192                                    }
193                                    else {
194                                            newName = getParameter( name.substring( 0,adrs ) + "_NEW" + name.substring( adrs ) );
195                                    }
196                            }
197    
198                            // 3.8.1.3 (2006/02/06) 新ファイルåã«æ‹¡å¼µå­ãŒãªã?¨ã?
199                            // 旧ファイルåã‹ã‚‰æ‹¡å¼µå­å–å¾—ã—æ–°ãƒ•ァイルåã«æ–?­—å?連çµ?
200                            if( newName != null && newName.length() > 0 ) {
201                                    // 新ファイルåã‹ã‚‰æ‹¡å¼µå­å–å¾?
202                                    String newExt = getExtension( newName );
203                                    if( newExt == null || newExt.length() == 0 ) {
204                                            String oldExt = getExtension( getOriginalFileName( name ) );
205                                            newName = new StringBuilder().append( newName ).append( "." ).append( oldExt ).toString();
206                                    }
207                            }
208                            else {
209                                    newName = getOriginalFileName( name );
210                            }
211    
212                            // 3.8.1.2 (2005/12/19) 基本çš?«ã¯ã™ã¹ã¦ã®ã‚±ãƒ¼ã‚¹ã§ãƒ•ァイルå変更ãŒç™ºç”Ÿã™ã‚‹ã?
213                            if( file != null && newName != null && newName.length() > 0 ) {
214                                    File newFile = new File( dir,newName );
215    
216                                    // 5.6.5.3 (2013/06/28) useBackup ファイルアãƒ??ロード時ã«ã€ãƒãƒ?‚¯ã‚¢ãƒ??処ç?™ã‚‹ã‹ã©ã?‹[true/false]を指å®?
217    //                              if( newFile.exists() && !newFile.delete() ) {
218    //                                      String errMsg = "既存ã?ファイル[" + newName + "]ãŒå‰Šé™¤ã§ãã¾ã›ã‚“ã§ã—ãŸã€?;
219    //                                      throw new RuntimeException( errMsg );
220    //                              }
221                                    if( newFile.exists() ) {
222                                            if( useBackup ) {
223                                                    // newName ã«ãƒ•ォルãƒ?šŽå±¤ã‚’å«ã‚??åˆã«ã€ããªãˆã¦ã€?
224                                                    File parent = newFile.getParentFile();                  // ãƒãƒƒã‚¯ã‚¢ãƒ??ã™ã¹ãファイルã®ãƒ•ォルãƒ?
225                                                    File backup = new File( parent , "_backup" );   // ãã?直下ã«ã€?_backup" フォルãƒ?‚’作æ?
226                                                    if( backup != null && !backup.exists() && !backup.mkdirs() ) {
227                                                            String errMsg = "ãƒãƒƒã‚¯ã‚¢ãƒ??処ç?§backupフォルãƒ??作æ?ã«å¤±æ•—ã—ã¾ã—ãŸã€?" + backup + "]";
228                                                            throw new RuntimeException( errMsg );
229                                                    }
230                                                    // ãƒãƒƒã‚¯ã‚¢ãƒ??ファイルåã?ã€å?ã®ãƒ•ァイルå?æ‹¡å¼µå­å«ã‚? ??"_" + ç¾åœ¨æ™‚刻ã®long値 + "." + å…??ãƒ•ã‚¡ã‚¤ãƒ«ã®æ‹¡å¼µå­?
231                                                    String bkupName = newFile.getName() + "_" + System.currentTimeMillis() + "."  + getExtension( newName ) ;
232                                                    File fromFile = new File( dir,newName );                // オリジナル㮠newFile ã‚’rename ã™ã‚‹ã¨ã¾ãšã„ã®ã§ã€åŒåã?Fileオブジェクトを作æ?
233                                                    File bkupFile = new File( backup,bkupName );
234    
235                                                    if( !fromFile.renameTo( bkupFile ) ) {
236                                                            String errMsg = "ãƒãƒƒã‚¯ã‚¢ãƒ??処ç?§ãƒãƒƒã‚¯ã‚¢ãƒ??ファイルをリãƒã?ãƒ?§ãã¾ã›ã‚“ã§ã—ãŸã€?" + bkupFile + "]" ;
237                                                            throw new RuntimeException( errMsg );
238                                                    }
239                                            }
240                                            else if( !newFile.delete() ) {
241                                                    String errMsg = "既存ã?ファイル[" + newName + "]ãŒå‰Šé™¤ã§ãã¾ã›ã‚“ã§ã—ãŸã€?;
242                                                    throw new RuntimeException( errMsg );
243                                            }
244                                    }
245    
246    //                              file.renameTo( newFile );
247                                    if( !file.renameTo( newFile ) ) {
248                                            String errMsg = "æ‰?®šã?ファイルをリãƒã?ãƒ?§ãã¾ã›ã‚“ã§ã—ãŸã€?" + file + "]" ;
249                                            throw new RuntimeException( errMsg );
250                                    }
251                                    UploadedFile fup = files.get( name );
252                                    fup.setFilesystemName( newName );
253                            }
254                    }
255            }
256    
257            /**
258             * リクエストパラメータã®åå‰é…å?ã‚’å–å¾—ã—ã¾ã™ã?
259             *
260             * @return      リクエストパラメータã®åå‰é…å?
261             */
262            public String[] getParameterNames() {
263                    Set<String> keyset = parameters.keySet();
264                    return keyset.toArray( new String[keyset.size()] );
265            }
266    
267            /**
268             * ファイルアãƒ??ロードã•れãŸãƒ•ァイル群ã®ãƒ•ァイルåé?列をå–å¾—ã—ã¾ã™ã?
269             *
270             * @return      ã‚¢ãƒ??ロードã•れãŸãƒ•ァイルåé?åˆ?
271             */
272            public String[] getFilenames() {
273                    Set<String> keyset = files.keySet();
274                    return keyset.toArray( new String[keyset.size()] );
275            }
276    
277            /**
278             * æŒ?®šã?åå‰ã®ãƒªã‚¯ã‚¨ã‚¹ãƒˆãƒ‘ラメータã®å€¤ã‚’å–å¾—ã—ã¾ã™ã?
279             *
280             * @param       name    リクエストパラメータå?
281             *
282             * @return      パラメータã®å€¤
283             */
284            public String getParameter( final String name ) {
285                    List<String> values = parameters.get(name);
286                    if( values == null || values.isEmpty() ) {
287                            return null;
288                    }
289                    return values.get(values.size() - 1);
290            }
291    
292            /**
293             * æŒ?®šã?åå‰ã®ãƒªã‚¯ã‚¨ã‚¹ãƒˆãƒ‘ラメータã®å€¤ã‚’é?列型å¼ã§å–å¾—ã—ã¾ã™ã?
294             *
295             * @og.rev 5.3.2.0 (2011/02/01) æ–°è¦ä½œæ?
296             *
297             * @param       name    リクエストパラメータå?
298             *
299             * @return      パラメータã®å€¤é…å?
300             */
301            public String[] getParameters( final String name ) {
302                    List<String> values = parameters.get(name);
303                    if( values == null || values.isEmpty() ) {
304                            return null;
305                    }
306    //              return values.toArray( new String[0] );
307                    return values.toArray( new String[values.size()] );
308            }
309    
310            /**
311             * æŒ?®šã?åå‰ã®ãƒªã‚¯ã‚¨ã‚¹ãƒˆãƒ‘ラメータã®å€¤ã‚’é?åˆ?int)åž‹å¼ã§å–å¾—ã—ã¾ã™ã?
312             *
313             * @og.rev 5.3.2.0 (2011/02/01) æ–°è¦ä½œæ?
314             * @og.rev 5.3.6.0 (2011/06/01) é…å?値ã?"ã®å ´åˆã«NumberFormatExceptionãŒç™ºç”Ÿã™ã‚‹ãƒã‚°ã‚’修正
315             *
316             * @param       name    リクエストパラメータå?
317             *
318             * @return      パラメータã®å€¤é…å?
319             */
320            public int[] getIntParameters( final String name ) {
321                    List<String> values = parameters.get(name);
322                    if( values == null || values.isEmpty() ) {
323                            return null;
324                    }
325    
326    //              int[] rtn = new int[values.size()];
327    //              for( int i=0; i<values.size(); i++ ) {
328    //                      rtn[i] = Integer.valueOf( values.get(i) );
329    //              }
330    
331                    // 5.3.6.0 (2011/06/01) ゼロストリング("")ã¯int変æ›å¯¾è±¡ã‹ã‚‰äºˆã‚除外ã™ã‚?
332                    List<Integer> intVals = new ArrayList<Integer>();
333                    for( int i=0; i<values.size(); i++ ) {
334                            String str = values.get(i);
335                            if( str != null && str.length() > 0 ) {
336                                    intVals.add( Integer.parseInt( str ) );
337                            }
338                    }
339                    if( intVals.isEmpty() ) {
340                            return null;
341                    }
342    
343                    int[] rtn = new int[intVals.size()];
344                    for( int i=0; i<intVals.size(); i++ ) {
345                            rtn[i] = intVals.get(i).intValue();
346                    }
347    
348                    return rtn;
349            }
350    
351            /**
352             * æŒ?®šã?åå‰ã® UploadedFile オブジェクトã‹ã‚?登録ã•れるファイルåã‚’å–å¾—ã—ã¾ã™ã?
353             *
354             * 登録ã•れるファイルåã¨ã¯ã€æ–°ãŸã«æ›¸ãæ›ãˆã‚‰ã‚ŒãŸãƒ•ァイルåã?ã“ã¨ã§ã™ã?
355             *
356             * @og.rev 5.6.6.1 (2013/07/12) null 対�
357             *
358             * @param       name    キー�?
359             *
360             * @return      æ–°ãŸã«æ›¸ãæ›ãˆã‚‰ã‚ŒãŸãƒ•ァイルå?
361             */
362            public String getFilesystemName( final String name ) {
363                    UploadedFile file = files.get(name);
364    //              return file.getFilesystemName();  // may be null
365                    return (file == null) ? null : file.getFilesystemName();  // may be null
366            }
367    
368            /**
369             * æŒ?®šã?åå‰ã® UploadedFile オブジェクトã‹ã‚?ã‚¢ãƒ??ロードã•れãŸãƒ•ァイルåã‚’å–å¾—ã—ã¾ã™ã?
370             *
371             * ã‚¢ãƒ??ロードã•れãŸãƒ•ァイルåã¨ã¯ã€ã‚ªãƒªã‚¸ãƒŠãƒ«ã®ãƒ•ァイルåã?ã“ã¨ã§ã™ã?
372             *
373             * @og.rev 5.6.6.1 (2013/07/12) null 対�
374             *
375             * @param       name    キー�?
376             *
377             * @return      オリジナルã®ãƒ•ァイルå?
378             */
379            public String getOriginalFileName( final String name ) {
380                    UploadedFile file = files.get(name);
381    //              return file.getOriginalFileName();  // may be null
382                    return (file == null) ? null : file.getOriginalFileName();  // may be null
383            }
384    
385            /**
386             * æŒ?®šã?åå‰ã® UploadedFile オブジェクトã‹ã‚?File オブジェクトをå–å¾—ã—ã¾ã™ã?
387             *
388             * @og.rev 5.6.6.1 (2013/07/12) null 対�
389             *
390             * @param       name    キー�?
391             *
392             * @return      Fileオブジェク�
393             */
394            public File getFile( final String name ) {
395                    UploadedFile file = files.get(name);
396    //              return file.getFile();  // may be null
397                    return (file == null) ? null : file.getFile();  // may be null
398            }
399    
400            /**
401             * ファイルåã‹ã‚?æ‹¡å¼µå­ã‚’å–å¾—ã—ã¾ã™ã?
402             *
403             * @param       fileName        ファイルå?
404             *
405             * @return      拡張�
406             */
407            private String getExtension( final String fileName ) {
408                    int index = fileName.lastIndexOf('.');
409                    if(index!=-1) {
410                            return fileName.substring(index + 1, fileName.length());
411                    }
412                    return "";
413            }
414    }
415    
416    /**
417     * ファイルをサーãƒã?ã«ã‚¢ãƒ??ロードã™ã‚‹å?åˆã«ä½¿ç”¨ã•れるファイル管ç??部クラスã§ã™ã?
418     *
419     * @og.group ãã?他機è?
420     *
421     * @version  4.0
422     * @author       Kazuhiko Hasegawa
423     * @since    JDK5.0,
424     */
425    final class UploadedFile {
426    
427            private String filename;
428            private final String dir;
429            private final String original;
430            private final String type;
431    
432            /**
433             * ã‚¢ãƒ??ロードファイルã®ç®¡ç?‚ªãƒ–ジェクトを作æ?ã—ã¾ã™ã?
434             *
435             * @param       dir     ファイルをä¿ç®¡ã™ã‚‹ãƒ•ォルãƒ?
436             * @param       filename        ファイルå?ç½®ãæ›ãˆå¾?
437             * @param       original        ファイルå?オリジナル)
438             * @param       type    コンãƒ?ƒ³ãƒˆã‚¿ã‚¤ãƒ?
439             */
440            UploadedFile( final String dir, final String filename, final String original, final String type) {
441                    this.dir = dir;
442                    this.filename = filename;
443                    this.original = original;
444                    this.type = type;
445            }
446    
447            /**
448             * コンãƒ?ƒ³ãƒˆã‚¿ã‚¤ãƒ—ã‚’å–å¾—ã—ã¾ã™ã?
449             *
450             * @return      コンãƒ?ƒ³ãƒˆã‚¿ã‚¤ãƒ?
451             */
452            public String getContentType() {
453                    return type;
454            }
455    
456            /**
457             * ファイルå?ç½®ãæ›ãˆå¾?ã‚’å–å¾—ã—ã¾ã™ã?
458             *
459             * @return      ファイルå?ç½®ãæ›ãˆå¾?
460             */
461            public String getFilesystemName() {
462                    return filename;
463            }
464    
465            /**
466             * ファイルå?ç½®ãæ›ãˆå¾?ã‚’ã‚»ãƒ?ƒˆã—ã¾ã™ã?
467             *
468             * @param       name    ファイルå?ç½®ãæ›ãˆå¾?
469             */
470            public void setFilesystemName( final String name ) {
471                    filename = name;
472            }
473    
474            /**
475             * ファイルå?オリジナル)ã‚’å–å¾—ã—ã¾ã™ã?
476             *
477             * @return      ファイルå?オリジナル)
478             */
479            public String getOriginalFileName() {
480                    return original;
481            }
482    
483            /**
484             * ファイルå?ç½®ãæ›ãˆå¾?ã® File オブジェクトをå–å¾—ã—ã¾ã™ã?
485             *
486             * @return File File オブジェク�
487             */
488            public File getFile() {
489                    if(dir == null || filename == null) {
490                            return null;
491                    }
492                    else {
493                            return new File(dir + File.separator + filename);
494                    }
495            }
496    }