/****************************************************************************** * INTERWOVEN, INC. PROPRIETARY AND CONFIDENTIAL * * * Copyright 2004 Interwoven, Inc. All rights reserved. * * Use of this product is subject to license terms. If this product * is acquired by the United States Government or any contractor * therefor, then the Government's rights in this product and related * documentation, which are "commercial computer software" and * "commercial computer documentation", respectively, will be only as * set forth in such license terms as specified in: (i) for * acquisitions by or on behalf of civilian agencies, 48 C.F.R. 12.212 * of the Federal Acquisition Regulations and its successors; and (ii) * for acquisition by or on behalf of units of the Department of * Defense ("DoD"), in 48 C.F.R. 227.7202-1 through 227.7202-4 of the * DoD F.A.R. Supplement and its successors. All United States * Government end users acquire this product with only those rights * set forth in such license terms. ******************************************************************************/package com.interwoven.livesite.external.runtime;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;import java.util.Iterator;import org.dom4j.Document;import org.dom4j.Element;import com.interwoven.livesite.dom4j.Dom4jUtils;import com.interwoven.livesite.runtime.RequestContext;/***************************************************************************** * DOCUMENT ME!!! * * @author $Author: achachan $ * @version $Revision: 1.6 $ *****************************************************************************/public class Web{ /** default ctor */ public Web() { } /** Parameter for getting URL */ public static final String GETURL = "URL"; /** Parameter for target element */ public static final String TARGET_ELEMENT = "targetElement"; /** * *************************************************************************** * Fetch URL content at GETURL parameter * Requred parameter: URL = URL to fetch * Optional parameters: targetElement = element name where the content will be added * XPath: /Properties/Data/Result/Response * * @param context Request * @return Document **************************************************************************** */ public Document urlGet(RequestContext context) { Document doc = Dom4jUtils.newDocument(); Element root = doc.addElement("Response"); String urlAddress = (String)context.getParameterString(GETURL); String targetElement = (String)context.getParameterString(TARGET_ELEMENT); if (null == targetElement) { targetElement = "data"; } if (null == urlAddress) { doc.getRootElement().addElement("error").addText("Required parameter not found: "+GETURL); } else { try { URL url = new URL(urlAddress); URLConnection conn = url.openConnection(); String str; Iterator it = conn.getHeaderFields().keySet().iterator(); if (it.hasNext()) { Element eh = root.addElement("request_header"); StringBuffer sb = new StringBuffer(); while (it.hasNext()) { String name = (String)it.next(); if (null != name) { String value = conn.getHeaderField(name); eh.addElement(name).addText(value); sb.append(name); sb.append("="); sb.append(value); sb.append("\r\n"); } } eh.addElement("_raw").addCDATA(sb.toString()); } str = conn.getContentType(); if (null != str) { root.addElement("Content-Type").addText(str); } str = conn.getContentEncoding(); if (null != str) { root.addElement("Content-Encoding").addText(str); } root.addElement("Content-Length").addText(new Integer(conn.getContentLength()).toString()); if (0 == conn.getContentType().indexOf("text/xml")) { //a_XML data inlined Document readDoc = Dom4jUtils.newDocument(conn.getInputStream()); root.add(readDoc.getRootElement()); } else { //a_Read content and add as CDATA BufferedReader rd = new BufferedReader( new InputStreamReader( conn.getInputStream() ) ); String line; StringBuffer sb = new StringBuffer(); while (null != (line = rd.readLine())) { sb.append(line); sb.append("\r\n"); } rd.close(); root.addElement(targetElement).addCDATA(sb.toString()); } } catch (Exception e) { root.addElement("error").addCDATA(e.toString()); } } return doc; } /** * Download XML from a given URL in parameter GETURL (see member above) * * Requred parameter: URL = URL to fetch * Optional parameters: targetElement = element name where the content will be added * * @param context request * @return Document XML read from the URL */ public Document urlGetXml(RequestContext context) { Document doc = Dom4jUtils.newDocument(); Element root = doc.addElement("Response"); String urlAddress = (String)context.getParameterString(GETURL); String targetElement = (String)context.getParameterString(TARGET_ELEMENT); if (null == targetElement) { targetElement = "data"; } if (null == urlAddress) { doc.getRootElement().addElement("error").addText("Required parameter not found: "+GETURL); } else { try { URL url = new URL(urlAddress); URLConnection conn = url.openConnection(); if (0 == conn.getContentType().indexOf("text/xml")) { //a_XML data inlined Document readDoc = Dom4jUtils.newDocument(conn.getInputStream()); root.add(readDoc.getRootElement()); } else { //a_Non-XML data add as CDATA and error BufferedReader rd = new BufferedReader( new InputStreamReader( conn.getInputStream() ) ); String line; StringBuffer sb = new StringBuffer(); while (null != (line = rd.readLine())) { sb.append(line); sb.append("\r\n"); } rd.close(); root.addElement("error").addText("Content-Type is not text/xml."); root.addElement("data").addCDATA(sb.toString()); } } catch (Exception e) { root.addElement("error").addCDATA(e.toString()); } } return doc; }}/****************************************************************************** * INTERWOVEN, INC. PROPRIETARY AND CONFIDENTIAL * * * Copyright 2004 Interwoven, Inc. All rights reserved. * * Use of this product is subject to license terms. If this product * is acquired by the United States Government or any contractor * therefor, then the Government's rights in this product and related * documentation, which are "commercial computer software" and * "commercial computer documentation", respectively, will be only as * set forth in such license terms as specified in: (i) for * acquisitions by or on behalf of civilian agencies, 48 C.F.R. 12.212 * of the Federal Acquisition Regulations and its successors; and (ii) * for acquisition by or on behalf of units of the Department of * Defense ("DoD"), in 48 C.F.R. 227.7202-1 through 227.7202-4 of the * DoD F.A.R. Supplement and its successors. All United States * Government end users acquire this product with only those rights * set forth in such license terms. ******************************************************************************/package com.interwoven.livesite.external.runtime;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;import java.util.Iterator;import org.dom4j.Document;import org.dom4j.Element;import com.interwoven.livesite.dom4j.Dom4jUtils;import com.interwoven.livesite.runtime.RequestContext;/***************************************************************************** * DOCUMENT ME!!! * * @author $Author: achachan $ * @version $Revision: 1.6 $ *****************************************************************************/public class Web{ /** default ctor */ public Web() { } /** Parameter for getting URL */ public static final String GETURL = "URL"; /** Parameter for target element */ public static final String TARGET_ELEMENT = "targetElement"; /** * *************************************************************************** * Fetch URL content at GETURL parameter * Requred parameter: URL = URL to fetch * Optional parameters: targetElement = element name where the content will be added * XPath: /Properties/Data/Result/Response * * @param context Request * @return Document **************************************************************************** */ public Document urlGet(RequestContext context) { Document doc = Dom4jUtils.newDocument(); Element root = doc.addElement("Response"); String urlAddress = (String)context.getParameterString(GETURL); String targetElement = (String)context.getParameterString(TARGET_ELEMENT); if (null == targetElement) { targetElement = "data"; } if (null == urlAddress) { doc.getRootElement().addElement("error").addText("Required parameter not found: "+GETURL); } else { try { URL url = new URL(urlAddress); URLConnection conn = url.openConnection(); String str; Iterator it = conn.getHeaderFields().keySet().iterator(); if (it.hasNext()) { Element eh = root.addElement("request_header"); StringBuffer sb = new StringBuffer(); while (it.hasNext()) { String name = (String)it.next(); if (null != name) { String value = conn.getHeaderField(name); eh.addElement(name).addText(value); sb.append(name); sb.append("="); sb.append(value); sb.append("\r\n"); } } eh.addElement("_raw").addCDATA(sb.toString()); } str = conn.getContentType(); if (null != str) { root.addElement("Content-Type").addText(str); } str = conn.getContentEncoding(); if (null != str) { root.addElement("Content-Encoding").addText(str); } root.addElement("Content-Length").addText(new Integer(conn.getContentLength()).toString()); if (0 == conn.getContentType().indexOf("text/xml")) { //a_XML data inlined Document readDoc = Dom4jUtils.newDocument(conn.getInputStream()); root.add(readDoc.getRootElement()); } else { //a_Read content and add as CDATA BufferedReader rd = new BufferedReader( new InputStreamReader( conn.getInputStream() ) ); String line; StringBuffer sb = new StringBuffer(); while (null != (line = rd.readLine())) { sb.append(line); sb.append("\r\n"); } rd.close(); root.addElement(targetElement).addCDATA(sb.toString()); } } catch (Exception e) { root.addElement("error").addCDATA(e.toString()); } } return doc; } /** * Download XML from a given URL in parameter GETURL (see member above) * * Requred parameter: URL = URL to fetch * Optional parameters: targetElement = element name where the content will be added * * @param context request * @return Document XML read from the URL */ public Document urlGetXml(RequestContext context) { Document doc = Dom4jUtils.newDocument(); Element root = doc.addElement("Response"); String urlAddress = (String)context.getParameterString(GETURL); String targetElement = (String)context.getParameterString(TARGET_ELEMENT); if (null == targetElement) { targetElement = "data"; } if (null == urlAddress) { doc.getRootElement().addElement("error").addText("Required parameter not found: "+GETURL); } else { try { URL url = new URL(urlAddress); URLConnection conn = url.openConnection(); if (0 == conn.getContentType().indexOf("text/xml")) { //a_XML data inlined Document readDoc = Dom4jUtils.newDocument(conn.getInputStream()); root.add(readDoc.getRootElement()); } else { //a_Non-XML data add as CDATA and error BufferedReader rd = new BufferedReader( new InputStreamReader( conn.getInputStream() ) ); String line; StringBuffer sb = new StringBuffer(); while (null != (line = rd.readLine())) { sb.append(line); sb.append("\r\n"); } rd.close(); root.addElement("error").addText("Content-Type is not text/xml."); root.addElement("data").addCDATA(sb.toString()); } } catch (Exception e) { root.addElement("error").addCDATA(e.toString()); } } return doc; }}