View Javadoc

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