📄 cmslinkcheck.java
字号:
retArray.add(new InternetAddress((String)tokens.nextElement()));
} catch (AddressException e) {
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error(e);
}
}
}
}
}
return retArray;
}
/**
* This method is called by the cron scheduler.<p>
*
* @param cms a OpenCms context object
* @param parameters link check parameters
* @return the String that is written to the OpenCms log
* @throws CmsException if something goes wrong
*/
public String launch(CmsObject cms, Map parameters) throws CmsException {
String parameter = (String)parameters.get((String)parameters.keySet().iterator().next());
if (parameter == null) {
parameter = "";
}
linksUrlCheck(cms, parameter);
return "CmsLinkCheck.launch(): Links checked.";
}
/**
* Checks all existing external links.<p>
*
* @param cms a OpenCms context object
* @param parameter link check parameters
* @throws CmsException if something goes wrong
*/
public void linksUrlCheck(CmsObject cms, String parameter) throws CmsException {
// hashtable that contains the urls that are not available and the resourcenames of the links
// where this url is referenced
Hashtable notAvailable = new Hashtable();
// vector that contains all external links from the database
Vector linkList = new Vector();
// vector and hashtable that contains the links of an owner that are not available,
// so there can be created a mail to the owner with all the broken links
Hashtable ownerLinkList = new Hashtable();
// get the hashtable with the last check result from the system table
Hashtable linkckecktable = CmsLinkCheck.readLinkCheckTable();
Hashtable newLinkchecktable = new Hashtable();
// get the values for email from the registry
// TODO: check REGISTRY "checklink"
int warning = 0;
Hashtable emailValues = CmsRegistry.getInstance().getSystemValues("checklink");
// Hashtable emailValues = new Hashtable();
// get templateFile this way because there is no actual file if
// method is called from scheduler ...
CmsXmlTemplateFile template = getOwnTemplateFile(cms, (String)emailValues.get("mailtemplate"), "", null, "");
// set the current date and time
GregorianCalendar actDate = new GregorianCalendar();
String actDateString = getDateString(actDate);
template.setData("actdate", actDateString);
newLinkchecktable.put(CmsLinkCheck.C_LINKCHECKTABLE_DATE, actDateString);
StringBuffer mailContent = new StringBuffer(template.getProcessedDataValue("single_message"));
// get all links from the database
linkList = new Vector(cms.readResources("/", CmsResourceFilter.ONLY_VISIBLE_NO_DELETED
.addRequireType(CmsResourceTypePointer.getStaticTypeId())));
for (int i = 0; i < linkList.size(); i++) {
CmsFile linkElement = (CmsFile)linkList.elementAt(i);
String linkName = cms.getSitePath(linkElement);
String linkUrl = new String(linkElement.getContents());
// do not check internal links
if (!linkUrl.startsWith("/")) {
// get the number of failed checks for the link
int failedCheck = 0;
String numFromTable = (String)linkckecktable.get(linkName + ", " + linkUrl);
if ((numFromTable != null) && (!"".equals(numFromTable.trim()))) {
failedCheck = Integer.parseInt(numFromTable);
}
// check the url,
// if the url is not readable add it to the list of not available urls
if (!CmsPointerLinkValidator.checkUrl(cms, linkUrl)) {
// get the vector of resourcenames from the hashtable of urls
Vector inList = null;
inList = (Vector)notAvailable.get(linkUrl);
if (inList == null) {
inList = new Vector();
}
inList.addElement(linkName);
notAvailable.put(linkUrl, inList);
// create the hashtable for the owner mails if requested
if ((parameter != null) && ("owneremail".equals(parameter.trim()))) {
// add the failed link to the links of the owner
// first try to get the email
String ownerEmail = null;
if ((ownerEmail == null) || ("".equals(ownerEmail.trim()))) {
ownerEmail = (String)emailValues.get("mailto");
}
Hashtable ownerLinks = null;
ownerLinks = (Hashtable)ownerLinkList.get(ownerEmail);
if (ownerLinks == null) {
ownerLinks = new Hashtable();
}
ownerLinks.put(linkName, linkUrl);
ownerLinkList.put(ownerEmail, ownerLinks);
}
// add the failed link to the new linkchecktable
newLinkchecktable.put(linkName + ", " + linkUrl, "" + (failedCheck + 1));
}
}
}
// write the linkchecktable to database
CmsLinkCheck.writeLinkCheckTable(newLinkchecktable);
// get the information for the output
if ((parameter != null) && (!"".equals(parameter.trim()))) {
// send an email to the owner of the link
if ("owneremail".equals(parameter.trim())) {
// get the owners from the owner list
if (ownerLinkList.size() > 0) {
Enumeration ownerKeys = ownerLinkList.keys();
while (ownerKeys.hasMoreElements()) {
StringBuffer ownerContent = new StringBuffer();
ownerContent.append(mailContent.toString());
String mailTo = (String)ownerKeys.nextElement();
Hashtable linknames = (Hashtable)ownerLinkList.get(mailTo);
// get all failed links of the owner
Enumeration linkKeys = linknames.keys();
String singleLink = "";
while (linkKeys.hasMoreElements()) {
// set the data for the link
singleLink = (String)linkKeys.nextElement();
template.setData("ownerlinkname", singleLink);
template.setData("ownerlinkurl", (String)linknames.get(singleLink));
ownerContent.append(template.getProcessedDataValue("ownermail_link"));
}
// get the email data
String mailSubject = template.getProcessedDataValue("emailsubject");
String mailFrom = (String)emailValues.get("mailfrom");
List mailCc = getReceiverList(template.getDataValue("emailcc"));
List mailBcc = getReceiverList(template.getDataValue("emailbcc"));
String mailType = template.getDataValue("emailtype");
generateEmail(mailFrom, getReceiverList(mailTo), mailCc, mailBcc, mailSubject, ownerContent.toString(), mailType);
}
}
} else {
// if there are not readable urls create the content of the eMail
// and send it to the specified user(s)
if (notAvailable.size() > 0) {
Enumeration linkKeys = notAvailable.keys();
StringBuffer mailUrls = new StringBuffer();
while (linkKeys.hasMoreElements()) {
String url = (String)linkKeys.nextElement();
template.setData("url", url);
Vector linknames = (Vector)notAvailable.get(url);
StringBuffer mailLinks = new StringBuffer();
for (int j = 0; j < linknames.size(); j++) {
String nextLink = (String)linknames.elementAt(j);
template.setData("linkname", nextLink);
mailLinks.append(template.getProcessedDataValue("single_link"));
}
template.setData("links", mailLinks.toString());
mailUrls.append(template.getProcessedDataValue("single_url"));
}
mailContent.append(mailUrls.toString());
if ("email".equals(parameter.trim())) {
// get the eMail information
String mailSubject = template.getProcessedDataValue("emailsubject");
String mailFrom = (String)emailValues.get("mailfrom");
List mailTo = getReceiverList((String)emailValues.get("mailto"));
List mailCc = getReceiverList(template.getDataValue("emailcc"));
List mailBcc = getReceiverList(template.getDataValue("emailbcc"));
String mailType = template.getDataValue("emailtype");
generateEmail(mailFrom, mailTo, mailCc, mailBcc, mailSubject, mailContent.toString(), mailType);
} else {
generateFile(mailContent.toString(), parameter, actDate);
}
}
}
}
}
/**
* Writes the Linkchecktable.
*
* @param newLinkchecktable The hashtable that contains the links that were not reachable
* @throws CmsException if something goes wrong
*/
public static void writeLinkCheckTable(Hashtable newLinkchecktable) throws CmsException {
FileOutputStream fOut = null;
try {
File tableInRfs = new File(LINKTABLE_FILENAME);
if (tableInRfs.exists()) {
tableInRfs.delete();
tableInRfs.createNewFile();
}
// serialize the object
fOut = new FileOutputStream(tableInRfs);
ObjectOutputStream oout = new ObjectOutputStream(fOut);
oout.writeObject(newLinkchecktable);
oout.close();
oout.flush();
} catch (IOException e) {
throw new CmsLegacyException("Error writing serialized hashtable. "+e);
} finally {
try {
if (fOut != null) {
fOut.close();
}
} catch (IOException e) {
// ignore
}
}
}
/**
* Gets the Linkchecktable.
*
* @return the linkchecktable
*
* @throws CmsException if something goes wrong
*/
public static Hashtable readLinkCheckTable() throws CmsException {
Hashtable result = new Hashtable();
FileInputStream fIn = null;
byte[] buffer;
int charsRead;
int size;
try {
File tableInRfs = new File(LINKTABLE_FILENAME);
if (tableInRfs.exists()) {
fIn = new FileInputStream(tableInRfs);
charsRead = 0;
size = new Long(tableInRfs.length()).intValue();
buffer = new byte[size];
while (charsRead < size) {
charsRead += fIn.read(buffer, charsRead, size - charsRead);
}
// now deserialize the object
ByteArrayInputStream bin = new ByteArrayInputStream(buffer);
ObjectInputStream oin = new ObjectInputStream(bin);
Serializable s = (Serializable) oin.readObject();
result = (Hashtable) s;
}
} catch (Exception e) {
throw new CmsLegacyException("Error writing serialized hashtable. "+e);
} finally {
try {
if (fIn != null) {
fIn.close();
}
} catch (IOException e) {
// ignore
}
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -