📄 opencms.java
字号:
* the running OpenCmsHttpServlet.<p>
*
* In case a directory name is requested, the default files of the
* directory will be looked up and the first match is returned.
*
* @param cms the current CmsObject
* @return CmsFile the requested file read from the VFS
*
* @throws CmsException in case the file does not exist or the user has insufficient access permissions
*/
CmsFile initResource(CmsObject cms) throws CmsException {
CmsFile file = null;
// Get the requested filename from the request context
String resourceName = cms.getRequestContext().getUri();
try {
// Try to read the requested file
file = cms.readFile(resourceName);
} catch (CmsException e) {
if (e.getType() == CmsException.C_NOT_FOUND) {
// The requested file was not found
// Check if a folder name was requested, and if so, try
// to read the default pages in that folder
try {
// Try to read the requested resource name as a folder
CmsFolder folder = cms.readFolder(resourceName);
// If above call did not throw an exception the folder
// was sucessfully read, so lets go on check for default
// pages in the folder now
// Check if C_PROPERTY_DEFAULT_FILE is set on folder
String defaultFileName = cms.readProperty(folder.getPath(), I_CmsConstants.C_PROPERTY_DEFAULT_FILE);
if (defaultFileName != null) {
// Property was set, so look up this file first
String tmpResourceName = folder.getPath() + defaultFileName;
try {
file = cms.readFile(tmpResourceName);
// No exception? So we have found the default file
cms.getRequestContext().getRequest().setRequestedResource(tmpResourceName);
} catch (CmsException exc) {
if (exc.getType() == CmsException.C_NO_ACCESS) {
// Maybe no access to default file?
throw exc;
}
}
}
if (file == null) {
// No luck with the property, so check default files specified in opencms.properties (if required)
for (int i=0; i<m_defaultFilenames.length; i++) {
String tmpResourceName = folder.getPath() + m_defaultFilenames[i];
try {
file = cms.readFile(tmpResourceName);
// No exception? So we have found the default file
cms.getRequestContext().getRequest().setRequestedResource(tmpResourceName);
// Stop looking for default files
break;
} catch (CmsException exc) {
if (exc.getType() == CmsException.C_NO_ACCESS) {
// Maybe no access to default file?
throw exc;
}
// Otherwise just continue looking for files
}
}
}
if (file == null) {
// No default file was found, throw original exception
throw e;
}
} catch (CmsException ex) {
// Exception trying to read the folder (or it's properties)
if (ex.getType() == CmsException.C_NOT_FOUND) {
// Folder with the name does not exist, throw original exception
throw e;
}
// If the folder was found there might have been a permission problem
throw ex;
}
} else {
// Throw the CmsException (possible cause e.g. no access permissions)
throw e;
}
}
if (file != null) {
// test if this file is only available for internal access operations
if ((file.getAccessFlags() & C_ACCESS_INTERNAL_READ) > 0) {
throw new CmsException(
CmsException.C_EXTXT[CmsException.C_INTERNAL_FILE]
+ cms.getRequestContext().getUri(),
CmsException.C_INTERNAL_FILE);
}
// test if this file has to be checked or modified
Iterator i = m_checkFile.iterator();
while (i.hasNext()) {
try {
file = ((I_CmsCheckResource) i.next()).checkResource(file, cms);
// the loop has to be interrupted when the exception is thrown!
} catch (CmsCheckResourceException e) {
break;
}
}
}
// Return the file read from the VFS
return file;
}
/**
* Inits a user and updates the given CmsObject withs this users information.<p>
*
* @param cms The CmsObject to update
* @param cmsReq The current I_CmsRequest (usually initialized form the HttpServletRequest)
* @param cmsRes The current I_CmsResponse (usually initialized form the HttpServletResponse)
* @param user The name of the user to init
* @param group The name of the current group
* @param project The id of the current project
* @param sessionStorage The session storage for this OpenCms instance
*/
public void initUser(CmsObject cms, I_CmsRequest cmsReq, I_CmsResponse cmsRes, String user,
String group, int project, CmsCoreSession sessionStorage) throws CmsException {
if((!m_enableElementCache) || (project == C_PROJECT_ONLINE_ID)){
cms.init(m_resourceBroker, cmsReq, cmsRes, user, group, project, m_streaming, c_elementCache, sessionStorage, m_directoryTranslator, m_fileTranslator);
}else{
cms.init(m_resourceBroker, cmsReq, cmsRes, user, group, project, m_streaming, new CmsElementCache(10,200,10), sessionStorage, m_directoryTranslator, m_fileTranslator);
}
}
/**
* Sets the mimetype of the response.<p>
*
* The mimetype is selected by the file extension of the requested document.
* If no available mimetype is found, it is set to the default
* "application/octet-stream".
*
* @param cms The current initialized CmsObject
* @param file The requested document
*/
void setResponse(CmsObject cms, CmsFile file) {
String mimetype = null;
int lastDot = file.getName().lastIndexOf(".");
// check if there was a file extension
if((lastDot > 0) && (lastDot < (file.getName().length()-1))) {
String ext = file.getName().substring(lastDot + 1, file.getName().length());
mimetype = (String)m_mt.get(ext);
// was there a mimetype fo this extension?
if(mimetype == null) {
mimetype = C_DEFAULT_MIMETYPE;
}
} else {
mimetype = C_DEFAULT_MIMETYPE;
}
mimetype = mimetype.toLowerCase();
if (mimetype.startsWith("text")
&& (mimetype.indexOf("charset") == -1)) {
mimetype += "; charset=" + cms.getRequestContext().getEncoding();
}
cms.getRequestContext().getResponse().setContentType(mimetype);
}
/**
* Selects the appropriate launcher for a given file by analyzing the
* file's launcher id and calls the initlaunch() method to initiate the
* generating of the output.
*
* @param cms CmsObject containing all document and user information
* @param file CmsFile object representing the selected file.
* @throws CmsException In case of problems acessing the resource.
*/
public void showResource(CmsObject cms, CmsFile file) throws CmsException {
int launcherId = file.getLauncherType();
String startTemplateClass = file.getLauncherClassname();
I_CmsLauncher launcher = m_launcherManager.getLauncher(launcherId);
if(launcher == null) {
String errorMessage = "Could not launch file " + file.getName() + ". Launcher for requested launcher ID "
+ launcherId + " could not be found.";
if(C_LOGGING && isLogging(C_OPENCMS_INFO)) {
log(C_OPENCMS_INFO, "[OpenCms] " + errorMessage);
}
throw new CmsException(errorMessage, CmsException.C_UNKNOWN_EXCEPTION);
}
cms.setLauncherManager(m_launcherManager);
launcher.initlaunch(cms, file, startTemplateClass, this);
}
/**
* This method loads old sessiondata from the database. It is used
* for session failover.
*
* @param oldSessionId the id of the old session.
* @return the old sessiondata.
*/
Hashtable restoreSession(String oldSessionId) throws CmsException {
// is session-failopver enabled?
if(m_sessionFailover) {
// yes
return m_resourceBroker.restoreSession(oldSessionId);
}
else {
// no - do nothing
return null;
}
}
/**
* This method stores sessiondata into the database. It is used
* for session failover.
*
* @param sessionId the id of the session.
* @param isNew determines, if the session is new or not.
* @return data the sessionData.
*/
void storeSession(String sessionId, Hashtable sessionData) throws CmsException {
// is session failover enabled?
if(m_sessionFailover) {
// yes
m_resourceBroker.storeSession(sessionId, sessionData);
}
}
/**
* Starts a schedule job with a correct instantiated CmsObject.
*
* @param entry the CmsCronEntry to start.
*/
void startScheduleJob(CmsCronEntry entry) {
// create a valid cms-object
CmsObject cms = new CmsObject();
try {
initUser(cms, null, null, entry.getUserName(), entry.getGroupName(), C_PROJECT_ONLINE_ID, null);
// create a new ScheduleJob and start it
CmsCronScheduleJob job = new CmsCronScheduleJob(cms, entry);
job.start();
} catch(Exception exc) {
if(C_LOGGING && isLogging(C_OPENCMS_CRONSCHEDULER)) {
log(C_OPENCMS_CRONSCHEDULER, "Error initialising job for " + entry + " Error: " + Utils.getStackTrace(exc));
}
}
}
/**
* Reads the actual entries from the database and updates the Crontable
*/
void updateCronTable() {
try {
m_table.update(m_resourceBroker.readCronTable(null, null));
} catch(Exception exc) {
if(C_LOGGING && isLogging(C_OPENCMS_CRITICAL)) {
log(C_OPENCMS_CRITICAL, "[OpenCms] crontable corrupt. Scheduler is now disabled!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -