📄 cmsrequesthttpservlet.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/core/CmsRequestHttpServlet.java,v $
* Date : $Date: 2005/06/21 15:49:59 $
* Version: $Revision: 1.3 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001 The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.opencms.core;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsResourceTranslator;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
/**
* Implementation of the I_CmsRequest interface which wraps a HttpServletRequest
* and includes handling of multipart - requests.<p>
*
* This implementation uses a HttpServletRequest as original request to create a
* CmsRequestHttpServlet. This either can be a normal HttpServletRequest or a
* CmsMultipartRequest which is used to upload file into the OpenCms.<p>
*
* This class contains a modification of the MultipartRequest published in the O'Reilly
* book <it>Java Servlet Programming </it> by J. Junte, <a href=http://www.servlets.com/ > www.servlets.com </a>
* <p>
* It Constructs a new MultipartRequest to handle the specified request,
* saving any uploaded files to the given directory, and limiting the upload size to
* a maximum size of 8 MB by default.
* <p>
* The idea is to modify the given MultiPartRequest to make it transparent to normal
* requests and store file into CmsFile objects so that they can be transferred into
* the OpenCms document database.
*
* @author Michael Emmerich
* @author Alexander Lucas
*
* @version $Revision: 1.3 $ $Date: 2005/06/21 15:49:59 $
*
* @deprecated Will not be supported past the OpenCms 6 release.
*/
public class CmsRequestHttpServlet implements I_CmsRequest {
/**
* Definition of the error message for missing boundary.
*/
static final String C_REQUEST_NOBOUNDARY = "Separation boundary was not specified";
/**
* Definition of the error message for being not a multipart request.
*/
static final String C_REQUEST_NOMULTIPART = "Posted content type isn't multipart/form-data";
/**
* Definition of the error message for an empty request.
*/
static final String C_REQUEST_NOTNULL = "The Request cannot be null.";
/**
* Definition of the error message for a premature end.
*/
static final String C_REQUEST_PROMATUREEND = "Corrupt form data: premature ending";
/**
* Definition of the error message for a negative maximum post size.
*/
static final String C_REQUEST_SIZENOTNEGATIVE = "The maxPostSize must be positive.";
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsRequestHttpServlet.class);
/**
* File counter.
*/
int m_filecounter;
/**
* Storage for all uploaded files.
*/
private Hashtable m_files = new Hashtable();
/**
* Storage for all uploaded name values.
*/
private Hashtable m_parameters = new Hashtable();
/**
* The original request.
*/
private HttpServletRequest m_req;
/** String to the requested resource. */
private String m_requestedResource;
private String m_scheme="";
private String m_serverName="";
private int m_serverPort;
private String m_servletUrl="";
/**
* Resource translator (for uploaded file names).
*/
private CmsResourceTranslator m_translator;
/**
* The type of theis CmsRequest.
*/
private int m_type = com.opencms.core.I_CmsConstants.C_REQUEST_HTTP;
/**
* The data from the original request. We save them to get them after the
* original request is expired.
*/
private String m_webAppUrl="";
/**
* Constructor, creates a new CmsRequestHttpServlet object.
*
* @param req The original HttpServletRequest used to create this CmsRequest
* @param translator the translator
* @throws IOException if something goes wrong
*/
public CmsRequestHttpServlet(HttpServletRequest req, CmsResourceTranslator translator) throws IOException {
m_req = req;
m_translator = translator;
// get the webAppUrl and the servletUrl
try {
m_webAppUrl = m_req.getContextPath();
} catch (NoSuchMethodError err) {
// this is the old servlet-api without this method
// ignore this missing method and the context-path
}
m_serverName = m_req.getServerName();
m_scheme = m_req.getScheme();
m_serverPort = m_req.getServerPort();
m_servletUrl = m_webAppUrl + m_req.getServletPath();
// Test if this is a multipart-request.
// If it is, extract all files from it.
String type = req.getHeader("content-type");
if ((type != null) && type.startsWith("multipart/form-data") && (req.getContentLength() > -1)) {
readRequest();
} else {
// Encoding project:
// Set request content encoding
String encoding = req.getCharacterEncoding();
if (encoding == null) {
// First try to get current encoding from session
HttpSession httpSession = req.getSession(false);
I_CmsSession session = (httpSession != null)
? new CmsSession(httpSession) : null;
if (session != null) {
encoding = (String)session.getValue(
com.opencms.core.I_CmsConstants.C_SESSION_CONTENT_ENCODING);
}
// If encoding not found in session - use default one
if (encoding == null) {
encoding = OpenCms.getSystemInfo().getDefaultEncoding();
}
req.setCharacterEncoding(encoding);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Request character encoding is: '" + req.getCharacterEncoding() + "'");
}
}
}
/**
* Returns the content of an uploaded file.
* Returns null if no file with this name has been uploaded with this request.
* Returns an empty byte[] if a file without content has been uploaded.
*
* @param name The name of the uploaded file.
* @return The selected uploaded file content.
*/
public byte[] getFile(String name) {
return (byte[])m_files.get(name);
}
/**
* Returns the names of all uploaded files in this request.
* Returns an empty eumeration if no files were included in the request.
*
* @return An Enumeration of file names.
*/
public Enumeration getFileNames() {
Enumeration names = m_files.keys();
return names;
}
/**
* Returns the original request that was used to create the CmsRequest.
*
* @return The original request of the CmsRequest.
*/
public HttpServletRequest getOriginalRequest() {
return m_req;
}
/**
* Returns the type of the request that was used to create the CmsRequest.
* The returned int must be one of the constants defined above in this interface.
*
* @return The type of the CmsRequest.
*/
public int getOriginalRequestType() {
return m_type;
}
/**
* Returns the value of a named parameter as a String.
* Returns null if the parameter does not exist or an empty string if the parameter
* exists but without a value.
*
* @param name The name of the parameter.
* @return The value of the parameter.
*/
public String getParameter(String name) {
String parameter = null;
// Test if this is a multipart-request.
// If it is, extract all files from it.
String type = m_req.getHeader("content-type");
if ((type != null) && type.startsWith("multipart/form-data")) {
parameter = (String)m_parameters.get(name);
} else {
parameter = m_req.getParameter(name);
}
return parameter;
}
/**
* Returns all parameter names as an Enumeration of String objects.
* Returns an empty Enumeratrion if no parameters were included in the request.
*
* @return Enumeration of parameter names.
*/
public Enumeration getParameterNames() {
String type = m_req.getHeader("content-type");
if ((type != null) && type.startsWith("multipart/form-data")) {
// add all parameters extreacted in the multipart handling
return m_parameters.keys();
} else {
// add all parameters from the original request
return m_req.getParameterNames();
}
}
/**
* Returns all parameter values of a parameter key.
*
* @param key the parameter key
* @return Aarray of String containing the parameter values.
*/
public String[] getParameterValues(String key) {
return m_req.getParameterValues(key);
}
/**
* This funtion returns the name of the requested resource.
* <P>
* For a http request, the name of the resource is extracted as follows:
* <CODE>http://{servername}/{servletpath}/{path to the cms resource}</CODE>
* In the following example:
* <CODE>http://my.work.server/servlet/opencms/system/def/explorer</CODE>
* the requested resource is <CODE>/system/def/explorer</CODE>.
* </P>
*
* @return The path to the requested resource.
*/
public String getRequestedResource() {
if (m_requestedResource != null) {
return m_requestedResource;
}
m_requestedResource = m_req.getPathInfo();
if (m_requestedResource == null) {
m_requestedResource = "/";
}
return m_requestedResource;
}
/**
* Methods to get the data from the original request.
*
* @return the scheme
*/
public String getScheme() {
return m_scheme;
}
/**
* Methods to get the data from the original request.
*
* @return the server name
*/
public String getServerName() {
return m_serverName;
}
/**
* Methods to get the data from the original request.
*
* @return the server port
*/
public int getServerPort() {
return m_serverPort;
}
/**
* Gets the part of the Url that describes the current servlet of this
* Web-Application.
*
* @return the servlet part of the url
*/
public String getServletUrl() {
return m_servletUrl;
}
/**
* Returns the part of the Url that descibes the Web-Application.
*
* E.g: http://www.myserver.com/opencms/engine/index.html returns
* http://www.myserver.com/opencms
*
* @return the web application part of the url
*/
public String getWebAppUrl() {
return m_webAppUrl;
}
/**
* Overwrites the original request that was used to create the CmsRequest.
*
* @param request the request
*/
public void setOriginalRequest(HttpServletRequest request) {
m_req = request;
}
/**
* Set the name returned by getRequestedResource().
* This is required in case there was a folder name requested and
* a default file (e.g. index.html) has to be used instead of the folder.
*
* @param resourceName The name to set the requested resource name to
*/
public void setRequestedResource(String resourceName) {
m_requestedResource = resourceName;
}
/**
* Extracts and returns the boundary token from a line.
*
* @param line with boundary from input stream.
* @return The boundary token.
*/
private String extractBoundary(String line) {
int index = line.indexOf("boundary=");
if (index == -1) {
return null;
}
// 9 for "boundary="
String boundary = line.substring(index + 9);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -