/* * Copyright (c) 2005-2007 Extreme! Lab, Indiana University. All rights reserved. * * This software is open source. See the bottom of this file for the license. * * $Id: */ package edu.indiana.extreme.xbaya.component.registry; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.swing.text.MutableAttributeSet; import javax.swing.text.html.HTML; import javax.swing.text.html.HTMLEditorKit; import javax.swing.text.html.HTML.Tag; import javax.swing.text.html.parser.ParserDelegator; import xsul5.MLogger; import edu.indiana.extreme.xbaya.component.ComponentException; import edu.indiana.extreme.xbaya.component.gui.ComponentTreeNode; import edu.indiana.extreme.xbaya.component.ws.WSComponent; import edu.indiana.extreme.xbaya.component.ws.WSComponentFactory; import edu.indiana.extreme.xbaya.util.IOUtil; /** * @author Satoshi Shirasuna */ public class WebComponentRegistry extends ComponentRegistry { private static final MLogger logger = MLogger.getLogger(); private URL url; private ComponentTreeNode tree; private Map> componentsMap; /** * Creates a WebComponentRegistryClient * * @param urlString * @throws MalformedURLException * @throws IOException */ public WebComponentRegistry(String urlString) throws MalformedURLException, IOException { this(new URL(urlString)); } /** * Creates a WebComponentRegistryClient * * @param url * The URL of the web page. */ public WebComponentRegistry(URL url) { this.url = url; this.componentsMap = new HashMap>(); } /** * @see edu.indiana.extreme.xbaya.component.registry.ComponentRegistry#getName() */ @Override public String getName() { return this.url.toString(); } /** * @see edu.indiana.extreme.xbaya.component.registry.ComponentRegistry#getComponentTree() */ @Override public ComponentTreeNode getComponentTree() throws ComponentRegistryException { this.tree = new ComponentTreeNode(this); parse(); return this.tree; } /** * Returns a list of component of a specified name. * * @param name * The name of the component. The name here is a relative URL * specified in tag, and is same as the name of a * corresponding ComponentTree. * @return The list of components of the specified name */ public List getComponents(String name) { // This method is only used from a test. List components = this.componentsMap.get(name); return components; } private void parse() throws ComponentRegistryException { try { HttpURLConnection connection = (HttpURLConnection) this.url .openConnection(); connection.setInstanceFollowRedirects(false); connection.connect(); int count = 0; // TODO checking 3 is not enough while (String.valueOf(connection.getResponseCode()).startsWith("3")) { String location = connection.getHeaderField("Location"); logger.finest("Redirecting to " + location); connection.disconnect(); this.url = new URL(location); connection = (HttpURLConnection) this.url.openConnection(); connection.setInstanceFollowRedirects(false); connection.connect(); count++; if (count > 10) { throw new ComponentRegistryException("Too many redirect"); } } InputStream inputStream = connection.getInputStream(); InputStreamReader reader = new InputStreamReader(inputStream); HtmlRegistryParserCallback callback = new HtmlRegistryParserCallback(); ParserDelegator parser = new ParserDelegator(); parser.parse(reader, callback, false); } catch (IOException e) { throw new ComponentRegistryException(e); } } private void addComponents(String name) { try { URL wsdlUrl = new URL(this.url, name); logger.finest("WSDL URL: " + wsdlUrl); String wsdlString = IOUtil.readToString(wsdlUrl.openStream()); logger.finest("WSDL: " + wsdlString); List components = WSComponentFactory .createComponents(wsdlString); addComponents(name, components); } catch (MalformedURLException e) { // Ignore logger.caught(e); } catch (IOException e) { // Ignore logger.caught(e); } catch (ComponentException e) { // Malformed WSDL. logger.caught(e); } catch (RuntimeException e) { logger.caught(e); } } private void addComponents(String name, List components) { this.componentsMap.put(name, components); WebComponentReference componentReference = new WebComponentReference( name, components); ComponentTreeNode treeLeaf = new ComponentTreeNode(componentReference); this.tree.add(treeLeaf); } private class HtmlRegistryParserCallback extends HTMLEditorKit.ParserCallback { /** * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleStartTag(javax.swing.text.html.HTML.Tag, * javax.swing.text.MutableAttributeSet, int) */ @Override public void handleStartTag(Tag tag, MutableAttributeSet attrSet, int pos) { logger.entering(new Object[] { tag, attrSet, new Integer(pos) }); if (tag == HTML.Tag.A) { String name = (String) attrSet .getAttribute(HTML.Attribute.HREF); addComponents(name); } } } } /* * Indiana University Extreme! Lab Software License, Version 1.2 * * Copyright (c) 2005-2007 The Trustees of Indiana University. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1) All redistributions of source code must retain the above copyright notice, * the list of authors in the original source code, this list of conditions and * the disclaimer listed in this license; * * 2) All redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the disclaimer listed in this license in * the documentation and/or other materials provided with the distribution; * * 3) Any documentation included with all redistributions must include the * following acknowledgement: * * "This product includes software developed by the Indiana University Extreme! * Lab. For further information please visit http://www.extreme.indiana.edu/" * * Alternatively, this acknowledgment may appear in the software itself, and * wherever such third-party acknowledgments normally appear. * * 4) The name "Indiana University" or "Indiana University Extreme! Lab" shall * not be used to endorse or promote products derived from this software without * prior written permission from Indiana University. For written permission, * please contact http://www.extreme.indiana.edu/. * * 5) Products derived from this software may not use "Indiana University" name * nor may "Indiana University" appear in their name, without prior written * permission of the Indiana University. * * Indiana University provides no reassurances that the source code provided * does not infringe the patent or any other intellectual property rights of any * other entity. Indiana University disclaims any liability to any recipient for * claims brought by any other entity based on infringement of intellectual * property rights or otherwise. * * LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO * WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES * NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF * INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. * INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", * "VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE. * LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR * ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION * GENERATED USING SOFTWARE. */