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