Coverage Report - org.seasar.cubby.controller.impl.MultipartRequestParserImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
MultipartRequestParserImpl
12%
6/50
6%
1/18
0
 
 1  
 /*
 2  
  * Copyright 2004-2008 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.cubby.controller.impl;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.io.UnsupportedEncodingException;
 20  
 import java.util.ArrayList;
 21  
 import java.util.HashMap;
 22  
 import java.util.LinkedHashMap;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 import java.util.Map.Entry;
 26  
 
 27  
 import javax.servlet.http.HttpServletRequest;
 28  
 
 29  
 import org.apache.commons.fileupload.FileItem;
 30  
 import org.apache.commons.fileupload.FileUpload;
 31  
 import org.apache.commons.fileupload.FileUploadException;
 32  
 import org.apache.commons.fileupload.RequestContext;
 33  
 import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
 34  
 import org.apache.commons.fileupload.servlet.ServletFileUpload;
 35  
 import org.seasar.cubby.controller.RequestParser;
 36  
 import org.seasar.cubby.exception.FileUploadRuntimeException;
 37  
 import org.seasar.framework.container.S2Container;
 38  
 import org.seasar.framework.exception.IORuntimeException;
 39  
 import org.seasar.framework.util.StringUtil;
 40  
 
 41  
 /**
 42  
  * contentType が multipart/form-data のリクエストに対応したリクエスト解析器です。
 43  
  * <p>
 44  
  * リクエストの解析には <a href="http://commons.apache.org/fileupload/">Commons FileUpload</a>
 45  
  * を使用します。
 46  
  * </p>
 47  
  * 
 48  
  * @author baba
 49  
  * @see <a href="http://commons.apache.org/fileupload/">Commons FileUpload</a>
 50  
  * @since 1.0.0
 51  
  */
 52  
 public class MultipartRequestParserImpl implements RequestParser {
 53  
 
 54  
         /** コンテナ。 */
 55  
         private final S2Container container;
 56  
 
 57  
         /**
 58  
          * インスタンス化します。
 59  
          * 
 60  
          * @param container
 61  
          *            コンテナ
 62  
          */
 63  11
         public MultipartRequestParserImpl(final S2Container container) {
 64  11
                 this.container = container;
 65  11
         }
 66  
 
 67  
         /**
 68  
          * {@inheritDoc}
 69  
          * <p>
 70  
          * 指定されたリクエストがマルチパートのリクエスト(contentType が multipart/form-data)であれば、コンテナに登録された
 71  
          * {@link FileUpload} と {@link RequestContext} を使用してリクエストを解析します。
 72  
          * <p>
 73  
          * リクエストパラメータを戻り値の {@link Map} に格納する際には以下のように変換します。
 74  
          * <ul>
 75  
          * <li> フォームのフィールド
 76  
          * <p>
 77  
          * 文字列に変換
 78  
          * </p>
 79  
          * </li>
 80  
          * <li> フォームのフィールド以外(アップロードされたファイル)
 81  
          * <p>
 82  
          * {@link FileItem}に変換
 83  
          * </p>
 84  
          * </li>
 85  
          * </ul>
 86  
          * </p>
 87  
          * </p>
 88  
          * <p>
 89  
          * 指定されたリクエストが通常のリクエストであれば、{@link HttpServletRequest#getParameterMap()}
 90  
          * の結果をそのまま返します。
 91  
          * </p>
 92  
          * 
 93  
          * @see FileUpload
 94  
          */
 95  
         @SuppressWarnings("unchecked")
 96  
         public Map<String, Object[]> getParameterMap(
 97  
                         final HttpServletRequest request) {
 98  2
                 final Map<String, Object[]> parameterMap = new HashMap<String, Object[]>(
 99  
                                 request.getParameterMap());
 100  2
                 if (ServletFileUpload.isMultipartContent(request)) {
 101  0
                         final S2Container root = container.getRoot();
 102  0
                         final FileUpload fileUpload = (FileUpload) root
 103  
                                         .getComponent(FileUpload.class);
 104  0
                         final RequestContext requestContext = (RequestContext) root
 105  
                                         .getComponent(RequestContext.class);
 106  0
                         parameterMap.putAll(this.getMultipartParameterMap(fileUpload,
 107  
                                         requestContext));
 108  
                 }
 109  2
                 return parameterMap;
 110  
         }
 111  
 
 112  
         @SuppressWarnings("unchecked")
 113  
         Map<String, Object[]> getMultipartParameterMap(final FileUpload fileUpload,
 114  
                         final RequestContext requestContext) {
 115  
                 try {
 116  0
                         final String encoding = requestContext.getCharacterEncoding();
 117  0
                         fileUpload.setHeaderEncoding(encoding);
 118  0
                         final List<FileItem> items = fileUpload
 119  
                                         .parseRequest(requestContext);
 120  
 
 121  
                         // Fieldごとにパラメータを集める
 122  0
                         final Map<String, Object[]> parameterMap = toParameterMap(encoding,
 123  
                                         items);
 124  
 
 125  0
                         return parameterMap;
 126  0
                 } catch (final FileUploadException e) {
 127  
                         final String messageCode;
 128  
                         final Object[] args;
 129  0
                         if (e instanceof SizeLimitExceededException) {
 130  0
                                 final SizeLimitExceededException sle = (SizeLimitExceededException) e;
 131  0
                                 messageCode = "ECUB0202";
 132  0
                                 args = new Object[] { sle.getPermittedSize(), sle.getActualSize() };
 133  0
                         } else {
 134  0
                                 messageCode = "ECUB0201";
 135  0
                                 args = new Object[] { e };
 136  
                         }
 137  0
                         throw new FileUploadRuntimeException(messageCode, args, e);
 138  0
                 } catch (final IOException e) {
 139  0
                         throw new IORuntimeException(e);
 140  
                 }
 141  
         }
 142  
 
 143  
         Map<String, Object[]> toParameterMap(final String encoding,
 144  
                         final List<FileItem> items) throws UnsupportedEncodingException {
 145  0
                 final Map<String, List<Object>> valueListParameterMap = new LinkedHashMap<String, List<Object>>();
 146  0
                 for (final FileItem item : items) {
 147  
                         final Object value;
 148  0
                         if (item.isFormField()) {
 149  0
                                 value = item.getString(encoding);
 150  
                         } else {
 151  0
                                 if (StringUtil.isEmpty(item.getName()) || item.getSize() == 0) {
 152  
                                         // ファイル名無し、あるいは0バイトのファイル
 153  0
                                         value = null;
 154  
                                 } else {
 155  0
                                         value = item;
 156  
                                 }
 157  
                         }
 158  
                         final List<Object> values;
 159  0
                         if (valueListParameterMap.containsKey(item.getFieldName())) {
 160  0
                                 values = valueListParameterMap.get(item.getFieldName());
 161  
                         } else {
 162  0
                                 values = new ArrayList<Object>();
 163  0
                                 valueListParameterMap.put(item.getFieldName(), values);
 164  
                         }
 165  0
                         values.add(value);
 166  0
                 }
 167  
 
 168  0
                 final Map<String, Object[]> parameterMap = fromValueListToValueArray(valueListParameterMap);
 169  0
                 return parameterMap;
 170  
         }
 171  
 
 172  
         Map<String, Object[]> fromValueListToValueArray(
 173  
                         final Map<String, List<Object>> collectParameterMap) {
 174  
                 // 配列でパラメータMapを構築
 175  0
                 final Map<String, Object[]> parameterMap = new HashMap<String, Object[]>();
 176  
                 for (final Entry<String, List<Object>> entry : collectParameterMap
 177  0
                                 .entrySet()) {
 178  0
                         final List<Object> values = entry.getValue();
 179  
                         final Object[] valueArray;
 180  0
                         if (values.get(0) instanceof String) {
 181  0
                                 valueArray = new String[values.size()];
 182  
                         } else {
 183  0
                                 valueArray = new FileItem[values.size()];
 184  
                         }
 185  0
                         parameterMap.put(entry.getKey(), values.toArray(valueArray));
 186  0
                 }
 187  0
                 return parameterMap;
 188  
         }
 189  
 
 190  
 }