package com.ecyrd.jspwiki.plugin; import com.ecyrd.jspwiki.*; import com.ecyrd.jspwiki.plugin.*; import java.util.*; import java.net.*; import java.io.*; /** * Insert content into a WikiPage. This plugin supports inserting several * kinds of content. * * Parameters * */ public class InsertAnyPagePlugin implements WikiPlugin { public static final String PARAM_WIKI_PAGE_NAME = "wikiPageName"; public static final String PARAM_URL_TO_HTML = "urlToHtml"; public static void main(String[] args) { try { String url= "http://www.google.com"; System.err.println("Start InsertAnyPagePlugin URL=" + url); InsertAnyPagePlugin plugin= new InsertAnyPagePlugin(); System.err.println(plugin.getUrlToHtml(url)); } catch (Exception e) { e.printStackTrace(); } } public String execute(WikiContext context, Map params) throws PluginException { // Dispatch to the correct page getting method based on the passed in // page name. String wikiPageName = (String) params.get(PARAM_WIKI_PAGE_NAME); if (wikiPageName != null) { return getWikiPage(context, wikiPageName); } String urlToHtml = (String) params.get(PARAM_URL_TO_HTML); if (urlToHtml != null) { return getUrlToHtml(urlToHtml); } return "no page found"; } /** * Get the html representation of a wiki page. * * @return a string containing the html page */ public String getWikiPage(WikiContext context, String wikiPageName) throws PluginException { WikiEngine engine = context.getEngine(); // We should now have a page to insert. // WikiPage wpage = engine.getPage(wikiPageName); // Lets make sure we got a page // if (wpage == null) return "'" + wikiPageName + "' page not found"; else return engine.getHTML(context, wpage); } /** * Get the html content of a URL. The URL must point to an html page. * * @param The URL to the html page * @return a string containing the html page */ public String getUrlToHtml(String urlToHtml) throws PluginException { try { URL url= new URL(urlToHtml); URLConnection connection= url.openConnection(); printInfo(connection); return cvtToString(connection); } catch (Exception e) { throw new PluginException("Failed to get URL=" + urlToHtml, e); } } /** * Print attributes about a connection. * * @param u the connection to print out information about. */ public static void printInfo(URLConnection u) throws IOException { // Display the URL address, and information about it. System.out.println(u.getURL().toExternalForm() + ":"); System.out.println(" Content Type: " + u.getContentType()); System.out.println(" Content Length: " + u.getContentLength()); System.out.println(" Last Modified: " + new Date(u.getLastModified())); System.out.println(" Expiration: " + u.getExpiration()); System.out.println(" Content Encoding: " + u.getContentEncoding()); } /** * Converts a connection to a string representation. Assumes the underlying * content can be represented as a string. */ public static String cvtToString(URLConnection u) throws IOException { byte[] b= new byte[u.getContentLength()]; DataInputStream ds= new DataInputStream(u.getInputStream()); ds.readFully(b); return new String(b); } }