From 4e83fdee6421a2dae5cbcf8d987e487702fc3202 Mon Sep 17 00:00:00 2001 From: jensp Date: Mon, 29 Aug 2011 17:18:08 +0000 Subject: [PATCH] =?UTF-8?q?-=20SiteProxy=20ben=C3=B6tigt=20keine=20extra?= =?UTF-8?q?=20JSP-Template=20mehr.=20-=20Au=C3=9Ferdem=20Kommentar=20f?= =?UTF-8?q?=C3=BCr=20ContentItem#getExtraXMLGenerators()=20um=20Beispiel?= =?UTF-8?q?=20erg=C3=A4nzt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.libreccm.org/ccm/trunk@1078 8810af33-2d31-482b-a856-94f89814c4df --- ...m.jsp => siteproxy-item.jsp.noLongerInUse} | 0 .../arsdigita/cms/contenttypes/SiteProxy.java | 12 ++ .../SiteProxyExtraXMLGenerator.java | 177 ++++++++++++++++++ .../cms/contenttypes/SiteProxyLoader.java | 5 +- .../src/com/arsdigita/cms/ContentItem.java | 18 +- 5 files changed, 209 insertions(+), 3 deletions(-) rename ccm-cms-types-siteproxy/src/WEB-INF/content-types/com/arsdigita/cms/contenttypes/{siteproxy-item.jsp => siteproxy-item.jsp.noLongerInUse} (100%) create mode 100644 ccm-cms-types-siteproxy/src/com/arsdigita/cms/contenttypes/SiteProxyExtraXMLGenerator.java diff --git a/ccm-cms-types-siteproxy/src/WEB-INF/content-types/com/arsdigita/cms/contenttypes/siteproxy-item.jsp b/ccm-cms-types-siteproxy/src/WEB-INF/content-types/com/arsdigita/cms/contenttypes/siteproxy-item.jsp.noLongerInUse similarity index 100% rename from ccm-cms-types-siteproxy/src/WEB-INF/content-types/com/arsdigita/cms/contenttypes/siteproxy-item.jsp rename to ccm-cms-types-siteproxy/src/WEB-INF/content-types/com/arsdigita/cms/contenttypes/siteproxy-item.jsp.noLongerInUse diff --git a/ccm-cms-types-siteproxy/src/com/arsdigita/cms/contenttypes/SiteProxy.java b/ccm-cms-types-siteproxy/src/com/arsdigita/cms/contenttypes/SiteProxy.java index 0117da507..6e25a6ef6 100755 --- a/ccm-cms-types-siteproxy/src/com/arsdigita/cms/contenttypes/SiteProxy.java +++ b/ccm-cms-types-siteproxy/src/com/arsdigita/cms/contenttypes/SiteProxy.java @@ -24,9 +24,11 @@ import org.apache.log4j.Logger; import com.arsdigita.cms.ContentPage; import com.arsdigita.cms.ContentType; +import com.arsdigita.cms.ExtraXMLGenerator; import com.arsdigita.domain.DataObjectNotFoundException; import com.arsdigita.persistence.DataObject; import com.arsdigita.persistence.OID; +import java.util.List; /** * This content type represents a SiteProxy. @@ -105,6 +107,7 @@ public class SiteProxy extends ContentPage { set(USED_IN_ATOZ, new Boolean(usedInAtoZ)); } + @Override protected void initialize() { super.initialize(); @@ -112,4 +115,13 @@ public class SiteProxy extends ContentPage { set(USED_IN_ATOZ, Boolean.FALSE); } } + + @Override + public List getExtraXMLGenerators() { + final List generators = super.getExtraXMLGenerators(); + + generators.add(new SiteProxyExtraXMLGenerator()); + + return generators; + } } diff --git a/ccm-cms-types-siteproxy/src/com/arsdigita/cms/contenttypes/SiteProxyExtraXMLGenerator.java b/ccm-cms-types-siteproxy/src/com/arsdigita/cms/contenttypes/SiteProxyExtraXMLGenerator.java new file mode 100644 index 000000000..df674724b --- /dev/null +++ b/ccm-cms-types-siteproxy/src/com/arsdigita/cms/contenttypes/SiteProxyExtraXMLGenerator.java @@ -0,0 +1,177 @@ +package com.arsdigita.cms.contenttypes; + +import com.arsdigita.bebop.Page; +import com.arsdigita.bebop.PageState; +import com.arsdigita.cms.CMS; +import com.arsdigita.cms.ContentItem; +import com.arsdigita.cms.ExtraXMLGenerator; +import com.arsdigita.cms.contenttypes.util.SiteProxyGlobalizationUtil; +import com.arsdigita.util.url.URLCache; +import com.arsdigita.util.url.URLData; +import com.arsdigita.util.url.URLFetcher; +import com.arsdigita.util.url.URLPool; +import com.arsdigita.xml.Document; +import com.arsdigita.xml.Element; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.util.Enumeration; +import javax.servlet.http.HttpServletRequest; +import org.apache.log4j.Logger; + +/** + * + * @author Jens Pelzetter + * @version $Id$ + */ +public class SiteProxyExtraXMLGenerator implements ExtraXMLGenerator { + + private final static Logger logger = + Logger.getLogger( + SiteProxyExtraXMLGenerator.class); + private static final String SITE_PROXY_PANEL_NAME = "cms:siteProxyPanel"; + private static final String DATA_TYPE = "dataType"; + private static final String C_DATA_DATA_TYPE = "cdata"; + private static final String XML_DATA_TYPE = "xml"; + private static final String s_cacheServiceKey = "SiteProxyPanel"; + private static final URLCache s_cache = new URLCache(1000000, 1 * 60 * 1000); + private static final URLPool s_pool = new URLPool(); + + static { + logger.debug("Static initalizer starting..."); + URLFetcher.registerService(s_cacheServiceKey, s_pool, s_cache); + logger.debug("Static initalizer finished."); + } + + public void generateXML(final ContentItem item, + final Element element, + final PageState state) { + if (!(item instanceof SiteProxy)) { + throw new IllegalArgumentException( + "Only SiteProxy items are supported"); + } + + final SiteProxy siteProxy = (SiteProxy) item; + final String url = passParameters(state.getRequest(), + siteProxy.getURL()); + final Element child = element.newChildElement(SITE_PROXY_PANEL_NAME, + CMS.CMS_XML_NS); + final URLData data = getRemoteXML(child, url); + + if (data == null) { + final String[] urlArray = {url}; + final Element error = child.newChildElement("siteProxyError"); + error.setText((String) SiteProxyGlobalizationUtil.globalize( + "cms.contenttypes.siteproxy.error_fetching_url", + urlArray). + localize()); + } else if (data.getException() != null) { + final String[] urlArray = {url, + data.getException().getClass().getName(), + data.getException().getMessage()}; + final Element error = child.newChildElement("siteProxyError"); + error.setText((String) SiteProxyGlobalizationUtil.globalize( + "cms.contenttypes.siteproxy.exception_fetching_url", + urlArray). + localize()); + } else if (data.getContent().length == 0) { + final String[] urlArray = {url}; + final Element error = child.newChildElement("siteProxyError"); + error.setText((String) SiteProxyGlobalizationUtil.globalize( + "cms.contenttypes.siteproxy.empty_page_returned", + urlArray). + localize()); + } + } + + public void addGlobalStateParams(final Page page) { + } + + private String passParameters(final HttpServletRequest request, + final String url) { + StringBuilder sb = new StringBuilder(url); + String enc = request.getCharacterEncoding(); + if (enc == null) { + enc = "UTF-8"; + } + Enumeration en = request.getParameterNames(); + while (en.hasMoreElements()) { + String paramName = (String) en.nextElement(); + String[] paramValues = request.getParameterValues(paramName); + for (int i = 0; i < paramValues.length; ++i) { + if (sb.indexOf("?") < 0) { + sb.append("?"); + } else { + sb.append("&"); + } + sb.append(paramName).append("="); + try { + sb.append(URLEncoder.encode(paramValues[i], enc)); + } catch (UnsupportedEncodingException ex) { + logger.error("Unable to encode SiteProxy request", ex); + return url; + } + } + } + return sb.toString(); + } + + /** + * Retrieve remote XML for SiteProxy item. + * + * @param child com.arsdigita.xml.Element where remote XML is placed + * @param url remote XML URL (text/xml) + * @return + */ + public URLData getRemoteXML(Element child, String url) { + URLData data = URLFetcher.fetchURLData(url, s_cacheServiceKey); + if (data == null || data.getException() != null || data.getContent().length + == 0) { + return data; + } + + String contentType = data.getContentType(); + + boolean success = false; + if (contentType != null && contentType.toLowerCase().indexOf("/xml") + > -1) { + // we use the /xml intead of text/xml because + // things like application/xml are also valid + Document document = null; + try { + document = new Document(data.getContent()); + success = true; + } catch (Exception ex) { + logger.info("The document is not proper XML, trying to " + + "add the property xml headers to the file " + + "retrieved from " + url, ex); + try { + //String xmlString = data.getContentAsString(); + final byte[] xml = data.getContent(); + final String xmlString = String.format( + " \n%s", + new String(xml, + Charset.forName("UTF-8"))); + document = new Document(xmlString); + success = true; + logger.info("Adding the headers to " + url + + " allowed it to be properly parsed."); + } catch (Exception exception) { + logger.info("The document found at " + url + + " is not correctly formed XML", exception); + } + } + if (success) { + child.addContent(document.getRootElement()); + child.addAttribute(DATA_TYPE, XML_DATA_TYPE); + } + } + if (!success) { + // just add the item as CDATA + child.setCDATASection(new String(data.getContent(), + Charset.forName("UTF-8"))); + child.addAttribute(DATA_TYPE, C_DATA_DATA_TYPE); + } + return data; + } +} diff --git a/ccm-cms-types-siteproxy/src/com/arsdigita/cms/contenttypes/SiteProxyLoader.java b/ccm-cms-types-siteproxy/src/com/arsdigita/cms/contenttypes/SiteProxyLoader.java index 0bd145c29..2b40a0404 100755 --- a/ccm-cms-types-siteproxy/src/com/arsdigita/cms/contenttypes/SiteProxyLoader.java +++ b/ccm-cms-types-siteproxy/src/com/arsdigita/cms/contenttypes/SiteProxyLoader.java @@ -62,7 +62,8 @@ public class SiteProxyLoader extends AbstractContentTypeLoader { return TYPES; } - @Override + //A special template is not longer necessary. + /*@Override protected void prepareSection(final ContentSection section, final ContentType type, final LifecycleDefinition ld, @@ -74,5 +75,5 @@ public class SiteProxyLoader extends AbstractContentTypeLoader { (InputStream)get(m_template), section, type,ld, wf); - } + }*/ } diff --git a/ccm-cms/src/com/arsdigita/cms/ContentItem.java b/ccm-cms/src/com/arsdigita/cms/ContentItem.java index d7c098482..aaf1a50aa 100755 --- a/ccm-cms/src/com/arsdigita/cms/ContentItem.java +++ b/ccm-cms/src/com/arsdigita/cms/ContentItem.java @@ -2171,8 +2171,24 @@ public class ContentItem extends VersionedACSObject implements CustomCopy { } /** + *

* Override this to explicit that your content items - * have extra XML to generate. + * have extra XML to generate. An overriding implementation should call + * the super method, and append its generators to the list. Example: + *

+ *
+     * {@code
+     * @Override
+     * public List getExtraXMLGenerators() {
+     *   final List generators = 
+     *     super.getExtraXMLGenerators();
+     *   
+     *   generators.add(new YourExtraXMLGenerator());
+     * 
+     *   return generators;
+     * }
+     * }
+     * 
* @return A list of all extra XML Generators for this content item. */ public List getExtraXMLGenerators() {