From: <ap...@vh...> - 2005-12-11 20:48:42
|
Author: apevec Date: 2005-12-11 21:46:12 +0100 (Sun, 11 Dec 2005) New Revision: 1046 Modified: trunk/ccm-cms-types-siteproxy/src/com/arsdigita/cms/dispatcher/SiteProxyPanel.java Log: expose SiteProxy remote XML document retrieval, used by AtoZSiteProxyProvider Modified: trunk/ccm-cms-types-siteproxy/src/com/arsdigita/cms/dispatcher/SiteProxyPanel.java =================================================================== --- trunk/ccm-cms-types-siteproxy/src/com/arsdigita/cms/dispatcher/SiteProxyPanel.java 2005-12-09 15:33:20 UTC (rev 1045) +++ trunk/ccm-cms-types-siteproxy/src/com/arsdigita/cms/dispatcher/SiteProxyPanel.java 2005-12-11 20:46:12 UTC (rev 1046) @@ -61,10 +61,9 @@ private static String s_cacheServiceKey = "SiteProxyPanel"; private static URLCache s_cache = new URLCache(1000000, 15*60*1000); private static URLPool s_pool = new URLPool(); - private static URLFetcher s_fetcher; static { - s_fetcher.registerService(s_cacheServiceKey, s_pool, s_cache); + URLFetcher.registerService(s_cacheServiceKey, s_pool, s_cache); }; public SiteProxyPanel() { @@ -84,6 +83,58 @@ } + /** + * Retrieve remote XML for SiteProxy item. + * + * @param child com.arsdigita.xml.Element where remote XML is placed + * @param url remote XML URL (text/xml) + */ + public static URLData internalGetRemoteXML(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) { + s_log.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(); + xmlString = "<?xml version=\"1.0\"?> \n" + xmlString; + document = new Document(xmlString); + success = true; + s_log.info("Adding the headers to " + url + + " allowed it to be properly parsed."); + } catch (Exception exception) { + s_log.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(data.getContentAsString()); + child.addAttribute(DATA_TYPE, C_DATA_DATA_TYPE); + } + return data; + } + class SiteProxyXMLGenerator extends SimpleXMLGenerator { public void generateXML(PageState state, Element parent, String useContext) { @@ -92,13 +143,15 @@ SiteProxy item = (SiteProxy)getContentItem(state); String url = passParameters(state.getRequest(), item.getURL()); - URLData data = s_fetcher.fetchURLData(url, s_cacheServiceKey); + Element child = parent.newChildElement(SITE_PROXY_PANEL_NAME, + CMS.CMS_XML_NS); + URLData data = internalGetRemoteXML(child, url); + if (data == null) { String[] urlArray = {url}; (new Label(SiteProxyGlobalizationUtil.globalize ("cms.contenttypes.siteproxy.error_fetching_url", urlArray))).generateXML(state, parent); - return; } else if (data.getException() != null) { String[] urlArray = {url, data.getException().getClass().getName(), @@ -111,49 +164,7 @@ (new Label(SiteProxyGlobalizationUtil.globalize ("cms.contenttypes.siteproxy.empty_page_returned", urlArray))).generateXML(state, parent); - return; } - - String contentType = data.getContentType(); - - boolean success = false; - Element child = parent.newChildElement(SITE_PROXY_PANEL_NAME, - CMS.CMS_XML_NS); - 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) { - s_log.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(); - xmlString = "<?xml version=\"1.0\"?> \n" + xmlString; - document = new Document(xmlString); - success = true; - s_log.info("Adding the headers to " + url + - " allowed it to be properly parsed."); - } catch (Exception exception) { - s_log.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(data.getContentAsString()); - child.addAttribute(DATA_TYPE, C_DATA_DATA_TYPE); - } } } |