001: package org.apache.turbine.services.upload;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import javax.servlet.ServletConfig;
023: import javax.servlet.http.HttpServletRequest;
024:
025: import org.apache.turbine.Turbine;
026: import org.apache.turbine.services.TurbineBaseService;
027: import org.apache.turbine.services.servlet.TurbineServlet;
028: import org.apache.turbine.util.ServletUtils;
029: import org.apache.turbine.util.TurbineException;
030: import org.apache.turbine.util.parser.ParameterParser;
031:
032: /**
033: * <p> This class is a base implementation of
034: * {@link org.apache.turbine.services.upload.UploadService}.
035: *
036: * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
037: * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
038: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
039: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
040: * @version $Id: BaseUploadService.java 534527 2007-05-02 16:10:59Z tv $
041: */
042: public abstract class BaseUploadService extends TurbineBaseService
043: implements UploadService {
044: /**
045: * A maximum lenght of a single header line that will be
046: * parsed. (1024 bytes).
047: */
048: public static final int MAX_HEADER_SIZE = 1024;
049:
050: /**
051: * Initializes the service.
052: *
053: * This method processes the repository path, to make it relative to the
054: * web application root, if neccessary
055: */
056: public void init() {
057: String path = getProperties().getProperty(
058: UploadService.REPOSITORY_KEY,
059: UploadService.REPOSITORY_DEFAULT.toString());
060: if (!path.startsWith("/")) {
061: String realPath = TurbineServlet.getRealPath(path);
062: if (realPath != null) {
063: path = realPath;
064: }
065: }
066: getProperties().setProperty(UploadService.REPOSITORY_KEY, path);
067: setInit(true);
068: }
069:
070: /**
071: * <p> Processes an <a href="http://rf.cx/rfc1867.html">RFC
072: * 1867</a> compliant <code>multipart/form-data</code> stream.
073: *
074: * @param req The servlet request to be parsed.
075: * @param params The ParameterParser instance to insert form
076: * fields into.
077: * @param path The location where the files should be stored.
078: * @exception TurbineException If there are problems reading/parsing
079: * the request or storing files.
080: */
081: public abstract void parseRequest(HttpServletRequest req,
082: ParameterParser params, String path)
083: throws TurbineException;
084:
085: /**
086: * <p> Retrieves the value of <code>size.max</code> property of the
087: * {@link org.apache.turbine.services.upload.UploadService}.
088: *
089: * @return The maximum upload size.
090: */
091: public long getSizeMax() {
092: return getConfiguration().getLong(UploadService.SIZE_MAX_KEY,
093: UploadService.SIZE_MAX_DEFAULT);
094: }
095:
096: /**
097: * <p> Retrieves the value of <code>size.threshold</code> property of
098: * {@link org.apache.turbine.services.upload.UploadService}.
099: *
100: * @return The threshold beyond which files are written directly to disk.
101: */
102: public int getSizeThreshold() {
103: return getConfiguration().getInt(
104: UploadService.SIZE_THRESHOLD_KEY,
105: UploadService.SIZE_THRESHOLD_DEFAULT);
106: }
107:
108: /**
109: * <p> Retrieves the value of the <code>repository</code> property of
110: * {@link org.apache.turbine.services.upload.UploadService}.
111: *
112: * @return The repository.
113: */
114: public String getRepository() {
115: // get the reposity value from TR.props
116: String tmpPath = getConfiguration().getString(
117: UploadService.REPOSITORY_KEY,
118: UploadService.REPOSITORY_DEFAULT);
119:
120: // return the expanded path name
121: ServletConfig config = Turbine.getTurbineServletConfig();
122: return ServletUtils.expandRelative(config, tmpPath);
123:
124: }
125:
126: /**
127: * Retrieves the value of the 'automatic' property of {@link
128: * UploadService}. This reports whether the Parameter parser
129: * should allow "automatic" uploads if it is submitted to
130: * Turbine.
131: *
132: * @return The value of 'automatic' property of {@link
133: * UploadService}.
134: */
135: public boolean getAutomatic() {
136: return getConfiguration().getBoolean(
137: UploadService.AUTOMATIC_KEY,
138: UploadService.AUTOMATIC_DEFAULT);
139: }
140: }
|