View Javadoc

1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.plugins.guice;
18  
19  import javax.servlet.http.HttpServletRequest;
20  
21  import org.apache.commons.fileupload.FileItemFactory;
22  import org.apache.commons.fileupload.FileUpload;
23  import org.apache.commons.fileupload.RequestContext;
24  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
25  import org.apache.commons.fileupload.servlet.ServletFileUpload;
26  import org.apache.commons.fileupload.servlet.ServletRequestContext;
27  
28  import com.google.inject.AbstractModule;
29  import com.google.inject.Provides;
30  import com.google.inject.servlet.RequestScoped;
31  
32  /**
33   * commons-fileupload の設定を行います。
34   * <p>
35   * {@link org.apache.commons.fileupload.disk.DiskFileItemFactory}
36   * はテンポラリにローカルファイルシステムを使用します。Goolgle&nbsp;App&nbsp;Engine
37   * のようにファイルシステムへの書き込みに制限がある場合は
38   * {@link org.apache.commons.fileupload.disk.DiskFileItemFactory} ではなく、
39   * {@link org.seasar.cubby.fileupload.StreamFileItemFactory} を試してみてください。
40   * これはマルチパートのデータをテンポラリを使用せずにオンメモリで処理します。
41   * </p>
42   */
43  public class FileUploadModule extends AbstractModule {
44  
45  	/**
46  	 * {@inheritDoc}
47  	 */
48  	@Override
49  	protected void configure() {
50  	}
51  
52  	@Provides
53  	FileUpload provideFileUpload() {
54  		final FileItemFactory fileItemFactory = new DiskFileItemFactory();
55  		final FileUpload fileUpload = new ServletFileUpload(fileItemFactory);
56  		return fileUpload;
57  	}
58  
59  	@Provides
60  	@RequestScoped
61  	RequestContext provideRequestContext(final HttpServletRequest request) {
62  		final RequestContext requestContext = new ServletRequestContext(request);
63  		return requestContext;
64  	}
65  
66  }