📄 cmsrequesthttpservlet.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/core/CmsRequestHttpServlet.java,v $
* Date : $Date: 2003/02/15 11:14:57 $
* Version: $Revision: 1.36 $
*
* 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 com.opencms.boot.I_CmsLogChannels;
import com.opencms.flex.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;
/**
* 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.36 $ $Date: 2003/02/15 11:14:57 $
*/
public class CmsRequestHttpServlet implements I_CmsRequest {
/**
* 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 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 a negative maximum post size.
*/
static final String C_REQUEST_SIZENOTNEGATIVE = "The maxPostSize must be positive.";
/**
* 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 missing boundary.
*/
static final String C_REQUEST_NOBOUNDARY = "Separation boundary was not specified";
/**
* The original request.
*/
private HttpServletRequest m_req;
/**
* The type of theis CmsRequest.
*/
private int m_type = I_CmsConstants.C_REQUEST_HTTP;
/**
* Storage for all uploaded files.
*/
private Hashtable m_files = new Hashtable();
/**
* Storage for all uploaded name values
*/
private Hashtable m_parameters = new Hashtable();
int filecounter = 0;
/**
* The data from the original request. We save them to get them after the
* original request is expired.
*/
private String m_webAppUrl="";
private String m_servletUrl="";
private String m_serverName="";
private String m_scheme="";
private int m_serverPort;
/**
* Resource translator (for uploaded file names)
*/
private CmsResourceTranslator m_translator;
/**
* Constructor, creates a new CmsRequestHttpServlet object.
*
* @param req The original HttpServletRequest used to create this CmsRequest.
*/
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(
I_CmsConstants.C_SESSION_CONTENT_ENCODING);
}
// If encoding not found in session - use default one
if (encoding == null) {
encoding = OpenCms.getDefaultEncoding();
}
req.setCharacterEncoding(encoding);
}
if (I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_DEBUG))
A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_DEBUG, "Request character encoding is: '" + req.getCharacterEncoding() + "'");
}
}
/**
* 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);
// The real boundary is always preceeded by an extra "--"
boundary = "--" + boundary;
return boundary;
}
/**
* Extracts and returns the content type from a line, or null if the
* line was empty.
* @param line Line from input stream.
* @return Content type of the line.
* @throws IOException Throws an IOException if the line is malformatted.
*/
private String extractContentType(String line) throws IOException {
String contentType = null;
// Convert the line to a lowercase string
String origline = line;
line = origline.toLowerCase();
// Get the content type, if any
if(line.startsWith("content-type")) {
int start = line.indexOf(" ");
if(start == -1) {
throw new IOException("Content type corrupt: " + origline);
}
contentType = line.substring(start + 1);
}
else {
if(line.length() != 0) {
// no content type, so should be empty
throw new IOException("Malformed line after disposition: " + origline);
}
}
return contentType;
}
/**
* Extracts and returns disposition info from a line, as a String array
* with elements: disposition, name, filename. Throws an IOException
* if the line is malformatted.
*
* @param line Line from input stream.
* @return Array of string containing disposition information.
* @throws IOException if the line is malformatted
*/
private String[] extractDispositionInfo(String line) throws IOException {
// Return the line's data as an array: disposition, name, filename
String[] retval = new String[3];
// Convert the line to a lowercase string without the ending \r\n
// Keep the original line for error messages and for variable names.
String origline = line;
line = origline.toLowerCase();
// Get the content disposition, should be "form-data"
int start = line.indexOf("content-disposition: ");
int end = line.indexOf(";");
if(start == -1 || end == -1) {
throw new IOException("Content disposition corrupt: " + origline);
}
String disposition = line.substring(start + 21, end);
if(!disposition.equals("form-data")) {
throw new IOException("Invalid content disposition: " + disposition);
}
// Get the field name
// start at last semicolon
start = line.indexOf("name=\"", end);
// skip name=\"
end = line.indexOf("\"", start + 7);
if(start == -1 || end == -1) {
throw new IOException("Content disposition corrupt: " + origline);
}
String name = origline.substring(start + 6, end);
// Get the filename, if given
String filename = null;
// start after name
start = line.indexOf("filename=\"", end + 2);
// skip filename=\"
end = line.indexOf("\"", start + 10);
// note the !=
if(start != -1 && end != -1) {
filename = origline.substring(start + 10, end);
// The filename may contain a full path. Cut to just the filename.
int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
if(slash > -1) {
filename = filename.substring(slash + 1); // past last slash
}
if(filename.equals("")) {
filename = "unknown"; // sanity check
}
}
// Translate the filename using the resource translator
filename = m_translator.translateResource(filename);
// Return a String array: disposition, name, filename
retval[0] = disposition;
retval[1] = name;
retval[2] = filename;
return retval;
}
/**
* 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) {
byte[] content = null;
content = (byte[])m_files.get(name);
return content;
}
/**
* 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 Object getOriginalRequest() {
return m_req;
}
/**
* Overwrites the original request that was used to create the CmsRequest.
*/
public void setOriginalRequest(Object request) {
m_req = (HttpServletRequest)request;
}
/**
* 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() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -