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.http.HttpServletRequest;
20  
21  import org.apache.turbine.services.InstantiationException;
22  import org.apache.turbine.services.TurbineServices;
23  import org.apache.turbine.util.TurbineException;
24  import org.apache.turbine.util.parser.ParameterParser;
25  
26  /***
27   * <p> This is a facade class for {@link UploadService}.
28   *
29   * <p> This class provides static methods that retrieve the configured
30   * (in TurbineResource.properties) implementation of {@link
31   * UploadService} and perform certain operations on it.  It uses
32   * constants defined in {@link UploadService} interface for accessing
33   * the service's properties and default values for them.
34   *
35   * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
36   * @version $Id: TurbineUpload.java 264148 2005-08-29 14:21:04Z henning $
37   */
38  public abstract class TurbineUpload
39  {
40      /***
41       * <p> Retrieves an instance of system's configured implementation
42       * of <code>UploadService</code>
43       *
44       * @return An instance of UploadService
45       */
46      public static UploadService getService()
47      {
48          return (UploadService) TurbineServices.getInstance().
49                  getService(UploadService.SERVICE_NAME);
50      }
51  
52      /***
53       * Checks whether an Upload Service is configured.
54       * This method is safe to call even with no Upload
55       * service installed.
56       *
57       * @return True if an upload Service is configured
58       */
59      public static boolean isAvailable()
60      {
61          UploadService upload = null;
62          try
63          {
64              upload = getService();
65          }
66          catch (InstantiationException ie)
67          {
68              // If the service couldn't be instantiated, it obviously
69              // isn't configured.
70              return false;
71          }
72          return true;
73      }
74  
75      /***
76       * Retrieves the value of the 'automatic' property of {@link
77       * UploadService}. This reports whether the Upload Service
78       * is available and (if yes), the Parameter parser should
79       * allow "automatic" uploads if it is submitted to Turbine.
80       *
81       * This method is safe to call even with no Upload Service
82       * configured.
83       *
84       * @return The value of 'automatic' property of {@link
85       * UploadService}.
86       */
87      public static boolean getAutomatic()
88      {
89          // Short circuit evaluation of the && operator!
90          return isAvailable() && getService().getAutomatic();
91      }
92  
93      /***
94       * <p> Retrieves the value of 'size.max' property of {@link
95       * UploadService}.
96       *
97       * @return The value of 'size.max' property of {@link
98       * UploadService}.
99       */
100     public static long getSizeMax()
101     {
102         return getService().getSizeMax();
103     }
104 
105     /***
106      * <p> Retrieves the value of <code>size.threshold</code> property of
107      * {@link org.apache.turbine.services.upload.UploadService}.
108      *
109      * @return The threshold beyond which files are written directly to disk.
110      */
111     public static int getSizeThreshold()
112     {
113         return getService().getSizeThreshold();
114     }
115 
116     /***
117      * <p> Retrieves the value of the <code>repository</code> property of
118      * {@link org.apache.turbine.services.upload.UploadService}.
119      *
120      * @return The repository.
121      */
122     public static String getRepository()
123     {
124         return getService().getRepository();
125     }
126 
127     /***
128      * <p> Performs parsing the request and storing files and form
129      * fields.  Default file repository is used.  This method is
130      * called by the {@link ParameterParser} if automatic upload is
131      * enabled.
132      *
133      * @param req The servlet request to be parsed.
134      * @param params The ParameterParser instance to insert form
135      * fields into.
136      * @exception TurbineException If there are problems reading/parsing
137      * the request or storing files.
138      */
139     public static void parseRequest(HttpServletRequest req,
140                                     ParameterParser params)
141             throws TurbineException
142     {
143         UploadService upload = getService();
144         upload.parseRequest(req, params, upload.getRepository());
145     }
146 
147     /***
148      * <p> Performs parsing the request and storing files and form
149      * fields.  Custom file repository may be specified.  You can call
150      * this method in your file upload {@link
151      * org.apache.turbine.modules.Action} to if you need to specify a
152      * custom directory for storing files.
153      *
154      * @param req The servlet request to be parsed.
155      * @param params The ParameterParser instance to insert form
156      * fields into.
157      * @param path The location where the files should be stored.
158      * @exception TurbineException If there are problems reading/parsing
159      * the request or storing files.
160      */
161     public static void parseRequest(HttpServletRequest req,
162                                     ParameterParser params,
163                                     String path)
164             throws TurbineException
165     {
166         getService().parseRequest(req, params, path);
167     }
168 }