📄 cmsnewresourceexternallink.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsNewResourceExternallink.java,v $
* Date : $Date: 2005/06/27 23:22:07 $
* Version: $Revision: 1.5 $
*
* 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.workplace;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsFolder;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.types.CmsResourceTypePointer;
import org.opencms.i18n.CmsEncoder;
import org.opencms.main.CmsException;
import org.opencms.workplace.CmsWorkplaceSettings;
import com.opencms.core.I_CmsSession;
import com.opencms.legacy.CmsLegacyException;
import com.opencms.legacy.CmsXmlTemplateLoader;
import com.opencms.template.A_CmsXmlContent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* Template class for displaying the new resource screen for a new link
* of the OpenCms workplace.<P>
* Reads template files of the content type <code>CmsXmlWpTemplateFile</code>.
*
* @author Michael Emmerich
* @version $Revision: 1.5 $ $Date: 2005/06/27 23:22:07 $
*/
public class CmsNewResourceExternallink extends CmsWorkplaceDefault {
private static final String C_PARA_KEEP_PROPERTIES = "keepproperties";
private static final String C_PARA_ADD_TO_NAV = "addtonav";
private static final int DEBUG = 0;
/**
* Overwrites the getContent method of the CmsWorkplaceDefault.<br>
* Gets the content of the new resource othertype template and processed the data input.
* @param cms The CmsObject.
* @param templateFile The lock template file
* @param elementName not used
* @param parameters Parameters of the request and the template.
* @param templateSelector Selector of the template tag to be displayed.
* @return Bytearry containing the processed data of the template.
* @throws Throws CmsException if something goes wrong.
*/
public byte[] getContent(CmsObject cms, String templateFile, String elementName,
Hashtable parameters, String templateSelector) throws CmsException {
String error = "";
boolean checkurl = true;
String filename = null;
String targetName = null;
String foldername = null;
// String type = null;
I_CmsSession session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
// get the document to display
CmsXmlWpTemplateFile xmlTemplateDocument = new CmsXmlWpTemplateFile(cms, templateFile);
CmsXmlLanguageFile lang = xmlTemplateDocument.getLanguageFile();
// clear session values on first load
String initial = (String)parameters.get(CmsWorkplaceDefault.C_PARA_INITIAL);
if(initial != null){
// remove all session values
session.removeValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
session.removeValue(CmsWorkplaceDefault.C_PARA_LINK);
session.removeValue(CmsWorkplaceDefault.C_PARA_VIEWFILE);
session.removeValue(CmsWorkplaceDefault.C_PARA_NAVPOS);
session.removeValue(CmsWorkplaceDefault.C_PARA_NAVTEXT);
session.removeValue("lasturl");
}
// get the lasturl from parameters or from session
String lastUrl = getLastUrl(cms, parameters);
if(lastUrl != null){
session.putValue("lasturl", lastUrl);
}
// get the linkname and the linkurl
filename = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getParameter(CmsWorkplaceDefault.C_PARA_RESOURCE);
if(filename != null) {
session.putValue(CmsWorkplaceDefault.C_PARA_RESOURCE, filename);
} else {
// try to get the value from the session, e.g. after an error
filename = (String)session.getValue(CmsWorkplaceDefault.C_PARA_RESOURCE)!=null?(String)session.getValue(CmsWorkplaceDefault.C_PARA_RESOURCE):"";
}
targetName = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getParameter(CmsWorkplaceDefault.C_PARA_LINK);
if(targetName != null) {
session.putValue(CmsWorkplaceDefault.C_PARA_LINK, targetName);
} else {
// try to get the value from the session, e.g. after an error
targetName = (String)session.getValue(CmsWorkplaceDefault.C_PARA_LINK)!=null?(String)session.getValue(CmsWorkplaceDefault.C_PARA_LINK):"";
}
// get the parameters
String navtitle = (String)parameters.get(CmsWorkplaceDefault.C_PARA_NAVTEXT);
if(navtitle != null) {
session.putValue(CmsWorkplaceDefault.C_PARA_NAVTEXT, navtitle);
} else {
// try to get the value from the session, e.g. after an error
navtitle = (String)session.getValue(CmsWorkplaceDefault.C_PARA_NAVTEXT)!=null?(String)session.getValue(CmsWorkplaceDefault.C_PARA_NAVTEXT):"";
}
String navpos = (String)parameters.get(CmsWorkplaceDefault.C_PARA_NAVPOS);
if(navpos != null) {
session.putValue(CmsWorkplaceDefault.C_PARA_NAVPOS, navpos);
} else {
// try to get the value from the session, e.g. after an error
navpos = (String)session.getValue(CmsWorkplaceDefault.C_PARA_NAVPOS)!=null?(String)session.getValue(CmsWorkplaceDefault.C_PARA_NAVPOS):"";
}
String dummy = (String)parameters.get(CmsNewResourceExternallink.C_PARA_KEEP_PROPERTIES);
if (DEBUG>0) System.out.println( "parameter " + CmsNewResourceExternallink.C_PARA_KEEP_PROPERTIES + ": " + dummy );
boolean keepTargetProperties = false;
if (dummy!=null) {
session.putValue(CmsNewResourceExternallink.C_PARA_KEEP_PROPERTIES, dummy);
}
else {
dummy = (String)session.getValue(CmsNewResourceExternallink.C_PARA_KEEP_PROPERTIES)!=null?(String)session.getValue(CmsNewResourceExternallink.C_PARA_KEEP_PROPERTIES):"true";
}
keepTargetProperties = dummy.trim().equalsIgnoreCase("true");
dummy = (String)parameters.get(CmsNewResourceExternallink.C_PARA_ADD_TO_NAV);
if (DEBUG>0) System.out.println( "parameter " + CmsNewResourceExternallink.C_PARA_ADD_TO_NAV + ": " + dummy );
boolean addToNav = false;
if (dummy!=null) {
session.putValue(CmsNewResourceExternallink.C_PARA_ADD_TO_NAV, dummy);
}
else {
dummy = (String)session.getValue(CmsNewResourceExternallink.C_PARA_ADD_TO_NAV)!=null?(String)session.getValue(CmsNewResourceExternallink.C_PARA_ADD_TO_NAV):"false";
}
addToNav = dummy.trim().equalsIgnoreCase("true");
String notChange = (String)parameters.get("newlink");
CmsResource linkResource = null;
String step = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getParameter("step");
// set the values e.g. after an error
xmlTemplateDocument.setData("LINKNAME", filename);
xmlTemplateDocument.setData("LINKVALUE", targetName);
xmlTemplateDocument.setData("NAVTITLE", CmsEncoder.escapeHtml(navtitle));
xmlTemplateDocument.setData("KEEPPROPERTIES", keepTargetProperties==true ? "true" : "false" );
xmlTemplateDocument.setData("ADDTONAV", addToNav==true ? "true" : "false" );
// if an existing link should be edited show the change page
if(notChange != null && notChange.equals("false") && step == null) {
try{
CmsFile currentFile = cms.readFile(filename);
String content = new String(currentFile.getContents());
xmlTemplateDocument.setData("LINKNAME", currentFile.getName());
xmlTemplateDocument.setData("LINK", cms.getSitePath(currentFile));
xmlTemplateDocument.setData("LINKVALUE", content);
templateSelector = "change";
} catch (CmsException e){
error = e.getMessage();
}
}
// get the current phase of this wizard
if(step != null) {
// step 1 - show the final selection screen
if(step.equals("1") || step.equals("2")) {
try{
// step 1 - create the link with checking http-link
// step 2 - create the link without link check
// get folder- and filename
// foldername = (String)session.getValue(C_PARA_FILELIST);
foldername = CmsWorkplaceAction.getCurrentFolder(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest());
if(foldername == null) {
foldername = cms.getSitePath(cms.readFolder("/"));
}
String title = lang.getLanguageValue("explorer.linkto") + " " + targetName;
// type = "link";
if(notChange != null && notChange.equals("false")) {
// change old file
CmsFile editFile = cms.readFile(filename);
editFile.setContents(targetName.getBytes());
if(step.equals("1")){
// TODO: write some better link check which will not fail if there is
// no access to the internet
//if(!targetName.startsWith("/")){
// checkurl = CmsLinkCheck.checkUrl(targetName);
//}
}
checkurl=true;
if(checkurl){
cms.writeFile(editFile);
cms.writeProperty(filename, CmsPropertyDefinition.PROPERTY_TITLE, title);
}
linkResource = editFile;
} else {
// link URL is a file, so create the new file
List properties = null;
if (title != null) {
properties = new ArrayList();
properties.add(new org.opencms.file.CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, title, null));
} else {
properties = Collections.EMPTY_LIST;
}
checkurl=true;
if (checkurl ) {
// TODO VFS links: creates an external HTTP link following the new linking paradigm
//linkResource = cms.createVfsLink(foldername + filename, targetName, targetProperties);
linkResource = cms.createResource(foldername + filename, CmsResourceTypePointer.getStaticTypeId());
}
}
// now check if navigation informations have to be added to the new page.
if(addToNav && checkurl) {
cms.writeProperty(cms.getSitePath(linkResource), CmsPropertyDefinition.PROPERTY_NAVTEXT, navtitle);
// update the navposition.
if(navpos != null) {
updateNavPos(cms, linkResource, navpos);
}
}
// remove values from session
session.removeValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
session.removeValue(CmsWorkplaceDefault.C_PARA_VIEWFILE);
session.removeValue(CmsWorkplaceDefault.C_PARA_LINK);
session.removeValue(CmsWorkplaceDefault.C_PARA_NAVTEXT);
session.removeValue(CmsWorkplaceDefault.C_PARA_NAVPOS);
// now return to appropriate filelist
} catch (CmsException e){
error = e.getMessage();
}
if(checkurl && ("".equals(error.trim()))){
try {
if(lastUrl != null) {
CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendRedirect(lastUrl);
} else {
CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendCmsRedirect(getConfigFile(cms).getWorkplaceActionPath()
+ CmsWorkplaceAction.getExplorerFileUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()));
}
} catch(Exception e) {
throw new CmsLegacyException("Redirect fails :" + getConfigFile(cms).getWorkplaceActionPath()
+ CmsWorkplaceAction.getExplorerFileUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()), e);
}
return null;
}
}
} else {
session.removeValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
session.removeValue(CmsWorkplaceDefault.C_PARA_VIEWFILE);
session.removeValue(CmsWorkplaceDefault.C_PARA_LINK);
session.removeValue(CmsWorkplaceDefault.C_PARA_NAVTEXT);
}
// set lasturl
if(lastUrl == null) {
lastUrl = CmsWorkplaceAction.getExplorerFileUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest());
}
xmlTemplateDocument.setData("lasturl", lastUrl);
// set the templateselector if there was an error
if(!checkurl){
xmlTemplateDocument.setData("folder", foldername);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -