diff --git a/LICENSE.txt b/LICENSE.txt index 324f532c6..00e231a78 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -67,7 +67,17 @@ Public domain except as listed below: Router: -Public domain +Public domain except as listed below: + UPnP.java: + From freenet + See licenses/LICENSE-GPLv2.txt + + UPnP subsystem: + Copyright (C) 2003-2006 Satoshi Konno + See licenses/LICENSE-UPnP.txt + + XMLPull library used by UPnP: + See licenses/LICENSE-Apache2.0.txt diff --git a/licenses/LICENSE-UPnP.txt b/licenses/LICENSE-UPnP.txt new file mode 100644 index 000000000..7e2a36863 --- /dev/null +++ b/licenses/LICENSE-UPnP.txt @@ -0,0 +1,12 @@ +Copyright (C) 2003-2006 Satoshi Konno +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. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/router/java/src/net/i2p/router/transport/TransportEventListener.java b/router/java/src/net/i2p/router/transport/TransportEventListener.java index 31d52758c..2be28bea4 100644 --- a/router/java/src/net/i2p/router/transport/TransportEventListener.java +++ b/router/java/src/net/i2p/router/transport/TransportEventListener.java @@ -14,4 +14,5 @@ import net.i2p.data.i2np.I2NPMessage; public interface TransportEventListener { public void messageReceived(I2NPMessage message, RouterIdentity fromRouter, Hash fromRouterHash); + public void transportAddressChanged(); } diff --git a/router/java/src/net/i2p/router/transport/TransportImpl.java b/router/java/src/net/i2p/router/transport/TransportImpl.java index 04a2cde91..f9ac683b6 100644 --- a/router/java/src/net/i2p/router/transport/TransportImpl.java +++ b/router/java/src/net/i2p/router/transport/TransportImpl.java @@ -410,6 +410,8 @@ public abstract class TransportImpl implements Transport { protected void replaceAddress(RouterAddress address) { // _log.error("Replacing address for " + getStyle() + " was " + _currentAddress + " now " + address); _currentAddress = address; + if (_listener != null) + _listener.transportAddressChanged(); if ("SSU".equals(getStyle())) _context.commSystem().notifyReplaceAddress(address); } diff --git a/router/java/src/net/i2p/router/transport/TransportManager.java b/router/java/src/net/i2p/router/transport/TransportManager.java index bade75913..0b7d8d547 100644 --- a/router/java/src/net/i2p/router/transport/TransportManager.java +++ b/router/java/src/net/i2p/router/transport/TransportManager.java @@ -35,6 +35,7 @@ public class TransportManager implements TransportEventListener { private Log _log; private List _transports; private RouterContext _context; + private UPnPManager _upnpManager; private final static String PROP_ENABLE_UDP = "i2np.udp.enable"; private final static String PROP_ENABLE_NTCP = "i2np.ntcp.enable"; @@ -51,6 +52,7 @@ public class TransportManager implements TransportEventListener { _context.statManager().createRateStat("transport.bidFailNoTransports", "Could not attempt to bid on message, as none of the transports could attempt it", "Transport", new long[] { 60*1000, 10*60*1000, 60*60*1000 }); _context.statManager().createRateStat("transport.bidFailAllTransports", "Could not attempt to bid on message, as all of the transports had failed", "Transport", new long[] { 60*1000, 10*60*1000, 60*60*1000 }); _transports = new ArrayList(); + _upnpManager = new UPnPManager(context); } public void addTransport(Transport transport) { @@ -91,6 +93,7 @@ public class TransportManager implements TransportEventListener { } public void startListening() { + _upnpManager.start(); configTransports(); _log.debug("Starting up the transport manager"); for (int i = 0; i < _transports.size(); i++) { @@ -109,6 +112,7 @@ public class TransportManager implements TransportEventListener { } public void stopListening() { + _upnpManager.stop(); for (int i = 0; i < _transports.size(); i++) { ((Transport)_transports.get(i)).stopListening(); } @@ -341,6 +345,10 @@ public class TransportManager implements TransportEventListener { } } + public void transportAddressChanged() { + _upnpManager.update(getAddresses()); + } + public List getMostRecentErrorMessages() { List rv = new ArrayList(16); for (int i = 0; i < _transports.size(); i++) { diff --git a/router/java/src/net/i2p/router/transport/UPnP.java b/router/java/src/net/i2p/router/transport/UPnP.java new file mode 100644 index 000000000..aef65adc5 --- /dev/null +++ b/router/java/src/net/i2p/router/transport/UPnP.java @@ -0,0 +1,624 @@ +/* This code is part of Freenet. It is distributed under the GNU General + * Public License, version 2 (or at your option any later version). See + * http://www.gnu.org/ for further details of the GPL. */ +package net.i2p.router.transport; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import net.i2p.util.Log; +import net.i2p.I2PAppContext; +import net.i2p.router.RouterContext; + +import org.cybergarage.upnp.Action; +import org.cybergarage.upnp.ActionList; +import org.cybergarage.upnp.Argument; +import org.cybergarage.upnp.ArgumentList; +import org.cybergarage.upnp.ControlPoint; +import org.cybergarage.upnp.Device; +import org.cybergarage.upnp.DeviceList; +import org.cybergarage.upnp.Service; +import org.cybergarage.upnp.ServiceList; +import org.cybergarage.upnp.ServiceStateTable; +import org.cybergarage.upnp.StateVariable; +import org.cybergarage.upnp.device.DeviceChangeListener; +import org.freenetproject.DetectedIP; +import org.freenetproject.ForwardPort; +import org.freenetproject.ForwardPortCallback; +import org.freenetproject.ForwardPortStatus; + +/** + * This (and all in org/freenet, org/cybergarage, org/xmlpull) + * grabbed from freenet SVN, mid-February 2009 by zzz. + * This file modded somewhat to remove freenet-specific stuff, + * but most of the glue to I2P is in UPnPManager (which was written + * from scratch and is not the Limewire one referred to below). + * + * ================== + * + * This plugin implements UP&P support on a Freenet node. + * + * @author Florent Daignière <nextgens@freenetproject.org> + * + * + * some code has been borrowed from Limewire : @see com.limegroup.gnutella.UPnPManager + * + * @see http://www.upnp.org/ + * @see http://en.wikipedia.org/wiki/Universal_Plug_and_Play + * + * TODO: Support multiple IGDs ? + * TODO: Advertise the node like the MDNS plugin does + * TODO: Implement EventListener and react on ip-change + */ +public class UPnP extends ControlPoint implements DeviceChangeListener { + private Log _log; + private I2PAppContext _context; + + /** some schemas */ + private static final String ROUTER_DEVICE = "urn:schemas-upnp-org:device:InternetGatewayDevice:1"; + private static final String WAN_DEVICE = "urn:schemas-upnp-org:device:WANDevice:1"; + private static final String WANCON_DEVICE = "urn:schemas-upnp-org:device:WANConnectionDevice:1"; + private static final String WAN_IP_CONNECTION = "urn:schemas-upnp-org:service:WANIPConnection:1"; + private static final String WAN_PPP_CONNECTION = "urn:schemas-upnp-org:service:WANPPPConnection:1"; + + private Device _router; + private Service _service; + private boolean isDisabled = false; // We disable the plugin if more than one IGD is found + private final Object lock = new Object(); + // FIXME: detect it for real and deal with it! @see #2524 + private volatile boolean thinksWeAreDoubleNatted = false; + + /** List of ports we want to forward */ + private Set portsToForward; + /** List of ports we have actually forwarded */ + private Set portsForwarded; + /** Callback to call when a forward fails or succeeds */ + private ForwardPortCallback forwardCallback; + + public UPnP(I2PAppContext context) { + super(); + _context = context; + _log = _context.logManager().getLog(UPnP.class); + portsForwarded = new HashSet(); + addDeviceChangeListener(this); + } + + public void runPlugin() { + super.start(); + } + + public void terminate() { + unregisterPortMappings(); + super.stop(); + } + + public DetectedIP[] getAddress() { + _log.info("UP&P.getAddress() is called \\o/"); + if(isDisabled) { + _log.warn("Plugin has been disabled previously, ignoring request."); + return null; + } else if(!isNATPresent()) { + _log.warn("No UP&P device found, detection of the external ip address using the plugin has failed"); + return null; + } + + DetectedIP result = null; + final String natAddress = getNATAddress(); + try { + InetAddress detectedIP = InetAddress.getByName(natAddress); + short status = DetectedIP.NOT_SUPPORTED; + thinksWeAreDoubleNatted = !TransportImpl.isPubliclyRoutable(detectedIP.getAddress()); + // If we have forwarded a port AND we don't have a private address + if((portsForwarded.size() > 1) && (!thinksWeAreDoubleNatted)) + status = DetectedIP.FULL_INTERNET; + + result = new DetectedIP(detectedIP, status); + + _log.warn("Successful UP&P discovery :" + result); + + return new DetectedIP[] { result }; + } catch (UnknownHostException e) { + _log.error("Caught an UnknownHostException resolving " + natAddress, e); + return null; + } + } + + public void deviceAdded(Device dev) { + synchronized (lock) { + if(isDisabled) { + _log.warn("Plugin has been disabled previously, ignoring new device."); + return; + } + } + if(!ROUTER_DEVICE.equals(dev.getDeviceType()) || !dev.isRootDevice()) + return; // Silently ignore non-IGD devices + else if(isNATPresent()) { + _log.error("We got a second IGD on the network! the plugin doesn't handle that: let's disable it."); + isDisabled = true; + + synchronized(lock) { + _router = null; + _service = null; + } + + stop(); + return; + } + + _log.warn("UP&P IGD found : " + dev.getFriendlyName()); + synchronized(lock) { + _router = dev; + } + + discoverService(); + // We have found the device we need: stop the listener thread + stop(); + synchronized(lock) { + if(_service == null) { + _log.error("The IGD device we got isn't suiting our needs, let's disable the plugin"); + isDisabled = true; + _router = null; + return; + } + } + registerPortMappings(); + } + + private void registerPortMappings() { + Set ports; + synchronized(lock) { + ports = portsToForward; + } + if(ports == null) return; + registerPorts(ports); + } + + /** + * Traverses the structure of the router device looking for the port mapping service. + */ + private void discoverService() { + synchronized (lock) { + for (Iterator iter = _router.getDeviceList().iterator();iter.hasNext();) { + Device current = (Device)iter.next(); + if (!current.getDeviceType().equals(WAN_DEVICE)) + continue; + + DeviceList l = current.getDeviceList(); + for (int i=0;i"); + for(int i=0; i"); + } + sb.append(""); + } + + private void listActionsArguments(Action action, StringBuilder sb) { + ArgumentList ar = action.getArgumentList(); + for(int i=0; iargument ("+i+") :" + argument.getName()+""); + } + } + + private void listActions(Service service, StringBuilder sb) { + ActionList al = service.getActionList(); + for(int i=0; iaction ("+i+") :" + action.getName()); + listActionsArguments(action, sb); + sb.append(""); + } + } + + private String toString(String action, String Argument, Service serv) { + Action getIP = serv.getAction(action); + if(getIP == null || !getIP.postControlAction()) + return null; + + Argument ret = getIP.getOutputArgumentList().getArgument(Argument); + return ret.getValue(); + } + + // TODO: extend it! RTFM + private void listSubServices(Device dev, StringBuilder sb) { + ServiceList sl = dev.getServiceList(); + for(int i=0; iservice ("+i+") : "+serv.getServiceType()+"
"); + if("urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1".equals(serv.getServiceType())){ + sb.append("WANCommonInterfaceConfig"); + sb.append(" status: " + toString("GetCommonLinkProperties", "NewPhysicalLinkStatus", serv)); + sb.append(" type: " + toString("GetCommonLinkProperties", "NewWANAccessType", serv)); + sb.append(" upstream: " + toString("GetCommonLinkProperties", "NewLayer1UpstreamMaxBitRate", serv)); + sb.append(" downstream: " + toString("GetCommonLinkProperties", "NewLayer1DownstreamMaxBitRate", serv) + "
"); + }else if("urn:schemas-upnp-org:service:WANPPPConnection:1".equals(serv.getServiceType())){ + sb.append("WANPPPConnection"); + sb.append(" status: " + toString("GetStatusInfo", "NewConnectionStatus", serv)); + sb.append(" type: " + toString("GetConnectionTypeInfo", "NewConnectionType", serv)); + sb.append(" upstream: " + toString("GetLinkLayerMaxBitRates", "NewUpstreamMaxBitRate", serv)); + sb.append(" downstream: " + toString("GetLinkLayerMaxBitRates", "NewDownstreamMaxBitRate", serv) + "
"); + sb.append(" external IP: " + toString("GetExternalIPAddress", "NewExternalIPAddress", serv) + "
"); + }else if("urn:schemas-upnp-org:service:Layer3Forwarding:1".equals(serv.getServiceType())){ + sb.append("Layer3Forwarding"); + sb.append("DefaultConnectionService: " + toString("GetDefaultConnectionService", "NewDefaultConnectionService", serv)); + }else if(WAN_IP_CONNECTION.equals(serv.getServiceType())){ + sb.append("WANIPConnection"); + sb.append(" status: " + toString("GetStatusInfo", "NewConnectionStatus", serv)); + sb.append(" type: " + toString("GetConnectionTypeInfo", "NewConnectionType", serv)); + sb.append(" external IP: " + toString("GetExternalIPAddress", "NewExternalIPAddress", serv) + "
"); + }else if("urn:schemas-upnp-org:service:WANEthernetLinkConfig:1".equals(serv.getServiceType())){ + sb.append("WANEthernetLinkConfig"); + sb.append(" status: " + toString("GetEthernetLinkStatus", "NewEthernetLinkStatus", serv) + "
"); + }else + sb.append("~~~~~~~ "+serv.getServiceType()); + listActions(serv, sb); + listStateTable(serv, sb); + sb.append(""); + } + } + + private void listSubDev(String prefix, Device dev, StringBuilder sb){ + sb.append("

Device : "+dev.getFriendlyName()+" - "+ dev.getDeviceType()+"
"); + listSubServices(dev, sb); + + DeviceList dl = dev.getDeviceList(); + for(int j=0; j"); + listSubDev(dev.getFriendlyName(), subDev, sb); + sb.append("

"); + } + sb.append("

"); + } + +/***** + public String handleHTTPGet(HTTPRequest request) throws PluginHTTPException { + if(request.isParameterSet("getDeviceCapabilities")) { + final StringBuilder sb = new StringBuilder(); + sb.append("UPnP report"); + listSubDev("WANDevice", _router, sb); + sb.append(""); + return sb.toString(); + } + + HTMLNode pageNode = pr.getPageMaker().getPageNode("UP&P plugin configuration page", false, null); + HTMLNode contentNode = pr.getPageMaker().getContentNode(pageNode); + + if(isDisabled) { + HTMLNode disabledInfobox = contentNode.addChild("div", "class", "infobox infobox-error"); + HTMLNode disabledInfoboxHeader = disabledInfobox.addChild("div", "class", "infobox-header"); + HTMLNode disabledInfoboxContent = disabledInfobox.addChild("div", "class", "infobox-content"); + + disabledInfoboxHeader.addChild("#", "UP&P plugin report"); + disabledInfoboxContent.addChild("#", "The plugin has been disabled; Do you have more than one UP&P IGD on your LAN ?"); + return pageNode.generate(); + } else if(!isNATPresent()) { + HTMLNode notFoundInfobox = contentNode.addChild("div", "class", "infobox infobox-warning"); + HTMLNode notFoundInfoboxHeader = notFoundInfobox.addChild("div", "class", "infobox-header"); + HTMLNode notFoundInfoboxContent = notFoundInfobox.addChild("div", "class", "infobox-content"); + + notFoundInfoboxHeader.addChild("#", "UP&P plugin report"); + notFoundInfoboxContent.addChild("#", "The plugin hasn't found any UP&P aware, compatible device on your LAN."); + return pageNode.generate(); + } + + HTMLNode foundInfobox = contentNode.addChild("div", "class", "infobox infobox-normal"); + HTMLNode foundInfoboxHeader = foundInfobox.addChild("div", "class", "infobox-header"); + HTMLNode foundInfoboxContent = foundInfobox.addChild("div", "class", "infobox-content"); + + // FIXME L10n! + foundInfoboxHeader.addChild("#", "UP&P plugin report"); + foundInfoboxContent.addChild("p", "The following device has been found : ").addChild("a", "href", "?getDeviceCapabilities").addChild("#", _router.getFriendlyName()); + foundInfoboxContent.addChild("p", "Our current external ip address is : " + getNATAddress()); + int downstreamMaxBitRate = getDownstreamMaxBitRate(); + int upstreamMaxBitRate = getUpstramMaxBitRate(); + if(downstreamMaxBitRate > 0) + foundInfoboxContent.addChild("p", "Our reported max downstream bit rate is : " + getDownstreamMaxBitRate()+ " bits/sec"); + if(upstreamMaxBitRate > 0) + foundInfoboxContent.addChild("p", "Our reported max upstream bit rate is : " + getUpstramMaxBitRate()+ " bits/sec"); + synchronized(lock) { + if(portsToForward != null) { + for(ForwardPort port : portsToForward) { + if(portsForwarded.contains(port)) { + foundInfoboxContent.addChild("p", "The "+port.name+" port "+port.portNumber+" / "+port.protocol+" has been forwarded successfully."); + } else { + foundInfoboxContent.addChild("p", "The "+port.name+" port "+port.portNumber+" / "+port.protocol+" has not been forwarded."); + } + } + } + } + + return pageNode.generate(); + } + + public String handleHTTPPost(HTTPRequest request) throws PluginHTTPException { + return null; + } +***/ + + private boolean addMapping(String protocol, int port, String description, ForwardPort fp) { + if(isDisabled || !isNATPresent() || _router == null) { + _log.error("Can't addMapping: " + isDisabled + " " + isNATPresent() + " " + _router); + return false; + } + + // Just in case... + // this confuses my linksys - zzz + // removeMapping(protocol, port, fp, true); + + Action add = _service.getAction("AddPortMapping"); + if(add == null) { + _log.error("Couldn't find AddPortMapping action!"); + return false; + } + + + add.setArgumentValue("NewRemoteHost", ""); + add.setArgumentValue("NewExternalPort", port); + add.setArgumentValue("NewInternalClient", _router.getInterfaceAddress()); + add.setArgumentValue("NewInternalPort", port); + add.setArgumentValue("NewProtocol", protocol); + add.setArgumentValue("NewPortMappingDescription", description); + add.setArgumentValue("NewEnabled","1"); + add.setArgumentValue("NewLeaseDuration", 0); + + if(add.postControlAction()) { + synchronized(lock) { + portsForwarded.add(fp); + } + return true; + } else return false; + } + + private boolean removeMapping(String protocol, int port, ForwardPort fp, boolean noLog) { + if(isDisabled || !isNATPresent()) + return false; + + Action remove = _service.getAction("DeletePortMapping"); + if(remove == null) { + _log.error("Couldn't find DeletePortMapping action!"); + return false; + } + + // remove.setArgumentValue("NewRemoteHost", ""); + remove.setArgumentValue("NewExternalPort", port); + remove.setArgumentValue("NewProtocol", protocol); + + boolean retval = remove.postControlAction(); + synchronized(lock) { + portsForwarded.remove(fp); + } + + if(!noLog) + _log.warn("UPnP: Removed mapping for "+fp.name+" "+port+" / "+protocol); + return retval; + } + + public void onChangePublicPorts(Set ports, ForwardPortCallback cb) { + Set portsToDumpNow = null; + Set portsToForwardNow = null; + _log.warn("UP&P Forwarding "+ports.size()+" ports..."); + synchronized(lock) { + if(forwardCallback != null && forwardCallback != cb && cb != null) { + _log.error("ForwardPortCallback changed from "+forwardCallback+" to "+cb+" - using new value, but this is very strange!"); + } + forwardCallback = cb; + if(portsToForward == null || portsToForward.isEmpty()) { + portsToForward = ports; + portsToForwardNow = ports; + portsToDumpNow = null; + } else if(ports == null || ports.isEmpty()) { + portsToDumpNow = portsToForward; + portsToForward = ports; + portsToForwardNow = null; + } else { + // Some ports to keep, some ports to dump + // Ports in ports but not in portsToForwardNow we must forward + // Ports in portsToForwardNow but not in ports we must dump + for(ForwardPort port: ports) { + if(portsToForward.contains(port)) { + // We have forwarded it, and it should be forwarded, cool. + } else { + // Needs forwarding + if(portsToForwardNow == null) portsToForwardNow = new HashSet(); + portsToForwardNow.add(port); + } + } + for(ForwardPort port : portsToForward) { + if(ports.contains(port)) { + // Should be forwarded, has been forwarded, cool. + } else { + // Needs dropping + if(portsToDumpNow == null) portsToDumpNow = new HashSet(); + portsToDumpNow.add(port); + } + } + portsToForward = ports; + } + if(_router == null) return; // When one is found, we will do the forwards + } + if(portsToDumpNow != null) + unregisterPorts(portsToDumpNow); + if(portsToForwardNow != null) + registerPorts(portsToForwardNow); + } + + private void registerPorts(Set portsToForwardNow) { + for(ForwardPort port : portsToForwardNow) { + String proto; + if(port.protocol == ForwardPort.PROTOCOL_UDP_IPV4) + proto = "UDP"; + else if(port.protocol == ForwardPort.PROTOCOL_TCP_IPV4) + proto = "TCP"; + else { + HashMap map = new HashMap(); + map.put(port, new ForwardPortStatus(ForwardPortStatus.DEFINITE_FAILURE, "Protocol not supported", port.portNumber)); + forwardCallback.portForwardStatus(map); + continue; + } + if(tryAddMapping(proto, port.portNumber, port.name, port)) { + HashMap map = new HashMap(); + map.put(port, new ForwardPortStatus(ForwardPortStatus.MAYBE_SUCCESS, "Port apparently forwarded by UPnP", port.portNumber)); + forwardCallback.portForwardStatus(map); + continue; + } else { + HashMap map = new HashMap(); + map.put(port, new ForwardPortStatus(ForwardPortStatus.PROBABLE_FAILURE, "UPnP port forwarding apparently failed", port.portNumber)); + forwardCallback.portForwardStatus(map); + continue; + } + } + } + + private void unregisterPorts(Set portsToForwardNow) { + for(ForwardPort port : portsToForwardNow) { + String proto; + if(port.protocol == ForwardPort.PROTOCOL_UDP_IPV4) + proto = "UDP"; + else if(port.protocol == ForwardPort.PROTOCOL_TCP_IPV4) + proto = "TCP"; + else { + // Ignore, we've already complained about it + continue; + } + removeMapping(proto, port.portNumber, port, false); + } + } + + public static void main(String[] args) throws Exception { + UPnP upnp = new UPnP(I2PAppContext.getGlobalContext()); + ControlPoint cp = new ControlPoint(); + System.out.println("Searching for up&p devices:"); + cp.start(); + cp.search(); + while(true) { + DeviceList list = cp.getDeviceList(); + System.out.println("Found " + list.size() + " devices!"); + StringBuilder sb = new StringBuilder(); + Iterator it = list.iterator(); + while(it.hasNext()) { + Device device = it.next(); + upnp.listSubDev(device.toString(), device, sb); + System.out.println("Here is the listing for " + device.toString() + " :"); + System.out.println(sb.toString()); + sb = new StringBuilder(); + } + System.out.println("End"); + Thread.sleep(2000); + } + } +} diff --git a/router/java/src/net/i2p/router/transport/UPnPManager.java b/router/java/src/net/i2p/router/transport/UPnPManager.java new file mode 100644 index 000000000..4deaebc4d --- /dev/null +++ b/router/java/src/net/i2p/router/transport/UPnPManager.java @@ -0,0 +1,115 @@ +package net.i2p.router.transport; + +/* + * public domain + */ + +import java.util.HashSet; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import net.i2p.data.RouterAddress; +import net.i2p.router.RouterContext; +import net.i2p.util.Log; + +import org.cybergarage.util.Debug; +import org.freenetproject.DetectedIP; +import org.freenetproject.ForwardPort; +import org.freenetproject.ForwardPortCallback; +import org.freenetproject.ForwardPortStatus; + +/** + * Bridge from the I2P RouterAddress data structure to + * the freenet data structures + * + * No disable option yet. + * UPnP listens on ports 1900, 8008, and 8058 - no config option yet. + * No routerconsole support yet. + * + * @author zzz + */ +public class UPnPManager { + private Log _log; + private RouterContext _context; + private UPnP _upnp; + private UPnPCallback _upnpCallback; + private boolean _isRunning; + + public UPnPManager(RouterContext context) { + _context = context; + _log = _context.logManager().getLog(UPnPManager.class); + _upnp = new UPnP(context); + _upnpCallback = new UPnPCallback(); + _isRunning = false; + } + + public synchronized void start() { + _log.error("UPnP Start"); + Debug.on(); // UPnP stuff -> wrapper log + if (!_isRunning) + _upnp.runPlugin(); + _isRunning = true; + } + + public synchronized void stop() { + _log.error("UPnP Stop"); + if (_isRunning) + _upnp.terminate(); + _isRunning = false; + } + + /** call when the ports might have changed */ + public void update(Map addresses) { + _log.error("UPnP Update:"); + if (!_isRunning) + return; + Set forwards = new HashSet(addresses.size()); + for (String style : addresses.keySet()) { + RouterAddress ra = addresses.get(style); + if (ra == null) + continue; + Properties opts = ra.getOptions(); + if (opts == null) + continue; + String s = opts.getProperty("port"); + if (s == null) + continue; + int port = -1; + try { + port = Integer.parseInt(s); + } catch (NumberFormatException nfe) { continue; } + int protocol = -1; + if ("SSU".equals(style)) + protocol = ForwardPort.PROTOCOL_UDP_IPV4; + else if ("NTCP".equals(style)) + protocol = ForwardPort.PROTOCOL_TCP_IPV4; + else + continue; + _log.error("Adding: " + style + " " + port); + ForwardPort fp = new ForwardPort(style, false, protocol, port); + forwards.add(fp); + } + _upnp.onChangePublicPorts(forwards, _upnpCallback); + } + + /** just logs for now */ + private class UPnPCallback implements ForwardPortCallback { + + /** Called to indicate status on one or more forwarded ports. */ + public void portForwardStatus(Map statuses) { + _log.error("UPnP Callback:"); + + DetectedIP[] ips = _upnp.getAddress(); + for (DetectedIP ip : ips) { + _log.error("External address: " + ip.publicAddress + " type: " + ip.natType); + } + + for (ForwardPort fp : statuses.keySet()) { + ForwardPortStatus fps = statuses.get(fp); + _log.error(fp.name + " " + fp.protocol + " " + fp.portNumber + + " status: " + fps.status + " reason: " + fps.reasonString + " ext port: " + fps.externalPort); + } + } + } +} diff --git a/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java b/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java index c23245bae..2d5df4be0 100644 --- a/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java +++ b/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java @@ -543,6 +543,8 @@ public class NTCPTransport extends TransportImpl { NTCPConnection con = (NTCPConnection)iter.next(); con.close(); } + // will this work? + replaceAddress(null); } public static final String STYLE = "NTCP"; diff --git a/router/java/src/net/i2p/router/transport/udp/UDPTransport.java b/router/java/src/net/i2p/router/transport/udp/UDPTransport.java index e5185defa..0a2e7f1be 100644 --- a/router/java/src/net/i2p/router/transport/udp/UDPTransport.java +++ b/router/java/src/net/i2p/router/transport/udp/UDPTransport.java @@ -589,11 +589,13 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority return true; } +/*** infinite loop public RouterAddress getCurrentAddress() { if (needsRebuild()) rebuildExternalAddress(false); return super.getCurrentAddress(); } +***/ public void messageReceived(I2NPMessage inMsg, RouterIdentity remoteIdent, Hash remoteIdentHash, long msToReceive, int bytesReceived) { if (inMsg.getType() == DatabaseStoreMessage.MESSAGE_TYPE) { @@ -996,6 +998,9 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority public void stopListening() { shutdown(); + // will this work? + _externalAddress = null; + replaceAddress(null); } private boolean explicitAddressSpecified() { diff --git a/router/java/src/org/cybergarage/http/Date.java b/router/java/src/org/cybergarage/http/Date.java new file mode 100644 index 000000000..117964d34 --- /dev/null +++ b/router/java/src/org/cybergarage/http/Date.java @@ -0,0 +1,165 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File : Date.java +* +* Revision; +* +* 01/05/03 +* - first revision +* 10/20/04 +* - Theo Beisch +* - Fixed the following methods to use HOUR_OF_DAY instead of HOUR. +* getHour(), getDateString() getTimeString() +* - Fixed getInstance() to return GMT instance. +* +******************************************************************/ + +package org.cybergarage.http; + +import java.util.Calendar; +import java.util.TimeZone; + +public class Date +{ + private Calendar cal; + + public Date(Calendar cal) + { + this.cal = cal; + } + + public Calendar getCalendar() + { + return cal; + } + + //////////////////////////////////////////////// + // Time + //////////////////////////////////////////////// + + public int getHour() + { + // Thanks for Theo Beisch (10/20/04) + return getCalendar().get(Calendar.HOUR_OF_DAY); + } + + public int getMinute() + { + return getCalendar().get(Calendar.MINUTE); + } + + public int getSecond() + { + return getCalendar().get(Calendar.SECOND); + } + + //////////////////////////////////////////////// + // paint + //////////////////////////////////////////////// + + public final static Date getLocalInstance() + { + return new Date(Calendar.getInstance()); + } + + public final static Date getInstance() + { + // Thanks for Theo Beisch (10/20/04) + return new Date(Calendar.getInstance(TimeZone.getTimeZone("GMT"))); + } + + //////////////////////////////////////////////// + // getDateString + //////////////////////////////////////////////// + + public final static String toDateString(int value) + { + if (value < 10) + return "0" + Integer.toString(value); + return Integer.toString(value); + } + + private final static String MONTH_STRING[] = { + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec", + }; + + public final static String toMonthString(int value) + { + value -= Calendar.JANUARY; + if (0 <= value && value < 12) + return MONTH_STRING[value]; + return ""; + } + + private final static String WEEK_STRING[] = { + "Sun", + "Mon", + "Tue", + "Wed", + "Thu", + "Fri", + "Sat", + }; + + public final static String toWeekString(int value) + { + value -= Calendar.SUNDAY; + if (0 <= value && value < 7) + return WEEK_STRING[value]; + return ""; + } + + public final static String toTimeString(int value) + { + String str = ""; + if (value < 10) + str += "0"; + str += Integer.toString(value); + return str; + } + + public String getDateString() + { + // Thanks for Theo Beisch (10/20/04) + Calendar cal = getCalendar(); + return + toWeekString(cal.get(Calendar.DAY_OF_WEEK)) +", " + + toTimeString(cal.get(Calendar.DATE)) + " " + + toMonthString(cal.get(Calendar.MONTH)) + " " + + Integer.toString(cal.get(Calendar.YEAR)) + " " + + toTimeString(cal.get(Calendar.HOUR_OF_DAY)) + ":" + + toTimeString(cal.get(Calendar.MINUTE)) + ":" + + toTimeString(cal.get(Calendar.SECOND)) + " GMT"; + } + + //////////////////////////////////////////////// + // getTimeString + //////////////////////////////////////////////// + + public String getTimeString() + { + // Thanks for Theo Beisch (10/20/04) + Calendar cal = getCalendar(); + return + toDateString(cal.get(Calendar.HOUR_OF_DAY)) + + (((cal.get(Calendar.SECOND) % 2) == 0) ? ":" : " ") + + toDateString(cal.get(Calendar.MINUTE)); + } + +} + diff --git a/router/java/src/org/cybergarage/http/HTML.java b/router/java/src/org/cybergarage/http/HTML.java new file mode 100644 index 000000000..0d06e7ca5 --- /dev/null +++ b/router/java/src/org/cybergarage/http/HTML.java @@ -0,0 +1,22 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: HTML.java +* +* Revision; +* +* 01/05/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.http; + +public class HTML +{ + public static final String CONTENT_TYPE = "text/html; charset=\"utf-8\""; +} + diff --git a/router/java/src/org/cybergarage/http/HTTP.java b/router/java/src/org/cybergarage/http/HTTP.java new file mode 100644 index 000000000..ec7ccbde1 --- /dev/null +++ b/router/java/src/org/cybergarage/http/HTTP.java @@ -0,0 +1,210 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: HTTP.java +* +* Revision: +* +* 11/18/02 +* - first revision. +* 08/30/03 +* - Giordano Sassaroli +* - Problem : the method getPort should return the default http port 80 when a port is not specified +* - Description : the method is used in ControlRequest.setRequestHost() and in SubscriptionRequest.setService(). maybe the default port check could be done in these methods. +* 09/03/02 +* - Added getRequestHostURL(). +* 03/11/04 +* - Added the following methods to send big content stream. +* post(HTTPResponse, byte[]) +* post(HTTPResponse, InputStream) +* 05/26/04 +* - Added NO_CATCH and MAX_AGE. +* 10/20/04 +* - Brent Hills +* - Added Range and MYNAME; +* +******************************************************************/ + +package org.cybergarage.http; + +import java.net.*; + +public class HTTP +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public static final String HOST = "HOST"; + + public static final String VERSION = "1.1"; + public static final String VERSION_10 = "1.0"; + public static final String VERSION_11 = "1.1"; + + public static final String CRLF = "\r\n"; + public static final String TAB = "\t"; + + public static final String SOAP_ACTION = "SOAPACTION"; + + public static final String M_SEARCH = "M-SEARCH"; + public static final String NOTIFY = "NOTIFY"; + public static final String POST = "POST"; + public static final String GET = "GET"; + public static final String HEAD = "HEAD"; + public static final String SUBSCRIBE = "SUBSCRIBE"; + public static final String UNSUBSCRIBE = "UNSUBSCRIBE"; + + public static final String DATE = "Date"; + public static final String CACHE_CONTROL = "Cache-Control"; + public static final String NO_CACHE = "no-cache"; + public static final String MAX_AGE = "max-age"; + public static final String CONNECTION = "Connection"; + public static final String CLOSE = "close"; + public static final String KEEP_ALIVE = "Keep-Alive"; + public static final String CONTENT_TYPE = "Content-Type"; + public static final String CONTENT_LENGTH = "Content-Length"; + public static final String CONTENT_RANGE = "Content-Range"; + public static final String CONTENT_RANGE_BYTES = "bytes"; + // Thanks for Brent Hills (10/20/04) + public static final String RANGE = "Range"; + public static final String TRANSFER_ENCODING = "Transfer-Encoding"; + public static final String CHUNKED = "Chunked"; + public static final String LOCATION = "Location"; + public static final String SERVER = "Server"; + + + public static final String ST = "ST"; + public static final String MX = "MX"; + public static final String MAN = "MAN"; + public static final String NT = "NT"; + public static final String NTS = "NTS"; + public static final String USN = "USN"; + public static final String EXT = "EXT"; + public static final String SID = "SID"; + public static final String SEQ = "SEQ"; + public final static String CALLBACK = "CALLBACK"; + public final static String TIMEOUT = "TIMEOUT"; + // Thanks for Brent Hills (10/20/04) + public final static String MYNAME = "MYNAME"; + + public static final String REQEST_LINE_DELIM = " "; + public static final String HEADER_LINE_DELIM = " :"; + public static final String STATUS_LINE_DELIM = " "; + + public static final int DEFAULT_PORT = 80; + public static final int DEFAULT_CHUNK_SIZE = 512 * 1024; + public static final int DEFAULT_TIMEOUT = 30; + + //////////////////////////////////////////////// + // URL + //////////////////////////////////////////////// + + public static final boolean isAbsoluteURL(String urlStr) + { + try { + new URL(urlStr); + return true; + } + catch (Exception e) { + return false; + } + } + + public static final String getHost(String urlStr) + { + try { + URL url = new URL(urlStr); + return url.getHost(); + } + catch (Exception e) { + return ""; + } + } + + public static final int getPort(String urlStr) + { + try { + URL url = new URL(urlStr); + // Thanks for Giordano Sassaroli (08/30/03) + int port = url.getPort(); + if (port <= 0) + port = DEFAULT_PORT; + return port; + } + catch (Exception e) { + return DEFAULT_PORT; + } + } + + public static final String getRequestHostURL(String host, int port) + { + String reqHost = "http://" + host + ":" + port; + return reqHost; + } + + public static final String toRelativeURL(String urlStr, boolean withParam) + { + String uri = urlStr; + if (isAbsoluteURL(urlStr) == false) { + if (0 < urlStr.length() && urlStr.charAt(0) != '/') + uri = "/" + urlStr; + } + else { + try{ + URL url = new URL(urlStr); + uri = url.getPath(); + if (withParam == true) { + String queryStr = url.getQuery(); + if (!queryStr.equals("")){ + uri += "?" + queryStr; + } + } + if (uri.endsWith("/")) + uri = uri.substring(0,uri.length()-1); + }catch(Exception e){} + } + return uri; + } + + public static final String toRelativeURL(String urlStr) + { + return toRelativeURL(urlStr, true); + } + + public static final String getAbsoluteURL(String baseURLStr, String relURlStr) + { + try { + URL baseURL = new URL(baseURLStr); + String url = + baseURL.getProtocol() + "://" + + baseURL.getHost() + ":" + + baseURL.getPort() + + toRelativeURL(relURlStr); + return url; + } + catch (Exception e) { + return ""; + } + } + + //////////////////////////////////////////////// + // Chunk Size + //////////////////////////////////////////////// + + private static int chunkSize = DEFAULT_CHUNK_SIZE; + + public static final void setChunkSize(int size) + { + chunkSize = size; + } + + public static final int getChunkSize() + { + return chunkSize; + } + +} + diff --git a/router/java/src/org/cybergarage/http/HTTPHeader.java b/router/java/src/org/cybergarage/http/HTTPHeader.java new file mode 100644 index 000000000..d09446bdc --- /dev/null +++ b/router/java/src/org/cybergarage/http/HTTPHeader.java @@ -0,0 +1,144 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: HTTPHeader.java +* +* Revision; +* +* 11/19/02 +* - first revision. +* 05/26/04 +* - Jan Newmarch (05/26/04) +* - Fixed getValue() to compare using String::equals() instead of String::startWidth(). +* +******************************************************************/ + +package org.cybergarage.http; + +import java.io.*; + +import org.cybergarage.util.*; + +public class HTTPHeader +{ + private String name; + private String value; + + public HTTPHeader(String name, String value) + { + setName(name); + setValue(value); + } + + public HTTPHeader(String lineStr) + { + setName(""); + setValue(""); + if (lineStr == null) + return; + int colonIdx = lineStr.indexOf(':'); + if (colonIdx < 0) + return; + String name = new String(lineStr.getBytes(), 0, colonIdx); + String value = new String(lineStr.getBytes(), colonIdx+1, lineStr.length()-colonIdx-1); + setName(name.trim()); + setValue(value.trim()); + } + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + public void setName(String name) + { + this.name = name; + } + + public void setValue(String value) + { + this.value = value; + } + + public String getName() + { + return name; + } + + public String getValue() + { + return value; + } + + public boolean hasName() + { + if (name == null || name.length() <= 0) + return false; + return true; + } + + //////////////////////////////////////////////// + // static methods + //////////////////////////////////////////////// + + public final static String getValue(LineNumberReader reader, String name) + { + String bigName = name.toUpperCase(); + try { + String lineStr = reader.readLine(); + while (lineStr != null && 0 < lineStr.length()) { + HTTPHeader header = new HTTPHeader(lineStr); + if (header.hasName() == false) { + lineStr = reader.readLine(); + continue; + } + String bigLineHeaderName = header.getName().toUpperCase(); + // Thanks for Jan Newmarch (05/26/04) + if (bigLineHeaderName.equals(bigName) == false) { + lineStr = reader.readLine(); + continue; + } + return header.getValue(); + } + } + catch (IOException e) { + Debug.warning(e); + return ""; + } + return ""; + } + + public final static String getValue(String data, String name) + { + StringReader strReader = new StringReader(data); + LineNumberReader lineReader = new LineNumberReader(strReader); + return getValue(lineReader, name); + } + + public final static String getValue(byte[] data, String name) + { + return getValue(new String(data), name); + } + + public final static int getIntegerValue(String data, String name) + { + try { + return Integer.parseInt(getValue(data, name)); + } + catch (Exception e) { + return 0; + } + } + + public final static int getIntegerValue(byte[] data, String name) + { + try { + return Integer.parseInt(getValue(data, name)); + } + catch (Exception e) { + return 0; + } + } +} diff --git a/router/java/src/org/cybergarage/http/HTTPPacket.java b/router/java/src/org/cybergarage/http/HTTPPacket.java new file mode 100644 index 000000000..3c0c78e63 --- /dev/null +++ b/router/java/src/org/cybergarage/http/HTTPPacket.java @@ -0,0 +1,808 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: HTTPConnection.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* 09/02/03 +* - Giordano Sassaroli +* - Problem : The API is unable to receive responses from the Microsoft UPnP stack +* - Error : the Microsoft UPnP stack is based on ISAPI on IIS, and whenever IIS +* receives a post request, it answers with two responses: the first one has no +* body and it is a code 100 (continue) response, which has to be ignored. The +* second response is the actual one and should be parsed as the response. +* 02/09/04 +* - Ralf G. R. Bergs" +* - Why do you strip leading and trailing white space from the response body? +* - Disabled to trim the content string. +* 03/11/04 +* - Added some methods about InputStream content. +* setContentInputStream(), getContentInputStream() and hasContentInputStream(). +* 03/16/04 +* - Thanks for Darrell Young +* - Added setVersion() and getVersion(); +* 03/17/04 +* - Added hasFirstLine(); +* 05/26/04 +* - Jan Newmarch (05/26/04) +* - Changed setCacheControl() and getChcheControl(); +* 08/25/04 +* - Added the following methods. +* hasContentRange(), setContentRange(), getContentRange(), +* getContentRangeFirstPosition(), getContentRangeLastPosition() and getContentRangeInstanceLength() +* 08/26/04 +* - Added the following methods. +* hasConnection(), setConnection(), getConnection(), +* isCloseConnection() and isKeepAliveConnection() +* 08/27/04 +* - Added a updateWithContentLength paramger to setContent(). +* - Changed to HTTPPacket::set() not to change the header of Content-Length. +* 08/28/04 +* - Added init() and read(). +* 09/19/04 +* - Added a onlyHeaders parameter to set(). +* 10/20/04 +* - Brent Hills +* - Changed hasContentRange() to check Content-Range and Range header. +* - Added support for Range header to getContentRange(). +* 02/02/05 +* - Mark Retallack +* - Fixed set() not to read over the content length when the stream is keep alive. +* 02/28/05 +* - Added the following methods for chunked stream support. +* hasTransferEncoding(), setTransferEncoding(), getTransferEncoding(), isChunked(). +* 03/02/05 +* - Changed post() to suppot chunked stream. +* +*******************************************************************/ + +package org.cybergarage.http; + +import java.io.*; +import java.util.*; + +import org.cybergarage.net.*; +import org.cybergarage.util.*; +import java.util.Calendar; + +public class HTTPPacket +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public HTTPPacket() + { + setVersion(HTTP.VERSION); + setContentInputStream(null); + } + + public HTTPPacket(HTTPPacket httpPacket) + { + setVersion(HTTP.VERSION); + set(httpPacket); + setContentInputStream(null); + } + + public HTTPPacket(InputStream in) + { + setVersion(HTTP.VERSION); + set(in); + setContentInputStream(null); + } + + //////////////////////////////////////////////// + // init + //////////////////////////////////////////////// + + public void init() + { + setFirstLine(""); + clearHeaders(); + setContent(new byte[0], false); + setContentInputStream(null); + } + + //////////////////////////////////////////////// + // Version + //////////////////////////////////////////////// + + private String version; + + public void setVersion(String ver) + { + version = ver; + } + + public String getVersion() + { + return version; + } + + //////////////////////////////////////////////// + // set + //////////////////////////////////////////////// + + protected boolean set(InputStream in, boolean onlyHeaders) + { + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + + String firstLine = reader.readLine(); + if (firstLine == null || firstLine.length() <= 0) + return false; + setFirstLine(firstLine); + + // Thanks for Giordano Sassaroli (09/03/03) + HTTPStatus httpStatus = new HTTPStatus(firstLine); + int statCode = httpStatus.getStatusCode(); + if (statCode == HTTPStatus.CONTINUE){ + //ad hoc code for managing iis non-standard behaviour + //iis sends 100 code response and a 200 code response in the same + //stream, so the code should check the presence of the actual + //response in the stream. + //skip all header lines + String headerLine = reader.readLine(); + while ((headerLine != null) && (0 < headerLine.length()) ) { + HTTPHeader header = new HTTPHeader(headerLine); + if (header.hasName() == true) + setHeader(header); + headerLine = reader.readLine(); + } + //look forward another first line + String actualFirstLine = reader.readLine(); + if ((actualFirstLine != null) && (0 < actualFirstLine.length()) ) { + //this is the actual first line + setFirstLine(actualFirstLine); + }else{ + return true; + } + } + + String headerLine = reader.readLine(); + while ((headerLine != null) && (0 < headerLine.length()) ) { + HTTPHeader header = new HTTPHeader(headerLine); + if (header.hasName() == true) + setHeader(header); + headerLine = reader.readLine(); + } + + if (onlyHeaders == true) { + setContent("", false); + return true; + } + + boolean isChunkedRequest = isChunked(); + + long contentLen = 0; + if (isChunkedRequest == true) { + try { + String chunkSizeLine = reader.readLine(); + contentLen = Long.parseLong(new String(chunkSizeLine.getBytes(), 0, chunkSizeLine.length()-2)); + } + catch (Exception e) {}; + } + else + contentLen = getContentLength(); + + StringBuilder contentBuf = new StringBuilder(); + + while (0 < contentLen) { + int chunkSize = HTTP.getChunkSize(); + char readBuf[] = new char[chunkSize]; + long readCnt = 0; + while (readCnt < contentLen) { + try { + // Thanks for Mark Retallack (02/02/05) + long bufReadLen = contentLen - readCnt; + if (chunkSize < bufReadLen) + bufReadLen = chunkSize; + int readLen = reader.read(readBuf, 0, (int)bufReadLen); + if (readLen < 0) + break; + contentBuf.append(new String(readBuf, 0, readLen)); + readCnt += readLen; + } + catch (Exception e) + { + Debug.warning(e); + break; + } + } + if (isChunkedRequest == true) { + // skip CRLF + long skipLen = 0; + do { + long skipCnt = reader.skip(HTTP.CRLF.length() - skipLen); + if (skipCnt < 0) + break; + skipLen += skipCnt; + } while (skipLen < HTTP.CRLF.length()); + // read next chunk size + try { + String chunkSizeLine = reader.readLine(); + contentLen = Long.parseLong(new String(chunkSizeLine.getBytes(), 0, chunkSizeLine.length()-2)); + } + catch (Exception e) { + contentLen = 0; + }; + } + else + contentLen = 0; + } + + // Thanks for Ralf G. R. Bergs (02/09/04) + String contentStr = contentBuf.toString(); + setContent(contentStr.getBytes(), false); + } + catch (Exception e) { + Debug.warning(e); + return false; + } + + return true; + } + + protected boolean set(InputStream in) + { + return set(in, false); + } + + protected boolean set(HTTPSocket httpSock) + { + return set(httpSock.getInputStream()); + } + + protected void set(HTTPPacket httpPacket) + { + setFirstLine(httpPacket.getFirstLine()); + + clearHeaders(); + int nHeaders = httpPacket.getNHeaders(); + for (int n=0; n 0) ? true : false; + } + + //////////////////////////////////////////////// + // Contents (InputStream) + //////////////////////////////////////////////// + + private InputStream contentInput = null; + + public void setContentInputStream(InputStream in) + { + contentInput = in; + } + + public InputStream getContentInputStream() + { + return contentInput; + } + + public boolean hasContentInputStream() + { + return (contentInput != null) ? true : false; + } + + //////////////////////////////////////////////// + // ContentType + //////////////////////////////////////////////// + + public void setContentType(String type) + { + setHeader(HTTP.CONTENT_TYPE, type); + } + + public String getContentType() + { + return getHeaderValue(HTTP.CONTENT_TYPE); + } + + //////////////////////////////////////////////// + // ContentLength + //////////////////////////////////////////////// + + public void setContentLength(long len) + { + setLongHeader(HTTP.CONTENT_LENGTH, len); + } + + public long getContentLength() + { + return getLongHeaderValue(HTTP.CONTENT_LENGTH); + } + + //////////////////////////////////////////////// + // Connection + //////////////////////////////////////////////// + + public boolean hasConnection() + { + return hasHeader(HTTP.CONNECTION); + } + + public void setConnection(String value) + { + setHeader(HTTP.CONNECTION, value); + } + + public String getConnection() + { + return getHeaderValue(HTTP.CONNECTION); + } + + public boolean isCloseConnection() + { + if (hasConnection() == false) + return false; + String connection = getConnection(); + if (connection == null) + return false; + return connection.equalsIgnoreCase(HTTP.CLOSE); + } + + public boolean isKeepAliveConnection() + { + if (hasConnection() == false) + return false; + String connection = getConnection(); + if (connection == null) + return false; + return connection.equalsIgnoreCase(HTTP.KEEP_ALIVE); + } + + //////////////////////////////////////////////// + // ContentRange + //////////////////////////////////////////////// + + public boolean hasContentRange() + { + return (hasHeader(HTTP.CONTENT_RANGE) || hasHeader(HTTP.RANGE)); + } + + public void setContentRange(long firstPos, long lastPos, long length) + { + String rangeStr = ""; + rangeStr += HTTP.CONTENT_RANGE_BYTES + " "; + rangeStr += Long.toString(firstPos) + "-"; + rangeStr += Long.toString(lastPos) + "/"; + rangeStr += ((0 < length) ? Long.toString(length) : "*"); + setHeader(HTTP.CONTENT_RANGE, rangeStr); + } + + public long[] getContentRange() + { + long range[] = new long[3]; + range[0] = range[1] = range[2] = 0; + if (hasContentRange() == false) + return range; + String rangeLine = getHeaderValue(HTTP.CONTENT_RANGE); + // Thanks for Brent Hills (10/20/04) + if (rangeLine.length() <= 0) + rangeLine = getHeaderValue(HTTP.RANGE); + if (rangeLine.length() <= 0) + return range; + // Thanks for Brent Hills (10/20/04) + StringTokenizer strToken = new StringTokenizer(rangeLine, " ="); + // Skip bytes + if (strToken.hasMoreTokens() == false) + return range; + strToken.nextToken(" "); + // Get first-byte-pos + if (strToken.hasMoreTokens() == false) + return range; + String firstPosStr = strToken.nextToken(" -"); + try { + range[0] = Long.parseLong(firstPosStr); + } + catch (NumberFormatException e) {}; + if (strToken.hasMoreTokens() == false) + return range; + String lastPosStr = strToken.nextToken("-/"); + try { + range[1] = Long.parseLong(lastPosStr); + } + catch (NumberFormatException e) {}; + if (strToken.hasMoreTokens() == false) + return range; + String lengthStr = strToken.nextToken("/"); + try { + range[2] = Long.parseLong(lengthStr); + } + catch (NumberFormatException e) {}; + return range; + } + + public long getContentRangeFirstPosition() + { + long range[] = getContentRange(); + return range[0]; + } + + public long getContentRangeLastPosition() + { + long range[] = getContentRange(); + return range[1]; + } + + public long getContentRangeInstanceLength() + { + long range[] = getContentRange(); + return range[2]; + } + + //////////////////////////////////////////////// + // CacheControl + //////////////////////////////////////////////// + + public void setCacheControl(String directive) + { + setHeader(HTTP.CACHE_CONTROL, directive); + } + + public void setCacheControl(String directive, int value) + { + String strVal = directive + "=" + Integer.toString(value); + setHeader(HTTP.CACHE_CONTROL, strVal); + } + + public void setCacheControl(int value) + { + setCacheControl(HTTP.MAX_AGE, value); + } + + public String getCacheControl() + { + return getHeaderValue(HTTP.CACHE_CONTROL); + } + + //////////////////////////////////////////////// + // Server + //////////////////////////////////////////////// + + public void setServer(String name) + { + setHeader(HTTP.SERVER, name); + } + + public String getServer() + { + return getHeaderValue(HTTP.SERVER); + } + + //////////////////////////////////////////////// + // Host + //////////////////////////////////////////////// + + public void setHost(String host, int port) + { + String hostAddr = host; + if (HostInterface.isIPv6Address(host) == true) + hostAddr = "[" + host + "]"; + setHeader(HTTP.HOST, hostAddr + ":" + Integer.toString(port)); + } + + public String getHost() + { + return getHeaderValue(HTTP.HOST); + } + + + //////////////////////////////////////////////// + // Date + //////////////////////////////////////////////// + + public void setDate(Calendar cal) + { + Date date = new Date(cal); + setHeader(HTTP.DATE, date.getDateString()); + } + + public String getDate() + { + return getHeaderValue(HTTP.DATE); + } + + //////////////////////////////////////////////// + // Connection + //////////////////////////////////////////////// + + public boolean hasTransferEncoding() + { + return hasHeader(HTTP.TRANSFER_ENCODING); + } + + public void setTransferEncoding(String value) + { + setHeader(HTTP.TRANSFER_ENCODING, value); + } + + public String getTransferEncoding() + { + return getHeaderValue(HTTP.TRANSFER_ENCODING); + } + + public boolean isChunked() + { + if (hasTransferEncoding() == false) + return false; + String transEnc = getTransferEncoding(); + if (transEnc == null) + return false; + return transEnc.equalsIgnoreCase(HTTP.CHUNKED); + } + + //////////////////////////////////////////////// + // set + //////////////////////////////////////////////// + +/* + public final static boolean parse(HTTPPacket httpPacket, InputStream in) + { + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + return parse(httpPacket, reader); + } + catch (Exception e) { + Debug.warning(e); + } + return false; + } +*/ +} + diff --git a/router/java/src/org/cybergarage/http/HTTPRequest.java b/router/java/src/org/cybergarage/http/HTTPRequest.java new file mode 100644 index 000000000..f4c6f16e2 --- /dev/null +++ b/router/java/src/org/cybergarage/http/HTTPRequest.java @@ -0,0 +1,495 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: HTTPRequest.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* 05/23/03 +* - Giordano Sassaroli +* - Add a relative URL check to setURI(). +* 09/02/03 +* - Giordano Sassaroli +* - Problem : Devices whose description use absolute urls receive wrong http requests +* - Error : the presence of a base url is not mandatory, the API code makes the assumption that control and event subscription urls are relative +* - Description: The method setURI should be changed as follows +* 02/01/04 +* - Added URI parameter methods. +* 03/16/04 +* - Removed setVersion() because the method is added to the super class. +* - Changed getVersion() to return the version when the first line string has the length. +* 05/19/04 +* - Changed post(HTTPResponse *) to close the socket stream from the server. +* 08/19/04 +* - Fixed getFirstLineString() and getHTTPVersion() no to return "HTTP/HTTP/version". +* 08/25/04 +* - Added isHeadRequest(). +* 08/26/04 +* - Changed post(HTTPResponse) not to close the connection. +* - Changed post(String, int) to add a connection header to close. +* 08/27/04 +* - Changed post(String, int) to support the persistent connection. +* 08/28/04 +* - Added isKeepAlive(). +* 10/26/04 +* - Brent Hills +* - Added a fix to post() when the last position of Content-Range header is 0. +* - Added a Content-Range header to the response in post(). +* - Changed the status code for the Content-Range request in post(). +* - Added to check the range of Content-Range request in post(). +* 03/02/05 +* - Changed post() to suppot chunked stream. +* +******************************************************************/ + +package org.cybergarage.http; + +import java.io.*; +import java.net.*; +import java.util.*; + +public class HTTPRequest extends HTTPPacket +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public HTTPRequest() + { + } + + public HTTPRequest(InputStream in) + { + super(in); + } + + public HTTPRequest(HTTPSocket httpSock) + { + this(httpSock.getInputStream()); + setSocket(httpSock); + } + + //////////////////////////////////////////////// + // Method + //////////////////////////////////////////////// + + private String method = null; + + public void setMethod(String value) + { + method = value; + } + + public String getMethod() + { + if (method != null) + return method; + return getFirstLineToken(0); + } + + public boolean isMethod(String method) + { + String headerMethod = getMethod(); + if (headerMethod == null) + return false; + return headerMethod.equalsIgnoreCase(method); + } + + public boolean isGetRequest() + { + return isMethod(HTTP.GET); + } + + public boolean isPostRequest() + { + return isMethod(HTTP.POST); + } + + public boolean isHeadRequest() + { + return isMethod(HTTP.HEAD); + } + + public boolean isSubscribeRequest() + { + return isMethod(HTTP.SUBSCRIBE); + } + + public boolean isUnsubscribeRequest() + { + return isMethod(HTTP.UNSUBSCRIBE); + } + + public boolean isNotifyRequest() + { + return isMethod(HTTP.NOTIFY); + } + + //////////////////////////////////////////////// + // URI + //////////////////////////////////////////////// + + private String uri = null; + + public void setURI(String value, boolean isCheckRelativeURL) + { + uri = value; + if (isCheckRelativeURL == false) + return; + // Thanks for Giordano Sassaroli (09/02/03) + uri = HTTP.toRelativeURL(uri); + } + + public void setURI(String value) + { + setURI(value, false); + } + + public String getURI() + { + if (uri != null) + return uri; + return getFirstLineToken(1); + } + + //////////////////////////////////////////////// + // URI Parameter + //////////////////////////////////////////////// + + public ParameterList getParameterList() + { + ParameterList paramList = new ParameterList(); + String uri = getURI(); + if (uri == null) + return paramList; + int paramIdx = uri.indexOf('?'); + if (paramIdx < 0) + return paramList; + while (0 < paramIdx) { + int eqIdx = uri.indexOf('=', (paramIdx+1)); + String name = uri.substring(paramIdx+1, eqIdx); + int nextParamIdx = uri.indexOf('&', (eqIdx+1)); + String value = uri.substring(eqIdx+1, (0 < nextParamIdx) ? nextParamIdx : uri.length()); + Parameter param = new Parameter(name, value); + paramList.add(param); + paramIdx = nextParamIdx; + } + return paramList; + } + + public String getParameterValue(String name) + { + ParameterList paramList = getParameterList(); + return paramList.getValue(name); + } + + //////////////////////////////////////////////// + // SOAPAction + //////////////////////////////////////////////// + + public boolean isSOAPAction() + { + return hasHeader(HTTP.SOAP_ACTION); + } + + //////////////////////////////////////////////// + // Host / Port + //////////////////////////////////////////////// + + private String requestHost = ""; + + public void setRequestHost(String host) + { + requestHost = host; + } + + public String getRequestHost() + { + return requestHost; + } + + private int requestPort = -1; + + public void setRequestPort(int host) + { + requestPort = host; + } + + public int getRequestPort() + { + return requestPort; + } + + //////////////////////////////////////////////// + // Socket + //////////////////////////////////////////////// + + private HTTPSocket httpSocket = null; + + public void setSocket(HTTPSocket value) + { + httpSocket = value; + } + + public HTTPSocket getSocket() + { + return httpSocket; + } + + /////////////////////////// ///////////////////// + // local address/port + //////////////////////////////////////////////// + + public String getLocalAddress() + { + return getSocket().getLocalAddress(); + } + + public int getLocalPort() + { + return getSocket().getLocalPort(); + } + + //////////////////////////////////////////////// + // parseRequest + //////////////////////////////////////////////// + + public boolean parseRequestLine(String lineStr) + { + StringTokenizer st = new StringTokenizer(lineStr, HTTP.REQEST_LINE_DELIM); + if (st.hasMoreTokens() == false) + return false; + setMethod(st.nextToken()); + if (st.hasMoreTokens() == false) + return false; + setURI(st.nextToken()); + if (st.hasMoreTokens() == false) + return false; + setVersion(st.nextToken()); + return true; + } + + //////////////////////////////////////////////// + // First Line + //////////////////////////////////////////////// + + public String getHTTPVersion() + { + if (hasFirstLine() == true) + return getFirstLineToken(2); + return "HTTP/" + super.getVersion(); + } + + public String getFirstLineString() + { + return getMethod() + " " + getURI() + " " + getHTTPVersion() + HTTP.CRLF; + } + + //////////////////////////////////////////////// + // getHeader + //////////////////////////////////////////////// + + public String getHeader() + { + StringBuilder str = new StringBuilder(); + + str.append(getFirstLineString()); + + String headerString = getHeaderString(); + str.append(headerString); + + return str.toString(); + } + + //////////////////////////////////////////////// + // isKeepAlive + //////////////////////////////////////////////// + + public boolean isKeepAlive() + { + if (isCloseConnection() == true) + return false; + if (isKeepAliveConnection() == true) + return true; + String httpVer = getHTTPVersion(); + boolean isHTTP10 = (0 < httpVer.indexOf("1.0")) ? true : false; + if (isHTTP10 == true) + return false; + return true; + } + + //////////////////////////////////////////////// + // read + //////////////////////////////////////////////// + + public boolean read() + { + return super.read(getSocket()); + } + + //////////////////////////////////////////////// + // POST (Response) + //////////////////////////////////////////////// + + public boolean post(HTTPResponse httpRes) + { + HTTPSocket httpSock = getSocket(); + long offset = 0; + long length = httpRes.getContentLength(); + if (hasContentRange() == true) { + long firstPos = getContentRangeFirstPosition(); + long lastPos = getContentRangeLastPosition(); + + // Thanks for Brent Hills (10/26/04) + if (lastPos <= 0) + lastPos = length - 1; + if ((firstPos > length ) || (lastPos > length)) + return returnResponse(HTTPStatus.INVALID_RANGE); + httpRes.setContentRange(firstPos, lastPos, length); + httpRes.setStatusCode(HTTPStatus.PARTIAL_CONTENT); + + offset = firstPos; + length = lastPos - firstPos + 1; + } + return httpSock.post(httpRes, offset, length, isHeadRequest()); + //httpSock.close(); + } + + //////////////////////////////////////////////// + // POST (Request) + //////////////////////////////////////////////// + + private Socket postSocket = null; + + public HTTPResponse post(String host, int port, boolean isKeepAlive) + { + HTTPResponse httpRes = new HTTPResponse(); + + setConnection((isKeepAlive == true) ? HTTP.KEEP_ALIVE : HTTP.CLOSE); + + boolean isHeaderRequest = isHeadRequest(); + + OutputStream out = null; + InputStream in = null; + + try { + if (postSocket == null) + postSocket = new Socket(host, port); + + out = postSocket.getOutputStream(); + PrintStream pout = new PrintStream(out); + pout.print(getHeader()); + pout.print(HTTP.CRLF); + + boolean isChunkedRequest = isChunked(); + + String content = getContentString(); + int contentLength = 0; + if (content != null) + contentLength = content.length(); + + if (0 < contentLength) { + if (isChunkedRequest == true) { + String chunSizeBuf = Long.toString(contentLength); + pout.print(chunSizeBuf); + pout.print(HTTP.CRLF); + } + pout.print(content); + if (isChunkedRequest == true) + pout.print(HTTP.CRLF); + } + + if (isChunkedRequest == true) { + pout.print("0"); + pout.print(HTTP.CRLF); + } + + pout.flush(); + + in = postSocket.getInputStream(); + httpRes.set(in, isHeaderRequest); + } + catch (Exception e) { + httpRes.setStatusCode(HTTPStatus.INTERNAL_SERVER_ERROR); + } finally { + if (isKeepAlive == false) { + try { + in.close(); + } catch (Exception e) {}; + if (in != null) + try { + out.close(); + } catch (Exception e) {}; + if (out != null) + try { + postSocket.close(); + } catch (Exception e) {}; + postSocket = null; + } + } + + return httpRes; + } + + public HTTPResponse post(String host, int port) + { + return post(host, port, false); + } + + //////////////////////////////////////////////// + // set + //////////////////////////////////////////////// + + public void set(HTTPRequest httpReq) + { + set((HTTPPacket)httpReq); + setSocket(httpReq.getSocket()); + } + + //////////////////////////////////////////////// + // OK/BAD_REQUEST + //////////////////////////////////////////////// + + public boolean returnResponse(int statusCode) + { + HTTPResponse httpRes = new HTTPResponse(); + httpRes.setStatusCode(statusCode); + httpRes.setContentLength(0); + return post(httpRes); + } + + public boolean returnOK() + { + return returnResponse(HTTPStatus.OK); + } + + public boolean returnBadRequest() + { + return returnResponse(HTTPStatus.BAD_REQUEST); + } + + //////////////////////////////////////////////// + // toString + //////////////////////////////////////////////// + + public String toString() + { + StringBuilder str = new StringBuilder(); + + str.append(getHeader()); + str.append(HTTP.CRLF); + str.append(getContentString()); + + return str.toString(); + } + + public void print() + { + System.out.println(toString()); + } +} diff --git a/router/java/src/org/cybergarage/http/HTTPRequestListener.java b/router/java/src/org/cybergarage/http/HTTPRequestListener.java new file mode 100644 index 000000000..12fcf349e --- /dev/null +++ b/router/java/src/org/cybergarage/http/HTTPRequestListener.java @@ -0,0 +1,21 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: HTTPRequestListener.java +* +* Revision; +* +* 12/13/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.http; + +public interface HTTPRequestListener +{ + public void httpRequestRecieved(HTTPRequest httpReq); +} diff --git a/router/java/src/org/cybergarage/http/HTTPResponse.java b/router/java/src/org/cybergarage/http/HTTPResponse.java new file mode 100644 index 000000000..b5c247878 --- /dev/null +++ b/router/java/src/org/cybergarage/http/HTTPResponse.java @@ -0,0 +1,115 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: HTTPResponse.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* 10/22/03 +* - Changed to initialize a content length header. +* 10/22/04 +* - Added isSuccessful(). +* +******************************************************************/ + +package org.cybergarage.http; + +import java.io.*; + +public class HTTPResponse extends HTTPPacket +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public HTTPResponse() + { + setContentType(HTML.CONTENT_TYPE); + setServer(HTTPServer.getName()); + setContent(""); + } + + public HTTPResponse(HTTPResponse httpRes) + { + set(httpRes); + } + + public HTTPResponse(InputStream in) + { + super(in); + } + + public HTTPResponse(HTTPSocket httpSock) + { + this(httpSock.getInputStream()); + } + + //////////////////////////////////////////////// + // Status Line + //////////////////////////////////////////////// + + private int statusCode = 0; + + public void setStatusCode(int code) + { + statusCode = code; + } + + public int getStatusCode() + { + if (statusCode != 0) + return statusCode; + HTTPStatus httpStatus = new HTTPStatus(getFirstLine()); + return httpStatus.getStatusCode(); + } + + public boolean isSuccessful() + { + return HTTPStatus.isSuccessful(getStatusCode()); + } + + public String getStatusLineString() + { + return "HTTP/" + getVersion() + " " + getStatusCode() + " " + HTTPStatus.code2String(statusCode) + HTTP.CRLF; + } + + //////////////////////////////////////////////// + // getHeader + //////////////////////////////////////////////// + + public String getHeader() + { + StringBuilder str = new StringBuilder(); + + str.append(getStatusLineString()); + str.append(getHeaderString()); + + return str.toString(); + } + + //////////////////////////////////////////////// + // toString + //////////////////////////////////////////////// + + public String toString() + { + StringBuilder str = new StringBuilder(); + + str.append(getStatusLineString()); + str.append(getHeaderString()); + str.append(HTTP.CRLF); + str.append(getContentString()); + + return str.toString(); + } + + public void print() + { + System.out.println(toString()); + } +} diff --git a/router/java/src/org/cybergarage/http/HTTPServer.java b/router/java/src/org/cybergarage/http/HTTPServer.java new file mode 100644 index 000000000..f589e2e06 --- /dev/null +++ b/router/java/src/org/cybergarage/http/HTTPServer.java @@ -0,0 +1,204 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: HTTPServer.java +* +* Revision; +* +* 12/12/02 +* - first revision. +* 10/20/03 +* - Improved the HTTP server using multithreading. +* 08/27/04 +* - Changed accept() to set a default timeout, HTTP.DEFAULT_TIMEOUT, to the socket. +* +******************************************************************/ + +package org.cybergarage.http; + +import java.io.*; +import java.net.*; + +import org.cybergarage.util.*; + +public class HTTPServer implements Runnable +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String NAME = "CyberHTTP"; + public final static String VERSION = "1.0"; + + public final static int DEFAULT_PORT = 80; + + public static String getName() + { + String osName = System.getProperty("os.name"); + String osVer = System.getProperty("os.version"); + return osName + "/" + osVer + " " + NAME + "/" + VERSION; + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public HTTPServer() + { + serverSock = null; + } + + //////////////////////////////////////////////// + // ServerSocket + //////////////////////////////////////////////// + + private ServerSocket serverSock = null; + private InetAddress bindAddr = null; + private int bindPort = 0; + + public ServerSocket getServerSock() + { + return serverSock; + } + + public String getBindAddress() + { + if (bindAddr == null) + return ""; + return bindAddr.toString(); + } + + public int getBindPort() + { + return bindPort; + } + + //////////////////////////////////////////////// + // open/close + //////////////////////////////////////////////// + + public boolean open(String addr, int port) + { + if (serverSock != null) + return true; + try { + bindAddr = InetAddress.getByName(addr); + bindPort = port; + serverSock = new ServerSocket(bindPort, 0, bindAddr); + serverSock.setSoTimeout(10*1000); + } + catch (IOException e) { + return false; + } + return true; + } + + public boolean close() + { + if (serverSock == null) + return true; + try { + serverSock.close(); + serverSock = null; + bindAddr = null; + bindPort = 0; + } + catch (Exception e) { + Debug.warning(e); + return false; + } + return true; + } + + public Socket accept() + { + if (serverSock == null) + return null; + try { + Socket sock = serverSock.accept(); + sock.setSoTimeout(HTTP.DEFAULT_PORT * 1000); + return sock; + } + catch (Exception e) { + return null; + } + } + + public boolean isOpened() + { + return (serverSock != null) ? true : false; + } + + //////////////////////////////////////////////// + // httpRequest + //////////////////////////////////////////////// + + private ListenerList httpRequestListenerList = new ListenerList(); + + public void addRequestListener(HTTPRequestListener listener) + { + httpRequestListenerList.add(listener); + } + + public void removeRequestListener(HTTPRequestListener listener) + { + httpRequestListenerList.remove(listener); + } + + public void performRequestListener(HTTPRequest httpReq) + { + int listenerSize = httpRequestListenerList.size(); + for (int n=0; n +* - Added PARTIAL_CONTENT and INVALID_RANGE; +* 10/22/04 +* - Added isSuccessful(). +* 10/29/04 +* - Fixed set() to set the version and the response code when the mothod is null. +* - Fixed set() to read multi words of the response sring such as Not Found. +* +******************************************************************/ + +package org.cybergarage.http; + +import java.util.*; + +import org.cybergarage.util.*; + +public class HTTPStatus +{ + //////////////////////////////////////////////// + // Code + //////////////////////////////////////////////// + + public static final int CONTINUE = 100; + public static final int OK = 200; + // Thanks for Brent Hills (10/20/04) + public static final int PARTIAL_CONTENT = 206; + public static final int BAD_REQUEST = 400; + public static final int NOT_FOUND = 404; + public static final int PRECONDITION_FAILED = 412; + // Thanks for Brent Hills (10/20/04) + public static final int INVALID_RANGE = 416; + public static final int INTERNAL_SERVER_ERROR = 500; + + public static final String code2String(int code) + { + switch (code) { + case CONTINUE: return "Continue"; + case OK: return "OK"; + case PARTIAL_CONTENT: return "Partial Content"; + case BAD_REQUEST: return "Bad Request"; + case NOT_FOUND: return "Not Found"; + case PRECONDITION_FAILED: return "Precondition Failed"; + case INVALID_RANGE: return "Invalid Range"; + case INTERNAL_SERVER_ERROR: return "Internal Server Error"; + } + return ""; + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public HTTPStatus() + { + setVersion(""); + setStatusCode(0); + setReasonPhrase(""); + } + + public HTTPStatus(String ver, int code, String reason) + { + setVersion(ver); + setStatusCode(code); + setReasonPhrase(reason); + } + + public HTTPStatus(String lineStr) + { + set(lineStr); + } + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private String version = ""; + private int statusCode = 0; + private String reasonPhrase = ""; + + public void setVersion(String value) + { + version = value; + } + + public void setStatusCode(int value) + { + statusCode = value; + } + + public void setReasonPhrase(String value) + { + reasonPhrase = value; + } + + public String getVersion() + { + return version; + } + + public int getStatusCode() + { + return statusCode; + } + + public String getReasonPhrase() + { + return reasonPhrase; + } + + //////////////////////////////////////////////// + // Status + //////////////////////////////////////////////// + + final public static boolean isSuccessful(int statCode) + { + if (200 <= statCode && statCode < 300) + return true; + return false; + } + + public boolean isSuccessful() + { + return isSuccessful(getStatusCode()); + } + + //////////////////////////////////////////////// + // set + //////////////////////////////////////////////// + + public void set(String lineStr) + { + if (lineStr == null) { + setVersion(HTTP.VERSION); + setStatusCode(INTERNAL_SERVER_ERROR); + setReasonPhrase(code2String(INTERNAL_SERVER_ERROR)); + return; + } + + try { + StringTokenizer st = new StringTokenizer(lineStr, HTTP.STATUS_LINE_DELIM); + + if (st.hasMoreTokens() == false) + return; + String ver = st.nextToken(); + setVersion(ver.trim()); + + if (st.hasMoreTokens() == false) + return; + String codeStr = st.nextToken(); + int code = 0; + try { + code = Integer.parseInt(codeStr); + } + catch (Exception e1) {} + setStatusCode(code); + + String reason = ""; + while (st.hasMoreTokens() == true) { + if (0 <= reason.length()) + reason += " "; + reason += st.nextToken(); + } + setReasonPhrase(reason.trim()); + } + catch (Exception e) { + Debug.warning(e); + } + + } +} diff --git a/router/java/src/org/cybergarage/http/Parameter.java b/router/java/src/org/cybergarage/http/Parameter.java new file mode 100644 index 000000000..fb29ae994 --- /dev/null +++ b/router/java/src/org/cybergarage/http/Parameter.java @@ -0,0 +1,61 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: Parameter.java +* +* Revision; +* +* 02/01/04 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.http; + +public class Parameter +{ + private String name = new String(); + private String value = new String(); + + public Parameter() + { + } + + public Parameter(String name, String value) + { + setName(name); + setValue(value); + } + + //////////////////////////////////////////////// + // name + //////////////////////////////////////////////// + + public void setName(String name) + { + this.name = name; + } + + public String getName() + { + return name; + } + + //////////////////////////////////////////////// + // value + //////////////////////////////////////////////// + + public void setValue(String value) + { + this.value = value; + } + + public String getValue() + { + return value; + } +} + diff --git a/router/java/src/org/cybergarage/http/ParameterList.java b/router/java/src/org/cybergarage/http/ParameterList.java new file mode 100644 index 000000000..9a0b2a6d1 --- /dev/null +++ b/router/java/src/org/cybergarage/http/ParameterList.java @@ -0,0 +1,60 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: ParameterList.java +* +* Revision; +* +* 02/01/04 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.http; + +import java.util.*; + +public class ParameterList extends Vector +{ + private static final long serialVersionUID = -6026765325018137641L; + + public ParameterList() + { + } + + public Parameter at(int n) + { + return (Parameter)get(n); + } + + public Parameter getParameter(int n) + { + return (Parameter)get(n); + } + + public Parameter getParameter(String name) + { + if (name == null) + return null; + + int nLists = size(); + for (int n=0; n +* - Changed isUseAddress() to isUsableAddress(). +* +******************************************************************/ + +package org.cybergarage.net; + +import java.net.*; +import java.util.*; + +public class HostInterface +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public static boolean USE_LOOPBACK_ADDR = false; + public static boolean USE_ONLY_IPV4_ADDR = false; + public static boolean USE_ONLY_IPV6_ADDR = false; + + //////////////////////////////////////////////// + // Network Interfaces + //////////////////////////////////////////////// + + private static String ifAddress = ""; + + public final static void setInterface(String ifaddr) + { + ifAddress = ifaddr; + } + + public final static String getInterface() + { + return ifAddress; + } + + private final static boolean hasAssignedInterface() + { + return (0 < ifAddress.length()) ? true : false; + } + + //////////////////////////////////////////////// + // Network Interfaces + //////////////////////////////////////////////// + + // Thanks for Theo Beisch (10/27/04) + + private final static boolean isUsableAddress(InetAddress addr) + { + if (USE_LOOPBACK_ADDR == false) { + if (addr.isLoopbackAddress() == true) + return false; + } + if (USE_ONLY_IPV4_ADDR == true) { + if (addr instanceof Inet6Address) + return false; + } + if (USE_ONLY_IPV6_ADDR == true) { + if (addr instanceof Inet4Address) + return false; + } + return true; + } + + public final static int getNHostAddresses() + { + if (hasAssignedInterface() == true) + return 1; + + int nHostAddrs = 0; + try { + Enumeration nis = NetworkInterface.getNetworkInterfaces(); + while (nis.hasMoreElements()){ + NetworkInterface ni = (NetworkInterface)nis.nextElement(); + Enumeration addrs = ni.getInetAddresses(); + while (addrs.hasMoreElements()) { + InetAddress addr = (InetAddress)addrs.nextElement(); + if (isUsableAddress(addr) == false) + continue; + nHostAddrs++; + } + } + } + catch(Exception e){}; + return nHostAddrs; + } + + public final static String getHostAddress(int n) + { + if (hasAssignedInterface() == true) + return getInterface(); + + int hostAddrCnt = 0; + try { + Enumeration nis = NetworkInterface.getNetworkInterfaces(); + while (nis.hasMoreElements()){ + NetworkInterface ni = (NetworkInterface)nis.nextElement(); + Enumeration addrs = ni.getInetAddresses(); + while (addrs.hasMoreElements()) { + InetAddress addr = (InetAddress)addrs.nextElement(); + if (isUsableAddress(addr) == false) + continue; + if (hostAddrCnt < n) { + hostAddrCnt++; + continue; + } + String host = addr.getHostAddress(); + //if (addr instanceof Inet6Address) + // host = "[" + host + "]"; + return host; + } + } + } + catch(Exception e){}; + return ""; + } + + //////////////////////////////////////////////// + // isIPv?Address + //////////////////////////////////////////////// + + public final static boolean isIPv6Address(String host) + { + try { + InetAddress addr = InetAddress.getByName(host); + return (addr instanceof Inet6Address); + } + catch (Exception e) {} + return false; + } + + public final static boolean isIPv4Address(String host) + { + try { + InetAddress addr = InetAddress.getByName(host); + return (addr instanceof Inet4Address); + } + catch (Exception e) {} + return false; + } + + //////////////////////////////////////////////// + // hasIPv?Interfaces + //////////////////////////////////////////////// + + public final static boolean hasIPv4Addresses() + { + int addrCnt = getNHostAddresses(); + for (int n=0; n"; + + //////////////////////////////////////////////// + // createEnvelopeBodyNode + //////////////////////////////////////////////// + + public final static Node createEnvelopeBodyNode() + { + // + Node envNode = new Node(SOAP.XMLNS + SOAP.DELIM + SOAP.ENVELOPE); + envNode.setAttribute("xmlns" + SOAP.DELIM + SOAP.XMLNS, SOAP.XMLNS_URL); + envNode.setAttribute(SOAP.XMLNS + SOAP.DELIM + "encodingStyle", SOAP.ENCSTYLE_URL); + + // + Node bodyNode = new Node(SOAP.XMLNS + SOAP.DELIM + SOAP.BODY); + envNode.addNode(bodyNode); + + return envNode; + } + + //////////////////////////////////////////////// + // XML Parser + //////////////////////////////////////////////// + + private static Parser xmlParser; + + public final static void setXMLParser(Parser parser) + { + xmlParser = parser; + } + + public final static Parser getXMLParser() + { + return xmlParser; + } +} + diff --git a/router/java/src/org/cybergarage/soap/SOAPRequest.java b/router/java/src/org/cybergarage/soap/SOAPRequest.java new file mode 100644 index 000000000..d07b12d68 --- /dev/null +++ b/router/java/src/org/cybergarage/soap/SOAPRequest.java @@ -0,0 +1,183 @@ +/****************************************************************** +* +* CyberSOAP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SOAPRequest.java +* +* Revision; +* +* 12/11/02 +* - first revision. +* 02/13/04 +* - Ralf G. R. Bergs , Inma Marin Lopez . +* - Added XML header, to setContent(). +* 05/11/04 +* - Changed the XML header to in setContent(). +* +******************************************************************/ + +package org.cybergarage.soap; + +import java.io.*; + +import org.cybergarage.http.*; +import org.cybergarage.xml.*; +import org.cybergarage.util.*; + +public class SOAPRequest extends HTTPRequest +{ + private final static String SOAPACTION = "SOAPACTION"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SOAPRequest() + { + setContentType(SOAP.CONTENT_TYPE); + setMethod(HTTP.POST); + } + + public SOAPRequest(HTTPRequest httpReq) + { + set(httpReq); + } + + //////////////////////////////////////////////// + // SOAPACTION + //////////////////////////////////////////////// + + public void setSOAPAction(String action) + { + setStringHeader(SOAPACTION, action); + } + + public String getSOAPAction() + { + return getStringHeaderValue(SOAPACTION); + } + + public boolean isSOAPAction(String value) + { + String headerValue = getHeaderValue(SOAPACTION); + if (headerValue == null) + return false; + if (headerValue.equals(value) == true) + return true; + String soapAction = getSOAPAction(); + if (soapAction == null) + return false; + return soapAction.equals(value); + } + + //////////////////////////////////////////////// + // post + //////////////////////////////////////////////// + + public SOAPResponse postMessage(String host, int port) + { + HTTPResponse httpRes = post(host, port); + + SOAPResponse soapRes = new SOAPResponse(httpRes); + + byte content[] = soapRes.getContent(); + if (content.length <= 0) + return soapRes; + + try { + ByteArrayInputStream byteIn = new ByteArrayInputStream(content); + Parser xmlParser = SOAP.getXMLParser(); + Node rootNode = xmlParser.parse(byteIn); + soapRes.setEnvelopeNode(rootNode); + } + catch (Exception e) { + Debug.warning(e); + } + + return soapRes; + } + + //////////////////////////////////////////////// + // Node + //////////////////////////////////////////////// + + private Node rootNode; + + private void setRootNode(Node node) + { + rootNode = node; + } + + private synchronized Node getRootNode() + { + if (rootNode != null) + return rootNode; + + try { + byte content[] = getContent(); + ByteArrayInputStream contentIn = new ByteArrayInputStream(content); + Parser parser = SOAP.getXMLParser(); + rootNode = parser.parse(contentIn); + } + catch (ParserException e) { + Debug.warning(e); + } + + return rootNode; + } + + //////////////////////////////////////////////// + // XML + //////////////////////////////////////////////// + + public void setEnvelopeNode(Node node) + { + setRootNode(node); + } + + public Node getEnvelopeNode() + { + return getRootNode(); + } + + public Node getBodyNode() + { + Node envNode = getEnvelopeNode(); + if (envNode == null) + return null; + if (envNode.hasNodes() == false) + return null; + return envNode.getNode(0); + } + + //////////////////////////////////////////////// + // XML Contents + //////////////////////////////////////////////// + + public void setContent(Node node) + { + // Thanks for Ralf G. R. Bergs , Inma Marin Lopez . + String conStr = ""; + conStr += SOAP.VERSION_HEADER; + conStr += "\n"; + conStr += node.toString(); + setContent(conStr); + } + + //////////////////////////////////////////////// + // print + //////////////////////////////////////////////// + + public void print() + { + System.out.println(toString()); + if (hasContent() == true) + return; + Node rootElem = getRootNode(); + if (rootElem == null) + return; + System.out.println(rootElem.toString()); + } +} diff --git a/router/java/src/org/cybergarage/soap/SOAPResponse.java b/router/java/src/org/cybergarage/soap/SOAPResponse.java new file mode 100644 index 000000000..fb59f3a21 --- /dev/null +++ b/router/java/src/org/cybergarage/soap/SOAPResponse.java @@ -0,0 +1,191 @@ +/****************************************************************** +* +* CyberSOAP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SOAPResponse.java +* +* Revision; +* +* 12/17/02 +* - first revision. +* 02/13/04 +* - Ralf G. R. Bergs , Inma Marin Lopez . +* - Added XML header, to setContent(). +* 05/11/04 +* - Changed the XML header to in setContent(). +* +******************************************************************/ + +package org.cybergarage.soap; + +import org.cybergarage.http.*; +import org.cybergarage.xml.*; + +public class SOAPResponse extends HTTPResponse +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SOAPResponse() + { + setRootNode(SOAP.createEnvelopeBodyNode()); + setContentType(XML.CONTENT_TYPE); + } + + public SOAPResponse(HTTPResponse httpRes) + { + super(httpRes); + setRootNode(SOAP.createEnvelopeBodyNode()); + setContentType(XML.CONTENT_TYPE); + } + + public SOAPResponse(SOAPResponse soapRes) + { + super(soapRes); + setEnvelopeNode(soapRes.getEnvelopeNode()); + setContentType(XML.CONTENT_TYPE); + } + + //////////////////////////////////////////////// + // Node + //////////////////////////////////////////////// + + private Node rootNode; + + private void setRootNode(Node node) + { + rootNode = node; + } + + private Node getRootNode() + { + return rootNode; + } + + //////////////////////////////////////////////// + // SOAP Basic + //////////////////////////////////////////////// + + public void setEnvelopeNode(Node node) + { + setRootNode(node); + } + + public Node getEnvelopeNode() + { + return getRootNode(); + } + + public Node getBodyNode() + { + Node envNode = getEnvelopeNode(); + if (envNode == null) + return null; + return envNode.getNodeEndsWith(SOAP.BODY); + } + + public Node getMethodResponseNode(String name) + { + Node bodyNode = getBodyNode(); + if (bodyNode == null) + return null; + String methodResName = name + SOAP.RESPONSE; + return bodyNode.getNodeEndsWith(methodResName); + } + + public Node getFaultNode() + { + Node bodyNode = getBodyNode(); + if (bodyNode == null) + return null; + return bodyNode.getNodeEndsWith(SOAP.FAULT); + } + + public Node getFaultCodeNode() + { + Node faultNode = getFaultNode(); + if (faultNode == null) + return null; + return faultNode.getNodeEndsWith(SOAP.FAULT_CODE); + } + + public Node getFaultStringNode() + { + Node faultNode = getFaultNode(); + if (faultNode == null) + return null; + return faultNode.getNodeEndsWith(SOAP.FAULT_STRING); + } + + public Node getFaultActorNode() + { + Node faultNode = getFaultNode(); + if (faultNode == null) + return null; + return faultNode.getNodeEndsWith(SOAP.FAULTACTOR); + } + + public Node getFaultDetailNode() + { + Node faultNode = getFaultNode(); + if (faultNode == null) + return null; + return faultNode.getNodeEndsWith(SOAP.DETAIL); + } + + public String getFaultCode() + { + Node node = getFaultCodeNode(); + if (node == null) + return ""; + return node.getValue(); + } + + public String getFaultString() + { + Node node = getFaultStringNode(); + if (node == null) + return ""; + return node.getValue(); + } + + public String getFaultActor() + { + Node node = getFaultActorNode(); + if (node == null) + return ""; + return node.getValue(); + } + + //////////////////////////////////////////////// + // XML Contents + //////////////////////////////////////////////// + + public void setContent(Node node) + { + // Thanks for Ralf G. R. Bergs , Inma Marin Lopez . + String conStr = ""; + conStr += SOAP.VERSION_HEADER; + conStr += "\n"; + conStr += node.toString(); + setContent(conStr); + } + + //////////////////////////////////////////////// + // print + //////////////////////////////////////////////// + + public void print() + { + System.out.println(toString()); + if (hasContent() == true) + return; + Node rootElem = getRootNode(); + if (rootElem == null) + return; + System.out.println(rootElem.toString()); + } +} diff --git a/router/java/src/org/cybergarage/upnp/Action.java b/router/java/src/org/cybergarage/upnp/Action.java new file mode 100644 index 000000000..9cf98a44d --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/Action.java @@ -0,0 +1,370 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: Action.java +* +* Revision; +* +* 12/05/02 +* - first revision. +* 08/30/03 +* - Gordano Sassaroli +* - Problem : When invoking an action that has at least one out parameter, an error message is returned +* - Error : The action post method gets the entire list of arguments instead of only the in arguments +* 01/04/04 +* - Added UPnP status methods. +* - Changed about new ActionListener interface. +* 01/05/04 +* - Added clearOutputAgumentValues() to initialize the output values before calling performActionListener(). +* 07/09/04 +* - Thanks for Dimas and Stefano Lenzi +* - Changed postControlAction() to set the status code to the UPnPStatus. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import org.cybergarage.xml.*; +import org.cybergarage.util.*; + +import org.cybergarage.upnp.xml.*; +import org.cybergarage.upnp.control.*; + +public class Action +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "action"; + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private Node serviceNode; + private Node actionNode; + + private Node getServiceNode() + { + return serviceNode; + } + + public Service getService() + { + return new Service(getServiceNode()); + } + + public Node getActionNode() + { + return actionNode; + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public Action(Node serviceNode, Node actionNode) + { + this.serviceNode = serviceNode; + this.actionNode = actionNode; + } + + public Action(Action action) + { + this.serviceNode = action.getServiceNode(); + this.actionNode = action.getActionNode(); + } + + //////////////////////////////////////////////// + // Mutex + //////////////////////////////////////////////// + + private Mutex mutex = new Mutex(); + + public void lock() + { + mutex.lock(); + } + + public void unlock() + { + mutex.unlock(); + } + + //////////////////////////////////////////////// + // isActionNode + //////////////////////////////////////////////// + + public static boolean isActionNode(Node node) + { + return Action.ELEM_NAME.equals(node.getName()); + } + + //////////////////////////////////////////////// + // name + //////////////////////////////////////////////// + + private final static String NAME = "name"; + + public void setName(String value) + { + getActionNode().setNode(NAME, value); + } + + public String getName() + { + return getActionNode().getNodeValue(NAME); + } + + //////////////////////////////////////////////// + // argumentList + //////////////////////////////////////////////// + + public ArgumentList getArgumentList() + { + ArgumentList argumentList = new ArgumentList(); + Node argumentListNode = getActionNode().getNode(ArgumentList.ELEM_NAME); + if (argumentListNode == null) + return argumentList; + int nodeCnt = argumentListNode.getNNodes(); + for (int n=0; n (08/30/03) + ArgumentList actionArgList = getArgumentList(); + ArgumentList actionInputArgList = getInputArgumentList(); + ActionRequest ctrlReq = new ActionRequest(); + ctrlReq.setRequest(this, actionInputArgList); + if (Debug.isOn() == true) + ctrlReq.print(); + ActionResponse ctrlRes = ctrlReq.post(); + if (Debug.isOn() == true) + ctrlRes.print(); + setControlResponse(ctrlRes); + // Thanks for Dimas and Stefano Lenzi (07/09/04) + int statCode = ctrlRes.getStatusCode(); + setStatus(statCode); + if (ctrlRes.isSuccessful() == false) + return false; + ArgumentList outArgList = ctrlRes.getResponse(); + actionArgList.set(outArgList); + return true; + } + + //////////////////////////////////////////////// + // Debug + //////////////////////////////////////////////// + + public void print() + { + System.out.println("Action : " + getName()); + ArgumentList argList = getArgumentList(); + int nArgs = argList.size(); + for (int n=0; n +* - Problem : when an event notification message is received and the message +* contains updates on more than one variable, only the first variable update +* is notified. +* - Error : the other xml nodes of the message are ignored +* - Fix : add two methods to the NotifyRequest for extracting the property array +* and modify the httpRequestRecieved method in ControlPoint +* 12/12/03 +* - Added a static() to initialize UPnP class. +* 01/06/04 +* - Added the following methods to remove expired devices automatically +* removeExpiredDevices() +* setExpiredDeviceMonitoringInterval()/getExpiredDeviceMonitoringInterval() +* setDeviceDisposer()/getDeviceDisposer() +* 04/20/04 +* - Added the following methods. +* start(String target, int mx) and start(String target). +* 06/23/04 +* - Added setNMPRMode() and isNMPRMode(). +* 07/08/04 +* - Added renewSubscriberService(). +* - Changed start() to create renew subscriber thread when the NMPR mode is true. +* 08/17/04 +* - Fixed removeExpiredDevices() to remove using the device array. +* 10/16/04 +* - Oliver Newell +* - Added this class to allow ControlPoint applications to be notified when +* the ControlPoint base class adds/removes a UPnP device +* 03/30/05 +* - Changed addDevice() to use Parser::parse(URL). +* +*******************************************************************/ + +package org.cybergarage.upnp; + +import org.cybergarage.net.*; +import org.cybergarage.util.*; +import org.cybergarage.xml.*; +import org.cybergarage.http.*; + +import org.cybergarage.upnp.control.*; +import org.cybergarage.upnp.ssdp.*; +import org.cybergarage.upnp.device.*; +import org.cybergarage.upnp.event.*; + +import java.net.*; + +public class ControlPoint implements HTTPRequestListener +{ + private final static int DEFAULT_EVENTSUB_PORT = 8058; + private final static int DEFAULT_SSDP_PORT = 8008; + private final static int DEFAULT_EXPIRED_DEVICE_MONITORING_INTERVAL = 60; + + private final static String DEFAULT_EVENTSUB_URI = "/evetSub"; + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private SSDPNotifySocketList ssdpNotifySocketList; + private SSDPSearchResponseSocketList ssdpSearchResponseSocketList; + + private SSDPNotifySocketList getSSDPNotifySocketList() + { + return ssdpNotifySocketList; + } + + private SSDPSearchResponseSocketList getSSDPSearchResponseSocketList() + { + return ssdpSearchResponseSocketList; + } + + //////////////////////////////////////////////// + // Initialize + //////////////////////////////////////////////// + + static + { + UPnP.initialize(); + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public ControlPoint(int ssdpPort, int httpPort) + { + ssdpNotifySocketList = new SSDPNotifySocketList(); + ssdpSearchResponseSocketList = new SSDPSearchResponseSocketList(); + + setSSDPPort(ssdpPort); + setHTTPPort(httpPort); + + setDeviceDisposer(null); + setExpiredDeviceMonitoringInterval(DEFAULT_EXPIRED_DEVICE_MONITORING_INTERVAL); + + setRenewSubscriber(null); + + setNMPRMode(false); + setRenewSubscriber(null); + } + + public ControlPoint() + { + this(DEFAULT_SSDP_PORT, DEFAULT_EVENTSUB_PORT); + } + + public void finalize() + { + stop(); + } + + //////////////////////////////////////////////// + // Mutex + //////////////////////////////////////////////// + + private Mutex mutex = new Mutex(); + + public void lock() + { + mutex.lock(); + } + + public void unlock() + { + mutex.unlock(); + } + + //////////////////////////////////////////////// + // Port (SSDP) + //////////////////////////////////////////////// + + private int ssdpPort = 0; + + public int getSSDPPort() { + return ssdpPort; + } + + public void setSSDPPort(int port) { + ssdpPort = port; + } + + //////////////////////////////////////////////// + // Port (EventSub) + //////////////////////////////////////////////// + + private int httpPort = 0; + + public int getHTTPPort() { + return httpPort; + } + + public void setHTTPPort(int port) { + httpPort = port; + } + + //////////////////////////////////////////////// + // NMPR + //////////////////////////////////////////////// + + private boolean nmprMode; + + public void setNMPRMode(boolean flag) + { + nmprMode = flag; + } + + public boolean isNMPRMode() + { + return nmprMode; + } + + //////////////////////////////////////////////// + // Device List + //////////////////////////////////////////////// + + private NodeList devNodeList = new NodeList(); + + private void addDevice(Node rootNode) + { + devNodeList.add(rootNode); + } + + private synchronized void addDevice(SSDPPacket ssdpPacket) + { + if (ssdpPacket.isRootDevice() == false) + return; + + String usn = ssdpPacket.getUSN(); + String udn = USN.getUDN(usn); + Device dev = getDevice(udn); + if (dev != null) { + dev.setSSDPPacket(ssdpPacket); + return; + } + + String location = ssdpPacket.getLocation(); + try { + URL locationUrl = new URL(location); + Parser parser = UPnP.getXMLParser(); + Node rootNode = parser.parse(locationUrl); + Device rootDev = getDevice(rootNode); + if (rootDev == null) + return; + rootDev.setSSDPPacket(ssdpPacket); + addDevice(rootNode); + + // Thanks for Oliver Newell (2004/10/16) + // After node is added, invoke the AddDeviceListener to notify high-level + // control point application that a new device has been added. (The + // control point application must implement the DeviceChangeListener interface + // to receive the notifications) + performAddDeviceListener( rootDev ); + } + catch (MalformedURLException me) { + Debug.warning(ssdpPacket.toString()); + Debug.warning(me); + } + catch (ParserException pe) { + Debug.warning(ssdpPacket.toString()); + Debug.warning(pe); + } + } + + private Device getDevice(Node rootNode) + { + if (rootNode == null) + return null; + Node devNode = rootNode.getNode(Device.ELEM_NAME); + if (devNode == null) + return null; + return new Device(rootNode, devNode); + } + + public DeviceList getDeviceList() + { + DeviceList devList = new DeviceList(); + int nRoots = devNodeList.size(); + for (int n=0; n +* - Problem : bad request response sent even with successful subscriptions +* - Error : a return statement is missing in the httpRequestRecieved method +* 10/21/03 +* - Updated a udn field by a original uuid. +* 10/22/03 +* - Added setActionListener(). +* - Added setQueryListener(). +* 12/12/03 +* - Added a static() to initialize UPnP class. +* 12/25/03 +* - Added advertiser functions. +* 01/05/04 +* - Added isExpired(). +* 03/23/04 +* - Oliver Newell +* - Changed to update the UDN only when the field is null. +* 04/21/04 +* - Added isDeviceType(). +* 06/18/04 +* - Added setNMPRMode() and isNMPRMode(). +* - Changed getDescriptionData() to update only when the NMPR mode is false. +* 06/21/04 +* - Changed start() to send a bye-bye before the announce. +* - Changed annouce(), byebye() and deviceSearchReceived() to send the SSDP +* messsage four times when the NMPR and the Wireless mode are true. +* 07/02/04 +* - Fixed announce() and byebye() to send the upnp::rootdevice message despite embedded devices. +* - Fixed getRootNode() to return the root node when the device is embedded. +* 07/24/04 +* - Thanks for Stefano Lenzi +* - Added getParentDevice(). +* 10/20/04 +* - Brent Hills +* - Changed postSearchResponse() to add MYNAME header. +* 11/19/04 +* - Theo Beisch +* - Added getStateVariable(String serviceType, String name). +* 03/22/05 +* - Changed httpPostRequestRecieved() to return the bad request when the post request isn't the soap action. +* 03/23/05 +* - Added loadDescription(String) to load the description from memory. +* 03/30/05 +* - Added getDeviceByDescriptionURI(). +* - Added getServiceBySCPDURL(). +* 03/31/05 +* - Changed httpGetRequestRecieved() to return the description stream using +* Device::getDescriptionData() and Service::getSCPDData() at first. +* 04/25/05 +* - Thanks for Mikael Hakman +* Changed announce() and byebye() to close the socket after the posting. +* 04/25/05 +* - Thanks for Mikael Hakman +* Changed deviceSearchResponse() answer with USN:UDN:: when request ST is device type. +* 04/25/05 +* - Thanks for Mikael Hakman +* - Changed getDescriptionData() to add a XML declaration at first line. +* 04/25/05 +* - Thanks for Mikael Hakman +* - Added a new setActionListener() and serQueryListner() to include the sub devices. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import java.net.*; +import java.io.*; +import java.util.*; + +import org.cybergarage.net.*; +import org.cybergarage.http.*; +import org.cybergarage.util.*; +import org.cybergarage.xml.*; +import org.cybergarage.soap.*; + +import org.cybergarage.upnp.ssdp.*; +import org.cybergarage.upnp.device.*; +import org.cybergarage.upnp.control.*; +import org.cybergarage.upnp.event.*; +import org.cybergarage.upnp.xml.*; + +public class Device implements org.cybergarage.http.HTTPRequestListener, SearchListener +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "device"; + public final static String UPNP_ROOTDEVICE = "upnp:rootdevice"; + + public final static int DEFAULT_STARTUP_WAIT_TIME = 1000; + public final static int DEFAULT_DISCOVERY_WAIT_TIME = 300; + public final static int DEFAULT_LEASE_TIME = 30 * 60; + + public final static int HTTP_DEFAULT_PORT = 4004; + + public final static String DEFAULT_DESCRIPTION_URI = "/description.xml"; + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private Node rootNode; + private Node deviceNode; + + public Node getRootNode() + { + if (rootNode != null) + return rootNode; + if (deviceNode == null) + return null; + return deviceNode.getRootNode(); + } + + public Node getDeviceNode() + { + return deviceNode; + } + + public void setRootNode(Node node) + { + rootNode = node; + } + + public void setDeviceNode(Node node) + { + deviceNode = node; + } + + //////////////////////////////////////////////// + // Initialize + //////////////////////////////////////////////// + + static + { + UPnP.initialize(); + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public Device(Node root, Node device) + { + rootNode = root; + deviceNode = device; + setUUID(UPnP.createUUID()); + setWirelessMode(false); + } + + public Device() + { + this(null, null); + } + + public Device(Node device) + { + this(null, device); + } + + public Device(File descriptionFile) throws InvalidDescriptionException + { + this(null, null); + loadDescription(descriptionFile); + } + + public Device(String descriptionFileName) throws InvalidDescriptionException + { + this(new File(descriptionFileName)); + } + + //////////////////////////////////////////////// + // Mutex + //////////////////////////////////////////////// + + private Mutex mutex = new Mutex(); + + public void lock() + { + mutex.lock(); + } + + public void unlock() + { + mutex.unlock(); + } + + //////////////////////////////////////////////// + // NMPR + //////////////////////////////////////////////// + + public void setNMPRMode(boolean flag) + { + Node devNode = getDeviceNode(); + if (devNode == null) + return; + if (flag == true) { + devNode.setNode(UPnP.INMPR03, UPnP.INMPR03_VERSION); + devNode.removeNode(Device.URLBASE_NAME); + } + else { + devNode.removeNode(UPnP.INMPR03); + } + } + + public boolean isNMPRMode() + { + Node devNode = getDeviceNode(); + if (devNode == null) + return false; + return (devNode.getNode(UPnP.INMPR03) != null) ? true : false; + } + + //////////////////////////////////////////////// + // Wireless + //////////////////////////////////////////////// + + private boolean wirelessMode; + + public void setWirelessMode(boolean flag) + { + wirelessMode = flag; + } + + public boolean isWirelessMode() + { + return wirelessMode; + } + + public int getSSDPAnnounceCount() + { + if (isNMPRMode() == true && isWirelessMode() == true) + return UPnP.INMPR03_DISCOVERY_OVER_WIRELESS_COUNT; + return 1; + } + + //////////////////////////////////////////////// + // Device UUID + //////////////////////////////////////////////// + + private String devUUID; + + private void setUUID(String uuid) + { + devUUID = uuid; + } + + private String getUUID() + { + return devUUID; + } + + private void updateUDN() + { + setUDN("uuid:" + getUUID()); + } + + //////////////////////////////////////////////// + // Root Device + //////////////////////////////////////////////// + + public Device getRootDevice() + { + Node rootNode = getRootNode(); + if (rootNode == null) + return null; + Node devNode = rootNode.getNode(Device.ELEM_NAME); + if (devNode == null) + return null; + return new Device(rootNode, devNode); + } + + //////////////////////////////////////////////// + // Parent Device + //////////////////////////////////////////////// + + // Thanks for Stefano Lenzi (07/24/04) + + public Device getParentDevice() + { + if(isRootDevice()) + return null; + Node devNode = getDeviceNode(); + // + devNode = devNode.getParentNode().getParentNode().getNode(Device.ELEM_NAME); + return new Device(devNode); + } + + //////////////////////////////////////////////// + // UserData + //////////////////////////////////////////////// + + private DeviceData getDeviceData() + { + Node node = getDeviceNode(); + DeviceData userData = (DeviceData)node.getUserData(); + if (userData == null) { + userData = new DeviceData(); + node.setUserData(userData); + userData.setNode(node); + } + return userData; + } + + //////////////////////////////////////////////// + // Description + //////////////////////////////////////////////// + + private void setDescriptionFile(File file) + { + getDeviceData().setDescriptionFile(file); + } + + public File getDescriptionFile() + { + return getDeviceData().getDescriptionFile(); + } + + private void setDescriptionURI(String uri) + { + getDeviceData().setDescriptionURI(uri); + } + + private String getDescriptionURI() + { + return getDeviceData().getDescriptionURI(); + } + + private boolean isDescriptionURI(String uri) + { + String descriptionURI = getDescriptionURI(); + if (uri == null || descriptionURI == null) + return false; + return descriptionURI.equals(uri); + } + + public String getDescriptionFilePath() + { + File descriptionFile = getDescriptionFile(); + if (descriptionFile == null) + return ""; + return descriptionFile.getAbsoluteFile().getParent(); + } + + public boolean loadDescription(String descString) throws InvalidDescriptionException + { + try { + Parser parser = UPnP.getXMLParser(); + rootNode = parser.parse(descString); + if (rootNode == null) + throw new InvalidDescriptionException(Description.NOROOT_EXCEPTION); + deviceNode = rootNode.getNode(Device.ELEM_NAME); + if (deviceNode == null) + throw new InvalidDescriptionException(Description.NOROOTDEVICE_EXCEPTION); + } + catch (ParserException e) { + throw new InvalidDescriptionException(e); + } + + if (initializeLoadedDescription() == false) + return false; + + setDescriptionFile(null); + + return true; + } + + public boolean loadDescription(File file) throws InvalidDescriptionException + { + try { + Parser parser = UPnP.getXMLParser(); + rootNode = parser.parse(file); + if (rootNode == null) + throw new InvalidDescriptionException(Description.NOROOT_EXCEPTION, file); + deviceNode = rootNode.getNode(Device.ELEM_NAME); + if (deviceNode == null) + throw new InvalidDescriptionException(Description.NOROOTDEVICE_EXCEPTION, file); + } + catch (ParserException e) { + throw new InvalidDescriptionException(e); + } + + if (initializeLoadedDescription() == false) + return false; + + setDescriptionFile(file); + + return true; + } + + private boolean initializeLoadedDescription() + { + setDescriptionURI(DEFAULT_DESCRIPTION_URI); + setLeaseTime(DEFAULT_LEASE_TIME); + setHTTPPort(HTTP_DEFAULT_PORT); + + // Thanks for Oliver Newell (03/23/04) + if (hasUDN() == false) + updateUDN(); + + return true; + } + + //////////////////////////////////////////////// + // isDeviceNode + //////////////////////////////////////////////// + + public static boolean isDeviceNode(Node node) + { + return Device.ELEM_NAME.equals(node.getName()); + } + + //////////////////////////////////////////////// + // Root Device + //////////////////////////////////////////////// + + public boolean isRootDevice() + { + return (getRootNode() != null) ? true : false; + } + + //////////////////////////////////////////////// + // Root Device + //////////////////////////////////////////////// + + public void setSSDPPacket(SSDPPacket packet) + { + getDeviceData().setSSDPPacket(packet); + } + + public SSDPPacket getSSDPPacket() + { + if (isRootDevice() == false) + return null; + return getDeviceData().getSSDPPacket(); + } + + //////////////////////////////////////////////// + // Location + //////////////////////////////////////////////// + + public void setLocation(String value) + { + getDeviceData().setLocation(value); + } + + public String getLocation() + { + SSDPPacket packet = getSSDPPacket(); + if (packet != null) + return packet.getLocation(); + return getDeviceData().getLocation(); + } + + //////////////////////////////////////////////// + // LeaseTime + //////////////////////////////////////////////// + + public void setLeaseTime(int value) + { + getDeviceData().setLeaseTime(value); + Advertiser adv = getAdvertiser(); + if (adv != null) { + announce(); + adv.restart(); + } + } + + public int getLeaseTime() + { + SSDPPacket packet = getSSDPPacket(); + if (packet != null) + return packet.getLeaseTime(); + return getDeviceData().getLeaseTime(); + } + + //////////////////////////////////////////////// + // TimeStamp + //////////////////////////////////////////////// + + public long getTimeStamp() + { + SSDPPacket packet = getSSDPPacket(); + if (packet != null) + return packet.getTimeStamp(); + return 0; + } + + public long getElapsedTime() + { + return (System.currentTimeMillis() - getTimeStamp()) / 1000; + } + + public boolean isExpired() + { + long elipsedTime = getElapsedTime(); + long leaseTime = getLeaseTime() + UPnP.DEFAULT_EXPIRED_DEVICE_EXTRA_TIME; + if (leaseTime < elipsedTime) + return true; + return false; + } + + //////////////////////////////////////////////// + // URL Base + //////////////////////////////////////////////// + + private final static String URLBASE_NAME = "URLBase"; + + private void setURLBase(String value) + { + if (isRootDevice() == true) { + Node node = getRootNode().getNode(URLBASE_NAME); + if (node != null) { + node.setValue(value); + return; + } + node = new Node(URLBASE_NAME); + node.setValue(value); + int index = 1; + if (getRootNode().hasNodes() == false) + index = 1; + getRootNode().insertNode(node, index); + } + } + + private void updateURLBase(String host) + { + String urlBase = HostInterface.getHostURL(host, getHTTPPort(), ""); + setURLBase(urlBase); + } + + public String getURLBase() + { + if (isRootDevice() == true) + return getRootNode().getNodeValue(URLBASE_NAME); + return ""; + } + + //////////////////////////////////////////////// + // deviceType + //////////////////////////////////////////////// + + private final static String DEVICE_TYPE = "deviceType"; + + public void setDeviceType(String value) + { + getDeviceNode().setNode(DEVICE_TYPE, value); + } + + public String getDeviceType() + { + return getDeviceNode().getNodeValue(DEVICE_TYPE); + } + + public boolean isDeviceType(String value) + { + if (value == null) + return false; + return value.equals(getDeviceType()); + } + + //////////////////////////////////////////////// + // friendlyName + //////////////////////////////////////////////// + + private final static String FRIENDLY_NAME = "friendlyName"; + + public void setFriendlyName(String value) + { + getDeviceNode().setNode(FRIENDLY_NAME, value); + } + + public String getFriendlyName() + { + return getDeviceNode().getNodeValue(FRIENDLY_NAME); + } + + //////////////////////////////////////////////// + // manufacture + //////////////////////////////////////////////// + + private final static String MANUFACTURE = "manufacture"; + + public void setManufacture(String value) + { + getDeviceNode().setNode(MANUFACTURE, value); + } + + public String getManufacture() + { + return getDeviceNode().getNodeValue(MANUFACTURE); + } + + //////////////////////////////////////////////// + // manufactureURL + //////////////////////////////////////////////// + + private final static String MANUFACTURE_URL = "manufactureURL"; + + public void setManufactureURL(String value) + { + getDeviceNode().setNode(MANUFACTURE_URL, value); + } + + public String getManufactureURL() + { + return getDeviceNode().getNodeValue(MANUFACTURE_URL); + } + + //////////////////////////////////////////////// + // modelDescription + //////////////////////////////////////////////// + + private final static String MODEL_DESCRIPTION = "modelDescription"; + + public void setModelDescription(String value) + { + getDeviceNode().setNode(MODEL_DESCRIPTION, value); + } + + public String getModelDescription() + { + return getDeviceNode().getNodeValue(MODEL_DESCRIPTION); + } + + //////////////////////////////////////////////// + // modelName + //////////////////////////////////////////////// + + private final static String MODEL_NAME = "modelName"; + + public void setModelName(String value) + { + getDeviceNode().setNode(MODEL_NAME, value); + } + + public String getModelName() + { + return getDeviceNode().getNodeValue(MODEL_NAME); + } + + //////////////////////////////////////////////// + // modelNumber + //////////////////////////////////////////////// + + private final static String MODEL_NUMBER = "modelNumber"; + + public void setModelNumber(String value) + { + getDeviceNode().setNode(MODEL_NUMBER, value); + } + + public String getModelNumber() + { + return getDeviceNode().getNodeValue(MODEL_NUMBER); + } + + //////////////////////////////////////////////// + // modelURL + //////////////////////////////////////////////// + + private final static String MODEL_URL = "modelURL"; + + public void setModelURL(String value) + { + getDeviceNode().setNode(MODEL_URL, value); + } + + public String getModelURL() + { + return getDeviceNode().getNodeValue(MODEL_URL); + } + + //////////////////////////////////////////////// + // serialNumber + //////////////////////////////////////////////// + + private final static String SERIAL_NUMBER = "serialNumber"; + + public void setSerialNumber(String value) + { + getDeviceNode().setNode(SERIAL_NUMBER, value); + } + + public String getSerialNumber() + { + return getDeviceNode().getNodeValue(SERIAL_NUMBER); + } + + //////////////////////////////////////////////// + // UDN + //////////////////////////////////////////////// + + private final static String UDN = "UDN"; + + public void setUDN(String value) + { + getDeviceNode().setNode(UDN, value); + } + + public String getUDN() + { + return getDeviceNode().getNodeValue(UDN); + } + + public boolean hasUDN() + { + String udn = getUDN(); + if (udn == null || udn.length() <= 0) + return false; + return true; + } + + //////////////////////////////////////////////// + // UPC + //////////////////////////////////////////////// + + private final static String UPC = "UPC"; + + public void setUPC(String value) + { + getDeviceNode().setNode(UPC, value); + } + + public String getUPC() + { + return getDeviceNode().getNodeValue(UPC); + } + + //////////////////////////////////////////////// + // presentationURL + //////////////////////////////////////////////// + + private final static String presentationURL = "presentationURL"; + + public void setPresentationURL(String value) + { + getDeviceNode().setNode(presentationURL, value); + } + + public String getPresentationURL() + { + return getDeviceNode().getNodeValue(presentationURL); + } + + //////////////////////////////////////////////// + // deviceList + //////////////////////////////////////////////// + + public DeviceList getDeviceList() + { + DeviceList devList = new DeviceList(); + Node devListNode = getDeviceNode().getNode(DeviceList.ELEM_NAME); + if (devListNode == null) + return devList; + int nNode = devListNode.getNNodes(); + for (int n=0; n +* - Fixed SERVICE_ID constant instead of "serviceId". +* 06/17/03 +* - Added notifyAllStateVariables(). +* 09/03/03 +* - Giordano Sassaroli +* - Problem : The device does not accepts request for services when control or subscription urls are absolute +* - Error : device methods, when requests are received, search for services that have a controlUrl (or eventSubUrl) equal to the request URI +* but request URI must be relative, so they cannot equal absolute urls +* 09/03/03 +* - Steven Yen +* - description: to retrieve service information based on information in URLBase and SCPDURL +* - problem: not able to retrieve service information when URLBase is missing and SCPDURL is relative +* - fix: modify to retrieve host information from Header's Location (required) field and update the +* BaseURL tag in the xml so subsequent information retrieval can be done (Steven Yen, 8.27.2003) +* - note: 1. in the case that Header's Location field combine with SCPDURL is not able to retrieve proper +* information, updating BaseURL would not hurt, since exception will be thrown with or without update. +* 2. this problem was discovered when using PC running MS win XP with ICS enabled (gateway). +* It seems that root device xml file does not have BaseURL and SCPDURL are all relative. +* 3. UPnP device architecture states that BaseURL is optional and SCPDURL may be relative as +* specified by UPnP vendor, so MS does not seem to violate the rule. +* 10/22/03 +* - Added setActionListener(). +* 01/04/04 +* - Changed about new QueryListener interface. +* 01/06/04 +* - Moved the following methods to StateVariable class. +* getQueryListener() +* setQueryListener() +* performQueryListener() +* - Added new setQueryListener() to set a listner to all state variables. +* 07/02/04 +* - Added serviceSearchResponse(). +* - Deleted getLocationURL(). +* - Fixed announce() to set the root device URL to the LOCATION field. +* 07/31/04 +* - Changed notify() to remove the expired subscribers and not to remove the invalid response subscribers for NMPR. +* 10/29/04 +* - Fixed a bug when notify() removes the expired devices(). +* 03/23/05 +* - Added loadSCPD() to load the description from memory. +* 03/30/05 +* - Added isSCPDURL(). +* - Removed setDescriptionURL() and getDescriptionURL() +* 03/31/05 +* - Added getSCPDData(). +* 04/25/05 +* - Thanks for Mikael Hakman +* - Changed getSCPDData() to add a XML declaration at first line. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import java.io.*; +import java.net.*; + +import org.cybergarage.http.*; +import org.cybergarage.xml.*; +import org.cybergarage.util.*; + +import org.cybergarage.upnp.ssdp.*; +import org.cybergarage.upnp.xml.*; +import org.cybergarage.upnp.device.*; +import org.cybergarage.upnp.control.*; +import org.cybergarage.upnp.event.*; + +public class Service +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "service"; + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private Node serviceNode; + + public Node getServiceNode() + { + return serviceNode; + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public Service(Node node) + { + serviceNode = node; + } + + //////////////////////////////////////////////// + // Mutex + //////////////////////////////////////////////// + + private Mutex mutex = new Mutex(); + + public void lock() + { + mutex.lock(); + } + + public void unlock() + { + mutex.unlock(); + } + + //////////////////////////////////////////////// + // isServiceNode + //////////////////////////////////////////////// + + public static boolean isServiceNode(Node node) + { + return Service.ELEM_NAME.equals(node.getName()); + } + + //////////////////////////////////////////////// + // Device/Root Node + //////////////////////////////////////////////// + + private Node getDeviceNode() + { + Node node = getServiceNode().getParentNode(); + if (node == null) + return null; + return node.getParentNode(); + } + + private Node getRootNode() + { + return getServiceNode().getRootNode(); + } + + //////////////////////////////////////////////// + // Device + //////////////////////////////////////////////// + + public Device getDevice() + { + return new Device(getRootNode(), getDeviceNode()); + } + + public Device getRootDevice() + { + return getDevice().getRootDevice(); + } + + //////////////////////////////////////////////// + // serviceType + //////////////////////////////////////////////// + + private final static String SERVICE_TYPE = "serviceType"; + + public void setServiceType(String value) + { + getServiceNode().setNode(SERVICE_TYPE, value); + } + + public String getServiceType() + { + return getServiceNode().getNodeValue(SERVICE_TYPE); + } + + //////////////////////////////////////////////// + // serviceID + //////////////////////////////////////////////// + + private final static String SERVICE_ID = "serviceId"; + + public void setServiceID(String value) + { + getServiceNode().setNode(SERVICE_ID, value); + } + + public String getServiceID() + { + return getServiceNode().getNodeValue(SERVICE_ID); + } + + //////////////////////////////////////////////// + // isURL + //////////////////////////////////////////////// + + // Thanks for Giordano Sassaroli (09/03/03) + private boolean isURL(String referenceUrl, String url) + { + if (referenceUrl ==null || url == null) + return false; + boolean ret = url.equals(referenceUrl); + if (ret == true) + return true; + String relativeRefUrl = HTTP.toRelativeURL(referenceUrl, false); + ret = url.equals(relativeRefUrl); + if (ret == true) + return true; + return false; + } + + //////////////////////////////////////////////// + // SCPDURL + //////////////////////////////////////////////// + + private final static String SCPDURL = "SCPDURL"; + + public void setSCPDURL(String value) + { + getServiceNode().setNode(SCPDURL, value); + } + + public String getSCPDURL() + { + return getServiceNode().getNodeValue(SCPDURL); + } + + public boolean isSCPDURL(String url) + { + return isURL(getSCPDURL(), url); + } + + //////////////////////////////////////////////// + // controlURL + //////////////////////////////////////////////// + + private final static String CONTROL_URL = "controlURL"; + + public void setControlURL(String value) + { + getServiceNode().setNode(CONTROL_URL, value); + } + + public String getControlURL() + { + return getServiceNode().getNodeValue(CONTROL_URL); + } + + public boolean isControlURL(String url) + { + return isURL(getControlURL(), url); + } + + //////////////////////////////////////////////// + // eventSubURL + //////////////////////////////////////////////// + + private final static String EVENT_SUB_URL = "eventSubURL"; + + public void setEventSubURL(String value) + { + getServiceNode().setNode(EVENT_SUB_URL, value); + } + + public String getEventSubURL() + { + return getServiceNode().getNodeValue(EVENT_SUB_URL); + } + + public boolean isEventSubURL(String url) + { + return isURL(getEventSubURL(), url); + } + + //////////////////////////////////////////////// + // SCPD node + //////////////////////////////////////////////// + + public boolean loadSCPD(String scpdStr) throws InvalidDescriptionException + { + try { + Parser parser = UPnP.getXMLParser(); + Node scpdNode = parser.parse(scpdStr); + if (scpdNode == null) + return false; + ServiceData data = getServiceData(); + data.setSCPDNode(scpdNode); + } + catch (ParserException e) { + throw new InvalidDescriptionException(e); + } + return true; + } + + public boolean loadSCPD(File file) throws ParserException + { + Parser parser = UPnP.getXMLParser(); + Node scpdNode = parser.parse(file); + if (scpdNode == null) + return false; + ServiceData data = getServiceData(); + data.setSCPDNode(scpdNode); + return true; + } + + private Node getSCPDNode(URL scpdUrl) throws ParserException + { + Parser parser = UPnP.getXMLParser(); + return parser.parse(scpdUrl); + } + + private Node getSCPDNode(File scpdFile) throws ParserException + { + Parser parser = UPnP.getXMLParser(); + return parser.parse(scpdFile); + } + + private Node getSCPDNode() + { + ServiceData data = getServiceData(); + Node scpdNode = data.getSCPDNode(); + if (scpdNode != null) + return scpdNode; + + String scpdURLStr = getSCPDURL(); + try { + URL scpdUrl = new URL(scpdURLStr); + scpdNode = getSCPDNode(scpdUrl); + } + catch (Exception e1) { + Device rootDev = getRootDevice(); + String urlBaseStr = rootDev.getURLBase(); + // Thanks for Steven Yen (2003/09/03) + if (urlBaseStr == null || urlBaseStr.length() <= 0) { + String location = rootDev.getLocation(); + String locationHost = HTTP.getHost(location); + int locationPort = HTTP.getPort(location); + urlBaseStr = HTTP.getRequestHostURL(locationHost, locationPort); + } + scpdURLStr = HTTP.toRelativeURL(scpdURLStr); + String newScpdURLStr = urlBaseStr + scpdURLStr; + try { + URL newScpdURL = new URL(newScpdURLStr); + scpdNode = getSCPDNode(newScpdURL); + } + catch (Exception e2) { + newScpdURLStr = HTTP.getAbsoluteURL(urlBaseStr, scpdURLStr); + try { + URL newScpdURL = new URL(newScpdURLStr); + scpdNode = getSCPDNode(newScpdURL); + } + catch (Exception e3) { + newScpdURLStr = rootDev.getDescriptionFilePath() + scpdURLStr; + try { + scpdNode = getSCPDNode(new File(newScpdURLStr)); + } + catch (Exception e4) { + Debug.warning(e4); + } + } + } + } + + data.setSCPDNode(scpdNode); + + return scpdNode; + } + + public byte[] getSCPDData() + { + Node scpdNode = getSCPDNode(); + if (scpdNode == null) + return new byte[0]; + // Thanks for Mikael Hakman (04/25/05) + String desc = new String(); + desc += UPnP.XML_DECLARATION; + desc += "\n"; + desc += scpdNode.toString(); + return desc.getBytes(); + } + + //////////////////////////////////////////////// + // actionList + //////////////////////////////////////////////// + + public ActionList getActionList() + { + ActionList actionList = new ActionList(); + Node scdpNode = getSCPDNode(); + if (scdpNode == null) + return actionList; + Node actionListNode = scdpNode.getNode(ActionList.ELEM_NAME); + if (actionListNode == null) + return actionList; + Node serviceNode = getServiceNode(); + int nNode = actionListNode.getNNodes(); + for (int n=0; n and Stefano Lenzi +* - Changed postQuerylAction() to set the status code to the UPnPStatus. +* 11/09/04 +* - Theo Beisch +* - Changed StateVariable::setValue() to update and send the event when the value is not equal to the current value. +* 11/19/04 +* - Rick Keiner +* - Fixed setValue() to compare only when the current value is not null. +* 02/28/05 +* - Changed getAllowedValueList() to use AllowedValue instead of String as the member. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import org.cybergarage.xml.*; +import org.cybergarage.util.*; + +import org.cybergarage.upnp.control.*; +import org.cybergarage.upnp.xml.*; + +public class StateVariable extends NodeData +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "stateVariable"; + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private Node stateVariableNode; + private Node serviceNode; + + public Node getServiceNode() + { + return serviceNode; + } + + public Service getService() + { + Node serviceNode = getServiceNode(); + if (serviceNode == null) + return null; + return new Service(serviceNode); + } + + public Node getStateVariableNode() + { + return stateVariableNode; + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public StateVariable() + { + this.serviceNode = null; + this.stateVariableNode = new Node(); + } + + public StateVariable(Node serviceNode, Node stateVarNode) + { + this.serviceNode = serviceNode; + this.stateVariableNode = stateVarNode; + } + + //////////////////////////////////////////////// + // isStateVariableNode + //////////////////////////////////////////////// + + public static boolean isStateVariableNode(Node node) + { + return StateVariable.ELEM_NAME.equals(node.getName()); + } + + //////////////////////////////////////////////// + // name + //////////////////////////////////////////////// + + private final static String NAME = "name"; + + public void setName(String value) + { + getStateVariableNode().setNode(NAME, value); + } + + public String getName() + { + return getStateVariableNode().getNodeValue(NAME); + } + + //////////////////////////////////////////////// + // dataType + //////////////////////////////////////////////// + + private final static String DATATYPE = "dataType"; + + public void setDataType(String value) + { + getStateVariableNode().setNode(DATATYPE, value); + } + + public String getDataType() + { + return getStateVariableNode().getNodeValue(DATATYPE); + } + + //////////////////////////////////////////////// + // dataType + //////////////////////////////////////////////// + + private final static String SENDEVENTS = "sendEvents"; + private final static String SENDEVENTS_YES = "yes"; + private final static String SENDEVENTS_NO = "no"; + + public void setSendEvents(boolean state) + { + getStateVariableNode().setAttribute(SENDEVENTS, (state == true) ? SENDEVENTS_YES : SENDEVENTS_NO); + } + + public boolean isSendEvents() + { + String state = getStateVariableNode().getAttributeValue(SENDEVENTS); + if (state == null) + return false; + if (state.equalsIgnoreCase(SENDEVENTS_YES) == true) + return true; + return false; + } + + //////////////////////////////////////////////// + // set + //////////////////////////////////////////////// + + public void set(StateVariable stateVar) + { + setName(stateVar.getName()); + setValue(stateVar.getValue()); + setDataType(stateVar.getDataType()); + setSendEvents(stateVar.isSendEvents()); + } + + //////////////////////////////////////////////// + // UserData + //////////////////////////////////////////////// + + public StateVariableData getStateVariableData () + { + Node node = getStateVariableNode(); + StateVariableData userData = (StateVariableData)node.getUserData(); + if (userData == null) { + userData = new StateVariableData(); + node.setUserData(userData); + userData.setNode(node); + } + return userData; + } + + //////////////////////////////////////////////// + // Value + //////////////////////////////////////////////// + + public void setValue(String value) + { + // Thnaks for Tho Beisch (11/09/04) + String currValue = getStateVariableData().getValue(); + // Thnaks for Tho Rick Keiner (11/18/04) + if (currValue != null && currValue.equals(value) == true) + return; + + getStateVariableData().setValue(value); + + // notify event + Service service = getService(); + if (service == null) + return; + if (isSendEvents() == false) + return; + service.notify(this); + } + + public void setValue(int value) + { + setValue(Integer.toString(value)); + } + + public void setValue(long value) + { + setValue(Long.toString(value)); + } + + public String getValue() + { + return getStateVariableData().getValue(); + } + + //////////////////////////////////////////////// + // AllowedValueList + //////////////////////////////////////////////// + + public AllowedValueList getAllowedValueList() + { + AllowedValueList valueList= new AllowedValueList(); + Node valueListNode = getStateVariableNode().getNode(AllowedValueList.ELEM_NAME); + if (valueListNode == null) + return valueList; + int nNode = valueListNode.getNNodes(); + for (int n=0; n and Stefano Lenzi (07/09/04) + if (queryRes.isSuccessful() == false) { + setValue(queryRes.getReturnValue()); + return false; + } + setValue(queryRes.getReturnValue()); + return true; + } + + //////////////////////////////////////////////// + // UPnPStatus + //////////////////////////////////////////////// + + private UPnPStatus upnpStatus = new UPnPStatus(); + + public void setStatus(int code, String descr) + { + upnpStatus.setCode(code); + upnpStatus.setDescription(descr); + } + + public void setStatus(int code) + { + setStatus(code, UPnPStatus.code2String(code)); + } + + public UPnPStatus getStatus() + { + return upnpStatus; + } +} diff --git a/router/java/src/org/cybergarage/upnp/UPnP.java b/router/java/src/org/cybergarage/upnp/UPnP.java new file mode 100644 index 000000000..e27a5db40 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/UPnP.java @@ -0,0 +1,232 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: UPnP.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* 05/13/03 +* - Added support for IPv6 and loopback address. +* 12/26/03 +* - Added support for XML Parser +* 06/18/03 +* - Added INMPR03 and INMPR03_VERSION. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import org.cybergarage.upnp.ssdp.*; +//import org.cybergarage.util.*; +import org.cybergarage.xml.*; +import org.cybergarage.xml.parser.*; +import org.cybergarage.soap.*; +import org.cybergarage.net.*; + +public class UPnP +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String NAME = "CyberLink"; + public final static String VERSION = "1.7"; + + public final static int SERVER_RETRY_COUNT = 100; + public final static int DEFAULT_EXPIRED_DEVICE_EXTRA_TIME = 60; + + public final static String getServerName() + { + String osName = System.getProperty("os.name"); + String osVer = System.getProperty("os.version"); + return osName + "/" + osVer + " UPnP/1.0 " + NAME + "/" + VERSION; + } + + public final static String INMPR03 = "INMPR03"; + public final static String INMPR03_VERSION = "1.0"; + public final static int INMPR03_DISCOVERY_OVER_WIRELESS_COUNT = 4; + + public final static String XML_DECLARATION = ""; + + //////////////////////////////////////////////// + // Enable / Disable + //////////////////////////////////////////////// + + public final static int USE_ONLY_IPV6_ADDR = 1; + public final static int USE_LOOPBACK_ADDR = 2; + public final static int USE_IPV6_LINK_LOCAL_SCOPE = 3; + public final static int USE_IPV6_SUBNET_SCOPE = 4; + public final static int USE_IPV6_ADMINISTRATIVE_SCOPE = 5; + public final static int USE_IPV6_SITE_LOCAL_SCOPE = 6; + public final static int USE_IPV6_GLOBAL_SCOPE = 7; + public final static int USE_SSDP_SEARCHRESPONSE_MULTIPLE_INTERFACES = 8; + public final static int USE_ONLY_IPV4_ADDR = 9; + + public final static void setEnable(int value) + { + switch (value) { + case USE_ONLY_IPV6_ADDR: + { + HostInterface.USE_ONLY_IPV6_ADDR = true; + } + break; + case USE_ONLY_IPV4_ADDR: + { + HostInterface.USE_ONLY_IPV4_ADDR = true; + } + break; + case USE_LOOPBACK_ADDR: + { + HostInterface.USE_LOOPBACK_ADDR = true; + } + break; + case USE_IPV6_LINK_LOCAL_SCOPE: + { + SSDP.setIPv6Address(SSDP.IPV6_LINK_LOCAL_ADDRESS); + } + break; + case USE_IPV6_SUBNET_SCOPE: + { + SSDP.setIPv6Address(SSDP.IPV6_SUBNET_ADDRESS); + } + break; + case USE_IPV6_ADMINISTRATIVE_SCOPE: + { + SSDP.setIPv6Address(SSDP.IPV6_ADMINISTRATIVE_ADDRESS); + } + break; + case USE_IPV6_SITE_LOCAL_SCOPE: + { + SSDP.setIPv6Address(SSDP.IPV6_SITE_LOCAL_ADDRESS); + } + break; + case USE_IPV6_GLOBAL_SCOPE: + { + SSDP.setIPv6Address(SSDP.IPV6_GLOBAL_ADDRESS); + } + break; + } + } + + public final static void setDisable(int value) + { + switch (value) { + case USE_ONLY_IPV6_ADDR: + { + HostInterface.USE_ONLY_IPV6_ADDR = false; + } + break; + case USE_ONLY_IPV4_ADDR: + { + HostInterface.USE_ONLY_IPV4_ADDR = false; + } + break; + case USE_LOOPBACK_ADDR: + { + HostInterface.USE_LOOPBACK_ADDR = false; + } + break; + } + } + + public final static boolean isEnabled(int value) + { + switch (value) { + case USE_ONLY_IPV6_ADDR: + { + return HostInterface.USE_ONLY_IPV6_ADDR; + } + case USE_ONLY_IPV4_ADDR: + { + return HostInterface.USE_ONLY_IPV4_ADDR; + } + case USE_LOOPBACK_ADDR: + { + return HostInterface.USE_LOOPBACK_ADDR; + } + } + return false; + } + + //////////////////////////////////////////////// + // UUID + //////////////////////////////////////////////// + + private static final String toUUID(int seed) + { + String id = Integer.toString((int)(seed & 0xFFFF), 16); + int idLen = id.length(); + String uuid = ""; + for (int n=0; n<(4-idLen); n++) + uuid += "0"; + uuid += id; + return uuid; + } + + public static final String createUUID() + { + long time1 = System.currentTimeMillis(); + long time2 = (long)((double)System.currentTimeMillis() * Math.random()); + return + toUUID((int)(time1 & 0xFFFF)) + "-" + + toUUID((int)((time1 >> 32) | 0xA000) & 0xFFFF) + "-" + + toUUID((int)(time2 & 0xFFFF)) + "-" + + toUUID((int)((time2 >> 32) | 0xE000) & 0xFFFF); + } + + //////////////////////////////////////////////// + // XML Parser + //////////////////////////////////////////////// + + private static Parser xmlParser; + + public final static void setXMLParser(Parser parser) + { + xmlParser = parser; + SOAP.setXMLParser(parser); + } + + public final static Parser getXMLParser() + { + return xmlParser; + } + + //////////////////////////////////////////////// + // Initialize + //////////////////////////////////////////////// + + static + { + //////////////////////////// + // Interface Option + //////////////////////////// + + setXMLParser(new JaxpParser()); + //setXMLParser(new kXML2Parser()); + + //////////////////////////// + // Interface Option + //////////////////////////// + /* + if (HostInterface.hasIPv6Addresses() == true) + setEnable(USE_ONLY_IPV6_ADDR); + */ + + //////////////////////////// + // Debug Option + //////////////////////////// + + //Debug.on(); + } + + public final static void initialize() + { + // Dummy function to call UPnP.static + } + +} diff --git a/router/java/src/org/cybergarage/upnp/UPnPStatus.java b/router/java/src/org/cybergarage/upnp/UPnPStatus.java new file mode 100644 index 000000000..f00ba6921 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/UPnPStatus.java @@ -0,0 +1,81 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: UPnPStatus.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* 01/03/04 +* - Changed the class name from UPnPError to UPnPStatus. +* +******************************************************************/ + +package org.cybergarage.upnp; + +public class UPnPStatus +{ + //////////////////////////////////////////////// + // Code + //////////////////////////////////////////////// + + public static final int INVALID_ACTION = 401; + public static final int INVALID_ARGS = 402; + public static final int OUT_OF_SYNC = 403; + public static final int INVALID_VAR = 404; + public static final int PRECONDITION_FAILED = 412; + public static final int ACTION_FAILED = 501; + + public static final String code2String(int code) + { + switch (code) { + case INVALID_ACTION: return "Invalid Action"; + case INVALID_ARGS: return "Invalid Args"; + case OUT_OF_SYNC: return "Out of Sync"; + case INVALID_VAR: return "Invalid Var"; + case PRECONDITION_FAILED: return "Precondition Failed"; + case ACTION_FAILED: return "Action Failed"; + } + return ""; + } + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private int code; + private String description; + + public UPnPStatus() + { + setCode(0); + setDescription(""); + } + + public UPnPStatus(int code, String desc) + { + setCode(code); + setDescription(desc); + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/router/java/src/org/cybergarage/upnp/control/ActionListener.java b/router/java/src/org/cybergarage/upnp/control/ActionListener.java new file mode 100644 index 000000000..40f9244b5 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/control/ActionListener.java @@ -0,0 +1,23 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ActionListener.java +* +* Revision; +* +* 01/16/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.control; + +import org.cybergarage.upnp.*; + +public interface ActionListener +{ + public boolean actionControlReceived(Action action); +} diff --git a/router/java/src/org/cybergarage/upnp/control/ActionRequest.java b/router/java/src/org/cybergarage/upnp/control/ActionRequest.java new file mode 100644 index 000000000..e14db4b46 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/control/ActionRequest.java @@ -0,0 +1,145 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ControlRequest.java +* +* Revision; +* +* 01/29/03 +* - first revision. +* 05/09/05 +* - Changed getActionName() to return when the delimiter is not found. +* +******************************************************************/ + +package org.cybergarage.upnp.control; + +import org.cybergarage.http.*; +import org.cybergarage.xml.*; +import org.cybergarage.soap.*; + +import org.cybergarage.upnp.*; + +public class ActionRequest extends ControlRequest +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public ActionRequest() + { + } + + public ActionRequest(HTTPRequest httpReq) + { + set(httpReq); + } + + //////////////////////////////////////////////// + // Action + //////////////////////////////////////////////// + + public Node getActionNode() + { + Node bodyNode = getBodyNode(); + if (bodyNode == null) + return null; + if (bodyNode.hasNodes() == false) + return null; + return bodyNode.getNode(0); + } + + public String getActionName() + { + Node node = getActionNode(); + if (node == null) + return ""; + String name = node.getName(); + if (name == null) + return ""; + int idx = name.indexOf(SOAP.DELIM)+1; + if (idx < 0) + return ""; + return name.substring(idx, name.length()); + } + + public ArgumentList getArgumentList() + { + Node actNode = getActionNode(); + int nArgNodes = actNode.getNNodes(); + ArgumentList argList = new ArgumentList(); + for (int n=0; n +* - Problem : Action Responses do not contain the mandatory header field EXT +* - Error : ActionResponse class does not set the EXT header +* +******************************************************************/ + +package org.cybergarage.upnp.control; + +import org.cybergarage.upnp.*; +import org.cybergarage.http.*; +import org.cybergarage.soap.*; +import org.cybergarage.xml.*; + +public class ActionResponse extends ControlResponse +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public ActionResponse() + { + setHeader(HTTP.EXT, ""); + } + + public ActionResponse(SOAPResponse soapRes) + { + super(soapRes); + setHeader(HTTP.EXT, ""); + } + + + //////////////////////////////////////////////// + // Response + //////////////////////////////////////////////// + + public void setResponse(Action action) + { + setStatusCode(HTTPStatus.OK); + + Node bodyNode = getBodyNode(); + Node resNode = createResponseNode(action); + bodyNode.addNode(resNode); + + Node envNode = getEnvelopeNode(); + setContent(envNode); + } + + private Node createResponseNode(Action action) + { + String actionName = action.getName(); + Node actionNameResNode = new Node(SOAP.METHODNS + SOAP.DELIM + actionName + SOAP.RESPONSE); + + Service service = action.getService(); + if (service != null) { + actionNameResNode.setAttribute( + "xmlns:" + SOAP.METHODNS, + service.getServiceType()); + } + + ArgumentList argList = action.getArgumentList(); + int nArgs = argList.size(); + for (int n=0; n +* - Description: inserted a check at the beginning of the setRequestHost method +* - Problem : If the host does not start with a '/', the device could refuse the control action +* - Error : it is not an error, but adding the '/' when missing allows the integration with the Intel devices +* 09/02/03 +* - Giordano Sassaroli / Suzan Foster +* - Problem : NullpointerException thrown for devices whose description use absolute urls +* - Error : the presence of a base url is not mandatory, the API code makes the assumption that control and event subscription urls are relative. +* If the baseUrl is not present, the request host and port should be extracted from the control/subscription url +* - Description: The method setRequestHost/setService should be changed as follows +* 02/17/04 +* - Rob van den Boomen +* - Fixed to set a URLBase from the SSDP header when the URLBase of the description is null. +* 02/18/04 +* - Andre +* - The xml nodes controlUrl and eventSubUrl can contain absolut urls, but these absolut urls may have +* different ports than the base url! (so seen on my SMC 7004ABR Barricade Router, where xml files are +* requested from port 80, but soap requests are made on port 5440). Therefore whenever a request is made, +* the port specified by the controlUrl or eventSubUrl node should be used, else no response will be returned +* (oddly, there was a response returned even on port 80, but with empty body tags. but the correct response +* finally came from port 5440). +* - Fixed to get the port from the control url when it is absolute. +* 03/20/04 +* - Thanks for Thomas Schulz +* - Fixed setRequestHost() for Sony's UPnP stack when the URLBase has the path. +* +******************************************************************/ + +package org.cybergarage.upnp.control; + +import java.net.*; + +import org.cybergarage.http.*; +import org.cybergarage.soap.*; + +import org.cybergarage.upnp.*; + +public class ControlRequest extends SOAPRequest +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public ControlRequest() + { + } + + public ControlRequest(HTTPRequest httpReq) + { + set(httpReq); + } + + //////////////////////////////////////////////// + // Query + //////////////////////////////////////////////// + + public boolean isQueryControl() + { + return isSOAPAction(Control.QUERY_SOAPACTION); + } + + public boolean isActionControl() + { + return !isQueryControl(); + } + + //////////////////////////////////////////////// + // setRequest + //////////////////////////////////////////////// + + protected void setRequestHost(Service service) + { + String ctrlURL = service.getControlURL(); + + // Thanks for Thomas Schulz (2004/03/20) + String urlBase = service.getRootDevice().getURLBase(); + if (urlBase != null && 0 < urlBase.length()){ + try { + URL url = new URL(urlBase); + String basePath = url.getPath(); + int baseLen = basePath.length(); + if (0 < baseLen) { + if (1 < baseLen || (basePath.charAt(0) != '/')) + ctrlURL = basePath + ctrlURL; + } + } + catch (MalformedURLException e) {} + } + + // Thanks for Giordano Sassaroli (05/21/03) + setURI(ctrlURL, true); + + // Thanks for Giordano Sassaroli and Suzan Foster (09/02/03) + // Thanks for Andre (02/18/04) + String postURL = ""; + if (HTTP.isAbsoluteURL(ctrlURL) == true) + postURL = ctrlURL; + + if (postURL == null || postURL.length() <= 0) + postURL = service.getRootDevice().getURLBase(); + + // Thanks for Rob van den Boomen (02/17/04) + // BUGFIX, set urlbase from location string if not set in description.xml + if (postURL == null || postURL.length() <= 0) + postURL = service.getRootDevice().getLocation(); + + String reqHost = HTTP.getHost(postURL); + int reqPort = HTTP.getPort(postURL); + + setHost(reqHost, reqPort); + setRequestHost(reqHost); + setRequestPort(reqPort); + } + +} diff --git a/router/java/src/org/cybergarage/upnp/control/ControlResponse.java b/router/java/src/org/cybergarage/upnp/control/ControlResponse.java new file mode 100644 index 000000000..36fcab89d --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/control/ControlResponse.java @@ -0,0 +1,168 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ControlResponse.java +* +* Revision; +* +* 01/29/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.control; + +import org.cybergarage.http.*; +import org.cybergarage.xml.*; +import org.cybergarage.soap.*; + +import org.cybergarage.upnp.*; + +public class ControlResponse extends SOAPResponse +{ + public static final String FAULT_CODE = "Client"; + public static final String FAULT_STRING = "UPnPError"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public ControlResponse() + { + setServer(UPnP.getServerName()); + } + + public ControlResponse(SOAPResponse soapRes) + { + super(soapRes); + } + + //////////////////////////////////////////////// + // FaultResponse + //////////////////////////////////////////////// + + public void setFaultResponse(int errCode, String errDescr) + { + setStatusCode(HTTPStatus.INTERNAL_SERVER_ERROR); + + Node bodyNode = getBodyNode(); + Node faultNode = createFaultResponseNode(errCode, errDescr); + bodyNode.addNode(faultNode); + + Node envNode = getEnvelopeNode(); + setContent(envNode); + } + + public void setFaultResponse(int errCode) + { + setFaultResponse(errCode, UPnPStatus.code2String(errCode)); + } + + //////////////////////////////////////////////// + // createFaultResponseNode + //////////////////////////////////////////////// + + private Node createFaultResponseNode(int errCode, String errDescr) + { + // + Node faultNode = new Node(SOAP.XMLNS + SOAP.DELIM + SOAP.FAULT); + + // s:Client + Node faultCodeNode = new Node(SOAP.FAULT_CODE); + faultCodeNode.setValue(SOAP.XMLNS + SOAP.DELIM + FAULT_CODE); + faultNode.addNode(faultCodeNode); + + // UPnPError + Node faultStringNode = new Node(SOAP.FAULT_STRING); + faultStringNode.setValue(FAULT_STRING); + faultNode.addNode(faultStringNode); + + // + Node detailNode = new Node(SOAP.DETAIL); + faultNode.addNode(detailNode); + + // + Node upnpErrorNode = new Node(FAULT_STRING); + upnpErrorNode.setAttribute("xmlns", Control.XMLNS); + detailNode.addNode(upnpErrorNode); + + // error code + Node errorCodeNode = new Node(SOAP.ERROR_CODE); + errorCodeNode.setValue(errCode); + upnpErrorNode.addNode(errorCodeNode); + + // error string + Node errorDesctiprionNode = new Node(SOAP.ERROR_DESCRIPTION); + errorDesctiprionNode.setValue(errDescr); + upnpErrorNode.addNode(errorDesctiprionNode); + + return faultNode; + } + + //////////////////////////////////////////////// + // UPnP Error + //////////////////////////////////////////////// + + private UPnPStatus upnpErr = new UPnPStatus(); + + private Node getUPnPErrorNode() + { + Node detailNode = getFaultDetailNode(); + if (detailNode == null) + return null; + return detailNode.getNodeEndsWith(SOAP.UPNP_ERROR); + } + + private Node getUPnPErrorCodeNode() + { + Node errorNode = getUPnPErrorNode(); + if (errorNode == null) + return null; + return errorNode.getNodeEndsWith(SOAP.ERROR_CODE); + } + + private Node getUPnPErrorDescriptionNode() + { + Node errorNode = getUPnPErrorNode(); + if (errorNode == null) + return null; + return errorNode.getNodeEndsWith(SOAP.ERROR_DESCRIPTION); + } + + public int getUPnPErrorCode() + { + Node errorCodeNode = getUPnPErrorCodeNode(); + if (errorCodeNode == null) + return -1; + String errorCodeStr = errorCodeNode.getValue(); + try { + return Integer.parseInt(errorCodeStr); + } + catch (Exception e) { + return -1; + } + } + + public String getUPnPErrorDescription() + { + Node errorDescNode = getUPnPErrorDescriptionNode(); + if (errorDescNode == null) + return ""; + return errorDescNode.getValue(); + } + + public UPnPStatus getUPnPError() + { + int code = 0; + String desc = ""; + code = getUPnPErrorCode(); + desc = getUPnPErrorDescription(); + upnpErr.setCode(code); + upnpErr.setDescription(desc); + return upnpErr; + } + +} diff --git a/router/java/src/org/cybergarage/upnp/control/QueryListener.java b/router/java/src/org/cybergarage/upnp/control/QueryListener.java new file mode 100644 index 000000000..8af1ea13e --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/control/QueryListener.java @@ -0,0 +1,25 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: QueryListener.java +* +* Revision; +* +* 01/30/03 +* - first revision. +* 01/04/04 +* - Changed the interface. +* +******************************************************************/ + +package org.cybergarage.upnp.control; + +import org.cybergarage.upnp.*; + +public interface QueryListener +{ + public boolean queryControlReceived(StateVariable stateVar); +} diff --git a/router/java/src/org/cybergarage/upnp/control/QueryRequest.java b/router/java/src/org/cybergarage/upnp/control/QueryRequest.java new file mode 100644 index 000000000..d222051fc --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/control/QueryRequest.java @@ -0,0 +1,117 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: QueryRequest.java +* +* Revision; +* +* 01/29/03 +* - first revision. +* 09/02/03 +* - Giordano Sassaroli +* - Error : redundant code, the setRequest method in QueryRequest invokes setURI even if after a couple of rows setRequestHost is invoked +* +******************************************************************/ + +package org.cybergarage.upnp.control; + +import org.cybergarage.http.*; +import org.cybergarage.xml.*; +import org.cybergarage.soap.*; + +import org.cybergarage.upnp.*; + +public class QueryRequest extends ControlRequest +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public QueryRequest() + { + } + + public QueryRequest(HTTPRequest httpReq) + { + set(httpReq); + } + + //////////////////////////////////////////////// + // Qyery + //////////////////////////////////////////////// + + private Node getVarNameNode() + { + Node bodyNode = getBodyNode(); + if (bodyNode == null) + return null; + if (bodyNode.hasNodes() == false) + return null; + Node queryStateVarNode = bodyNode.getNode(0); + if (queryStateVarNode == null) + return null; + if (queryStateVarNode.hasNodes() == false) + return null; + return queryStateVarNode.getNode(0); + } + + public String getVarName() + { + Node node = getVarNameNode(); + if (node == null) + return ""; + return node.getValue(); + } + + //////////////////////////////////////////////// + // setRequest + //////////////////////////////////////////////// + + public void setRequest(StateVariable stateVar) + { + Service service = stateVar.getService(); + + setRequestHost(service); + + setEnvelopeNode(SOAP.createEnvelopeBodyNode()); + Node envNode = getEnvelopeNode(); + Node bodyNode = getBodyNode(); + Node qeuryNode = createContentNode(stateVar); + bodyNode.addNode(qeuryNode); + setContent(envNode); + + setSOAPAction(Control.QUERY_SOAPACTION); + } + + //////////////////////////////////////////////// + // Contents + //////////////////////////////////////////////// + + private Node createContentNode(StateVariable stateVar) + { + Node queryVarNode = new Node(); + queryVarNode.setName(Control.NS, Control.QUERY_STATE_VARIABLE); + queryVarNode.setNameSpace(Control.NS, Control.XMLNS); + + Node varNode = new Node(); + varNode.setName(Control.NS, Control.VAR_NAME); + varNode.setValue(stateVar.getName()); + queryVarNode.addNode(varNode); + + return queryVarNode; + } + + //////////////////////////////////////////////// + // post + //////////////////////////////////////////////// + + public QueryResponse post() + { + SOAPResponse soapRes = postMessage(getRequestHost(), getRequestPort()); + return new QueryResponse(soapRes); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/control/QueryResponse.java b/router/java/src/org/cybergarage/upnp/control/QueryResponse.java new file mode 100644 index 000000000..75ba30c84 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/control/QueryResponse.java @@ -0,0 +1,97 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: QueryResponse.java +* +* Revision; +* +* 01/30/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.control; + +import org.cybergarage.upnp.*; +import org.cybergarage.http.*; +import org.cybergarage.soap.*; +import org.cybergarage.xml.*; + +public class QueryResponse extends ControlResponse +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public QueryResponse() + { + } + + public QueryResponse(SOAPResponse soapRes) + { + super(soapRes); + } + + //////////////////////////////////////////////// + // Qyery + //////////////////////////////////////////////// + + private Node getReturnNode() + { + Node bodyNode = getBodyNode(); + if (bodyNode == null) + return null; + if (bodyNode.hasNodes() == false) + return null; + Node queryResNode = bodyNode.getNode(0); + if (queryResNode == null) + return null; + if (queryResNode.hasNodes() == false) + return null; + return queryResNode.getNode(0); + } + + public String getReturnValue() + { + Node node = getReturnNode(); + if (node == null) + return ""; + return node.getValue(); + } + + //////////////////////////////////////////////// + // Response + //////////////////////////////////////////////// + + public void setResponse(StateVariable stateVar) + { + String var = stateVar.getValue(); + + setStatusCode(HTTPStatus.OK); + + Node bodyNode = getBodyNode(); + Node resNode = createResponseNode(var); + bodyNode.addNode(resNode); + + Node envNodee = getEnvelopeNode(); + setContent(envNodee); + + } + + private Node createResponseNode(String var) + { + Node queryResNode = new Node(); + queryResNode.setName(Control.NS, Control.QUERY_STATE_VARIABLE_RESPONSE); + queryResNode.setNameSpace(Control.NS, Control.XMLNS); + + Node returnNode = new Node(); + returnNode.setName(Control.RETURN); + returnNode.setValue(var); + queryResNode.addNode(returnNode); + + return queryResNode; + } +} diff --git a/router/java/src/org/cybergarage/upnp/control/RenewSubscriber.java b/router/java/src/org/cybergarage/upnp/control/RenewSubscriber.java new file mode 100644 index 000000000..7b8d2e059 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/control/RenewSubscriber.java @@ -0,0 +1,65 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: RenewSubscriber.java +* +* Revision: +* +* 07/07/04 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.control; + +import org.cybergarage.util.*; +import org.cybergarage.upnp.*; + +public class RenewSubscriber extends ThreadCore +{ + public final static long INTERVAL = 120; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public RenewSubscriber(ControlPoint ctrlp) + { + setControlPoint(ctrlp); + } + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private ControlPoint ctrlPoint; + + public void setControlPoint(ControlPoint ctrlp) + { + ctrlPoint = ctrlp; + } + + public ControlPoint getControlPoint() + { + return ctrlPoint; + } + + //////////////////////////////////////////////// + // Thread + //////////////////////////////////////////////// + + public void run() + { + ControlPoint ctrlp = getControlPoint(); + long renewInterval = INTERVAL * 1000; + while (isRunnable() == true) { + try { + Thread.sleep(renewInterval); + } catch (InterruptedException e) {} + ctrlp.renewSubscriberService(); + } + } +} diff --git a/router/java/src/org/cybergarage/upnp/device/Advertiser.java b/router/java/src/org/cybergarage/upnp/device/Advertiser.java new file mode 100644 index 000000000..373fea82d --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/device/Advertiser.java @@ -0,0 +1,68 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: Advertiser.java +* +* Revision; +* +* 12/24/03 +* - first revision. +* 06/18/04 +* - Changed to advertise every 25%-50% of the periodic notification cycle for NMPR; +* +******************************************************************/ + +package org.cybergarage.upnp.device; + +import org.cybergarage.util.*; +import org.cybergarage.upnp.*; + +public class Advertiser extends ThreadCore +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public Advertiser(Device dev) + { + setDevice(dev); + } + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private Device device; + + public void setDevice(Device dev) + { + device = dev; + } + + public Device getDevice() + { + return device; + } + + //////////////////////////////////////////////// + // Thread + //////////////////////////////////////////////// + + public void run() + { + Device dev = getDevice(); + long leaseTime = dev.getLeaseTime(); + long notifyInterval; + while (isRunnable() == true) { + notifyInterval = (leaseTime/4) + (long)((float)leaseTime * (Math.random() * 0.25f)); + notifyInterval *= 1000; + try { + Thread.sleep(notifyInterval); + } catch (InterruptedException e) {} + dev.announce(); + } + } +} diff --git a/router/java/src/org/cybergarage/upnp/device/Description.java b/router/java/src/org/cybergarage/upnp/device/Description.java new file mode 100644 index 000000000..fe42dc5ea --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/device/Description.java @@ -0,0 +1,24 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: MAN.java +* +* Revision; +* +* 12/30/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.device; + +public class Description +{ + public final static String LOADING_EXCEPTION = "Couldn't load a specified description file "; + public final static String NOROOT_EXCEPTION = "Couldn't find a root node"; + public final static String NOROOTDEVICE_EXCEPTION = "Couldn't find a root device node"; +} + diff --git a/router/java/src/org/cybergarage/upnp/device/DeviceChangeListener.java b/router/java/src/org/cybergarage/upnp/device/DeviceChangeListener.java new file mode 100644 index 000000000..fc76020ab --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/device/DeviceChangeListener.java @@ -0,0 +1,27 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: DeviceChangeListener.java +* +* Revision; +* +* 09/12/04 +* - Oliver Newell +* - Added this class to allow ControlPoint applications to +* be notified when the ControlPoint base class adds/removes +* a UPnP device +* +******************************************************************/ + +package org.cybergarage.upnp.device; + +import org.cybergarage.upnp.Device; + +public interface DeviceChangeListener +{ + public void deviceAdded( Device dev ); + public void deviceRemoved( Device dev ); +} diff --git a/router/java/src/org/cybergarage/upnp/device/Disposer.java b/router/java/src/org/cybergarage/upnp/device/Disposer.java new file mode 100644 index 000000000..e6c0ddb8e --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/device/Disposer.java @@ -0,0 +1,65 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: Disposer.java +* +* Revision: +* +* 01/05/04 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.device; + +import org.cybergarage.upnp.*; +import org.cybergarage.util.*; + +public class Disposer extends ThreadCore +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public Disposer(ControlPoint ctrlp) + { + setControlPoint(ctrlp); + } + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private ControlPoint ctrlPoint; + + public void setControlPoint(ControlPoint ctrlp) + { + ctrlPoint = ctrlp; + } + + public ControlPoint getControlPoint() + { + return ctrlPoint; + } + + //////////////////////////////////////////////// + // Thread + //////////////////////////////////////////////// + + public void run() + { + ControlPoint ctrlp = getControlPoint(); + long monitorInterval = ctrlp.getExpiredDeviceMonitoringInterval() * 1000; + + while (isRunnable() == true) { + try { + Thread.sleep(monitorInterval); + } catch (InterruptedException e) {} + ctrlp.removeExpiredDevices(); + //ctrlp.print(); + } + } +} diff --git a/router/java/src/org/cybergarage/upnp/device/InvalidDescriptionException.java b/router/java/src/org/cybergarage/upnp/device/InvalidDescriptionException.java new file mode 100644 index 000000000..a08b3a393 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/device/InvalidDescriptionException.java @@ -0,0 +1,43 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: InvalidDescriptionException.java +* +* Revision; +* +* 12/26/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.device; + +import java.io.*; + +public class InvalidDescriptionException extends Exception +{ + private static final long serialVersionUID = -3144583349586910509L; + + public InvalidDescriptionException() + { + super(); + } + + public InvalidDescriptionException(String s) + { + super(s); + } + + public InvalidDescriptionException(String s, File file) + { + super(s + " (" + file.toString() + ")"); + } + + public InvalidDescriptionException(Exception e) + { + super(e.getMessage()); + } +} diff --git a/router/java/src/org/cybergarage/upnp/device/MAN.java b/router/java/src/org/cybergarage/upnp/device/MAN.java new file mode 100644 index 000000000..1e0093f74 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/device/MAN.java @@ -0,0 +1,31 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: MAN.java +* +* Revision; +* +* 12/30/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.device; + +public class MAN +{ + public final static String DISCOVER = "ssdp:discover"; + + public final static boolean isDiscover(String value) + { + if (value == null) + return false; + if (value.equals(MAN.DISCOVER) == true) + return true; + return value.equals("\"" + MAN.DISCOVER + "\""); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/device/NT.java b/router/java/src/org/cybergarage/upnp/device/NT.java new file mode 100644 index 000000000..1acb0d490 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/device/NT.java @@ -0,0 +1,30 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: NT.java +* +* Revision; +* +* 12/09/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.device; + +public class NT +{ + public final static String ROOTDEVICE = "upnp:rootdevice"; + public final static String EVENT = "upnp:event"; + + public final static boolean isRootDevice(String ntValue) + { + if (ntValue == null) + return false; + return ntValue.startsWith(ROOTDEVICE); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/device/NTS.java b/router/java/src/org/cybergarage/upnp/device/NTS.java new file mode 100644 index 000000000..7eab5f19c --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/device/NTS.java @@ -0,0 +1,38 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: NTS.java +* +* Revision; +* +* 12/09/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.device; + +public class NTS +{ + public final static String ALIVE = "ssdp:alive"; + public final static String BYEBYE = "ssdp:byebye"; + public final static String PROPCHANGE = "upnp:propchange"; + + public final static boolean isAlive(String ntsValue) + { + if (ntsValue == null) + return false; + return ntsValue.startsWith(NTS.ALIVE); + } + + public final static boolean isByeBye(String ntsValue) + { + if (ntsValue == null) + return false; + return ntsValue.startsWith(NTS.BYEBYE); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/device/NotifyListener.java b/router/java/src/org/cybergarage/upnp/device/NotifyListener.java new file mode 100644 index 000000000..bac853ea5 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/device/NotifyListener.java @@ -0,0 +1,23 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: DeviceNotifyListener.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.device; + +import org.cybergarage.upnp.ssdp.*; + +public interface NotifyListener +{ + public void deviceNotifyReceived(SSDPPacket ssdpPacket); +} diff --git a/router/java/src/org/cybergarage/upnp/device/ST.java b/router/java/src/org/cybergarage/upnp/device/ST.java new file mode 100644 index 000000000..dcbfaf8ce --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/device/ST.java @@ -0,0 +1,71 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: ST.java +* +* Revision; +* +* 01/07/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.device; + +public class ST +{ + public final static String ALL_DEVICE = "ssdp:all"; + public final static String ROOT_DEVICE = "upnp:rootdevice"; + public final static String UUID_DEVICE = "uuid"; + public final static String URN_DEVICE = "urn:schemas-upnp-org:device:"; + public final static String URN_SERVICE = "urn:schemas-upnp-org:service:"; + + public final static boolean isAllDevice(String value) + { + if (value == null) + return false; + if (value.equals(ALL_DEVICE) == true) + return true; + return value.equals("\"" + ALL_DEVICE + "\""); + } + + public final static boolean isRootDevice(String value) + { + if (value == null) + return false; + if (value.equals(ROOT_DEVICE) == true) + return true; + return value.equals("\"" + ROOT_DEVICE + "\""); + } + + public final static boolean isUUIDDevice(String value) + { + if (value == null) + return false; + if (value.startsWith(UUID_DEVICE) == true) + return true; + return value.startsWith("\"" + UUID_DEVICE); + } + + public final static boolean isURNDevice(String value) + { + if (value == null) + return false; + if (value.startsWith(URN_DEVICE) == true) + return true; + return value.startsWith("\"" + URN_DEVICE); + } + + public final static boolean isURNService(String value) + { + if (value == null) + return false; + if (value.startsWith(URN_SERVICE) == true) + return true; + return value.startsWith("\"" + URN_SERVICE); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/device/SearchListener.java b/router/java/src/org/cybergarage/upnp/device/SearchListener.java new file mode 100644 index 000000000..758a37c27 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/device/SearchListener.java @@ -0,0 +1,23 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SearchListener.java +* +* Revision; +* +* 11/18/02b +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.device; + +import org.cybergarage.upnp.ssdp.*; + +public interface SearchListener +{ + public void deviceSearchReceived(SSDPPacket ssdpPacket); +} diff --git a/router/java/src/org/cybergarage/upnp/device/SearchResponseListener.java b/router/java/src/org/cybergarage/upnp/device/SearchResponseListener.java new file mode 100644 index 000000000..c87c41a37 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/device/SearchResponseListener.java @@ -0,0 +1,23 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SearchResponseListener.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.device; + +import org.cybergarage.upnp.ssdp.*; + +public interface SearchResponseListener +{ + public void deviceSearchResponseReceived(SSDPPacket ssdpPacket); +} diff --git a/router/java/src/org/cybergarage/upnp/device/USN.java b/router/java/src/org/cybergarage/upnp/device/USN.java new file mode 100644 index 000000000..74c704027 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/device/USN.java @@ -0,0 +1,40 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: USN.java +* +* Revision; +* +* 12/09/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.device; + +public class USN +{ + public final static String ROOTDEVICE = "upnp:rootdevice"; + + public final static boolean isRootDevice(String usnValue) + { + if (usnValue == null) + return false; + return usnValue.endsWith(ROOTDEVICE); + } + + public final static String getUDN(String usnValue) + { + if (usnValue == null) + return ""; + int idx = usnValue.indexOf("::"); + if (idx < 0) + return usnValue.trim(); + String udnValue = new String(usnValue.getBytes(), 0, idx); + return udnValue.trim(); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/event/EventListener.java b/router/java/src/org/cybergarage/upnp/event/EventListener.java new file mode 100644 index 000000000..900b9307c --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/event/EventListener.java @@ -0,0 +1,21 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: EventListener.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.event; + +public interface EventListener +{ + public void eventNotifyReceived(String uuid, long seq, String varName, String value); +} diff --git a/router/java/src/org/cybergarage/upnp/event/NotifyRequest.java b/router/java/src/org/cybergarage/upnp/event/NotifyRequest.java new file mode 100644 index 000000000..c8891e1d2 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/event/NotifyRequest.java @@ -0,0 +1,187 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SOAPRequest.java +* +* Revision; +* +* 12/11/02 +* - first revision. +* 05/22/03 +* - Giordano Sassaroli +* - Description: removed the xml namespace +* - Problem : Notification messages refer to uncorrect variable names +* - Error : The NotifyRequest class introduces the XML namespace in variable names, too +* 05/22/03 +* - Giordano Sassaroli +* - Problem : Notification messages refer to uncorrect variable names +* - Error : The NotifyRequest class introduces the XML namespace in variable names, too +* - Description : removed the xml namespace +* 09/03/03 +* - Giordano Sassaroli +* - Problem : Notification messages refer to uncorrect variable names +* - Error : The NotifyRequest class introduces the XML namespace in variable names, too +* - Description: removed the xml namespace +* 09/08/03 +* - Giordano Sassaroli +* - Problem : when an event notification message is received and the message +* contains updates on more than one variable, only the first variable update +* is notified. +* - Error : the other xml nodes of the message are ignored +* - Fix : add two methods to the NotifyRequest for extracting the property array +* and modify the httpRequestRecieved method in ControlPoint +* +******************************************************************/ + +package org.cybergarage.upnp.event; + +import org.cybergarage.http.*; +import org.cybergarage.xml.*; +import org.cybergarage.soap.*; + +import org.cybergarage.upnp.device.*; + +public class NotifyRequest extends SOAPRequest +{ + private final static String XMLNS = "e"; + private final static String PROPERTY = "property"; + private final static String PROPERTYSET = "propertyset"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public NotifyRequest() + { + } + + public NotifyRequest(HTTPRequest httpReq) + { + set(httpReq); + } + + //////////////////////////////////////////////// + // NT + //////////////////////////////////////////////// + + public void setNT(String value) + { + setHeader(HTTP.NT, value); + } + + //////////////////////////////////////////////// + // NTS + //////////////////////////////////////////////// + + public void setNTS(String value) + { + setHeader(HTTP.NTS, value); + } + + //////////////////////////////////////////////// + // SID + //////////////////////////////////////////////// + + public void setSID(String id) + { + setHeader(HTTP.SID, Subscription.toSIDHeaderString(id)); + } + + public String getSID() + { + return Subscription.getSID(getHeaderValue(HTTP.SID)); + } + + //////////////////////////////////////////////// + // SEQ + //////////////////////////////////////////////// + + public void setSEQ(long value) + { + setHeader(HTTP.SEQ, Long.toString(value)); + } + + public long getSEQ() + { + return getLongHeaderValue(HTTP.SEQ); + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public boolean setRequest(Subscriber sub, String varName, String value) + { + String sid = sub.getSID(); + long notifyCnt = sub.getNotifyCount(); + String host = sub.getDeliveryHost(); + String path = sub.getDeliveryPath(); + int port = sub.getDeliveryPort(); + + setMethod(HTTP.NOTIFY); + setURI(path); + setHost(host, port); + setNT(NT.EVENT); + setNTS(NTS.PROPCHANGE); + setSID(sid); + setSEQ(notifyCnt); + + setContentType(XML.CONTENT_TYPE); + Node propSetNode = createPropertySetNode(varName, value); + setContent(propSetNode); + + return true; + } + + private Node createPropertySetNode(String varName, String value) + { + Node propSetNode = new Node(/*XMLNS + SOAP.DELIM + */PROPERTYSET); + + propSetNode.setNameSpace(XMLNS, Subscription.XMLNS); + + Node propNode = new Node(/*XMLNS + SOAP.DELIM + */PROPERTY); + propSetNode.addNode(propNode); + + // Thanks for Giordano Sassaroli (05/22/03) + //Node varNameNode = new Node(XMLNS + SOAP.DELIM + varName); + Node varNameNode = new Node(varName); + varNameNode.setValue(value); + propNode.addNode(varNameNode); + + return propSetNode; + } + + // Thanks for Giordano Sassaroli (09/08/03) + private Property getProperty(Node varNode) + { + Property prop = new Property(); + if (varNode == null) + return prop; + // remove the event namespace + String variableName = varNode.getName(); + int index = variableName.lastIndexOf(':'); + if (index != -1) + variableName = variableName.substring(index + 1); + prop.setName(variableName); + prop.setValue(varNode.getValue()); + return prop; + } + + // Thanks for Giordano Sassaroli (09/08/03) + public PropertyList getPropertyList() { + PropertyList properties = new PropertyList(); + Node varSetNode = getEnvelopeNode(); + for (int i = 0; i +* - Problem : the setName method does not set the name of the property +* - Error : the method contains a bug: +* 06/18/03 +* - Fixed a bug when a null value is received to the name and the value of property. +* +******************************************************************/ + +package org.cybergarage.upnp.event; + +public class Property +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public Property() + { + } + + //////////////////////////////////////////////// + // name + //////////////////////////////////////////////// + + private String name = ""; + + public String getName() { + return name; + } + + public void setName(String val) { + if (val == null) + val = ""; + name = val; + } + + //////////////////////////////////////////////// + // value + //////////////////////////////////////////////// + + private String value = ""; + + public String getValue() { + return value; + } + + public void setValue(String val) { + if (val == null) + val = ""; + value = val; + } +} diff --git a/router/java/src/org/cybergarage/upnp/event/PropertyList.java b/router/java/src/org/cybergarage/upnp/event/PropertyList.java new file mode 100644 index 000000000..f890bb3af --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/event/PropertyList.java @@ -0,0 +1,46 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: PropertyList.java +* +* Revision; +* +* 09/08/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.event; + +import java.util.*; + +public class PropertyList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + private static final long serialVersionUID = 8718064210738306226L; + public final static String ELEM_NAME = "PropertyList"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public PropertyList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public Property getProperty(int n) + { + return (Property)get(n); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/event/Subscriber.java b/router/java/src/org/cybergarage/upnp/event/Subscriber.java new file mode 100644 index 000000000..8ade71c72 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/event/Subscriber.java @@ -0,0 +1,180 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: Subscriber.java +* +* Revision; +* +* 01/29/03 +* - first revision. +* 07/31/04 +* - Added isExpired(). +* 10/26/04 +* - Oliver Newell +* - Added support the intinite time and fixed a bug in isExpired(). +* +******************************************************************/ + +package org.cybergarage.upnp.event; + +import java.net.*; + +public class Subscriber +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public Subscriber() + { + renew(); + } + + //////////////////////////////////////////////// + // SID + //////////////////////////////////////////////// + + private String SID = null; + + public String getSID() { + return SID; + } + + public void setSID(String sid) { + SID = sid; + } + + //////////////////////////////////////////////// + // deliveryURL + //////////////////////////////////////////////// + + private String ifAddr = ""; + + public void setInterfaceAddress(String addr) + { + ifAddr = addr; + } + + public String getInterfaceAddress() + { + return ifAddr; + } + + //////////////////////////////////////////////// + // deliveryURL + //////////////////////////////////////////////// + + private String deliveryURL = ""; + + public String getDeliveryURL() { + return deliveryURL; + } + + public void setDeliveryURL(String deliveryURL) { + this.deliveryURL = deliveryURL; + try { + URL url = new URL(deliveryURL); + deliveryHost = url.getHost(); + deliveryPath = url.getPath(); + deliveryPort = url.getPort(); + } + catch (Exception e) {} + } + + private String deliveryHost = ""; + private String deliveryPath = ""; + private int deliveryPort = 0; + + public String getDeliveryHost() { + return deliveryHost; + } + + public String getDeliveryPath() { + return deliveryPath; + } + + public int getDeliveryPort() { + return deliveryPort; + } + + + //////////////////////////////////////////////// + // Timeout + //////////////////////////////////////////////// + + private long timeOut = 0; + + public long getTimeOut() { + return timeOut; + } + + public void setTimeOut(long value) { + timeOut = value; + } + + public boolean isExpired() + { + long currTime = System.currentTimeMillis(); + + // Thanks for Oliver Newell (10/26/04) + if(timeOut == Subscription.INFINITE_VALUE ) + return false; + + // Thanks for Oliver Newell (10/26/04) + long expiredTime = getSubscriptionTime() + getTimeOut()*1000; + if (expiredTime < currTime) + return true; + + return false; + } + + //////////////////////////////////////////////// + // SubscriptionTIme + //////////////////////////////////////////////// + + private long subscriptionTime = 0; + + public long getSubscriptionTime() { + return subscriptionTime; + } + + public void setSubscriptionTime(long time) { + subscriptionTime = time; + } + + //////////////////////////////////////////////// + // SEQ + //////////////////////////////////////////////// + + private long notifyCount = 0; + + public long getNotifyCount() { + return notifyCount; + } + + public void setNotifyCount(int cnt) { + notifyCount = cnt; + } + + public void incrementNotifyCount() { + if (notifyCount == Long.MAX_VALUE) { + notifyCount = 1; + return; + } + notifyCount++; + } + + //////////////////////////////////////////////// + // renew + //////////////////////////////////////////////// + + public void renew() + { + setSubscriptionTime(System.currentTimeMillis()); + setNotifyCount(0); + } + +} diff --git a/router/java/src/org/cybergarage/upnp/event/SubscriberList.java b/router/java/src/org/cybergarage/upnp/event/SubscriberList.java new file mode 100644 index 000000000..0a991204a --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/event/SubscriberList.java @@ -0,0 +1,48 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SubscriberList.java +* +* Revision; +* +* 01/31/03 +* - first revision. +* 06/18/03 +* - Fixed to catch ArrayIndexOutOfBounds. +* +******************************************************************/ + +package org.cybergarage.upnp.event; + +import java.util.*; + +public class SubscriberList extends Vector +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + private static final long serialVersionUID = -648427977601494972L; + + public SubscriberList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public Subscriber getSubscriber(int n) + { + Object obj = null; + try { + obj = get(n); + } + catch (Exception e) {} + return (Subscriber)obj; + } +} + diff --git a/router/java/src/org/cybergarage/upnp/event/Subscription.java b/router/java/src/org/cybergarage/upnp/event/Subscription.java new file mode 100644 index 000000000..c03638daa --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/event/Subscription.java @@ -0,0 +1,75 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: ST.java +* +* Revision; +* +* 01/31/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.event; + +import org.cybergarage.upnp.*; + +public class Subscription +{ + public final static String XMLNS = "urn:schemas-upnp-org:event-1-0"; + public final static String TIMEOUT_HEADER = "Second-"; + public final static String INFINITE_STRING = "infinite"; + public final static int INFINITE_VALUE = -1; + public final static String UUID = "uuid:"; + public final static String SUBSCRIBE_METHOD = "SUBSCRIBE"; + public final static String UNSUBSCRIBE_METHOD = "UNSUBSCRIBE"; + + //////////////////////////////////////////////// + // Timeout + //////////////////////////////////////////////// + + public final static String toTimeoutHeaderString(long time) + { + if (time == Subscription.INFINITE_VALUE) + return Subscription.INFINITE_STRING; + return Subscription.TIMEOUT_HEADER + Long.toString(time); + } + + public final static long getTimeout(String headerValue) + { + int minusIdx = headerValue.indexOf('-'); + long timeout = Subscription.INFINITE_VALUE; + try { + String timeoutStr = headerValue.substring(minusIdx+1, headerValue.length()); + timeout = Long.parseLong(timeoutStr); + } + catch (Exception e) {} + return timeout; + } + + //////////////////////////////////////////////// + // SID + //////////////////////////////////////////////// + + public static final String createSID() + { + return UPnP.createUUID(); + } + + public final static String toSIDHeaderString(String id) + { + return Subscription.UUID + id; + } + + public final static String getSID(String headerValue) + { + if (headerValue == null) + return ""; + return headerValue.substring(Subscription.UUID.length(), headerValue.length()); + } + +} + diff --git a/router/java/src/org/cybergarage/upnp/event/SubscriptionRequest.java b/router/java/src/org/cybergarage/upnp/event/SubscriptionRequest.java new file mode 100644 index 000000000..a3e11cc65 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/event/SubscriptionRequest.java @@ -0,0 +1,221 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SubscriptionRequest.java +* +* Revision; +* +* 01/31/03 +* - first revision. +* 05/21/03 +* - Giordano Sassaroli +* - Description: inserted a check at the beginning of the setService method +* - Problem : If the EventSubURL does not start with a '/', the device could refuse event subscription +* - Error : it is not an error, but adding the '/' when missing allows the integration with the Intel devices +* 09/02/03 +* - Giordano Sassaroli +* - Problem : NullpointerException thrown for devices whose description use absolute urls +* - Error : the presence of a base url is not mandatory, the API code makes the assumption that control and event subscription urls are relative. If the baseUrl is not present, the request host and port should be extracted from the control/subscription url +* - Description: The method setRequestHost/setService should be changed as follows +* 06/11/04 +* - Markus Thurner (06/11/2004) +* - Changed setServie() to get the host address from the SSDP Location field when the URLBase is null. +* 12/06/04 +* - Grzegorz Lehmann +* - Stefano Lenzi +* - Fixed getSID() to loop between getSID() and hasSID(); +* +********************************************************************/ + +package org.cybergarage.upnp.event; + +import org.cybergarage.http.*; + +import org.cybergarage.upnp.*; +import org.cybergarage.upnp.device.*; + +public class SubscriptionRequest extends HTTPRequest +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SubscriptionRequest() + { + } + + public SubscriptionRequest(HTTPRequest httpReq) + { + set(httpReq); + } + + //////////////////////////////////////////////// + // setRequest + //////////////////////////////////////////////// + + private void setService(Service service) + { + String eventSubURL = service.getEventSubURL(); + + // Thanks for Giordano Sassaroli (05/21/03) + setURI(eventSubURL, true); + + String urlBaseStr = ""; + Device dev = service.getDevice(); + if (dev != null) + urlBaseStr = dev.getURLBase(); + + if (urlBaseStr == null || urlBaseStr.length() <= 0) { + Device rootDev = service.getRootDevice(); + if (rootDev != null) + urlBaseStr = rootDev.getURLBase(); + } + + // Thansk for Markus Thurner (06/11/2004) + if (urlBaseStr == null || urlBaseStr.length() <= 0) { + Device rootDev = service.getRootDevice(); + if (rootDev != null) + urlBaseStr = rootDev.getLocation(); + } + + // Thanks for Giordano Sassaroli (09/02/03) + if (urlBaseStr == null || urlBaseStr.length() <= 0) { + if (HTTP.isAbsoluteURL(eventSubURL)) + urlBaseStr = eventSubURL; + } + + String reqHost = HTTP.getHost(urlBaseStr); + int reqPort = HTTP.getPort(urlBaseStr); + + setHost(reqHost, reqPort); + setRequestHost(reqHost); + setRequestPort(reqPort); + } + + public void setSubscribeRequest(Service service, String callback, long timeout) + { + setMethod(Subscription.SUBSCRIBE_METHOD); + setService(service); + setCallback(callback); + setNT(NT.EVENT); + setTimeout(timeout); + } + + public void setRenewRequest(Service service, String uuid, long timeout) + { + setMethod(Subscription.SUBSCRIBE_METHOD); + setService(service); + setSID(uuid); + setTimeout(timeout); + } + + public void setUnsubscribeRequest(Service service) + { + setMethod(Subscription.UNSUBSCRIBE_METHOD); + setService(service); + setSID(service.getSID()); + } + + //////////////////////////////////////////////// + // NT + //////////////////////////////////////////////// + + public void setNT(String value) + { + setHeader(HTTP.NT, value); + } + + public String getNT() + { + return getHeaderValue(HTTP.NT); + } + + public boolean hasNT() + { + String nt = getNT(); + return (nt != null && 0 < nt.length()) ? true : false; + } + + //////////////////////////////////////////////// + // CALLBACK + //////////////////////////////////////////////// + + private final static String CALLBACK_START_WITH = "<"; + private final static String CALLBACK_END_WITH = ">"; + + public void setCallback(String value) + { + setStringHeader(HTTP.CALLBACK, value, CALLBACK_START_WITH, CALLBACK_END_WITH); + } + + public String getCallback() + { + return getStringHeaderValue(HTTP.CALLBACK, CALLBACK_START_WITH, CALLBACK_END_WITH); + } + + public boolean hasCallback() + { + String callback = getCallback(); + return (callback != null && 0 < callback.length()) ? true : false; + } + + //////////////////////////////////////////////// + // SID + //////////////////////////////////////////////// + + public void setSID(String id) + { + setHeader(HTTP.SID, Subscription.toSIDHeaderString(id)); + } + + public String getSID() + { + // Thanks for Grzegorz Lehmann and Stefano Lenzi(12/06/04) + String sid = Subscription.getSID(getHeaderValue(HTTP.SID)); + if (sid == null) + return ""; + return sid; + } + + public boolean hasSID() + { + String sid = getSID(); + return (sid != null && 0 < sid.length()) ? true : false; + } + + //////////////////////////////////////////////// + // Timeout + //////////////////////////////////////////////// + + public final void setTimeout(long value) + { + setHeader(HTTP.TIMEOUT, Subscription.toTimeoutHeaderString(value)); + } + + public long getTimeout() + { + return Subscription.getTimeout(getHeaderValue(HTTP.TIMEOUT)); + } + + //////////////////////////////////////////////// + // post (Response) + //////////////////////////////////////////////// + + public void post(SubscriptionResponse subRes) + { + super.post(subRes); + } + + //////////////////////////////////////////////// + // post + //////////////////////////////////////////////// + + public SubscriptionResponse post() + { + HTTPResponse httpRes = post(getRequestHost(), getRequestPort()); + return new SubscriptionResponse(httpRes); + } +} diff --git a/router/java/src/org/cybergarage/upnp/event/SubscriptionResponse.java b/router/java/src/org/cybergarage/upnp/event/SubscriptionResponse.java new file mode 100644 index 000000000..cf60ecd56 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/event/SubscriptionResponse.java @@ -0,0 +1,84 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SubscriptionResponse.java +* +* Revision; +* +* 01/29/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.event; + +import org.cybergarage.upnp.*; +import org.cybergarage.http.*; + +public class SubscriptionResponse extends HTTPResponse +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SubscriptionResponse() + { + setServer(UPnP.getServerName()); + } + + public SubscriptionResponse(HTTPResponse httpRes) + { + super(httpRes); + } + + //////////////////////////////////////////////// + // Error + //////////////////////////////////////////////// + + public void setResponse(int code) + { + setStatusCode(code); + setContentLength(0); + } + + //////////////////////////////////////////////// + // Error + //////////////////////////////////////////////// + + public void setErrorResponse(int code) + { + setStatusCode(code); + setContentLength(0); + } + + //////////////////////////////////////////////// + // SID + //////////////////////////////////////////////// + + public void setSID(String id) + { + setHeader(HTTP.SID, Subscription.toSIDHeaderString(id)); + } + + public String getSID() + { + return Subscription.getSID(getHeaderValue(HTTP.SID)); + } + + //////////////////////////////////////////////// + // Timeout + //////////////////////////////////////////////// + + public void setTimeout(long value) + { + setHeader(HTTP.TIMEOUT, Subscription.toTimeoutHeaderString(value)); + } + + public long getTimeout() + { + return Subscription.getTimeout(getHeaderValue(HTTP.TIMEOUT)); + } +} diff --git a/router/java/src/org/cybergarage/upnp/ssdp/HTTPMUSocket.java b/router/java/src/org/cybergarage/upnp/ssdp/HTTPMUSocket.java new file mode 100644 index 000000000..4f6361f59 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/HTTPMUSocket.java @@ -0,0 +1,198 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: HTTPMU.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* 09/03/03 +* - Changed to open the socket using setReuseAddress(). +* 12/10/03 +* - Fixed getLocalAddress() to return a valid interface address. +* 02/28/04 +* - Added getMulticastInetAddress(), getMulticastAddress(). +* 11/19/04 +* - Theo Beisch +* - Changed send() to set the TTL as 4. +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import java.net.*; +import java.util.*; + +import org.cybergarage.http.*; +import org.cybergarage.util.*; + +public class HTTPMUSocket +{ + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private InetSocketAddress ssdpMultiGroup = null; + private MulticastSocket ssdpMultiSock = null; + private NetworkInterface ssdpMultiIf = null; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public HTTPMUSocket() + { + } + + public HTTPMUSocket(String addr, int port, String bindAddr) + { + open(addr, port, bindAddr); + } + + protected void finalize() + { + close(); + } + + //////////////////////////////////////////////// + // bindAddr + //////////////////////////////////////////////// + + public String getLocalAddress() + { + InetAddress mcastAddr = ssdpMultiGroup.getAddress(); + Enumeration addrs = ssdpMultiIf.getInetAddresses(); + while (addrs.hasMoreElements()) { + InetAddress addr = (InetAddress)addrs.nextElement(); + if (mcastAddr instanceof Inet6Address && addr instanceof Inet6Address) + return addr.getHostAddress(); + if (mcastAddr instanceof Inet4Address && addr instanceof Inet4Address) + return addr.getHostAddress(); + } + return ""; + } + + //////////////////////////////////////////////// + // MulticastAddr + //////////////////////////////////////////////// + + public InetAddress getMulticastInetAddress() + { + return ssdpMultiGroup.getAddress(); + } + + public String getMulticastAddress() + { + return getMulticastInetAddress().getHostAddress(); + } + + //////////////////////////////////////////////// + // open/close + //////////////////////////////////////////////// + + public boolean open(String addr, int port, String bindAddr) + { + try { + ssdpMultiSock = new MulticastSocket(null); + ssdpMultiSock.setReuseAddress(true); + InetSocketAddress bindSockAddr = new InetSocketAddress(port); + ssdpMultiSock.bind(bindSockAddr); + ssdpMultiGroup = new InetSocketAddress(InetAddress.getByName(addr), port); + ssdpMultiIf = NetworkInterface.getByInetAddress(InetAddress.getByName(bindAddr)); + ssdpMultiSock.joinGroup(ssdpMultiGroup, ssdpMultiIf); + } + catch (Exception e) { + Debug.warning(e); + return false; + } + + return true; + } + + public boolean close() + { + if (ssdpMultiSock == null) + return true; + + try { + ssdpMultiSock.leaveGroup(ssdpMultiGroup, ssdpMultiIf); + ssdpMultiSock = null; + } + catch (Exception e) { + //Debug.warning(e); + return false; + } + + return true; + } + + //////////////////////////////////////////////// + // send + //////////////////////////////////////////////// + + public boolean send(String msg, String bindAddr, int bindPort) + { + try { + MulticastSocket msock; + if ((bindAddr) != null && (0 < bindPort)) { + msock = new MulticastSocket(null); + msock.bind(new InetSocketAddress(bindAddr, bindPort)); + } + else + msock = new MulticastSocket(); + DatagramPacket dgmPacket = new DatagramPacket(msg.getBytes(), msg.length(), ssdpMultiGroup); + // Thnaks for Tho Beisch (11/09/04) + msock.setTimeToLive(4); + msock.send(dgmPacket); + msock.close(); + } + catch (Exception e) { + Debug.warning(e); + return false; + } + return true; + } + + public boolean send(String msg) + { + return send(msg, null, -1); + } + + //////////////////////////////////////////////// + // post (HTTPRequest) + //////////////////////////////////////////////// + + public boolean post(HTTPRequest req, String bindAddr, int bindPort) + { + return send(req.toString(), bindAddr, bindPort); + } + + public boolean post(HTTPRequest req) + { + return send(req.toString(), null, -1); + } + + //////////////////////////////////////////////// + // reveive + //////////////////////////////////////////////// + + public SSDPPacket receive() + { + byte ssdvRecvBuf[] = new byte[SSDP.RECV_MESSAGE_BUFSIZE]; + SSDPPacket recvPacket = new SSDPPacket(ssdvRecvBuf, ssdvRecvBuf.length); + recvPacket.setLocalAddress(getLocalAddress()); + try { + ssdpMultiSock.receive(recvPacket.getDatagramPacket()); + recvPacket.setTimeStamp(System.currentTimeMillis()); + } + catch (Exception e) { + //Debug.warning(e); + } + return recvPacket; + } +} + diff --git a/router/java/src/org/cybergarage/upnp/ssdp/HTTPUSocket.java b/router/java/src/org/cybergarage/upnp/ssdp/HTTPUSocket.java new file mode 100644 index 000000000..221311c55 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/HTTPUSocket.java @@ -0,0 +1,239 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: HTTPMU.java +* +* Revision; +* +* 11/20/02 +* - first revision. +* 12/12/03 +* - Inma Mar?n +* - Changed open(addr, port) to send IPv6 SSDP packets. +* - The socket binds only the port without the interface address. +* - The full binding socket can send SSDP IPv4 packets. Is it a bug of J2SE v.1.4.2-b28 ?. +* 01/06/04 +* - Oliver Newell +* - Added to set a current timestamp when the packet are received. +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import java.net.*; + +import org.cybergarage.util.*; + +public class HTTPUSocket +{ + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private DatagramSocket ssdpUniSock = null; + //private MulticastSocket ssdpUniSock = null; + + public DatagramSocket getDatagramSocket() + { + return ssdpUniSock; + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public HTTPUSocket() + { + open(); + } + + public HTTPUSocket(String bindAddr, int bindPort) + { + open(bindAddr, bindPort); + } + + public HTTPUSocket(int bindPort) + { + open(bindPort); + } + + protected void finalize() + { + close(); + } + + //////////////////////////////////////////////// + // bindAddr + //////////////////////////////////////////////// + + private String localAddr = ""; + + public void setLocalAddress(String addr) + { + localAddr = addr; + } + + public String getLocalAddress() + { + if (0 < localAddr.length()) + return localAddr; + return ssdpUniSock.getLocalAddress().getHostAddress(); + } + + //////////////////////////////////////////////// + // open + //////////////////////////////////////////////// + + public boolean open() + { + close(); + + try { + ssdpUniSock = new DatagramSocket(); + } + catch (Exception e) { + Debug.warning(e); + return false; + } + + return true; + } + + public boolean open(String bindAddr, int bindPort) + { + close(); + + try { + // Bind only using the port without the interface address. (2003/12/12) + InetSocketAddress bindSock = new InetSocketAddress(/*InetAddress.getByName(bindAddr), */ bindPort); + ssdpUniSock = new DatagramSocket(null); + ssdpUniSock.setReuseAddress(true); + ssdpUniSock.bind(bindSock); + } + catch (Exception e) { + Debug.warning(e); + return false; + } + + setLocalAddress(bindAddr); + + return true; + } + + public boolean open(int bindPort) + { + close(); + + try { + InetSocketAddress bindSock = new InetSocketAddress(bindPort); + ssdpUniSock = new DatagramSocket(null); + ssdpUniSock.setReuseAddress(true); + ssdpUniSock.bind(bindSock); + } + catch (Exception e) { + //Debug.warning(e); + return false; + } + + return true; + } + + //////////////////////////////////////////////// + // close + //////////////////////////////////////////////// + + public boolean close() + { + if (ssdpUniSock == null) + return true; + + try { + ssdpUniSock.close(); + ssdpUniSock = null; + } + catch (Exception e) { + Debug.warning(e); + return false; + } + + return true; + } + + //////////////////////////////////////////////// + // send + //////////////////////////////////////////////// + + public boolean post(String addr, int port, String msg) + { + try { + InetAddress inetAddr = InetAddress.getByName(addr); + DatagramPacket dgmPacket = new DatagramPacket(msg.getBytes(), msg.length(), inetAddr, port); + ssdpUniSock.send(dgmPacket); + } + catch (Exception e) { + Debug.warning("addr = " +ssdpUniSock.getLocalAddress().getHostName()); + Debug.warning("port = " + ssdpUniSock.getLocalPort()); + Debug.warning(e); + return false; + } + return true; + } + + //////////////////////////////////////////////// + // reveive + //////////////////////////////////////////////// + + public SSDPPacket receive() + { + byte ssdvRecvBuf[] = new byte[SSDP.RECV_MESSAGE_BUFSIZE]; + SSDPPacket recvPacket = new SSDPPacket(ssdvRecvBuf, ssdvRecvBuf.length); + recvPacket.setLocalAddress(getLocalAddress()); + try { + ssdpUniSock.receive(recvPacket.getDatagramPacket()); + recvPacket.setTimeStamp(System.currentTimeMillis()); + } + catch (Exception e) { + //Debug.warning(e); + return null; + } + return recvPacket; + } + + //////////////////////////////////////////////// + // join/leave + //////////////////////////////////////////////// + +/* + boolean joinGroup(String mcastAddr, int mcastPort, String bindAddr) + { + try { + InetSocketAddress mcastGroup = new InetSocketAddress(InetAddress.getByName(mcastAddr), mcastPort); + NetworkInterface mcastIf = NetworkInterface.getByInetAddress(InetAddress.getByName(bindAddr)); + ssdpUniSock.joinGroup(mcastGroup, mcastIf); + } + catch (Exception e) { + Debug.warning(e); + return false; + } + return true; + } + + boolean leaveGroup(String mcastAddr, int mcastPort, String bindAddr) + { + try { + InetSocketAddress mcastGroup = new InetSocketAddress(InetAddress.getByName(mcastAddr), mcastPort); + NetworkInterface mcastIf = NetworkInterface.getByInetAddress(InetAddress.getByName(bindAddr)); + ssdpUniSock.leaveGroup(mcastGroup, mcastIf); + } + catch (Exception e) { + Debug.warning(e); + return false; + } + return true; + } +*/ +} + diff --git a/router/java/src/org/cybergarage/upnp/ssdp/SSDP.java b/router/java/src/org/cybergarage/upnp/ssdp/SSDP.java new file mode 100644 index 000000000..8f692355f --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/SSDP.java @@ -0,0 +1,77 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SSDP.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* 05/13/03 +* - Added constants for IPv6. +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +public class SSDP +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public static final int PORT = 1900; + + public static final String ADDRESS = "239.255.255.250"; + + public static final String IPV6_LINK_LOCAL_ADDRESS = "FF02::C"; + public static final String IPV6_SUBNET_ADDRESS = "FF03::C"; + public static final String IPV6_ADMINISTRATIVE_ADDRESS = "FF04::C"; + public static final String IPV6_SITE_LOCAL_ADDRESS = "FF05::C"; + public static final String IPV6_GLOBAL_ADDRESS = "FF0E::C"; + + private static String IPV6_ADDRESS; + + public static final void setIPv6Address(String addr) + { + IPV6_ADDRESS = addr; + } + + public static final String getIPv6Address() + { + return IPV6_ADDRESS; + } + + public static final int DEFAULT_MSEARCH_MX = 3; + + public static final int RECV_MESSAGE_BUFSIZE = 1024; + + //////////////////////////////////////////////// + // Initialize + //////////////////////////////////////////////// + + static + { + setIPv6Address(IPV6_LINK_LOCAL_ADDRESS); + } + + //////////////////////////////////////////////// + // LeaseTime + //////////////////////////////////////////////// + + public final static int getLeaseTime(String cacheCont) + { + int equIdx = cacheCont.indexOf('='); + int mx = 0; + try { + String mxStr = new String(cacheCont.getBytes(), equIdx+1, cacheCont.length() - (equIdx+1)); + mx = Integer.parseInt(mxStr); + } + catch (Exception e) {} + return mx; + } +} + diff --git a/router/java/src/org/cybergarage/upnp/ssdp/SSDPNotifyRequest.java b/router/java/src/org/cybergarage/upnp/ssdp/SSDPNotifyRequest.java new file mode 100644 index 000000000..b2fb3c5c0 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/SSDPNotifyRequest.java @@ -0,0 +1,31 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SSDPMSearchRequest.java +* +* Revision; +* +* 01/14/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import org.cybergarage.http.*; + +public class SSDPNotifyRequest extends SSDPRequest +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SSDPNotifyRequest() + { + setMethod(HTTP.NOTIFY); + setURI("*"); + } +} diff --git a/router/java/src/org/cybergarage/upnp/ssdp/SSDPNotifySocket.java b/router/java/src/org/cybergarage/upnp/ssdp/SSDPNotifySocket.java new file mode 100644 index 000000000..6a33af7c3 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/SSDPNotifySocket.java @@ -0,0 +1,131 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: SSDPNotifySocket.java +* +* Revision; +* +* 11/20/02 +* - first revision. +* 05/13/03 +* - Added support for IPv6. +* 02/20/04 +* - Inma Marin Lopez +* - Added a multicast filter using the SSDP pakcet. +* 04/20/05 +* - Mikael Hakman +* - Handle receive() returning null. +* - Added close() in stop(). +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import java.net.*; + +import org.cybergarage.net.*; +import org.cybergarage.util.*; +import org.cybergarage.http.*; +import org.cybergarage.upnp.*; + +public class SSDPNotifySocket extends HTTPMUSocket implements Runnable +{ + private boolean useIPv6Address; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SSDPNotifySocket(String bindAddr) + { + String addr = SSDP.ADDRESS; + useIPv6Address = false; + if (HostInterface.isIPv6Address(bindAddr) == true) { + addr = SSDP.getIPv6Address(); + useIPv6Address = true; + } + open(addr, SSDP.PORT, bindAddr); + setControlPoint(null); + } + + //////////////////////////////////////////////// + // ControlPoint + //////////////////////////////////////////////// + + private ControlPoint controlPoint = null; + + public void setControlPoint(ControlPoint ctrlp) + { + this.controlPoint = ctrlp; + } + + public ControlPoint getControlPoint() + { + return controlPoint; + } + + //////////////////////////////////////////////// + // post (SSDPNotifySocket) + //////////////////////////////////////////////// + + public boolean post(SSDPNotifyRequest req) + { + String ssdpAddr = SSDP.ADDRESS; + if (useIPv6Address == true) + ssdpAddr = SSDP.getIPv6Address(); + req.setHost(ssdpAddr, SSDP.PORT); + return post((HTTPRequest)req); + } + + //////////////////////////////////////////////// + // run + //////////////////////////////////////////////// + + private Thread deviceNotifyThread = null; + + public void run() + { + Thread thisThread = Thread.currentThread(); + + ControlPoint ctrlPoint = getControlPoint(); + + while (deviceNotifyThread == thisThread) { + Thread.yield(); + SSDPPacket packet = receive(); + + // Thanks for Mikael Hakman (04/20/05) + if (packet == null) + continue; + + // Thanks for Inma (02/20/04) + InetAddress maddr = getMulticastInetAddress(); + InetAddress pmaddr = packet.getHostInetAddress(); + if (maddr.equals(pmaddr) == false) { + Debug.warning("Invalidate Multicast Recieved : " + maddr + "," + pmaddr); + continue; + } + + if (ctrlPoint != null) + ctrlPoint.notifyReceived(packet); + } + } + + public void start() + { + deviceNotifyThread = new Thread(this, "UPnP-SSDPNotifySocket"); + deviceNotifyThread.setDaemon(true); + deviceNotifyThread.start(); + } + + public void stop() + { + // Thanks for Mikael Hakman (04/20/05) + close(); + + deviceNotifyThread = null; + } +} + diff --git a/router/java/src/org/cybergarage/upnp/ssdp/SSDPNotifySocketList.java b/router/java/src/org/cybergarage/upnp/ssdp/SSDPNotifySocketList.java new file mode 100644 index 000000000..d0541510c --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/SSDPNotifySocketList.java @@ -0,0 +1,106 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: HTTPServerList.java +* +* Revision; +* +* 05/11/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import java.util.*; + +import org.cybergarage.net.*; + +import org.cybergarage.upnp.*; + +public class SSDPNotifySocketList extends Vector +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + private static final long serialVersionUID = -7066290881503106399L; + + public SSDPNotifySocketList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public SSDPNotifySocket getSSDPNotifySocket(int n) + { + return (SSDPNotifySocket)get(n); + } + + //////////////////////////////////////////////// + // ControlPoint + //////////////////////////////////////////////// + + public void setControlPoint(ControlPoint ctrlPoint) + { + int nSockets = size(); + for (int n=0; n +* - Fixed isRootDevice() to check the ST header. +* 11/19/04 +* - Theo Beisch +* - Changed getRemoteAddress() to return the adresss instead of the host name. +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import java.net.*; + +import org.cybergarage.http.*; + +import org.cybergarage.upnp.device.*; + +public class SSDPPacket +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SSDPPacket(byte[] buf, int length) + { + dgmPacket = new DatagramPacket(buf, length); + } + + //////////////////////////////////////////////// + // DatagramPacket + //////////////////////////////////////////////// + + private DatagramPacket dgmPacket = null; + + public DatagramPacket getDatagramPacket() + { + return dgmPacket; + } + + //////////////////////////////////////////////// + // addr + //////////////////////////////////////////////// + + private String localAddr = ""; + + public void setLocalAddress(String addr) + { + localAddr = addr; + } + + public String getLocalAddress() + { + return localAddr; + } + + + //////////////////////////////////////////////// + // Time + //////////////////////////////////////////////// + + private long timeStamp; + + public void setTimeStamp(long value) + { + timeStamp = value; + } + + public long getTimeStamp() + { + return timeStamp; + } + + //////////////////////////////////////////////// + // Remote host + //////////////////////////////////////////////// + + public InetAddress getRemoteInetAddress() + { + return getDatagramPacket().getAddress(); + } + + public String getRemoteAddress() + { + // Thanks for Theo Beisch (11/09/04) + return getDatagramPacket().getAddress().getHostAddress(); + } + + public int getRemotePort() + { + return getDatagramPacket().getPort(); + } + + //////////////////////////////////////////////// + // Access Methods + //////////////////////////////////////////////// + + public byte[] packetBytes = null; + + public byte[] getData() + { + if (packetBytes != null) + return packetBytes; + + DatagramPacket packet = getDatagramPacket(); + int packetLen = packet.getLength(); + String packetData = new String(packet.getData(), 0, packetLen); + packetBytes = packetData.getBytes(); + + return packetBytes; + } + + //////////////////////////////////////////////// + // Access Methods + //////////////////////////////////////////////// + + public String getHost() + { + return HTTPHeader.getValue(getData(), HTTP.HOST); + } + + public String getCacheControl() + { + return HTTPHeader.getValue(getData(), HTTP.CACHE_CONTROL); + } + + public String getLocation() + { + return HTTPHeader.getValue(getData(), HTTP.LOCATION); + } + + public String getMAN() + { + return HTTPHeader.getValue(getData(), HTTP.MAN); + } + + public String getST() + { + return HTTPHeader.getValue(getData(), HTTP.ST); + } + + public String getNT() + { + return HTTPHeader.getValue(getData(), HTTP.NT); + } + + public String getNTS() + { + return HTTPHeader.getValue(getData(), HTTP.NTS); + } + + public String getServer() + { + return HTTPHeader.getValue(getData(), HTTP.SERVER); + } + + public String getUSN() + { + return HTTPHeader.getValue(getData(), HTTP.USN); + } + + public int getMX() + { + return HTTPHeader.getIntegerValue(getData(), HTTP.MX); + } + + //////////////////////////////////////////////// + // Access Methods + //////////////////////////////////////////////// + + public InetAddress getHostInetAddress() + { + String addrStr = "127.0.0.1"; + String host = getHost(); + int canmaIdx = host.lastIndexOf(":"); + if (0 <= canmaIdx) { + addrStr = host.substring(0, canmaIdx); + if (addrStr.charAt(0) == '[') + addrStr = addrStr.substring(1, addrStr.length()); + if (addrStr.charAt(addrStr.length()-1) == ']') + addrStr = addrStr.substring(0, addrStr.length()-1); + } + InetSocketAddress isockaddr = new InetSocketAddress(addrStr, 0); + return isockaddr.getAddress(); + } + + //////////////////////////////////////////////// + // Access Methods (Extension) + //////////////////////////////////////////////// + + public boolean isRootDevice() + { + if (NT.isRootDevice(getNT()) == true) + return true; + // Thanks for Theo Beisch (11/01/04) + if (ST.isRootDevice(getST()) == true) + return true; + return USN.isRootDevice(getUSN()); + } + + public boolean isDiscover() + { + return MAN.isDiscover(getMAN()); + } + + public boolean isAlive() + { + return NTS.isAlive(getNTS()); + } + + public boolean isByeBye() + { + return NTS.isByeBye(getNTS()); + } + + public int getLeaseTime() + { + return SSDP.getLeaseTime(getCacheControl()); + } + + //////////////////////////////////////////////// + // toString + //////////////////////////////////////////////// + + public String toString() + { + return new String(getData()); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/ssdp/SSDPRequest.java b/router/java/src/org/cybergarage/upnp/ssdp/SSDPRequest.java new file mode 100644 index 000000000..f40442f18 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/SSDPRequest.java @@ -0,0 +1,104 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SSDPRequest.java +* +* Revision; +* +* 01/14/03 +* - first revision. +* 03/16/04 +* - Thanks for Darrell Young +* - Fixed to set v1.1 to the HTTP version.; +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import org.cybergarage.http.*; + +public class SSDPRequest extends HTTPRequest +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SSDPRequest() + { + setVersion(HTTP.VERSION_11); + } + + //////////////////////////////////////////////// + // NT + //////////////////////////////////////////////// + + public void setNT(String value) + { + setHeader(HTTP.NT, value); + } + + public String getNT() + { + return getHeaderValue(HTTP.NT); + } + + //////////////////////////////////////////////// + // NTS + //////////////////////////////////////////////// + + public void setNTS(String value) + { + setHeader(HTTP.NTS, value); + } + + public String getNTS() + { + return getHeaderValue(HTTP.NTS); + } + + //////////////////////////////////////////////// + // Location + //////////////////////////////////////////////// + + public void setLocation(String value) + { + setHeader(HTTP.LOCATION, value); + } + + public String getLocation() + { + return getHeaderValue(HTTP.LOCATION); + } + + //////////////////////////////////////////////// + // USN + //////////////////////////////////////////////// + + public void setUSN(String value) + { + setHeader(HTTP.USN, value); + } + + public String getUSN() + { + return getHeaderValue(HTTP.USN); + } + + //////////////////////////////////////////////// + // CacheControl + //////////////////////////////////////////////// + + public void setLeaseTime(int len) + { + setHeader(HTTP.CACHE_CONTROL, "max-age=" + Integer.toString(len)); + } + + public int getLeaseTime() + { + String cacheCtrl = getHeaderValue(HTTP.CACHE_CONTROL); + return SSDP.getLeaseTime(cacheCtrl); + } +} diff --git a/router/java/src/org/cybergarage/upnp/ssdp/SSDPResponse.java b/router/java/src/org/cybergarage/upnp/ssdp/SSDPResponse.java new file mode 100644 index 000000000..31b4ed280 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/SSDPResponse.java @@ -0,0 +1,126 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SSDPResponse.java +* +* Revision; +* +* 01/14/03 +* - first revision. +* 01/23/04 +* - Oliver Newell +* - Overided HTTPResponse::getHeader() for Intel UPnP control points. +* 03/16/04 +* - Thanks for Darrell Young +* - Fixed to set v1.1 to the HTTP version. +* 10/20/04 +* - Brent Hills +* - Added setMYNAME() and getMYNAME(). +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import org.cybergarage.http.*; + +public class SSDPResponse extends HTTPResponse +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SSDPResponse() + { + setVersion(HTTP.VERSION_11); + } + + //////////////////////////////////////////////// + // ST (SearchTarget) + //////////////////////////////////////////////// + + public void setST(String value) + { + setHeader(HTTP.ST, value); + } + + public String getST() + { + return getHeaderValue(HTTP.ST); + } + + //////////////////////////////////////////////// + // Location + //////////////////////////////////////////////// + + public void setLocation(String value) + { + setHeader(HTTP.LOCATION, value); + } + + public String getLocation() + { + return getHeaderValue(HTTP.LOCATION); + } + + //////////////////////////////////////////////// + // USN + //////////////////////////////////////////////// + + public void setUSN(String value) + { + setHeader(HTTP.USN, value); + } + + public String getUSN() + { + return getHeaderValue(HTTP.USN); + } + + //////////////////////////////////////////////// + // MYNAME + //////////////////////////////////////////////// + + public void setMYNAME(String value) + { + setHeader(HTTP.MYNAME, value); + } + + public String getMYNAME() + { + return getHeaderValue(HTTP.MYNAME); + } + + //////////////////////////////////////////////// + // CacheControl + //////////////////////////////////////////////// + + public void setLeaseTime(int len) + { + setHeader(HTTP.CACHE_CONTROL, "max-age=" + Integer.toString(len)); + } + + public int getLeaseTime() + { + String cacheCtrl = getHeaderValue(HTTP.CACHE_CONTROL); + return SSDP.getLeaseTime(cacheCtrl); + } + + //////////////////////////////////////////////// + // getHeader (Override) + //////////////////////////////////////////////// + + public String getHeader() + { + StringBuilder str = new StringBuilder(); + + str.append(getStatusLineString()); + str.append(getHeaderString()); + str.append(HTTP.CRLF); // for Intel UPnP control points. + + return str.toString(); + } + +} diff --git a/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchRequest.java b/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchRequest.java new file mode 100644 index 000000000..fbe7cf812 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchRequest.java @@ -0,0 +1,61 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SSDPMSearchRequest.java +* +* Revision; +* +* 11/19/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import org.cybergarage.net.*; +import org.cybergarage.http.*; + +import org.cybergarage.upnp.device.*; + +public class SSDPSearchRequest extends SSDPRequest +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SSDPSearchRequest(String serachTarget, int mx) + { + setMethod(HTTP.M_SEARCH); + setURI("*"); + + setHeader(HTTP.ST, serachTarget); + setHeader(HTTP.MX, Integer.toString(mx)); + setHeader(HTTP.MAN, "\"" + MAN.DISCOVER + "\""); + } + + public SSDPSearchRequest(String serachTarget) + { + this(serachTarget, SSDP.DEFAULT_MSEARCH_MX); + } + + public SSDPSearchRequest() + { + this(ST.ROOT_DEVICE); + } + + //////////////////////////////////////////////// + // HOST + //////////////////////////////////////////////// + + public void setLocalAddress(String bindAddr) + { + String ssdpAddr = SSDP.ADDRESS; + if (HostInterface.isIPv6Address(bindAddr) == true) + ssdpAddr = SSDP.getIPv6Address(); + setHost(ssdpAddr, SSDP.PORT); + } + +} diff --git a/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchResponse.java b/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchResponse.java new file mode 100644 index 000000000..264f54429 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchResponse.java @@ -0,0 +1,35 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SSDPSearchResponse.java +* +* Revision; +* +* 01/14/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import org.cybergarage.http.*; + +import org.cybergarage.upnp.*; + +public class SSDPSearchResponse extends SSDPResponse +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SSDPSearchResponse() + { + setStatusCode(HTTPStatus.OK); + setCacheControl(Device.DEFAULT_LEASE_TIME); + setHeader(HTTP.SERVER, UPnP.getServerName()); + setHeader(HTTP.EXT, ""); + } +} diff --git a/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocket.java b/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocket.java new file mode 100644 index 000000000..61668872a --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocket.java @@ -0,0 +1,107 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SSDPSearchResponseSocket.java +* +* Revision; +* +* 11/20/02 +* - first revision. +* 05/28/03 +* - Added post() to send a SSDPSearchRequest. +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import org.cybergarage.upnp.*; + +public class SSDPSearchResponseSocket extends HTTPUSocket implements Runnable +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SSDPSearchResponseSocket() + { + setControlPoint(null); + } + + public SSDPSearchResponseSocket(String bindAddr, int port) + { + super(bindAddr, port); + setControlPoint(null); + } + + //////////////////////////////////////////////// + // ControlPoint + //////////////////////////////////////////////// + + private ControlPoint controlPoint = null; + + public void setControlPoint(ControlPoint ctrlp) + { + this.controlPoint = ctrlp; + } + + public ControlPoint getControlPoint() + { + return controlPoint; + } + + //////////////////////////////////////////////// + // run + //////////////////////////////////////////////// + + private Thread deviceSearchResponseThread = null; + + public void run() + { + Thread thisThread = Thread.currentThread(); + + ControlPoint ctrlPoint = getControlPoint(); + + while (deviceSearchResponseThread == thisThread) { + Thread.yield(); + SSDPPacket packet = receive(); + if (packet == null) + break; + if (ctrlPoint != null) + ctrlPoint.searchResponseReceived(packet); + } + } + + public void start() + { + deviceSearchResponseThread = new Thread(this, "UPnP-SSDPSearchResponseSocket"); + deviceSearchResponseThread.setDaemon(true); + deviceSearchResponseThread.start(); + } + + public void stop() + { + deviceSearchResponseThread = null; + } + + //////////////////////////////////////////////// + // post + //////////////////////////////////////////////// + + public boolean post(String addr, int port, SSDPSearchResponse res) + { + return post(addr, port, res.getHeader()); + } + + //////////////////////////////////////////////// + // post + //////////////////////////////////////////////// + + public boolean post(String addr, int port, SSDPSearchRequest req) + { + return post(addr, port, req.toString()); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocketList.java b/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocketList.java new file mode 100644 index 000000000..e9a758244 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocketList.java @@ -0,0 +1,144 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: SSDPSearchResponseSocketList.java +* +* Revision; +* +* 05/08/03 +* - first revision. +* 05/28/03 +* - Added post() to send a SSDPSearchRequest. +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import java.util.*; + +import org.cybergarage.net.*; + +import org.cybergarage.upnp.*; + +public class SSDPSearchResponseSocketList extends Vector +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + private static final long serialVersionUID = 4509857798038125744L; + + public SSDPSearchResponseSocketList() + { + } + + //////////////////////////////////////////////// + // ControlPoint + //////////////////////////////////////////////// + + public void setControlPoint(ControlPoint ctrlPoint) + { + int nSockets = size(); + for (int n=0; n +* - Added close() in stop(). +* - Added test for null return from receive() in run(). +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import org.cybergarage.net.*; +import org.cybergarage.util.*; + +import org.cybergarage.upnp.device.*; + +public class SSDPSearchSocket extends HTTPMUSocket implements Runnable +{ + public SSDPSearchSocket() + { + } + + public SSDPSearchSocket(String bindAddr) + { + open(bindAddr); + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public boolean open(String bindAddr) + { + String addr = SSDP.ADDRESS; + if (HostInterface.isIPv6Address(bindAddr) == true) { + addr = SSDP.getIPv6Address(); + } + return open(addr, SSDP.PORT, bindAddr); + } + + //////////////////////////////////////////////// + // deviceSearch + //////////////////////////////////////////////// + + private ListenerList deviceSearchListenerList = new ListenerList(); + + public void addSearchListener(SearchListener listener) + { + deviceSearchListenerList.add(listener); + } + + public void removeSearchListener(SearchListener listener) + { + deviceSearchListenerList.remove(listener); + } + + public void performSearchListener(SSDPPacket ssdpPacket) + { + int listenerSize = deviceSearchListenerList.size(); + for (int n=0; n +* - Added "&" and "\"" "\\" to toXMLString(). +* 11/19/04 +* - Theo Beisch +* - Changed XML::output() to use short notation when the tag value is null. +* 12/02/04 +* - Brian Owens +* - Fixed toXMLString() to convert from "'" to "'" instead of "\". +* +******************************************************************/ + +package org.cybergarage.xml; + +import java.io.ByteArrayOutputStream; +import java.io.PrintWriter; + +public class Node +{ + + public Node() + { + setUserData(null); + setParentNode(null); + } + + public Node(String name) + { + this(); + setName(name); + } + + public Node(String ns, String name) + { + this(); + setName(ns, name); + } + + //////////////////////////////////////////////// + // parent node + //////////////////////////////////////////////// + + private Node parentNode = null; + + public void setParentNode(Node node) + { + parentNode = node; + } + + public Node getParentNode() + { + return parentNode; + } + + //////////////////////////////////////////////// + // root node + //////////////////////////////////////////////// + + public Node getRootNode() + { + Node rootNode = null; + Node parentNode = getParentNode(); + while (parentNode != null) { + rootNode = parentNode; + parentNode = rootNode.getParentNode(); + } + return rootNode; + } + + //////////////////////////////////////////////// + // name + //////////////////////////////////////////////// + + private String name = new String(); + + public void setName(String name) + { + this.name = name; + } + + public void setName(String ns, String name) + { + this.name = ns + ":" + name; + } + + public String getName() + { + return name; + } + + public boolean isName(String value) + { + return name.equals(value); + } + + //////////////////////////////////////////////// + // value + //////////////////////////////////////////////// + + private String value = ""; + + public void setValue(String value) + { + this.value = value; + } + + public void setValue(int value) + { + setValue(Integer.toString(value)); + } + + public String getValue() + { + return value; + } + + //////////////////////////////////////////////// + // Attribute (Basic) + //////////////////////////////////////////////// + + private AttributeList attrList = new AttributeList(); + + public int getNAttributes() { + return attrList.size(); + } + + public Attribute getAttribute(int index) { + return attrList.getAttribute(index); + } + + public Attribute getAttribute(String name) + { + return attrList.getAttribute(name); + } + + public void addAttribute(Attribute attr) { + attrList.add(attr); + } + + public void insertAttributeAt(Attribute attr, int index) { + attrList.insertElementAt(attr, index); + } + + public void addAttribute(String name, String value) { + Attribute attr = new Attribute(name, value); + addAttribute(attr); + } + + public boolean removeAttribute(Attribute attr) { + return attrList.remove(attr); + } + + public boolean removeAttribute(String name) { + return removeAttribute(getAttribute(name)); + } + + public boolean hasAttributes() + { + if (0 < getNAttributes()) + return true; + return false; + } + + //////////////////////////////////////////////// + // Attribute (Extention) + //////////////////////////////////////////////// + + public void setAttribute(String name, String value) { + Attribute attr = getAttribute(name); + if (attr != null) { + attr.setValue(value); + return; + } + attr = new Attribute(name, value); + addAttribute(attr); + } + + public void setAttribute(String name, int value) { + setAttribute(name, Integer.toString(value)); + } + + public String getAttributeValue(String name) { + Attribute attr = getAttribute(name); + if (attr != null) + return attr.getValue(); + return ""; + } + + public int getAttributeIntegerValue(String name) { + String val = getAttributeValue(name); + try { + return Integer.parseInt(val); + } + catch (Exception e) {} + return 0; + } + + //////////////////////////////////////////////// + // Attribute (xmlns) + //////////////////////////////////////////////// + + public void setNameSpace(String ns, String value) + { + setAttribute("xmlns:" + ns, value); + } + + //////////////////////////////////////////////// + // Child node + //////////////////////////////////////////////// + + private NodeList nodeList = new NodeList(); + + public int getNNodes() { + return nodeList.size(); + } + + public Node getNode(int index) { + return nodeList.getNode(index); + } + + public Node getNode(String name) + { + return nodeList.getNode(name); + } + + public Node getNodeEndsWith(String name) + { + return nodeList.getEndsWith(name); + } + + public void addNode(Node node) { + node.setParentNode(this); + nodeList.add(node); + } + + public void insertNode(Node node, int index) { + node.setParentNode(this); + nodeList.insertElementAt(node, index); + } + + public boolean removeNode(Node node) { + node.setParentNode(null); + return nodeList.remove(node); + } + + public boolean removeNode(String name) { + return nodeList.remove(getNode(name)); + } + + public void removeAllNodes() + { + nodeList.clear(); + } + + public boolean hasNodes() + { + if (0 < getNNodes()) + return true; + return false; + } + + //////////////////////////////////////////////// + // Element (Child Node) + //////////////////////////////////////////////// + + public void setNode(String name, String value) { + Node node = getNode(name); + if (node != null) { + node.setValue(value); + return; + } + node = new Node(name); + node.setValue(value); + addNode(node); + } + + public String getNodeValue(String name) { + Node node = getNode(name); + if (node != null) + return node.getValue(); + return ""; + } + + //////////////////////////////////////////////// + // userData + //////////////////////////////////////////////// + + private Object userData = null; + + public void setUserData(Object data) + { + userData = data; + } + + public Object getUserData() + { + return userData; + } + + + //////////////////////////////////////////////// + // toString + //////////////////////////////////////////////// + + public String getIndentLevelString(int nIndentLevel) + { + char indentString[] = new char[nIndentLevel]; + for (int n=0; n + ps.println(" />"); + } else { + ps.println(">" + XML.escapeXMLChars(value) + ""); + } + + return; + } + + ps.print(indentString + "<" + name); + outputAttributes(ps); + ps.println(">"); + + int nChildNodes = getNNodes(); + for (int n=0; n"); + } + + public String toString(boolean hasChildNode) + { + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); + PrintWriter pr = new PrintWriter(byteOut); + output(pr, 0, hasChildNode); + pr.flush(); + return byteOut.toString(); + } + + @Override + public String toString() + { + return toString(true); + } + + public String toXMLString(boolean hasChildNode) + { + String xmlStr = toString(); + xmlStr = xmlStr.replaceAll("<", "<"); + xmlStr = xmlStr.replaceAll(">", ">"); + // Thanks for Theo Beisch (11/09/04) + xmlStr = xmlStr.replaceAll("&", "&"); + xmlStr = xmlStr.replaceAll("\"", """); + // Thanks for Brian Owens (12/02/04) + xmlStr = xmlStr.replaceAll("'", "'"); + return xmlStr; + } + + public String toXMLString() + { + return toXMLString(true); + } + + public void print(boolean hasChildNode) + { + PrintWriter pr = new PrintWriter(System.out); + output(pr, 0, hasChildNode); + pr.flush(); + } + + public void print() + { + print(true); + } +} diff --git a/router/java/src/org/cybergarage/xml/NodeList.java b/router/java/src/org/cybergarage/xml/NodeList.java new file mode 100644 index 000000000..53ac9bce8 --- /dev/null +++ b/router/java/src/org/cybergarage/xml/NodeList.java @@ -0,0 +1,65 @@ +/****************************************************************** +* +* CyberXML for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: NodeList.java +* +* Revision; +* +* 11/27/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.xml; + +import java.util.*; + +public class NodeList extends Vector +{ + private static final long serialVersionUID = 1528884682346143213L; + + public NodeList() + { + } + + public Node getNode(int n) + { + return (Node)get(n); + } + + public Node getNode(String name) + { + if (name == null) + return null; + + int nLists = size(); + for (int n=0; n': entity=">"; break; + case '\'': if (quote) { entity="'"; break; } + case '"': if (quote) { entity="""; break; } + } + if (entity != null) { + out.append(old,selstart,i-selstart); + out.append(entity); + selstart=i+1; + entity=null; + } + } + if (selstart == 0) + return input; + out.append(old,selstart,oldsize-selstart); + return out.toString(); + } + + public final static String escapeXMLChars(String input) + { + return escapeXMLChars(input, true); + } +} + diff --git a/router/java/src/org/cybergarage/xml/parser/JaxpParser.java b/router/java/src/org/cybergarage/xml/parser/JaxpParser.java new file mode 100644 index 000000000..8de6b06b8 --- /dev/null +++ b/router/java/src/org/cybergarage/xml/parser/JaxpParser.java @@ -0,0 +1,127 @@ +/****************************************************************** +* +* CyberXML for Java +* +* Copyright (C) Satoshi Konno 2004 +* +* Author: Markus Thurner (http://thoean.com) +* +* File: JaxpParser.java +* +* Revision; +* +* 06/15/04 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.xml.parser; + +import java.io.InputStream; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.cybergarage.xml.Node; +import org.cybergarage.xml.Parser; +import org.cybergarage.xml.ParserException; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.xml.sax.InputSource; + + +public class JaxpParser extends Parser +{ + + public JaxpParser() + { + super(); + } + + //////////////////////////////////////////////// + // parse (Node) + //////////////////////////////////////////////// + + public org.cybergarage.xml.Node parse(org.cybergarage.xml.Node parentNode, org.w3c.dom.Node domNode, int rank) + { + int domNodeType = domNode.getNodeType(); +// if (domNodeType != Node.ELEMENT_NODE) +// return; + + String domNodeName = domNode.getNodeName(); + String domNodeValue = domNode.getNodeValue(); + +// Debug.message("[" + rank + "] ELEM : " + domNodeName + ", " + domNodeValue + ", type = " + domNodeType + ", attrs = " + arrrsLen); + + if (domNodeType == org.w3c.dom.Node.TEXT_NODE) { + parentNode.setValue(domNodeValue); + return parentNode; + } + + if (domNodeType != org.w3c.dom.Node.ELEMENT_NODE) + return parentNode; + + org.cybergarage.xml.Node node = new org.cybergarage.xml.Node(); + node.setName(domNodeName); + node.setValue(domNodeValue); + + if (parentNode != null) + parentNode.addNode(node); + + NamedNodeMap attrMap = domNode.getAttributes(); + int attrLen = attrMap.getLength(); + //Debug.message("attrLen = " + attrLen); + for (int n = 0; n statuses); + +} diff --git a/router/java/src/org/freenetproject/ForwardPortStatus.java b/router/java/src/org/freenetproject/ForwardPortStatus.java new file mode 100644 index 000000000..88090da40 --- /dev/null +++ b/router/java/src/org/freenetproject/ForwardPortStatus.java @@ -0,0 +1,33 @@ +package org.freenetproject; + +public class ForwardPortStatus { + + public final int status; + /** The port forward definitely succeeded. */ + public static final int DEFINITE_SUCCESS = 3; + /** The port forward probably succeeded. I.e. it succeeded unless there was + * for example hostile action on the part of the router. */ + public static final int PROBABLE_SUCCESS = 2; + /** The port forward may have succeeded. Or it may not have. We should + * definitely try to check out of band. See UP&P: Many routers say they've + * forwarded the port when they haven't. */ + public static final int MAYBE_SUCCESS = 1; + /** The port forward is in progress */ + public static final int IN_PROGRESS = 0; + /** The port forward probably failed */ + public static final int PROBABLE_FAILURE = -1; + /** The port forward definitely failed. */ + public static final int DEFINITE_FAILURE = -2; + + public final String reasonString; + + /** Some plugins may need to change the external port. They can return it + * to the node here. */ + public final int externalPort; + + public ForwardPortStatus(int status, String reason, int externalPort) { + this.status = status; + this.reasonString = reason; + this.externalPort = externalPort; + } +} diff --git a/router/java/src/org/imports b/router/java/src/org/imports new file mode 100644 index 000000000..6fd39288b --- /dev/null +++ b/router/java/src/org/imports @@ -0,0 +1,57 @@ + * import java.io.IOException; + * import java.io.StringReader; + * import plugins.JabberLinker.org.xmlpull.v1.XmlPullParserException.html; + * import plugins.JabberLinker.org.xmlpull.v1.XmlPullParserFactory; + * import plugins.JabberLinker.org.xmlpull.v1.XmlPullParser; +//import plugins.UPnP.org.cybergarage.util.*; +import java.io.*; +import java.io.*; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.Reader; +import java.io.Writer; +import java.net.*; +import java.net.*; +import java.util.*; +import java.util.*; +import java.util.Calendar; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.TimeZone; +import java.util.Vector; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.xml.sax.InputSource; +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.org.cybergarage.net.*; +import plugins.UPnP.org.cybergarage.net.*; +import plugins.UPnP.org.cybergarage.soap.*; +import plugins.UPnP.org.cybergarage.soap.*; +import plugins.UPnP.org.cybergarage.upnp.*; +import plugins.UPnP.org.cybergarage.upnp.*; +import plugins.UPnP.org.cybergarage.upnp.Device; +import plugins.UPnP.org.cybergarage.upnp.control.*; +import plugins.UPnP.org.cybergarage.upnp.control.*; +import plugins.UPnP.org.cybergarage.upnp.device.*; +import plugins.UPnP.org.cybergarage.upnp.device.*; +import plugins.UPnP.org.cybergarage.upnp.event.*; +import plugins.UPnP.org.cybergarage.upnp.event.*; +import plugins.UPnP.org.cybergarage.upnp.ssdp.*; +import plugins.UPnP.org.cybergarage.upnp.ssdp.*; +import plugins.UPnP.org.cybergarage.upnp.xml.*; +import plugins.UPnP.org.cybergarage.upnp.xml.*; +import plugins.UPnP.org.cybergarage.util.*; +import plugins.UPnP.org.cybergarage.util.*; +import plugins.UPnP.org.cybergarage.xml.*; +import plugins.UPnP.org.cybergarage.xml.*; +import plugins.UPnP.org.cybergarage.xml.Node; +import plugins.UPnP.org.cybergarage.xml.Parser; +import plugins.UPnP.org.cybergarage.xml.ParserException; +import plugins.UPnP.org.cybergarage.xml.parser.*; diff --git a/router/java/src/org/xmlpull/v1/XmlPullParser.java b/router/java/src/org/xmlpull/v1/XmlPullParser.java new file mode 100644 index 000000000..24a34d2de --- /dev/null +++ b/router/java/src/org/xmlpull/v1/XmlPullParser.java @@ -0,0 +1,1116 @@ +/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/ +// for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/) + +package org.xmlpull.v1; + +import java.io.InputStream; +import java.io.IOException; +import java.io.Reader; + +/** + * XML Pull Parser is an interface that defines parsing functionlity provided + * in XMLPULL V1 API (visit this website to + * learn more about API and its implementations). + * + *

There are following different + * kinds of parser depending on which features are set:

    + *
  • non-validating parser as defined in XML 1.0 spec when + * FEATURE_PROCESS_DOCDECL is set to true + *
  • validating parser as defined in XML 1.0 spec when + * FEATURE_VALIDATION is true (and that implies that FEATURE_PROCESS_DOCDECL is true) + *
  • when FEATURE_PROCESS_DOCDECL is false (this is default and + * if different value is required necessary must be changed before parsing is started) + * then parser behaves like XML 1.0 compliant non-validating parser under condition that + * no DOCDECL is present in XML documents + * (internal entites can still be defined with defineEntityReplacementText()). + * This mode of operation is intened for operation in constrained environments such as J2ME. + *
+ * + * + *

There are two key methods: next() and nextToken(). While next() provides + * access to high level parsing events, nextToken() allows access to lower + * level tokens. + * + *

The current event state of the parser + * can be determined by calling the + * getEventType() method. + * Initially, the parser is in the START_DOCUMENT + * state. + * + *

The method next() advances the parser to the + * next event. The int value returned from next determines the current parser + * state and is identical to the value returned from following calls to + * getEventType (). + * + *

Th following event types are seen by next()

+ *
START_TAG
An XML start tag was read. + *
TEXT
Text content was read; + * the text content can be retreived using the getText() method. + * (when in validating mode next() will not report ignorable whitespaces, use nextToken() instead) + *
END_TAG
An end tag was read + *
END_DOCUMENT
No more events are available + *
+ * + *

after first next() or nextToken() (or any other next*() method) + * is called user application can obtain + * XML version, standalone and encoding from XML declaration + * in following ways:

    + *
  • version: + * getProperty("http://xmlpull.org/v1/doc/properties.html#xmldecl-version") + * returns String ("1.0") or null if XMLDecl was not read or if property is not supported + *
  • standalone: + * getProperty("http://xmlpull.org/v1/doc/features.html#xmldecl-standalone") + * returns Boolean: null if there was no standalone declaration + * or if property is not supported + * otherwise returns Boolean(true) if standalon="yes" and Boolean(false) when standalone="no" + *
  • encoding: obtained from getInputEncoding() + * null if stream had unknown encoding (not set in setInputStream) + * and it was not declared in XMLDecl + *
+ * + * A minimal example for using this API may look as follows: + *
+ * import java.io.IOException;
+ * import java.io.StringReader;
+ *
+ * import plugins.JabberLinker.org.xmlpull.v1.XmlPullParser;
+ * import plugins.JabberLinker.org.xmlpull.v1.XmlPullParserException.html;
+ * import plugins.JabberLinker.org.xmlpull.v1.XmlPullParserFactory;
+ *
+ * public class SimpleXmlPullApp
+ * {
+ *
+ *     public static void main (String args[])
+ *         throws XmlPullParserException, IOException
+ *     {
+ *         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
+ *         factory.setNamespaceAware(true);
+ *         XmlPullParser xpp = factory.newPullParser();
+ *
+ *         xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
+ *         int eventType = xpp.getEventType();
+ *         while (eventType != XmlPullParser.END_DOCUMENT) {
+ *          if(eventType == XmlPullParser.START_DOCUMENT) {
+ *              System.out.println("Start document");
+ *          } else if(eventType == XmlPullParser.END_DOCUMENT) {
+ *              System.out.println("End document");
+ *          } else if(eventType == XmlPullParser.START_TAG) {
+ *              System.out.println("Start tag "+xpp.getName());
+ *          } else if(eventType == XmlPullParser.END_TAG) {
+ *              System.out.println("End tag "+xpp.getName());
+ *          } else if(eventType == XmlPullParser.TEXT) {
+ *              System.out.println("Text "+xpp.getText());
+ *          }
+ *          eventType = xpp.next();
+ *         }
+ *     }
+ * }
+ * 
+ * + *

The above example will generate the following output: + *

+ * Start document
+ * Start tag foo
+ * Text Hello World!
+ * End tag foo
+ * 
+ * + *

For more details on API usage, please refer to the + * quick Introduction available at http://www.xmlpull.org + * + * @see XmlPullParserFactory + * @see #defineEntityReplacementText + * @see #getName + * @see #getNamespace + * @see #getText + * @see #next + * @see #nextToken + * @see #setInput + * @see #FEATURE_PROCESS_DOCDECL + * @see #FEATURE_VALIDATION + * @see #START_DOCUMENT + * @see #START_TAG + * @see #TEXT + * @see #END_TAG + * @see #END_DOCUMENT + * + * @author Stefan Haustein + * @author Aleksander Slominski + */ + +public interface XmlPullParser { + + /** This constant represents the default namespace (empty string "") */ + String NO_NAMESPACE = ""; + + // ---------------------------------------------------------------------------- + // EVENT TYPES as reported by next() + + /** + * Signalize that parser is at the very beginning of the document + * and nothing was read yet. + * This event type can only be observed by calling getEvent() + * before the first call to next(), nextToken, or nextTag()). + * + * @see #next + * @see #nextToken + */ + int START_DOCUMENT = 0; + + /** + * Logical end of the xml document. Returned from getEventType, next() + * and nextToken() + * when the end of the input document has been reached. + *

NOTE: calling again + * next() or nextToken() + * will result in exception being thrown. + * + * @see #next + * @see #nextToken + */ + int END_DOCUMENT = 1; + + /** + * Returned from getEventType(), + * next(), nextToken() when + * a start tag was read. + * The name of start tag is available from getName(), its namespace and prefix are + * available from getNamespace() and getPrefix() + * if namespaces are enabled. + * See getAttribute* methods to retrieve element attributes. + * See getNamespace* methods to retrieve newly declared namespaces. + * + * @see #next + * @see #nextToken + * @see #getName + * @see #getPrefix + * @see #getNamespace + * @see #getAttributeCount + * @see #getDepth + * @see #getNamespaceCount + * @see #getNamespace + * @see #FEATURE_PROCESS_NAMESPACES + */ + int START_TAG = 2; + + /** + * Returned from getEventType(), next(), or + * nextToken() when an end tag was read. + * The name of start tag is available from getName(), its + * namespace and prefix are + * available from getNamespace() and getPrefix(). + * + * @see #next + * @see #nextToken + * @see #getName + * @see #getPrefix + * @see #getNamespace + * @see #FEATURE_PROCESS_NAMESPACES + */ + int END_TAG = 3; + + + /** + * Character data was read and will is available by calling getText(). + *

Please note: next() will + * accumulate multiple + * events into one TEXT event, skipping IGNORABLE_WHITESPACE, + * PROCESSING_INSTRUCTION and COMMENT events, + * In contrast, nextToken() will stop reading + * text when any other event is observed. + * Also, when the state was reached by calling next(), the text value will + * be normalized, whereas getText() will + * return unnormalized content in the case of nextToken(). This allows + * an exact roundtrip without chnanging line ends when examining low + * level events, whereas for high level applications the text is + * normalized apropriately. + * + * @see #next + * @see #nextToken + * @see #getText + */ + int TEXT = 4; + + // ---------------------------------------------------------------------------- + // additional events exposed by lower level nextToken() + + /** + * A CDATA sections was just read; + * this token is available only from calls to nextToken(). + * A call to next() will accumulate various text events into a single event + * of type TEXT. The text contained in the CDATA section is available + * by callling getText(). + * + * @see #nextToken + * @see #getText + */ + int CDSECT = 5; + + /** + * An entity reference was just read; + * this token is available from nextToken() + * only. The entity name is available by calling getName(). If available, + * the replacement text can be obtained by calling getTextt(); otherwise, + * the user is responsibile for resolving the entity reference. + * This event type is never returned from next(); next() will + * accumulate the replacement text and other text + * events to a single TEXT event. + * + * @see #nextToken + * @see #getText + */ + int ENTITY_REF = 6; + + /** + * Ignorable whitespace was just read. + * This token is available only from nextToken()). + * For non-validating + * parsers, this event is only reported by nextToken() when outside + * the root element. + * Validating parsers may be able to detect ignorable whitespace at + * other locations. + * The ignorable whitespace string is available by calling getText() + * + *

NOTE: this is different from calling the + * isWhitespace() method, since text content + * may be whitespace but not ignorable. + * + * Ignorable whitespace is skipped by next() automatically; this event + * type is never returned from next(). + * + * @see #nextToken + * @see #getText + */ + int IGNORABLE_WHITESPACE = 7; + + /** + * An XML processing instruction declaration was just read. This + * event type is available only via nextToken(). + * getText() will return text that is inside the processing instruction. + * Calls to next() will skip processing instructions automatically. + * @see #nextToken + * @see #getText + */ + int PROCESSING_INSTRUCTION = 8; + + /** + * An XML comment was just read. This event type is this token is + * available via nextToken() only; + * calls to next() will skip comments automatically. + * The content of the comment can be accessed using the getText() + * method. + * + * @see #nextToken + * @see #getText + */ + int COMMENT = 9; + + /** + * An XML document type declaration was just read. This token is + * available from nextToken() only. + * The unparsed text inside the doctype is available via + * the getText() method. + * + * @see #nextToken + * @see #getText + */ + int DOCDECL = 10; + + /** + * This array can be used to convert the event type integer constants + * such as START_TAG or TEXT to + * to a string. For example, the value of TYPES[START_TAG] is + * the string "START_TAG". + * + * This array is intended for diagnostic output only. Relying + * on the contents of the array may be dangerous since malicous + * applications may alter the array, although it is final, due + * to limitations of the Java language. + */ + String [] TYPES = { + "START_DOCUMENT", + "END_DOCUMENT", + "START_TAG", + "END_TAG", + "TEXT", + "CDSECT", + "ENTITY_REF", + "IGNORABLE_WHITESPACE", + "PROCESSING_INSTRUCTION", + "COMMENT", + "DOCDECL" + }; + + + // ---------------------------------------------------------------------------- + // namespace related features + + /** + * This feature determines whether the parser processes + * namespaces. As for all features, the default value is false. + *

NOTE: The value can not be changed during + * parsing an must be set before parsing. + * + * @see #getFeature + * @see #setFeature + */ + String FEATURE_PROCESS_NAMESPACES = + "http://xmlpull.org/v1/doc/features.html#process-namespaces"; + + /** + * This feature determines whether namespace attributes are + * exposed via the attribute access methods. Like all features, + * the default value is false. This feature cannot be changed + * during parsing. + * + * @see #getFeature + * @see #setFeature + */ + String FEATURE_REPORT_NAMESPACE_ATTRIBUTES = + "http://xmlpull.org/v1/doc/features.html#report-namespace-prefixes"; + + /** + * This feature determines whether the document declaration + * is processed. If set to false, + * the DOCDECL event type is reported by nextToken() + * and ignored by next(). + * + * If this featue is activated, then the document declaration + * must be processed by the parser. + * + *

Please note: If the document type declaration + * was ignored, entity references may cause exceptions + * later in the parsing process. + * The default value of this feature is false. It cannot be changed + * during parsing. + * + * @see #getFeature + * @see #setFeature + */ + String FEATURE_PROCESS_DOCDECL = + "http://xmlpull.org/v1/doc/features.html#process-docdecl"; + + /** + * If this feature is activated, all validation errors as + * defined in the XML 1.0 sepcification are reported. + * This implies that FEATURE_PROCESS_DOCDECL is true and both, the + * internal and external document type declaration will be processed. + *

Please Note: This feature can not be changed + * during parsing. The default value is false. + * + * @see #getFeature + * @see #setFeature + */ + String FEATURE_VALIDATION = + "http://xmlpull.org/v1/doc/features.html#validation"; + + /** + * Use this call to change the general behaviour of the parser, + * such as namespace processing or doctype declaration handling. + * This method must be called before the first call to next or + * nextToken. Otherwise, an exception is thrown. + *

Example: call setFeature(FEATURE_PROCESS_NAMESPACES, true) in order + * to switch on namespace processing. The initial settings correspond + * to the properties requested from the XML Pull Parser factory. + * If none were requested, all feautures are deactivated by default. + * + * @exception XmlPullParserException If the feature is not supported or can not be set + * @exception IllegalArgumentException If string with the feature name is null + */ + void setFeature(String name, + boolean state) throws XmlPullParserException; + + /** + * Returns the current value of the given feature. + *

Please note: unknown features are + * always returned as false. + * + * @param name The name of feature to be retrieved. + * @return The value of the feature. + * @exception IllegalArgumentException if string the feature name is null + */ + + boolean getFeature(String name); + + /** + * Set the value of a property. + * + * The property name is any fully-qualified URI. + * + * @exception XmlPullParserException If the property is not supported or can not be set + * @exception IllegalArgumentException If string with the property name is null + */ + void setProperty(String name, + Object value) throws XmlPullParserException; + + /** + * Look up the value of a property. + * + * The property name is any fully-qualified URI. + *

NOTE: unknown properties are always + * returned as null. + * + * @param name The name of property to be retrieved. + * @return The value of named property. + */ + Object getProperty(String name); + + + /** + * Set the input source for parser to the given reader and + * resets the parser. The event type is set to the initial value + * START_DOCUMENT. + * Setting the reader to null will just stop parsing and + * reset parser state, + * allowing the parser to free internal resources + * such as parsing buffers. + */ + void setInput(Reader in) throws XmlPullParserException; + + + /** + * Sets the input stream the parser is going to process. + * This call resets the parser state and sets the event type + * to the initial value START_DOCUMENT. + * + *

NOTE: If an input encoding string is passed, + * it MUST be used. Otherwise, + * if inputEncoding is null, the parser SHOULD try to determine + * input encoding following XML 1.0 specification (see below). + * If encoding detection is supported then following feature + * http://xmlpull.org/v1/doc/features.html#detect-encoding + * MUST be true amd otherwise it must be false + * + * @param inputStream contains a raw byte input stream of possibly + * unknown encoding (when inputEncoding is null). + * + * @param inputEncoding if not null it MUST be used as encoding for inputStream + */ + void setInput(InputStream inputStream, String inputEncoding) + throws XmlPullParserException; + + /** + * Returns the input encoding if known, null otherwise. + * If setInput(InputStream, inputEncoding) was called with an inputEncoding + * value other than null, this value must be returned + * from this method. Otherwise, if inputEncoding is null and + * the parser suppports the encoding detection feature + * (http://xmlpull.org/v1/doc/features.html#detect-encoding), + * it must return the detected encoding. + * If setInput(Reader) was called, null is returned. + * After first call to next if XML declaration was present this method + * will return encoding declared. + */ + String getInputEncoding(); + + /** + * Set new value for entity replacement text as defined in + * XML 1.0 Section 4.5 + * Construction of Internal Entity Replacement Text. + * If FEATURE_PROCESS_DOCDECL or FEATURE_VALIDATION are set, calling this + * function will result in an exception -- when processing of DOCDECL is + * enabled, there is no need to the entity replacement text manually. + * + *

The motivation for this function is to allow very small + * implementations of XMLPULL that will work in J2ME environments. + * Though these implementations may not be able to process the document type + * declaration, they still can work with known DTDs by using this function. + * + *

Please notes: The given value is used literally as replacement text + * and it corresponds to declaring entity in DTD that has all special characters + * escaped: left angle bracket is replaced with &lt;, ampersnad with &amp; + * and so on. + * + *

Note: The given value is the literal replacement text and must not + * contain any other entity reference (if it contains any entity reference + * there will be no further replacement). + * + *

Note: The list of pre-defined entity names will + * always contain standard XML entities such as + * amp (&amp;), lt (&lt;), gt (&gt;), quot (&quot;), and apos (&apos;). + * Those cannot be redefined by this method! + * + * @see #setInput + * @see #FEATURE_PROCESS_DOCDECL + * @see #FEATURE_VALIDATION + */ + void defineEntityReplacementText( String entityName, + String replacementText ) throws XmlPullParserException; + + /** + * Returns the numbers of elements in the namespace stack for the given + * depth. + * If namespaces are not enabled, 0 is returned. + * + *

NOTE: when parser is on END_TAG then it is allowed to call + * this function with getDepth()+1 argument to retrieve position of namespace + * prefixes and URIs that were declared on corresponding START_TAG. + *

NOTE: to retrieve lsit of namespaces declared in current element:

+     *       XmlPullParser pp = ...
+     *       int nsStart = pp.getNamespaceCount(pp.getDepth()-1);
+     *       int nsEnd = pp.getNamespaceCount(pp.getDepth());
+     *       for (int i = nsStart; i < nsEnd; i++) {
+     *          String prefix = pp.getNamespacePrefix(i);
+     *          String ns = pp.getNamespaceUri(i);
+     *           // ...
+     *      }
+     * 
+ * + * @see #getNamespacePrefix + * @see #getNamespaceUri + * @see #getNamespace() + * @see #getNamespace(String) + */ + int getNamespaceCount(int depth) throws XmlPullParserException; + + /** + * Returns the namespace prefixe for the given position + * in the namespace stack. + * Default namespace declaration (xmlns='...') will have null as prefix. + * If the given index is out of range, an exception is thrown. + *

Please note: when the parser is on an END_TAG, + * namespace prefixes that were declared + * in the corresponding START_TAG are still accessible + * although they are no longer in scope. + */ + String getNamespacePrefix(int pos) throws XmlPullParserException; + + /** + * Returns the namespace URI for the given position in the + * namespace stack + * If the position is out of range, an exception is thrown. + *

NOTE: when parser is on END_TAG then namespace prefixes that were declared + * in corresponding START_TAG are still accessible even though they are not in scope + */ + String getNamespaceUri(int pos) throws XmlPullParserException; + + /** + * Returns the URI corresponding to the given prefix, + * depending on current state of the parser. + * + *

If the prefix was not declared in the current scope, + * null is returned. The default namespace is included + * in the namespace table and is available via + * getNamespace (null). + * + *

This method is a convenience method for + * + *

+     *  for (int i = getNamespaceCount(getDepth ())-1; i >= 0; i--) {
+     *   if (getNamespacePrefix(i).equals( prefix )) {
+     *     return getNamespaceUri(i);
+     *   }
+     *  }
+     *  return null;
+     * 
+ * + *

Please note: parser implementations + * may provide more efifcient lookup, e.g. using a Hashtable. + * The 'xml' prefix is bound to "http://www.w3.org/XML/1998/namespace", as + * defined in the + * Namespaces in XML + * specification. Analogous, the 'xmlns' prefix is resolved to + * http://www.w3.org/2000/xmlns/ + * + * @see #getNamespaceCount + * @see #getNamespacePrefix + * @see #getNamespaceUri + */ + String getNamespace (String prefix); + + + // -------------------------------------------------------------------------- + // miscellaneous reporting methods + + /** + * Returns the current depth of the element. + * Outside the root element, the depth is 0. The + * depth is incremented by 1 when a start tag is reached. + * The depth is decremented AFTER the end tag + * event was observed. + * + *

+     * <!-- outside -->     0
+     * <root>                  1
+     *   sometext                 1
+     *     <foobar>         2
+     *     </foobar>        2
+     * </root>              1
+     * <!-- outside -->     0
+     * 
+ */ + int getDepth(); + + /** + * Returns a short text describing the current parser state, including + * the position, a + * description of the current event and the data source if known. + * This method is especially useful to provide meaningful + * error messages and for debugging purposes. + */ + String getPositionDescription (); + + + /** + * Returns the current line number, starting from 1. + * When the parser does not know the current line number + * or can not determine it, -1 is returned (e.g. for WBXML). + * + * @return current line number or -1 if unknown. + */ + int getLineNumber(); + + /** + * Returns the current column number, starting from 0. + * When the parser does not know the current column number + * or can not determine it, -1 is returned (e.g. for WBXML). + * + * @return current column number or -1 if unknown. + */ + int getColumnNumber(); + + + // -------------------------------------------------------------------------- + // TEXT related methods + + /** + * Checks whether the current TEXT event contains only whitespace + * characters. + * For IGNORABLE_WHITESPACE, this is always true. + * For TEXT and CDSECT, false is returned when the current event text + * contains at least one non-white space character. For any other + * event type an exception is thrown. + * + *

Please note: non-validating parsers are not + * able to distinguish whitespace and ignorable whitespace, + * except from whitespace outside the root element. Ignorable + * whitespace is reported as separate event, which is exposed + * via nextToken only. + * + */ + boolean isWhitespace() throws XmlPullParserException; + + /** + * Returns the text content of the current event as String. + * The value returned depends on current event type, + * for example for TEXT event it is element content + * (this is typical case when next() is used). + * + * See description of nextToken() for detailed description of + * possible returned values for different types of events. + * + *

NOTE: in case of ENTITY_REF, this method returns + * the entity replacement text (or null if not available). This is + * the only case where + * getText() and getTextCharacters() return different values. + * + * @see #getEventType + * @see #next + * @see #nextToken + */ + String getText (); + + + /** + * Returns the buffer that contains the text of the current event, + * as well as the start offset and length relevant for the current + * event. See getText(), next() and nextToken() for description of possible returned values. + * + *

Please note: this buffer must not + * be modified and its content MAY change after a call to + * next() or nextToken(). This method will always return the + * same value as getText(), except for ENTITY_REF. In the case + * of ENTITY ref, getText() returns the replacement text and + * this method returns the actual input buffer containing the + * entity name. + * If getText() returns null, this method returns null as well and + * the values returned in the holder array MUST be -1 (both start + * and length). + * + * @see #getText + * @see #next + * @see #nextToken + * + * @param holderForStartAndLength Must hold an 2-element int array + * into which the start offset and length values will be written. + * @return char buffer that contains the text of the current event + * (null if the current event has no text associated). + */ + char[] getTextCharacters(int [] holderForStartAndLength); + + // -------------------------------------------------------------------------- + // START_TAG / END_TAG shared methods + + /** + * Returns the namespace URI of the current element. + * The default namespace is represented + * as empty string. + * If namespaces are not enabled, an empty String ("") is always returned. + * The current event must be START_TAG or END_TAG; otherwise, + * null is returned. + */ + String getNamespace (); + + /** + * For START_TAG or END_TAG events, the (local) name of the current + * element is returned when namespaces are enabled. When namespace + * processing is disabled, the raw name is returned. + * For ENTITY_REF events, the entity name is returned. + * If the current event is not START_TAG, END_TAG, or ENTITY_REF, + * null is returned. + *

Please note: To reconstruct the raw element name + * when namespaces are enabled and the prefix is not null, + * you will need to add the prefix and a colon to localName.. + * + */ + String getName(); + + /** + * Returns the prefix of the current element. + * If the element is in the default namespace (has no prefix), + * null is returned. + * If namespaces are not enabled, or the current event + * is not START_TAG or END_TAG, null is returned. + */ + String getPrefix(); + + /** + * Returns true if the current event is START_TAG and the tag + * is degenerated + * (e.g. <foobar/>). + *

NOTE: if the parser is not on START_TAG, an exception + * will be thrown. + */ + boolean isEmptyElementTag() throws XmlPullParserException; + + // -------------------------------------------------------------------------- + // START_TAG Attributes retrieval methods + + /** + * Returns the number of attributes of the current start tag, or + * -1 if the current event type is not START_TAG + * + * @see #getAttributeNamespace + * @see #getAttributeName + * @see #getAttributePrefix + * @see #getAttributeValue + */ + int getAttributeCount(); + + /** + * Returns the namespace URI of the attribute + * with the given index (starts from 0). + * Returns an empty string ("") if namespaces are not enabled + * or the attribute has no namespace. + * Throws an IndexOutOfBoundsException if the index is out of range + * or the current event type is not START_TAG. + * + *

NOTE: if FEATURE_REPORT_NAMESPACE_ATTRIBUTES is set + * then namespace attributes (xmlns:ns='...') must be reported + * with namespace + * http://www.w3.org/2000/xmlns/ + * (visit this URL for description!). + * The default namespace attribute (xmlns="...") will be reported with empty namespace. + *

NOTE:The xml prefix is bound as defined in + * Namespaces in XML + * specification to "http://www.w3.org/XML/1998/namespace". + * + * @param zero based index of attribute + * @return attribute namespace, + * empty string ("") is returned if namesapces processing is not enabled or + * namespaces processing is enabled but attribute has no namespace (it has no prefix). + */ + String getAttributeNamespace (int index); + + /** + * Returns the local name of the specified attribute + * if namespaces are enabled or just attribute name if namespaces are disabled. + * Throws an IndexOutOfBoundsException if the index is out of range + * or current event type is not START_TAG. + * + * @param zero based index of attribute + * @return attribute name (null is never returned) + */ + String getAttributeName (int index); + + /** + * Returns the prefix of the specified attribute + * Returns null if the element has no prefix. + * If namespaces are disabled it will always return null. + * Throws an IndexOutOfBoundsException if the index is out of range + * or current event type is not START_TAG. + * + * @param zero based index of attribute + * @return attribute prefix or null if namespaces processing is not enabled. + */ + String getAttributePrefix(int index); + + /** + * Returns the type of the specified attribute + * If parser is non-validating it MUST return CDATA. + * + * @param zero based index of attribute + * @return attribute type (null is never returned) + */ + String getAttributeType(int index); + + /** + * Returns if the specified attribute was not in input was declared in XML. + * If parser is non-validating it MUST always return false. + * This information is part of XML infoset: + * + * @param zero based index of attribute + * @return false if attribute was in input + */ + boolean isAttributeDefault(int index); + + /** + * Returns the given attributes value. + * Throws an IndexOutOfBoundsException if the index is out of range + * or current event type is not START_TAG. + * + *

NOTE: attribute value must be normalized + * (including entity replacement text if PROCESS_DOCDECL is false) as described in + * XML 1.0 section + * 3.3.3 Attribute-Value Normalization + * + * @see #defineEntityReplacementText + * + * @param zero based index of attribute + * @return value of attribute (null is never returned) + */ + String getAttributeValue(int index); + + /** + * Returns the attributes value identified by namespace URI and namespace localName. + * If namespaces are disabled namespace must be null. + * If current event type is not START_TAG then IndexOutOfBoundsException will be thrown. + * + *

NOTE: attribute value must be normalized + * (including entity replacement text if PROCESS_DOCDECL is false) as described in + * XML 1.0 section + * 3.3.3 Attribute-Value Normalization + * + * @see #defineEntityReplacementText + * + * @param namespace Namespace of the attribute if namespaces are enabled otherwise must be null + * @param name If namespaces enabled local name of attribute otherwise just attribute name + * @return value of attribute or null if attribute with given name does not exist + */ + String getAttributeValue(String namespace, + String name); + + // -------------------------------------------------------------------------- + // actual parsing methods + + /** + * Returns the type of the current event (START_TAG, END_TAG, TEXT, etc.) + * + * @see #next() + * @see #nextToken() + */ + int getEventType() + throws XmlPullParserException; + + /** + * Get next parsing event - element content wil be coalesced and only one + * TEXT event must be returned for whole element content + * (comments and processing instructions will be ignored and emtity references + * must be expanded or exception mus be thrown if entity reerence can not be exapnded). + * If element content is empty (content is "") then no TEXT event will be reported. + * + *

NOTE: empty element (such as <tag/>) will be reported + * with two separate events: START_TAG, END_TAG - it must be so to preserve + * parsing equivalency of empty element to <tag></tag>. + * (see isEmptyElementTag ()) + * + * @see #isEmptyElementTag + * @see #START_TAG + * @see #TEXT + * @see #END_TAG + * @see #END_DOCUMENT + */ + + int next() + throws XmlPullParserException, IOException; + + + /** + * This method works similarly to next() but will expose + * additional event types (COMMENT, CDSECT, DOCDECL, ENTITY_REF, PROCESSING_INSTRUCTION, or + * IGNORABLE_WHITESPACE) if they are available in input. + * + *

If special feature + * FEATURE_XML_ROUNDTRIP + * (identified by URI: http://xmlpull.org/v1/doc/features.html#xml-roundtrip) + * is enabled it is possible to do XML document round trip ie. reproduce + * exectly on output the XML input using getText(): + * returned content is always unnormalized (exactly as in input). + * Otherwise returned content is end-of-line normalized as described + * XML 1.0 End-of-Line Handling + * and. Also when this feature is enabled exact content of START_TAG, END_TAG, + * DOCDECL and PROCESSING_INSTRUCTION is available. + * + *

Here is the list of tokens that can be returned from nextToken() + * and what getText() and getTextCharacters() returns:

+ *
START_DOCUMENT
null + *
END_DOCUMENT
null + *
START_TAG
null unless FEATURE_XML_ROUNDTRIP + * enabled and then returns XML tag, ex: <tag attr='val'> + *
END_TAG
null unless FEATURE_XML_ROUNDTRIP + * id enabled and then returns XML tag, ex: </tag> + *
TEXT
return element content. + *
Note: that element content may be delivered in multiple consecutive TEXT events. + *
IGNORABLE_WHITESPACE
return characters that are determined to be ignorable white + * space. If the FEATURE_XML_ROUNDTRIP is enabled all whitespace content outside root + * element will always reported as IGNORABLE_WHITESPACE otherise rteporting is optional. + *
Note: that element content may be delevered in multiple consecutive IGNORABLE_WHITESPACE events. + *
CDSECT
+ * return text inside CDATA + * (ex. 'fo<o' from <!CDATA[fo<o]]>) + *
PROCESSING_INSTRUCTION
+ * if FEATURE_XML_ROUNDTRIP is true + * return exact PI content ex: 'pi foo' from <?pi foo?> + * otherwise it may be exact PI content or concatenation of PI target, + * space and data so for example for + * <?target data?> string "target data" may + * be returned if FEATURE_XML_ROUNDTRIP is false. + *
COMMENT
return comment content ex. 'foo bar' from <!--foo bar--> + *
ENTITY_REF
getText() MUST return entity replacement text if PROCESS_DOCDECL is false + * otherwise getText() MAY return null, + * additionally getTextCharacters() MUST return entity name + * (for example 'entity_name' for &entity_name;). + *
NOTE: this is the only place where value returned from getText() and + * getTextCharacters() are different + *
NOTE: it is user responsibility to resolve entity reference + * if PROCESS_DOCDECL is false and there is no entity replacement text set in + * defineEntityReplacementText() method (getText() will be null) + *
NOTE: character entities (ex. &#32;) and standard entities such as + * &amp; &lt; &gt; &quot; &apos; are reported as well + * and are not reported as TEXT tokens but as ENTITY_REF tokens! + * This requirement is added to allow to do roundtrip of XML documents! + *
DOCDECL
+ * if FEATURE_XML_ROUNDTRIP is true or PROCESS_DOCDECL is false + * then return what is inside of DOCDECL for example it returns:
+     * " titlepage SYSTEM "http://www.foo.bar/dtds/typo.dtd"
+     * [<!ENTITY % active.links "INCLUDE">]"
+ *

for input document that contained:

+     * <!DOCTYPE titlepage SYSTEM "http://www.foo.bar/dtds/typo.dtd"
+     * [<!ENTITY % active.links "INCLUDE">]>
+ * otherwise if FEATURE_XML_ROUNDTRIP is false and PROCESS_DOCDECL is true + * then what is returned is undefined (it may be even null) + *
+ *
+ * + *

NOTE: there is no gurantee that there will only one TEXT or + * IGNORABLE_WHITESPACE event from nextToken() as parser may chose to deliver element content in + * multiple tokens (dividing element content into chunks) + * + *

NOTE: whether returned text of token is end-of-line normalized + * is depending on FEATURE_XML_ROUNDTRIP. + * + *

NOTE: XMLDecl (<?xml ...?>) is not reported but its content + * is available through optional properties (see class description above). + * + * @see #next + * @see #START_TAG + * @see #TEXT + * @see #END_TAG + * @see #END_DOCUMENT + * @see #COMMENT + * @see #DOCDECL + * @see #PROCESSING_INSTRUCTION + * @see #ENTITY_REF + * @see #IGNORABLE_WHITESPACE + */ + int nextToken() + throws XmlPullParserException, IOException; + + //----------------------------------------------------------------------------- + // utility methods to mak XML parsing easier ... + + /** + * Test if the current event is of the given type and if the + * namespace and name do match. null will match any namespace + * and any name. If the test is not passed, an exception is + * thrown. The exception text indicates the parser position, + * the expected event and the current event that is not meeting the + * requirement. + * + *

Essentially it does this + *

+     *  if (type != getEventType()
+     *  || (namespace != null &&  !namespace.equals( getNamespace () ) )
+     *  || (name != null &&  !name.equals( getName() ) ) )
+     *     throw new XmlPullParserException( "expected "+ TYPES[ type ]+getPositionDescription());
+     * 
+ */ + void require(int type, String namespace, String name) + throws XmlPullParserException, IOException; + + /** + * If current event is START_TAG then if next element is TEXT then element content is returned + * or if next event is END_TAG then empty string is returned, otherwise exception is thrown. + * After calling this function successfully parser will be positioned on END_TAG. + * + *

The motivation for this function is to allow to parse consistently both + * empty elements and elements that has non empty content, for example for input:

    + *
  1. <tag>foo</tag> + *
  2. <tag></tag> (which is equivalent to <tag/> + * both input can be parsed with the same code: + *
    +     *   p.nextTag()
    +     *   p.requireEvent(p.START_TAG, "", "tag");
    +     *   String content = p.nextText();
    +     *   p.requireEvent(p.END_TAG, "", "tag");
    +     * 
    + * This function together with nextTag make it very easy to parse XML that has + * no mixed content. + * + * + *

    Essentially it does this + *

    +     *  if(getEventType() != START_TAG) {
    +     *     throw new XmlPullParserException(
    +     *       "parser must be on START_TAG to read next text", this, null);
    +     *  }
    +     *  int eventType = next();
    +     *  if(eventType == TEXT) {
    +     *     String result = getText();
    +     *     eventType = next();
    +     *     if(eventType != END_TAG) {
    +     *       throw new XmlPullParserException(
    +     *          "event TEXT it must be immediately followed by END_TAG", this, null);
    +     *      }
    +     *      return result;
    +     *  } else if(eventType == END_TAG) {
    +     *     return "";
    +     *  } else {
    +     *     throw new XmlPullParserException(
    +     *       "parser must be on START_TAG or TEXT to read text", this, null);
    +     *  }
    +     * 
    + */ + String nextText() throws XmlPullParserException, IOException; + + /** + * Call next() and return event if it is START_TAG or END_TAG + * otherwise throw an exception. + * It will skip whitespace TEXT before actual tag if any. + * + *

    essentially it does this + *

    +     *   int eventType = next();
    +     *   if(eventType == TEXT &&  isWhitespace()) {   // skip whitespace
    +     *      eventType = next();
    +     *   }
    +     *   if (eventType != START_TAG &&  eventType != END_TAG) {
    +     *      throw new XmlPullParserException("expected start or end tag", this, null);
    +     *   }
    +     *   return eventType;
    +     * 
    + */ + int nextTag() throws XmlPullParserException, IOException; + +} + diff --git a/router/java/src/org/xmlpull/v1/XmlPullParserException.java b/router/java/src/org/xmlpull/v1/XmlPullParserException.java new file mode 100644 index 000000000..0faba7e20 --- /dev/null +++ b/router/java/src/org/xmlpull/v1/XmlPullParserException.java @@ -0,0 +1,80 @@ +/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/ +// for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/) + +package org.xmlpull.v1; + +/** + * This exception is thrown to signal XML Pull Parser related faults. + * + * @author Aleksander Slominski + */ +public class XmlPullParserException extends Exception { + /** + * + */ + private static final long serialVersionUID = 1L; + protected Throwable detail; + protected int row = -1; + protected int column = -1; + + /* public XmlPullParserException() { + }*/ + + public XmlPullParserException(String s) { + super(s); + } + + /* + public XmlPullParserException(String s, Throwable thrwble) { + super(s); + this.detail = thrwble; + } + + public XmlPullParserException(String s, int row, int column) { + super(s); + this.row = row; + this.column = column; + } + */ + + public XmlPullParserException(String msg, XmlPullParser parser, Throwable chain) { + super ((msg == null ? "" : msg+" ") + + (parser == null ? "" : "(position:"+parser.getPositionDescription()+") ") + + (chain == null ? "" : "caused by: "+chain)); + + if (parser != null) { + this.row = parser.getLineNumber(); + this.column = parser.getColumnNumber(); + } + this.detail = chain; + } + + public Throwable getDetail() { return detail; } + // public void setDetail(Throwable cause) { this.detail = cause; } + public int getLineNumber() { return row; } + public int getColumnNumber() { return column; } + + /* + public String getMessage() { + if(detail == null) + return super.getMessage(); + else + return super.getMessage() + "; nested exception is: \n\t" + + detail.getMessage(); + } + */ + + //NOTE: code that prints this and detail is difficult in J2ME + public void printStackTrace() { + if (detail == null) { + super.printStackTrace(); + } else { + synchronized(System.err) { + System.err.println(super.getMessage() + "; nested exception is:"); + detail.printStackTrace(); + } + } + } + +} + diff --git a/router/java/src/org/xmlpull/v1/XmlPullParserFactory.java b/router/java/src/org/xmlpull/v1/XmlPullParserFactory.java new file mode 100644 index 000000000..af6c27adb --- /dev/null +++ b/router/java/src/org/xmlpull/v1/XmlPullParserFactory.java @@ -0,0 +1,356 @@ +/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/ +// for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/) + +package org.xmlpull.v1; + +import java.io.InputStream; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Vector; + +/** + * This class is used to create implementations of XML Pull Parser defined in XMPULL V1 API. + * The name of actual factory class will be determined based on several parameters. + * It works similar to JAXP but tailored to work in J2ME environments + * (no access to system properties or file system) so name of parser class factory to use + * and its class used for loading (no class loader - on J2ME no access to context class loaders) + * must be passed explicitly. If no name of parser factory was passed (or is null) + * it will try to find name by searching in CLASSPATH for + * META-INF/services/plugins.JabberLinker.org.xmlpull.v1.XmlPullParserFactory resource that should contain + * a comma separated list of class names of factories or parsers to try (in order from + * left to the right). If none found, it will throw an exception. + * + *
    NOTE:In J2SE or J2EE environments, you may want to use + * newInstance(property, classLoaderCtx) + * where first argument is + * System.getProperty(XmlPullParserFactory.PROPERTY_NAME) + * and second is Thread.getContextClassLoader().getClass() . + * + * @see XmlPullParser + * + * @author Aleksander Slominski + * @author Stefan Haustein + */ + +public class XmlPullParserFactory { + /** used as default class to server as context class in newInstance() */ + final static Class referenceContextClass; + + static { + XmlPullParserFactory f = new XmlPullParserFactory(); + referenceContextClass = f.getClass(); + } + + /** Name of the system or midlet property that should be used for + a system property containing a comma separated list of factory + or parser class names (value: + plugins.JabberLinker.org.xmlpull.v1.XmlPullParserFactory). */ + + + public static final String PROPERTY_NAME = + "plugins.JabberLinker.org.xmlpull.v1.XmlPullParserFactory"; + + private static final String RESOURCE_NAME = + "/META-INF/services/" + PROPERTY_NAME; + + + // public static final String DEFAULT_PROPERTY = + // "plugins.JabberLinker.org.xmlpull.xpp3.XmlPullParser,org.kxml2.io.KXmlParser"; + + + protected Vector parserClasses; + protected String classNamesLocation; + + protected Vector serializerClasses; + + + // features are kept there + protected Hashtable features = new Hashtable(); + + + /** + * Protected constructor to be called by factory implementations. + */ + + protected XmlPullParserFactory() { + } + + + + /** + * Set the features to be set when XML Pull Parser is created by this factory. + *

    NOTE: factory features are not used for XML Serializer. + * + * @param name string with URI identifying feature + * @param state if true feature will be set; if false will be ignored + */ + + public void setFeature(String name, + boolean state) throws XmlPullParserException { + + features.put(name, new Boolean(state)); + } + + + /** + * Return the current value of the feature with given name. + *

    NOTE: factory features are not used for XML Serializer. + * + * @param name The name of feature to be retrieved. + * @return The value of named feature. + * Unknown features are always returned as false + */ + + public boolean getFeature (String name) { + Boolean value = (Boolean) features.get(name); + return value != null ? value.booleanValue() : false; + } + + /** + * Specifies that the parser produced by this factory will provide + * support for XML namespaces. + * By default the value of this is set to false. + * + * @param awareness true if the parser produced by this code + * will provide support for XML namespaces; false otherwise. + */ + + public void setNamespaceAware(boolean awareness) { + features.put (XmlPullParser.FEATURE_PROCESS_NAMESPACES, new Boolean (awareness)); + } + + /** + * Indicates whether or not the factory is configured to produce + * parsers which are namespace aware + * (it simply set feature XmlPullParser.FEATURE_PROCESS_NAMESPACES to true or false). + * + * @return true if the factory is configured to produce parsers + * which are namespace aware; false otherwise. + */ + + public boolean isNamespaceAware() { + return getFeature (XmlPullParser.FEATURE_PROCESS_NAMESPACES); + } + + + /** + * Specifies that the parser produced by this factory will be validating + * (it simply set feature XmlPullParser.FEATURE_VALIDATION to true or false). + * + * By default the value of this is set to false. + * + * @param validating - if true the parsers created by this factory must be validating. + */ + + public void setValidating(boolean validating) { + features.put (XmlPullParser.FEATURE_VALIDATION, new Boolean (validating)); + } + + /** + * Indicates whether or not the factory is configured to produce parsers + * which validate the XML content during parse. + * + * @return true if the factory is configured to produce parsers + * which validate the XML content during parse; false otherwise. + */ + + public boolean isValidating() { + return getFeature (XmlPullParser.FEATURE_VALIDATION); + } + + /** + * Creates a new instance of a XML Pull Parser + * using the currently configured factory features. + * + * @return A new instance of a XML Pull Parser. + * @throws XmlPullParserException if a parser cannot be created which satisfies the + * requested configuration. + */ + + public XmlPullParser newPullParser() throws XmlPullParserException { + + if (parserClasses == null) throw new XmlPullParserException + ("Factory initialization was incomplete - has not tried "+classNamesLocation); + + if (parserClasses.size() == 0) throw new XmlPullParserException + ("No valid parser classes found in "+classNamesLocation); + + final StringBuilder issues = new StringBuilder (); + + for (int i = 0; i < parserClasses.size (); i++) { + final Class ppClass = (Class) parserClasses.elementAt (i); + try { + final XmlPullParser pp = (XmlPullParser) ppClass.newInstance(); + // if( ! features.isEmpty() ) { + //Enumeration keys = features.keys(); + // while(keys.hasMoreElements()) { + + for (Enumeration e = features.keys (); e.hasMoreElements ();) { + final String key = (String) e.nextElement(); + final Boolean value = (Boolean) features.get(key); + if(value != null && value.booleanValue()) { + pp.setFeature(key, true); + } + } + return pp; + + } catch(Exception ex) { + issues.append (ppClass.getName () + ": "+ ex.toString ()+"; "); + } + } + + throw new XmlPullParserException ("could not create parser: "+issues); + } + + + /** + * Creates a new instance of a XML Serializer. + * + *

    NOTE: factory features are not used for XML Serializer. + * + * @return A new instance of a XML Serializer. + * @throws XmlPullParserException if a parser cannot be created which satisfies the + * requested configuration. + */ + + public XmlSerializer newSerializer() throws XmlPullParserException { + + if (serializerClasses == null) { + throw new XmlPullParserException + ("Factory initialization incomplete - has not tried "+classNamesLocation); + } + if(serializerClasses.size() == 0) { + throw new XmlPullParserException + ("No valid serializer classes found in "+classNamesLocation); + } + + final StringBuilder issues = new StringBuilder (); + + for (int i = 0; i < serializerClasses.size (); i++) { + final Class ppClass = (Class) serializerClasses.elementAt (i); + try { + final XmlSerializer ser = (XmlSerializer) ppClass.newInstance(); + + // for (Enumeration e = features.keys (); e.hasMoreElements ();) { + // String key = (String) e.nextElement(); + // Boolean value = (Boolean) features.get(key); + // if(value != null && value.booleanValue()) { + // ser.setFeature(key, true); + // } + // } + return ser; + + } catch(Exception ex) { + issues.append (ppClass.getName () + ": "+ ex.toString ()+"; "); + } + } + + throw new XmlPullParserException ("could not create serializer: "+issues); + } + + /** + * Create a new instance of a PullParserFactory that can be used + * to create XML pull parsers (see class description for more + * details). + * + * @return a new instance of a PullParserFactory, as returned by newInstance (null, null); + */ + public static XmlPullParserFactory newInstance () throws XmlPullParserException { + return newInstance(null, null); + } + + public static XmlPullParserFactory newInstance (String classNames, Class context) + throws XmlPullParserException { + + if (context == null) { + //NOTE: make sure context uses the same class loader as API classes + // this is the best we can do without having access to context classloader in J2ME + // if API is in the same classloader as implementation then this will work + context = referenceContextClass; + } + + String classNamesLocation = null; + + if (classNames == null || classNames.length() == 0 || "DEFAULT".equals(classNames)) { + try { + InputStream is = context.getResourceAsStream (RESOURCE_NAME); + + if (is == null) throw new XmlPullParserException + ("resource not found: "+RESOURCE_NAME + +" make sure that parser implementing XmlPull API is available"); + final StringBuilder sb = new StringBuilder(); + + while (true) { + final int ch = is.read(); + if (ch < 0) break; + else if (ch > ' ') + sb.append((char) ch); + } + is.close (); + + classNames = sb.toString (); + } + catch (Exception e) { + throw new XmlPullParserException (null, null, e); + } + classNamesLocation = "resource "+RESOURCE_NAME+" that contained '"+classNames+"'"; + } else { + classNamesLocation = + "parameter classNames to newInstance() that contained '"+classNames+"'"; + } + + XmlPullParserFactory factory = null; + final Vector parserClasses = new Vector (); + final Vector serializerClasses = new Vector (); + int pos = 0; + + while (pos < classNames.length ()) { + int cut = classNames.indexOf (',', pos); + + if (cut == -1) cut = classNames.length (); + final String name = classNames.substring (pos, cut); + + Class candidate = null; + Object instance = null; + + try { + candidate = Class.forName (name); + // necessary because of J2ME .class issue + instance = candidate.newInstance (); + } + catch (Exception e) {} + + if (candidate != null) { + boolean recognized = false; + if (instance instanceof XmlPullParser) { + parserClasses.addElement (candidate); + recognized = true; + } + if (instance instanceof XmlSerializer) { + serializerClasses.addElement (candidate); + recognized = true; + } + if (instance instanceof XmlPullParserFactory) { + if (factory == null) { + factory = (XmlPullParserFactory) instance; + } + recognized = true; + } + if (!recognized) { + throw new XmlPullParserException ("incompatible class: "+name); + } + } + pos = cut + 1; + } + + if (factory == null) { + factory = new XmlPullParserFactory (); + } + factory.parserClasses = parserClasses; + factory.serializerClasses = serializerClasses; + factory.classNamesLocation = classNamesLocation; + return factory; + } +} + + diff --git a/router/java/src/org/xmlpull/v1/XmlSerializer.java b/router/java/src/org/xmlpull/v1/XmlSerializer.java new file mode 100644 index 000000000..8e85e2f0e --- /dev/null +++ b/router/java/src/org/xmlpull/v1/XmlSerializer.java @@ -0,0 +1,326 @@ +package org.xmlpull.v1; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; + +/** + * Define an interface to serialziation of XML Infoset. + * This interface abstracts away if serialized XML is XML 1.0 comaptible text or + * other formats of XML 1.0 serializations (such as binary XML for example with WBXML). + * + *

    PLEASE NOTE: This interface will be part of XmlPull 1.2 API. + * It is included as basis for discussion. It may change in any way. + * + *

    Exceptions that may be thrown are: IOException or runtime exception + * (more runtime exceptions can be thrown but are not declared and as such + * have no semantics defined for this interface): + *

      + *
    • IllegalArgumentException - for almost all methods to signal that + * argument is illegal + *
    • IllegalStateException - to signal that call has good arguments but + * is not expected here (violation of contract) and for features/properties + * when requesting setting unimplemented feature/property + * (UnsupportedOperationException would be better but it is not in MIDP) + *
    + * + *

    NOTE: writing CDSECT, ENTITY_REF, IGNORABLE_WHITESPACE, + * PROCESSING_INSTRUCTION, COMMENT, and DOCDECL in some implementations + * may not be supported (for example when serializing to WBXML). + * In such case IllegalStateException will be thrown and it is recommened + * to use an optional feature to signal that implementation is not + * supporting this kind of output. + */ + +public interface XmlSerializer { + + /** + * Set feature identified by name (recommended to be URI for uniqueness). + * Some well known optional features are defined in + * + * http://www.xmlpull.org/v1/doc/features.html. + * + * If feature is not recocgnized or can not be set + * then IllegalStateException MUST be thrown. + * + * @exception IllegalStateException If the feature is not supported or can not be set + */ + void setFeature(String name, + boolean state) + throws IllegalArgumentException, IllegalStateException; + + + /** + * Return the current value of the feature with given name. + *

    NOTE: unknown properties are always returned as null + * + * @param name The name of feature to be retrieved. + * @return The value of named feature. + * @exception IllegalArgumentException if feature string is null + */ + boolean getFeature(String name); + + + /** + * Set the value of a property. + * (the property name is recommened to be URI for uniqueness). + * Some well known optional properties are defined in + * + * http://www.xmlpull.org/v1/doc/properties.html. + * + * If property is not recocgnized or can not be set + * then IllegalStateException MUST be thrown. + * + * @exception IllegalStateException if the property is not supported or can not be set + */ + void setProperty(String name, + Object value) + throws IllegalArgumentException, IllegalStateException; + + /** + * Look up the value of a property. + * + * The property name is any fully-qualified URI. I + *

    NOTE: unknown properties are always returned as null + * + * @param name The name of property to be retrieved. + * @return The value of named property. + */ + Object getProperty(String name); + + /** + * Set to use binary output stream with given encoding. + */ + void setOutput (OutputStream os, String encoding) + throws IOException, IllegalArgumentException, IllegalStateException; + + /** + * Set the output to the given writer. + *

    WARNING no information about encoding is available! + */ + void setOutput (Writer writer) + throws IOException, IllegalArgumentException, IllegalStateException; + + /** + * Write <?xml declaration with encoding (if encoding not null) + * and standalone flag (if standalone not null) + * This method can only be called just after setOutput. + */ + void startDocument (String encoding, Boolean standalone) + throws IOException, IllegalArgumentException, IllegalStateException; + + /** + * Finish writing. All unclosed start tags will be closed and output + * will be flushed. After calling this method no more output can be + * serialized until next call to setOutput() + */ + void endDocument () + throws IOException, IllegalArgumentException, IllegalStateException; + + /** + * Binds the given prefix to the given namespace. + * This call is valid for the next element including child elements. + * The prefix and namespace MUST be always declared even if prefix + * is not used in element (startTag() or attribute()) - for XML 1.0 + * it must result in declaring xmlns:prefix='namespace' + * (or xmlns:prefix="namespace" depending what character is used + * to quote attribute value). + * + *

    NOTE: this method MUST be called directly before startTag() + * and if anything but startTag() or setPrefix() is called next there will be exception. + *

    NOTE: prefixes "xml" and "xmlns" are already bound + * and can not be redefined see: + * Namespaces in XML Errata. + *

    NOTE: to set default namespace use as prefix empty string. + * + * @param prefix must be not null (or IllegalArgumentException is thrown) + * @param namespace must be not null + */ + void setPrefix (String prefix, String namespace) + throws IOException, IllegalArgumentException, IllegalStateException; + + /** + * Return namespace that corresponds to given prefix + * If there is no prefix bound to this namespace return null + * but if generatePrefix is false then return generated prefix. + * + *

    NOTE: if the prefix is empty string "" and defualt namespace is bound + * to this prefix then empty string ("") is returned. + * + *

    NOTE: prefixes "xml" and "xmlns" are already bound + * will have values as defined + * Namespaces in XML specification + */ + String getPrefix (String namespace, boolean generatePrefix) + throws IllegalArgumentException; + + /** + * Returns the current depth of the element. + * Outside the root element, the depth is 0. The + * depth is incremented by 1 when startTag() is called. + * The depth is decremented after the call to endTag() + * event was observed. + * + *

    +     * <!-- outside -->     0
    +     * <root>               1
    +     *   sometext                 1
    +     *     <foobar>         2
    +     *     </foobar>        2
    +     * </root>              1
    +     * <!-- outside -->     0
    +     * 
    + */ + int getDepth(); + + /** + * Returns the namespace URI of the current element as set by startTag(). + * + *

    NOTE: that measn in particaulr that:

      + *
    • if there was startTag("", ...) then getNamespace() returns "" + *
    • if there was startTag(null, ...) then getNamespace() returns null + *
    + * + * @return namespace set by startTag() that is currently in scope + */ + String getNamespace (); + + /** + * Returns the name of the current element as set by startTag(). + * It can only be null before first call to startTag() + * or when last endTag() is called to close first startTag(). + * + * @return namespace set by startTag() that is currently in scope + */ + String getName(); + + /** + * Writes a start tag with the given namespace and name. + * If there is no prefix defined for the given namespace, + * a prefix will be defined automatically. + * The explicit prefixes for namespaces can be established by calling setPrefix() + * immediately before this method. + * If namespace is null no namespace prefix is printed but just name. + * If namespace is empty string then serialzier will make sure that + * default empty namespace is declared (in XML 1.0 xmlns='') + * or throw IllegalStateException if default namespace is already bound + * to non-empty string. + */ + XmlSerializer startTag (String namespace, String name) + throws IOException, IllegalArgumentException, IllegalStateException; + + /** + * Write an attribute. Calls to attribute() MUST follow a call to + * startTag() immediately. If there is no prefix defined for the + * given namespace, a prefix will be defined automatically. + * If namespace is null or empty string + * no namespace prefix is printed but just name. + */ + XmlSerializer attribute (String namespace, String name, String value) + throws IOException, IllegalArgumentException, IllegalStateException; + + /** + * Write end tag. Repetition of namespace and name is just for avoiding errors. + *

    Background: in kXML endTag had no arguments, and non matching tags were + * very difficult to find... + * If namespace is null no namespace prefix is printed but just name. + * If namespace is empty string then serialzier will make sure that + * default empty namespace is declared (in XML 1.0 xmlns=''). + */ + XmlSerializer endTag (String namespace, String name) + throws IOException, IllegalArgumentException, IllegalStateException; + + + // /** + // * Writes a start tag with the given namespace and name. + // *
    If there is no prefix defined (prefix == null) for the given namespace, + // * a prefix will be defined automatically. + // *
    If explicit prefixes is passed (prefix != null) then it will be used + // *and namespace declared if not already declared or + // * throw IllegalStateException the same prefix was already set on this + // * element (setPrefix()) and was bound to different namespace. + // *
    If namespace is null then prefix must be null too or IllegalStateException is thrown. + // *
    If namespace is null then no namespace prefix is printed but just name. + // *
    If namespace is empty string then serializer will make sure that + // * default empty namespace is declared (in XML 1.0 xmlns='') + // * or throw IllegalStateException if default namespace is already bound + // * to non-empty string. + // */ + // XmlSerializer startTag (String prefix, String namespace, String name) + // throws IOException, IllegalArgumentException, IllegalStateException; + // + // /** + // * Write an attribute. Calls to attribute() MUST follow a call to + // * startTag() immediately. + // *
    If there is no prefix defined (prefix == null) for the given namespace, + // * a prefix will be defined automatically. + // *
    If explicit prefixes is passed (prefix != null) then it will be used + // * and namespace declared if not already declared or + // * throw IllegalStateException the same prefix was already set on this + // * element (setPrefix()) and was bound to different namespace. + // *
    If namespace is null then prefix must be null too or IllegalStateException is thrown. + // *
    If namespace is null then no namespace prefix is printed but just name. + // *
    If namespace is empty string then serializer will make sure that + // * default empty namespace is declared (in XML 1.0 xmlns='') + // * or throw IllegalStateException if default namespace is already bound + // * to non-empty string. + // */ + // XmlSerializer attribute (String prefix, String namespace, String name, String value) + // throws IOException, IllegalArgumentException, IllegalStateException; + // + // /** + // * Write end tag. Repetition of namespace, prefix, and name is just for avoiding errors. + // *
    If namespace or name arguments are different from corresponding startTag call + // * then IllegalArgumentException is thrown, if prefix argument is not null and is different + // * from corresponding starTag then IllegalArgumentException is thrown. + // *
    If namespace is null then prefix must be null too or IllegalStateException is thrown. + // *
    If namespace is null then no namespace prefix is printed but just name. + // *
    If namespace is empty string then serializer will make sure that + // * default empty namespace is declared (in XML 1.0 xmlns=''). + // *

    Background: in kXML endTag had no arguments, and non matching tags were + // * very difficult to find...

    + // */ + // ALEK: This is really optional as prefix in end tag MUST correspond to start tag but good for error checking + // XmlSerializer endTag (String prefix, String namespace, String name) + // throws IOException, IllegalArgumentException, IllegalStateException; + + /** + * Writes text, where special XML chars are escaped automatically + */ + XmlSerializer text (String text) + throws IOException, IllegalArgumentException, IllegalStateException; + + /** + * Writes text, where special XML chars are escaped automatically + */ + XmlSerializer text (char [] buf, int start, int len) + throws IOException, IllegalArgumentException, IllegalStateException; + + void cdsect (String text) + throws IOException, IllegalArgumentException, IllegalStateException; + void entityRef (String text) throws IOException, + IllegalArgumentException, IllegalStateException; + void processingInstruction (String text) + throws IOException, IllegalArgumentException, IllegalStateException; + void comment (String text) + throws IOException, IllegalArgumentException, IllegalStateException; + void docdecl (String text) + throws IOException, IllegalArgumentException, IllegalStateException; + void ignorableWhitespace (String text) + throws IOException, IllegalArgumentException, IllegalStateException; + + /** + * Write all pending output to the stream. + * If method startTag() or attribute() was called then start tag is closed (final >) + * before flush() is called on underlying output stream. + * + *

    NOTE: if there is need to close start tag + * (so no more attribute() calls are allowed) but without flushinging output + * call method text() with empty string (text("")). + * + */ + void flush () + throws IOException; + +} +