View Javadoc

1   package org.apache.turbine.services.upload;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import javax.servlet.ServletConfig;
20  import javax.servlet.http.HttpServletRequest;
21  
22  import org.apache.turbine.Turbine;
23  import org.apache.turbine.services.TurbineBaseService;
24  import org.apache.turbine.services.servlet.TurbineServlet;
25  import org.apache.turbine.util.ServletUtils;
26  import org.apache.turbine.util.TurbineException;
27  import org.apache.turbine.util.parser.ParameterParser;
28  
29  /***
30   * <p> This class is a base implementation of
31   * {@link org.apache.turbine.services.upload.UploadService}.
32   *
33   * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
34   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
35   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
36   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
37   * @version $Id: BaseUploadService.java 264148 2005-08-29 14:21:04Z henning $
38   */
39  public abstract class BaseUploadService
40          extends TurbineBaseService
41          implements UploadService
42  {
43      /***
44       * A maximum lenght of a single header line that will be
45       * parsed. (1024 bytes).
46       */
47      public static final int MAX_HEADER_SIZE = 1024;
48  
49      /***
50       * Initializes the service.
51       *
52       * This method processes the repository path, to make it relative to the
53       * web application root, if neccessary
54       */
55      public void init()
56      {
57          String path = getProperties()
58                  .getProperty(UploadService.REPOSITORY_KEY,
59                          UploadService.REPOSITORY_DEFAULT.toString());
60          if (!path.startsWith("/"))
61          {
62              String realPath = TurbineServlet.getRealPath(path);
63              if (realPath != null)
64              {
65                  path = realPath;
66              }
67          }
68          getProperties().setProperty(UploadService.REPOSITORY_KEY, path);
69          setInit(true);
70      }
71  
72      /***
73       * <p> Processes an <a href="http://rf.cx/rfc1867.html">RFC
74       * 1867</a> compliant <code>multipart/form-data</code> stream.
75       *
76       * @param req The servlet request to be parsed.
77       * @param params The ParameterParser instance to insert form
78       * fields into.
79       * @param path The location where the files should be stored.
80       * @exception TurbineException If there are problems reading/parsing
81       * the request or storing files.
82       */
83      public abstract void parseRequest(HttpServletRequest req,
84                                        ParameterParser params,
85                                        String path)
86              throws TurbineException;
87  
88      /***
89       * <p> Retrieves the value of <code>size.max</code> property of the
90       * {@link org.apache.turbine.services.upload.UploadService}.
91       *
92       * @return The maximum upload size.
93       */
94      public long getSizeMax()
95      {
96          return getConfiguration().getLong(
97                  UploadService.SIZE_MAX_KEY,
98                  UploadService.SIZE_MAX_DEFAULT);
99      }
100 
101     /***
102      * <p> Retrieves the value of <code>size.threshold</code> property of
103      * {@link org.apache.turbine.services.upload.UploadService}.
104      *
105      * @return The threshold beyond which files are written directly to disk.
106      */
107     public int getSizeThreshold()
108     {
109         return getConfiguration().getInt(
110                 UploadService.SIZE_THRESHOLD_KEY,
111                 UploadService.SIZE_THRESHOLD_DEFAULT);
112     }
113 
114     /***
115      * <p> Retrieves the value of the <code>repository</code> property of
116      * {@link org.apache.turbine.services.upload.UploadService}.
117      *
118      * @return The repository.
119      */
120     public String getRepository()
121     {
122         // get the reposity value from TR.props
123         String tmpPath = getConfiguration().getString(
124                 UploadService.REPOSITORY_KEY,
125                 UploadService.REPOSITORY_DEFAULT);
126 
127         // return the expanded path name
128         ServletConfig config = Turbine.getTurbineServletConfig();
129         return ServletUtils.expandRelative(config, tmpPath);
130 
131     }
132 
133     /***
134      * Retrieves the value of the 'automatic' property of {@link
135      * UploadService}. This reports whether the Parameter parser
136      * should allow "automatic" uploads if it is submitted to
137      * Turbine.
138      *
139      * @return The value of 'automatic' property of {@link
140      * UploadService}.
141      */
142     public boolean getAutomatic()
143     {
144         return getConfiguration().getBoolean(
145                 UploadService.AUTOMATIC_KEY,
146                 UploadService.AUTOMATIC_DEFAULT);
147     }
148 }