1 31 32 package com.opencms.defaults; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsResourceFilter; 37 import org.opencms.file.types.CmsResourceTypePointer; 38 import org.opencms.mail.CmsHtmlMail; 39 import org.opencms.mail.CmsMailTransport; 40 import org.opencms.mail.CmsSimpleMail; 41 import org.opencms.main.CmsException; 42 import org.opencms.main.CmsLog; 43 import org.opencms.main.OpenCms; 44 import org.opencms.scheduler.I_CmsScheduledJob; 45 import org.opencms.validation.CmsPointerLinkValidator; 46 47 import com.opencms.legacy.CmsLegacyException; 48 import com.opencms.legacy.CmsRegistry; 49 import com.opencms.template.CmsXmlTemplate; 50 import com.opencms.template.CmsXmlTemplateFile; 51 52 import java.io.ByteArrayInputStream ; 53 import java.io.File ; 54 import java.io.FileInputStream ; 55 import java.io.FileOutputStream ; 56 import java.io.FileWriter ; 57 import java.io.IOException ; 58 import java.io.ObjectInputStream ; 59 import java.io.ObjectOutputStream ; 60 import java.io.Serializable ; 61 import java.net.HttpURLConnection ; 62 import java.net.MalformedURLException ; 63 import java.net.URL ; 64 import java.util.ArrayList ; 65 import java.util.Calendar ; 66 import java.util.Enumeration ; 67 import java.util.GregorianCalendar ; 68 import java.util.Hashtable ; 69 import java.util.List ; 70 import java.util.Map ; 71 import java.util.StringTokenizer ; 72 import java.util.Vector ; 73 74 import javax.mail.MessagingException ; 75 import javax.mail.internet.AddressException ; 76 import javax.mail.internet.InternetAddress ; 77 78 83 public class CmsLinkCheck extends CmsXmlTemplate implements I_CmsScheduledJob { 84 85 86 static final String LINKTABLE_FILENAME = OpenCms.getSystemInfo().getPackagesRfsPath() + "linkcheck"; 87 88 public static final String C_LINKCHECKTABLE_DATE = "linkcheckdate"; 89 90 93 public CmsLinkCheck() { 94 } 96 97 103 public String checkUrlGetMessage(String url) { 104 try { 105 URL checkedUrl = new URL (url); 106 HttpURLConnection con = (HttpURLConnection )checkedUrl.openConnection(); 107 if (con.getResponseCode() == 200) { 108 return ""; 109 } else { 110 return con.getResponseMessage(); 111 } 112 } catch (MalformedURLException mue) { 113 return "MalformedURLException: " + mue.getMessage(); 114 } catch (IOException ioe) { 115 return "IOException: " + ioe.getMessage(); 116 } 117 } 118 119 131 private void generateEmail( 132 String mailFrom, 133 List mailTo, 134 List mailCc, 135 List mailBcc, 136 String mailSubject, 137 String mailContent, 138 String mailType 139 ) throws CmsException { 140 if (mailType.toLowerCase().indexOf("html") != -1) { 142 CmsHtmlMail mail = new CmsHtmlMail(); 143 try { 144 mail.setFrom(mailFrom); 145 } catch (MessagingException e) { 146 throw new CmsLegacyException("[" + this.getClass().getName() + "] " + "Error in sending email, invalid recipient email address.", CmsLegacyException.C_BAD_NAME); 147 } 148 mail.setSubject(mailSubject); 149 mail.setHtmlMsg(mailContent); 150 mail.setTextMsg(mailContent); 151 if (mailTo != null) { 152 mail.setTo(mailTo); 153 } 154 if (mailCc != null) { 155 mail.setCc(mailCc); 156 } 157 if (mailBcc != null) { 158 mail.setCc(mailBcc); 159 } 160 new CmsMailTransport(mail).send(); 161 } else { 162 CmsSimpleMail mail = new CmsSimpleMail(); 163 try { 164 mail.setFrom(mailFrom); 165 } catch (MessagingException e) { 166 throw new CmsLegacyException("[" + this.getClass().getName() + "] " + "Error in sending email, invalid recipient email address.", CmsLegacyException.C_BAD_NAME); 167 } 168 mail.setSubject(mailSubject); 169 mail.setMsg(mailContent); 170 if (mailTo != null) { 171 mail.setTo(mailTo); 172 } 173 if (mailCc != null) { 174 mail.setCc(mailCc); 175 } 176 if (mailBcc != null) { 177 mail.setCc(mailBcc); 178 } 179 new CmsMailTransport(mail).send(); 180 } 181 } 182 183 191 private void generateFile(String content, String pathname, GregorianCalendar actDate) throws CmsException { 192 StringBuffer filename = new StringBuffer ("check_"); 193 String year = actDate.get(Calendar.YEAR) + ""; 194 String month = (actDate.get(Calendar.MONTH) + 1) + ""; 195 if (month.length() == 1) { 196 month = "0" + month; 197 } 198 String day = (actDate.get(Calendar.DATE) + ""); 199 if (day.length() == 1) { 200 day = "0" + day; 201 } 202 String hour = actDate.get(Calendar.HOUR_OF_DAY) + ""; 203 if (hour.length() == 1) { 204 hour = "0" + hour; 205 } 206 String minute = actDate.get(Calendar.MINUTE) + ""; 207 if (minute.length() == 1) { 208 minute = "0" + minute; 209 } 210 filename.append(year.substring(2)); 211 filename.append(month); 212 filename.append(day); 213 filename.append("_" + hour + "-"); 214 filename.append(minute); 215 filename.append(".html"); 216 try { 218 File outputFile = new File(pathname, filename.toString()); 219 FileWriter writer = new FileWriter (outputFile); 220 writer.write(content); 221 writer.close(); 222 } catch (IOException e) { 223 throw new CmsLegacyException("Cannot write output file.", e); 224 } 225 } 226 227 233 private String getDateString(GregorianCalendar actDate) { 234 String month = (actDate.get(Calendar.MONTH) + 1) + ""; 235 if (month.length() == 1) { 236 month = "0" + month; 237 } 238 String day = (actDate.get(Calendar.DATE) + ""); 239 if (day.length() == 1) { 240 day = "0" + day; 241 } 242 String hour = actDate.get(Calendar.HOUR_OF_DAY) + ""; 243 if (hour.length() == 1) { 244 hour = "0" + hour; 245 } 246 String minute = actDate.get(Calendar.MINUTE) + ""; 247 if (minute.length() == 1) { 248 minute = "0" + minute; 249 } 250 return (day + "." + month + "." + actDate.get(Calendar.YEAR) + " " + hour + ":" + minute); 251 } 252 253 261 private List getReceiverList(String receivers) { 262 List retArray = new ArrayList (); 263 if (receivers != null) { 264 if (!"".equals(receivers.trim())) { 265 StringTokenizer tokens = new StringTokenizer (receivers, ";"); 266 retArray = new ArrayList (); 267 while (tokens.hasMoreElements()) { 268 try { 269 retArray.add(new InternetAddress ((String )tokens.nextElement())); 270 } catch (AddressException e) { 271 if (CmsLog.getLog(this).isErrorEnabled()) { 272 CmsLog.getLog(this).error(e); 273 } 274 } 275 } 276 } 277 } 278 return retArray; 279 } 280 281 289 public String launch(CmsObject cms, Map parameters) throws CmsException { 290 String parameter = (String )parameters.get((String )parameters.keySet().iterator().next()); 291 if (parameter == null) { 292 parameter = ""; 293 } 294 linksUrlCheck(cms, parameter); 295 return "CmsLinkCheck.launch(): Links checked."; 296 } 297 298 305 public void linksUrlCheck(CmsObject cms, String parameter) throws CmsException { 306 Hashtable notAvailable = new Hashtable (); 309 310 Vector linkList = new Vector (); 312 313 Hashtable ownerLinkList = new Hashtable (); 316 317 Hashtable linkckecktable = CmsLinkCheck.readLinkCheckTable(); 319 320 Hashtable newLinkchecktable = new Hashtable (); 321 int warning = 0; 324 Hashtable emailValues = CmsRegistry.getInstance().getSystemValues("checklink"); 325 CmsXmlTemplateFile template = getOwnTemplateFile(cms, (String )emailValues.get("mailtemplate"), "", null, ""); 329 330 GregorianCalendar actDate = new GregorianCalendar (); 332 String actDateString = getDateString(actDate); 333 template.setData("actdate", actDateString); 334 newLinkchecktable.put(CmsLinkCheck.C_LINKCHECKTABLE_DATE, actDateString); 335 336 StringBuffer mailContent = new StringBuffer (template.getProcessedDataValue("single_message")); 337 338 linkList = new Vector (cms.readResources("/", CmsResourceFilter.ONLY_VISIBLE_NO_DELETED 340 .addRequireType(CmsResourceTypePointer.getStaticTypeId()))); 341 for (int i = 0; i < linkList.size(); i++) { 342 CmsFile linkElement = (CmsFile)linkList.elementAt(i); 343 String linkName = cms.getSitePath(linkElement); 344 String linkUrl = new String (linkElement.getContents()); 345 if (!linkUrl.startsWith("/")) { 347 int failedCheck = 0; 349 String numFromTable = (String )linkckecktable.get(linkName + ", " + linkUrl); 350 if ((numFromTable != null) && (!"".equals(numFromTable.trim()))) { 351 failedCheck = Integer.parseInt(numFromTable); 352 } 353 354 if (!CmsPointerLinkValidator.checkUrl(cms, linkUrl)) { 357 Vector inList = null; 359 inList = (Vector )notAvailable.get(linkUrl); 360 if (inList == null) { 361 inList = new Vector (); 362 } 363 inList.addElement(linkName); 364 notAvailable.put(linkUrl, inList); 365 366 if ((parameter != null) && ("owneremail".equals(parameter.trim()))) { 368 String ownerEmail = null; 371 if ((ownerEmail == null) || ("".equals(ownerEmail.trim()))) { 372 ownerEmail = (String )emailValues.get("mailto"); 373 } 374 Hashtable ownerLinks = null; 375 ownerLinks = (Hashtable )ownerLinkList.get(ownerEmail); 376 if (ownerLinks == null) { 377 ownerLinks = new Hashtable (); 378 } 379 ownerLinks.put(linkName, linkUrl); 380 ownerLinkList.put(ownerEmail, ownerLinks); 381 } 382 383 newLinkchecktable.put(linkName + ", " + linkUrl, "" + (failedCheck + 1)); 385 } 386 } 387 } 388 CmsLinkCheck.writeLinkCheckTable(newLinkchecktable); 390 391 if ((parameter != null) && (!"".equals(parameter.trim()))) { 393 if ("owneremail".equals(parameter.trim())) { 395 if (ownerLinkList.size() > 0) { 397 Enumeration ownerKeys = ownerLinkList.keys(); 398 while (ownerKeys.hasMoreElements()) { 399 StringBuffer ownerContent = new StringBuffer (); 400 ownerContent.append(mailContent.toString()); 401 String mailTo = (String )ownerKeys.nextElement(); 402 Hashtable linknames = (Hashtable )ownerLinkList.get(mailTo); 403 Enumeration linkKeys = linknames.keys(); 405 String singleLink = ""; 406 while (linkKeys.hasMoreElements()) { 407 singleLink = (String )linkKeys.nextElement(); 409 template.setData("ownerlinkname", singleLink); 410 template.setData("ownerlinkurl", (String )linknames.get(singleLink)); 411 ownerContent.append(template.getProcessedDataValue("ownermail_link")); 412 } 413 String mailSubject = template.getProcessedDataValue("emailsubject"); 415 String mailFrom = (String )emailValues.get("mailfrom"); 416 List mailCc = getReceiverList(template.getDataValue("emailcc")); 417 List mailBcc = getReceiverList(template.getDataValue("emailbcc")); 418 String mailType = template.getDataValue("emailtype"); 419 generateEmail(mailFrom, getReceiverList(mailTo), mailCc, mailBcc, mailSubject, ownerContent.toString(), mailType); 420 } 421 } 422 } else { 423 if (notAvailable.size() > 0) { 426 Enumeration linkKeys = notAvailable.keys(); 427 StringBuffer mailUrls = new StringBuffer (); 428 while (linkKeys.hasMoreElements()) { 429 String url = (String )linkKeys.nextElement(); 430 template.setData("url", url); 431 Vector linknames = (Vector )notAvailable.get(url); 432 StringBuffer mailLinks = new StringBuffer (); 433 for (int j = 0; j < linknames.size(); j++) { 434 String nextLink = (String )linknames.elementAt(j); 435 template.setData("linkname", nextLink); 436 mailLinks.append(template.getProcessedDataValue("single_link")); 437 } 438 template.setData("links", mailLinks.toString()); 439 mailUrls.append(template.getProcessedDataValue("single_url")); 440 } 441 mailContent.append(mailUrls.toString()); 442 if ("email".equals(parameter.trim())) { 443 String mailSubject = template.getProcessedDataValue("emailsubject"); 445 String mailFrom = (String )emailValues.get("mailfrom"); 446 List mailTo = getReceiverList((String )emailValues.get("mailto")); 447 List mailCc = getReceiverList(template.getDataValue("emailcc")); 448 List mailBcc = getReceiverList(template.getDataValue("emailbcc")); 449 String mailType = template.getDataValue("emailtype"); 450 generateEmail(mailFrom, mailTo, mailCc, mailBcc, mailSubject, mailContent.toString(), mailType); 451 } else { 452 generateFile(mailContent.toString(), parameter, actDate); 453 } 454 } 455 } 456 } 457 } 458 459 465 public static void writeLinkCheckTable(Hashtable newLinkchecktable) throws CmsException { 466 FileOutputStream fOut = null; 467 try { 468 File tableInRfs = new File(LINKTABLE_FILENAME); 469 if (tableInRfs.exists()) { 470 tableInRfs.delete(); 471 tableInRfs.createNewFile(); 472 } 473 fOut = new FileOutputStream (tableInRfs); 475 ObjectOutputStream oout = new ObjectOutputStream (fOut); 476 oout.writeObject(newLinkchecktable); 477 oout.close(); 478 oout.flush(); 479 } catch (IOException e) { 480 throw new CmsLegacyException("Error writing serialized hashtable. "+e); 481 } finally { 482 try { 483 if (fOut != null) { 484 fOut.close(); 485 } 486 } catch (IOException e) { 487 } 489 } 490 } 491 492 499 public static Hashtable readLinkCheckTable() throws CmsException { 500 Hashtable result = new Hashtable (); 501 FileInputStream fIn = null; 502 byte[] buffer; 503 int charsRead; 504 int size; 505 try { 506 File tableInRfs = new File(LINKTABLE_FILENAME); 507 if (tableInRfs.exists()) { 508 fIn = new FileInputStream (tableInRfs); 509 charsRead = 0; 510 size = new Long (tableInRfs.length()).intValue(); 511 buffer = new byte[size]; 512 while (charsRead < size) { 513 charsRead += fIn.read(buffer, charsRead, size - charsRead); 514 } 515 516 ByteArrayInputStream bin = new ByteArrayInputStream (buffer); 518 ObjectInputStream oin = new ObjectInputStream (bin); 519 Serializable s = (Serializable ) oin.readObject(); 520 result = (Hashtable ) s; 521 } 522 } catch (Exception e) { 523 throw new CmsLegacyException("Error writing serialized hashtable. "+e); 524 } finally { 525 try { 526 if (fIn != null) { 527 fIn.close(); 528 } 529 } catch (IOException e) { 530 } 532 } 533 return result; 534 } 535 } | Popular Tags |