KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > validation > CmsPointerLinkValidator


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/validation/CmsPointerLinkValidator.java,v $
3  * Date : $Date: 2005/08/15 10:48:46 $
4  * Version: $Revision: 1.10 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.validation;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsResource;
37 import org.opencms.file.CmsResourceFilter;
38 import org.opencms.file.types.CmsResourceTypePointer;
39 import org.opencms.main.CmsException;
40 import org.opencms.main.OpenCms;
41 import org.opencms.report.CmsLogReport;
42 import org.opencms.report.I_CmsReport;
43 import org.opencms.scheduler.I_CmsScheduledJob;
44
45 import java.io.IOException JavaDoc;
46 import java.net.HttpURLConnection JavaDoc;
47 import java.net.MalformedURLException JavaDoc;
48 import java.net.URL JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Map JavaDoc;
53
54 /**
55  * Class to validate pointer links.<p>
56  *
57  * @author Jan Baudisch
58  *
59  * @version $Revision: 1.10 $
60  *
61  * @since 6.0.0
62  */

63 public class CmsPointerLinkValidator implements I_CmsScheduledJob {
64
65     /** The report for the output. */
66     private I_CmsReport m_report;
67
68     /**
69      * Checks if the given url is valid.<p>
70      *
71      * @param url the url to check
72      * @param cms a OpenCms context object
73      *
74      * @return false if the url could not be accessed
75      */

76     public static boolean checkUrl(CmsObject cms, String JavaDoc url) {
77
78         try {
79             if (url.toLowerCase().startsWith("/")) {
80                 return cms.existsResource(cms.getRequestContext().removeSiteRoot(url));
81             } else if (url.startsWith("http")) {
82                 URL JavaDoc checkedUrl = new URL JavaDoc(url);
83                 HttpURLConnection JavaDoc httpcon = (HttpURLConnection JavaDoc)checkedUrl.openConnection();
84                 return (httpcon.getResponseCode() == 200);
85             } else {
86                 return true;
87             }
88         } catch (MalformedURLException JavaDoc mue) {
89             return false;
90         } catch (IOException JavaDoc ioe) {
91             return false;
92         }
93     }
94
95     /**
96      * This method is called by the cron scheduler.<p>
97      *
98      * @param cms a OpenCms context object
99      * @param parameters link check parameters
100      * @return the String that is written to the OpenCms log
101      * @throws CmsException if something goes wrong
102      */

103     public String JavaDoc launch(CmsObject cms, Map JavaDoc parameters) throws CmsException {
104
105         if (Boolean.valueOf((String JavaDoc)parameters.get("writeLog")).booleanValue()) {
106             m_report = new CmsLogReport(cms.getRequestContext().getLocale(), CmsPointerLinkValidator.class);
107         }
108
109         validateLinks(cms);
110         return "CmsExternLinkValidator.launch(): Links checked.";
111     }
112
113     /**
114      * Sets the report for the output.<p>
115      *
116      * @param report the report for the output
117      */

118     public void setReport(I_CmsReport report) {
119
120         m_report = report;
121     }
122
123     /**
124      * Validate all links.<p>
125      *
126      * @param cms a OpenCms context object
127      *
128      * @throws CmsException if something goes wrong
129      */

130     public void validateLinks(CmsObject cms) throws CmsException {
131
132         if (m_report == null) {
133             m_report = new CmsLogReport(cms.getRequestContext().getLocale(), CmsPointerLinkValidator.class);
134         }
135
136         m_report.println(
137             Messages.get().container(Messages.RPT_VALIDATE_EXTERNAL_LINKS_BEGIN_0),
138             I_CmsReport.FORMAT_HEADLINE);
139
140         // get all links
141
List JavaDoc links = cms.readResources(
142             "/",
143             CmsResourceFilter.ONLY_VISIBLE_NO_DELETED.addRequireType(CmsResourceTypePointer.getStaticTypeId()));
144         Iterator JavaDoc iterator = links.iterator();
145         Map JavaDoc brokenLinks = new HashMap JavaDoc();
146
147         for (int i = 1; iterator.hasNext(); i++) {
148             CmsFile link = cms.readFile(cms.getSitePath((CmsResource)iterator.next()));
149             String JavaDoc linkUrl = new String JavaDoc(link.getContents());
150
151             // print to the report
152
m_report.print(org.opencms.report.Messages.get().container(
153                 org.opencms.report.Messages.RPT_SUCCESSION_1,
154                 new Integer JavaDoc(i),
155                 new Integer JavaDoc(links.size())), I_CmsReport.FORMAT_NOTE);
156             m_report.print(Messages.get().container(Messages.RPT_VALIDATE_LINK_0), I_CmsReport.FORMAT_NOTE);
157             m_report.print(org.opencms.report.Messages.get().container(
158                 org.opencms.report.Messages.RPT_ARGUMENT_1,
159                 link.getRootPath()));
160             m_report.print(Messages.get().container(Messages.GUI_LINK_POINTING_TO_0), I_CmsReport.FORMAT_NOTE);
161             m_report.print(org.opencms.report.Messages.get().container(
162                 org.opencms.report.Messages.RPT_ARGUMENT_1,
163                 linkUrl));
164             m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
165
166             // check link and append it to the list of broken links, if test fails
167
if (!checkUrl(cms, linkUrl)) {
168                 brokenLinks.put(link.getRootPath(), linkUrl);
169                 m_report.println(Messages.get().container(Messages.RPT_BROKEN_0), I_CmsReport.FORMAT_ERROR);
170             } else {
171                 m_report.println(
172                     org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
173                     I_CmsReport.FORMAT_OK);
174             }
175         }
176
177         m_report.println(Messages.get().container(
178             Messages.RPT_LINK_VALIDATION_STAT_2,
179             new Integer JavaDoc(links.size()),
180             new Integer JavaDoc(brokenLinks.size())), I_CmsReport.FORMAT_HEADLINE);
181         m_report.println(
182             Messages.get().container(Messages.RPT_VALIDATE_EXTERNAL_LINKS_END_0),
183             I_CmsReport.FORMAT_HEADLINE);
184
185         OpenCms.getLinkManager().setPointerLinkValidationResult(new CmsPointerLinkValidationResult(brokenLinks));
186
187     }
188 }
189
Popular Tags