diff --git a/LICENSE.txt b/LICENSE.txt index 324f532c612b68a041dabe09515f2cfdb5dd71cc..00e231a7874c6ec3e4b31ce9347b2697b9967744 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 0000000000000000000000000000000000000000..7e2a36863ab69c5834876529c9c6d3e8a7191134 --- /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 31d52758c8a35d41d18ce46968f0425288676796..2be28bea415cbd5d473c4e660546f41888dba2de 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 04a2cde91454d7b15156ef820ad546f50aa81224..f9ac683b648df178660a69a912ed981d032b9ca4 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 bade7591305bae90df61ce840c5be907661991c3..0b7d8d5470934d5b2116720b89caa061362dc59c 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 0000000000000000000000000000000000000000..aef65adc5721a1be4e74b4095e15546bc58eb4e3 --- /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<ForwardPort> portsToForward; + /** List of ports we have actually forwarded */ + private Set<ForwardPort> 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<ForwardPort>(); + 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<current.getDeviceList().size();i++) { + Device current2 = l.getDevice(i); + if (!current2.getDeviceType().equals(WANCON_DEVICE)) + continue; + + _service = current2.getService(WAN_PPP_CONNECTION); + if(_service == null) { + _log.warn(_router.getFriendlyName()+ " doesn't seems to be using PPP; we won't be able to extract bandwidth-related informations out of it."); + _service = current2.getService(WAN_IP_CONNECTION); + if(_service == null) + _log.error(_router.getFriendlyName()+ " doesn't export WAN_IP_CONNECTION either: we won't be able to use it!"); + } + + return; + } + } + } + } + + public boolean tryAddMapping(String protocol, int port, String description, ForwardPort fp) { + _log.warn("Registering a port mapping for " + port + "/" + protocol); + int nbOfTries = 0; + boolean isPortForwarded = false; + while(nbOfTries++ < 5) { + isPortForwarded = addMapping(protocol, port, "I2P " + description, fp); + if(isPortForwarded) + break; + try { + Thread.sleep(5000); + } catch (InterruptedException e) {} + } + _log.warn((isPortForwarded ? "Mapping is successful!" : "Mapping has failed!") + " ("+ nbOfTries + " tries)"); + return isPortForwarded; + } + + public void unregisterPortMappings() { + Set ports; + synchronized(lock) { + ports = portsForwarded; + } + this.unregisterPorts(ports); + } + + public void deviceRemoved(Device dev ){ + synchronized (lock) { + if(_router == null) return; + if(_router.equals(dev)) { + _router = null; + _service = null; + } + } + } + + /** + * @return whether we are behind an UPnP-enabled NAT/router + */ + public boolean isNATPresent() { + return _router != null && _service != null; + } + + /** + * @return the external address the NAT thinks we have. Blocking. + * null if we can't find it. + */ + public String getNATAddress() { + if(!isNATPresent()) + return null; + + Action getIP = _service.getAction("GetExternalIPAddress"); + if(getIP == null || !getIP.postControlAction()) + return null; + + return (getIP.getOutputArgumentList().getArgument("NewExternalIPAddress")).getValue(); + } + + /** + * @return the reported upstream bit rate in bits per second. -1 if it's not available. Blocking. + */ + public int getUpstramMaxBitRate() { + if(!isNATPresent() || thinksWeAreDoubleNatted) + return -1; + + Action getIP = _service.getAction("GetLinkLayerMaxBitRates"); + if(getIP == null || !getIP.postControlAction()) + return -1; + + return Integer.valueOf(getIP.getOutputArgumentList().getArgument("NewUpstreamMaxBitRate").getValue()); + } + + /** + * @return the reported downstream bit rate in bits per second. -1 if it's not available. Blocking. + */ + public int getDownstreamMaxBitRate() { + if(!isNATPresent() || thinksWeAreDoubleNatted) + return -1; + + Action getIP = _service.getAction("GetLinkLayerMaxBitRates"); + if(getIP == null || !getIP.postControlAction()) + return -1; + + return Integer.valueOf(getIP.getOutputArgumentList().getArgument("NewDownstreamMaxBitRate").getValue()); + } + + private void listStateTable(Service serv, StringBuilder sb) { + ServiceStateTable table = serv.getServiceStateTable(); + sb.append("<div><small>"); + for(int i=0; i<table.size(); i++) { + StateVariable current = table.getStateVariable(i); + sb.append(current.getName() + " : " + current.getValue() + "<br>"); + } + sb.append("</small></div>"); + } + + private void listActionsArguments(Action action, StringBuilder sb) { + ArgumentList ar = action.getArgumentList(); + for(int i=0; i<ar.size(); i++) { + Argument argument = ar.getArgument(i); + if(argument == null ) continue; + sb.append("<div><small>argument ("+i+") :" + argument.getName()+"</small></div>"); + } + } + + private void listActions(Service service, StringBuilder sb) { + ActionList al = service.getActionList(); + for(int i=0; i<al.size(); i++) { + Action action = al.getAction(i); + if(action == null ) continue; + sb.append("<div>action ("+i+") :" + action.getName()); + listActionsArguments(action, sb); + sb.append("</div>"); + } + } + + 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; i<sl.size(); i++) { + Service serv = sl.getService(i); + if(serv == null) continue; + sb.append("<div>service ("+i+") : "+serv.getServiceType()+"<br>"); + 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) + "<br>"); + }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) + "<br>"); + sb.append(" external IP: " + toString("GetExternalIPAddress", "NewExternalIPAddress", serv) + "<br>"); + }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) + "<br>"); + }else if("urn:schemas-upnp-org:service:WANEthernetLinkConfig:1".equals(serv.getServiceType())){ + sb.append("WANEthernetLinkConfig"); + sb.append(" status: " + toString("GetEthernetLinkStatus", "NewEthernetLinkStatus", serv) + "<br>"); + }else + sb.append("~~~~~~~ "+serv.getServiceType()); + listActions(serv, sb); + listStateTable(serv, sb); + sb.append("</div>"); + } + } + + private void listSubDev(String prefix, Device dev, StringBuilder sb){ + sb.append("<div><p>Device : "+dev.getFriendlyName()+" - "+ dev.getDeviceType()+"<br>"); + listSubServices(dev, sb); + + DeviceList dl = dev.getDeviceList(); + for(int j=0; j<dl.size(); j++) { + Device subDev = dl.getDevice(j); + if(subDev == null) continue; + + sb.append("<div>"); + listSubDev(dev.getFriendlyName(), subDev, sb); + sb.append("</div></div>"); + } + sb.append("</p></div>"); + } + +/***** + public String handleHTTPGet(HTTPRequest request) throws PluginHTTPException { + if(request.isParameterSet("getDeviceCapabilities")) { + final StringBuilder sb = new StringBuilder(); + sb.append("<html><head><title>UPnP report</title></head><body>"); + listSubDev("WANDevice", _router, sb); + sb.append("</body></html>"); + 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<ForwardPort> ports, ForwardPortCallback cb) { + Set<ForwardPort> portsToDumpNow = null; + Set<ForwardPort> 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<ForwardPort>(); + 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<ForwardPort>(); + 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<ForwardPort> 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<ForwardPort, ForwardPortStatus> map = new HashMap<ForwardPort, ForwardPortStatus>(); + 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<ForwardPort, ForwardPortStatus> map = new HashMap<ForwardPort, ForwardPortStatus>(); + map.put(port, new ForwardPortStatus(ForwardPortStatus.MAYBE_SUCCESS, "Port apparently forwarded by UPnP", port.portNumber)); + forwardCallback.portForwardStatus(map); + continue; + } else { + HashMap<ForwardPort, ForwardPortStatus> map = new HashMap<ForwardPort, ForwardPortStatus>(); + map.put(port, new ForwardPortStatus(ForwardPortStatus.PROBABLE_FAILURE, "UPnP port forwarding apparently failed", port.portNumber)); + forwardCallback.portForwardStatus(map); + continue; + } + } + } + + private void unregisterPorts(Set<ForwardPort> 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<Device> 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 0000000000000000000000000000000000000000..4deaebc4d9668dab6569cd3dc8a848b18cc139f2 --- /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<String, RouterAddress> addresses) { + _log.error("UPnP Update:"); + if (!_isRunning) + return; + Set<ForwardPort> 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<ForwardPort,ForwardPortStatus> 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 c23245bae41edab88fb81c2903697474fb314b41..2d5df4be0e60b3c2f521e17b2b65ce2159e5cf4c 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 e5185defaf28f50b3975416dbf8d658a4ac981dc..0a2e7f1bea41e24183d6c61b898d79db5bd434ac 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 0000000000000000000000000000000000000000..117964d34b49b326059d9eeab0ae057cf4c362b9 --- /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 <theo.beisch@gmx.de> +* - 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 0000000000000000000000000000000000000000..0d06e7ca53862d1d237cbaf7544a5a688dda2ccb --- /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 0000000000000000000000000000000000000000..ec7ccbde18013f52dc9ea08145be26e0cd20f491 --- /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 <sassarol@cefriel.it> +* - 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 <bhills@openshores.com> +* - 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 <sassarol@cefriel.it> (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 0000000000000000000000000000000000000000..d09446bdc6ef934dca1f75dbf2cf596611caa839 --- /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 <jan.newmarch@infotech.monash.edu.au> (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 <jan.newmarch@infotech.monash.edu.au> (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 0000000000000000000000000000000000000000..3c0c78e63e732253f7be3033ac62067b2113066c --- /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 <sassarol@cefriel.it> +* - 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" <Ralf@Ber.gs> +* - 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 <jan.newmarch@infotech.monash.edu.au> (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 <bhills@openshores.com> +* - Changed hasContentRange() to check Content-Range and Range header. +* - Added support for Range header to getContentRange(). +* 02/02/05 +* - Mark Retallack <mretallack@users.sourceforge.net> +* - 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 <sassarol@cefriel.it> (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<nHeaders; n++) { + HTTPHeader header = httpPacket.getHeader(n); + addHeader(header); + } + setContent(httpPacket.getContent()); + } + + //////////////////////////////////////////////// + // read + //////////////////////////////////////////////// + + public boolean read(HTTPSocket httpSock) + { + init(); + return set(httpSock); + } + + //////////////////////////////////////////////// + // String + //////////////////////////////////////////////// + + private String firstLine = ""; + + private void setFirstLine(String value) + { + firstLine = value; + } + + protected String getFirstLine() + { + return firstLine; + } + + protected String getFirstLineToken(int num) + { + StringTokenizer st = new StringTokenizer(firstLine, HTTP.REQEST_LINE_DELIM); + String lastToken = ""; + for (int n=0; n<=num; n++) { + if (st.hasMoreTokens() == false) + return ""; + lastToken = st.nextToken(); + } + return lastToken; + } + + public boolean hasFirstLine() + { + return (0 < firstLine.length()) ? true : false; + } + + //////////////////////////////////////////////// + // Header + //////////////////////////////////////////////// + + private Vector httpHeaderList = new Vector(); + + public int getNHeaders() + { + return httpHeaderList.size(); + } + + public void addHeader(HTTPHeader header) + { + httpHeaderList.add(header); + } + + public void addHeader(String name, String value) + { + HTTPHeader header = new HTTPHeader(name, value); + httpHeaderList.add(header); + } + + public HTTPHeader getHeader(int n) + { + return (HTTPHeader)httpHeaderList.get(n); + } + + public HTTPHeader getHeader(String name) + { + int nHeaders = getNHeaders(); + for (int n=0; n<nHeaders; n++) { + HTTPHeader header = getHeader(n); + String headerName = header.getName(); + if (headerName.equalsIgnoreCase(name) == true) + return header; + } + return null; + } + + public void clearHeaders() + { + httpHeaderList.clear(); + httpHeaderList = new Vector(); + } + + public boolean hasHeader(String name) + { + return (getHeader(name) != null) ? true : false; + } + + public void setHeader(String name, String value) + { + HTTPHeader header = getHeader(name); + if (header != null) { + header.setValue(value); + return; + } + addHeader(name, value); + } + + public void setHeader(String name, int value) + { + setHeader(name, Integer.toString(value)); + } + + public void setHeader(String name, long value) + { + setHeader(name, Long.toString(value)); + } + + public void setHeader(HTTPHeader header) + { + setHeader(header.getName(), header.getValue()); + } + + public String getHeaderValue(String name) + { + HTTPHeader header = getHeader(name); + if (header == null) + return ""; + return header.getValue(); + } + + //////////////////////////////////////////////// + // set*Value + //////////////////////////////////////////////// + + public void setStringHeader(String name, String value, String startWidth, String endWidth) + { + String headerValue = value; + if (headerValue.startsWith(startWidth) == false) + headerValue = startWidth + headerValue; + if (headerValue.endsWith(endWidth) == false) + headerValue = headerValue + endWidth; + setHeader(name, headerValue); + } + + public void setStringHeader(String name, String value) + { + setStringHeader(name, value, "\"", "\""); + } + + public String getStringHeaderValue(String name, String startWidth, String endWidth) + { + String headerValue = getHeaderValue(name); + if (headerValue.startsWith(startWidth) == true) + headerValue = headerValue.substring(1, headerValue.length()); + if (headerValue.endsWith(endWidth) == true) + headerValue = headerValue.substring(0, headerValue.length()-1); + return headerValue; + } + + public String getStringHeaderValue(String name) + { + return getStringHeaderValue(name, "\"", "\""); + } + + public void setIntegerHeader(String name, int value) + { + setHeader(name, Integer.toString(value)); + } + + public void setLongHeader(String name, long value) + { + setHeader(name, Long.toString(value)); + } + + public int getIntegerHeaderValue(String name) + { + HTTPHeader header = getHeader(name); + if (header == null) + return 0; + return StringUtil.toInteger(header.getValue()); + } + + public long getLongHeaderValue(String name) + { + HTTPHeader header = getHeader(name); + if (header == null) + return 0; + return StringUtil.toLong(header.getValue()); + } + + //////////////////////////////////////////////// + // getHeader + //////////////////////////////////////////////// + + public String getHeaderString() + { + StringBuilder str = new StringBuilder(); + + int nHeaders = getNHeaders(); + for (int n=0; n<nHeaders; n++) { + HTTPHeader header = getHeader(n); + str.append(header.getName() + ": " + header.getValue() + HTTP.CRLF); + } + + return str.toString(); + } + + //////////////////////////////////////////////// + // Contents + //////////////////////////////////////////////// + + private byte content[] = new byte[0]; + + public void setContent(byte data[], boolean updateWithContentLength) + { + content = data; + if (updateWithContentLength == true) + setContentLength(data.length); + } + + public void setContent(byte data[]) + { + setContent(data, true); + } + + public void setContent(String data, boolean updateWithContentLength) + { + setContent(data.getBytes(), updateWithContentLength); + } + + public void setContent(String data) + { + setContent(data, true); + } + + public byte []getContent() + { + return content; + } + + public String getContentString() + { + return new String(content); + } + + public boolean hasContent() + { + return (content.length > 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 0000000000000000000000000000000000000000..f4c6f16e2cda3d58979995c969d1957966b40858 --- /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 <sassarol@cefriel.it> +* - Add a relative URL check to setURI(). +* 09/02/03 +* - Giordano Sassaroli <sassarol@cefriel.it> +* - 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 <bhills@openshores.com> +* - 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 <sassarol@cefriel.it> (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 0000000000000000000000000000000000000000..12fcf349ed4db18331ce073c10e3761a31894a95 --- /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 0000000000000000000000000000000000000000..b5c24787890b75668410ed046631cb43a7e68246 --- /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 0000000000000000000000000000000000000000..f589e2e06147f1112ee077a7e31bf53ae15db647 --- /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<listenerSize; n++) { + HTTPRequestListener listener = (HTTPRequestListener)httpRequestListenerList.get(n); + listener.httpRequestRecieved(httpReq); + } + } + + //////////////////////////////////////////////// + // run + //////////////////////////////////////////////// + + private Thread httpServerThread = null; + + public void run() + { + if (isOpened() == false) + return; + + Thread thisThread = Thread.currentThread(); + + while (httpServerThread == thisThread) { + Thread.yield(); + Socket sock; + try { + Debug.message("accept ..."); + sock = accept(); + if (sock != null) + Debug.message("sock = " + sock.getRemoteSocketAddress()); + } + catch (Exception e){ + Debug.warning(e); + break; + } + HTTPServerThread httpServThread = new HTTPServerThread(this, sock); + httpServThread.start(); + Debug.message("httpServThread ..."); + } + } + + public boolean start() + { + httpServerThread = new Thread(this, "UPnP-HTTPServer"); + httpServerThread.start(); + return true; + } + + public boolean stop() + { + httpServerThread = null; + return true; + } +} diff --git a/router/java/src/org/cybergarage/http/HTTPServerList.java b/router/java/src/org/cybergarage/http/HTTPServerList.java new file mode 100644 index 0000000000000000000000000000000000000000..3a4e6588760fef953b549117a38324db945c2a03 --- /dev/null +++ b/router/java/src/org/cybergarage/http/HTTPServerList.java @@ -0,0 +1,104 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: HTTPServerList.java +* +* Revision; +* +* 05/08/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.http; + +import java.util.*; + +import org.cybergarage.net.*; + +public class HTTPServerList extends Vector +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + private static final long serialVersionUID = 2379889735659369065L; + + public HTTPServerList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public void addRequestListener(HTTPRequestListener listener) + { + int nServers = size(); + for (int n=0; n<nServers; n++) { + HTTPServer server = getHTTPServer(n); + server.addRequestListener(listener); + } + } + + public HTTPServer getHTTPServer(int n) + { + return (HTTPServer)get(n); + } + + //////////////////////////////////////////////// + // open/close + //////////////////////////////////////////////// + + public void close() + { + int nServers = size(); + for (int n=0; n<nServers; n++) { + HTTPServer server = getHTTPServer(n); + server.close(); + } + } + + public boolean open(int port) + { + int nHostAddrs = HostInterface.getNHostAddresses(); + for (int n=0; n<nHostAddrs; n++) { + String bindAddr = HostInterface.getHostAddress(n); + HTTPServer httpServer = new HTTPServer(); + if (httpServer.open(bindAddr, port) == false) { + close(); + clear(); + return false; + } + add(httpServer); + } + return true; + } + + //////////////////////////////////////////////// + // start/stop + //////////////////////////////////////////////// + + public void start() + { + int nServers = size(); + for (int n=0; n<nServers; n++) { + HTTPServer server = getHTTPServer(n); + server.start(); + } + } + + public void stop() + { + int nServers = size(); + for (int n=0; n<nServers; n++) { + HTTPServer server = getHTTPServer(n); + server.stop(); + } + } + +} + diff --git a/router/java/src/org/cybergarage/http/HTTPServerThread.java b/router/java/src/org/cybergarage/http/HTTPServerThread.java new file mode 100644 index 0000000000000000000000000000000000000000..40351ecaf0b91c84e21eeb36df4be7cc128aceac --- /dev/null +++ b/router/java/src/org/cybergarage/http/HTTPServerThread.java @@ -0,0 +1,54 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: HTTPServerThread.java +* +* Revision; +* +* 10/10/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.http; + +import java.net.*; + +public class HTTPServerThread extends Thread +{ + private HTTPServer httpServer; + private Socket sock; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public HTTPServerThread(HTTPServer httpServer, Socket sock) + { + this.httpServer = httpServer; + this.sock = sock; + this.setDaemon(true); + } + + //////////////////////////////////////////////// + // run + //////////////////////////////////////////////// + + public void run() + { + HTTPSocket httpSock = new HTTPSocket(sock); + if (httpSock.open() == false) + return; + HTTPRequest httpReq = new HTTPRequest(); + httpReq.setSocket(httpSock); + while (httpReq.read() == true) { + httpServer.performRequestListener(httpReq); + if (httpReq.isKeepAlive() == false) + break; + } + httpSock.close(); + } +} diff --git a/router/java/src/org/cybergarage/http/HTTPSocket.java b/router/java/src/org/cybergarage/http/HTTPSocket.java new file mode 100644 index 0000000000000000000000000000000000000000..6709aa41a4c1f036aa25ced2606f0f30be68ab40 --- /dev/null +++ b/router/java/src/org/cybergarage/http/HTTPSocket.java @@ -0,0 +1,249 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: HTTPSocket.java +* +* Revision; +* +* 12/12/02 +* - first revision. +* 03/11/04 +* - Added the following methods about chunk size. +* setChunkSize(), getChunkSize(). +* 08/26/04 +* - Added a isOnlyHeader to 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 HTTPSocket +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public HTTPSocket(Socket socket) + { + setSocket(socket); + open(); + } + + public HTTPSocket(HTTPSocket socket) + { + setSocket(socket.getSocket()); + setInputStream(socket.getInputStream()); + setOutputStream(socket.getOutputStream()); + } + + public void finalize() + { + close(); + } + + //////////////////////////////////////////////// + // Socket + //////////////////////////////////////////////// + + private Socket socket = null; + + private void setSocket(Socket socket) + { + this.socket = socket; + } + + public Socket getSocket() + { + return socket; + } + + //////////////////////////////////////////////// + // local address/port + //////////////////////////////////////////////// + + public String getLocalAddress() + { + return getSocket().getLocalAddress().getHostAddress(); + } + + public int getLocalPort() + { + return getSocket().getLocalPort(); + } + + //////////////////////////////////////////////// + // in/out + //////////////////////////////////////////////// + + private InputStream sockIn = null; + private OutputStream sockOut = null; + + private void setInputStream(InputStream in) + { + sockIn = in; + } + + public InputStream getInputStream() + { + return sockIn; + } + + private void setOutputStream(OutputStream out) + { + sockOut = out; + } + + private OutputStream getOutputStream() + { + return sockOut; + } + + //////////////////////////////////////////////// + // open/close + //////////////////////////////////////////////// + + public boolean open() + { + Socket sock = getSocket(); + try { + sockIn = sock.getInputStream(); + sockOut = sock.getOutputStream(); + } + catch (Exception e) { + return false; + } + return true; + } + + public boolean close() + { + try { + if (sockIn != null) + sockIn.close(); + if (sockOut != null) + sockOut.close(); + getSocket().close(); + } + catch (Exception e) { + //Debug.warning(e); + return false; + } + return true; + } + + //////////////////////////////////////////////// + // post + //////////////////////////////////////////////// + + private boolean post(HTTPResponse httpRes, byte content[], long contentOffset, long contentLength, boolean isOnlyHeader) + { + httpRes.setDate(Calendar.getInstance()); + OutputStream out = getOutputStream(); + + try { + httpRes.setContentLength(contentLength); + + out.write(httpRes.getHeader().getBytes()); + out.write(HTTP.CRLF.getBytes()); + if (isOnlyHeader == true) { + out.flush(); + return true; + } + + boolean isChunkedResponse = httpRes.isChunked(); + + if (isChunkedResponse == true) { + String chunSizeBuf = Long.toString(contentLength); + out.write(chunSizeBuf.getBytes()); + out.write(HTTP.CRLF.getBytes()); + } + + out.write(content, (int)contentOffset, (int)contentLength); + + if (isChunkedResponse == true) { + out.write(HTTP.CRLF.getBytes()); + out.write("0".getBytes()); + out.write(HTTP.CRLF.getBytes()); + } + + out.flush(); + } + catch (Exception e) { + //Debug.warning(e); + return false; + } + + return true; + } + + private boolean post(HTTPResponse httpRes, InputStream in, long contentOffset, long contentLength, boolean isOnlyHeader) + { + httpRes.setDate(Calendar.getInstance()); + OutputStream out = getOutputStream(); + + try { + httpRes.setContentLength(contentLength); + + out.write(httpRes.getHeader().getBytes()); + out.write(HTTP.CRLF.getBytes()); + + if (isOnlyHeader == true) { + out.flush(); + return true; + } + + boolean isChunkedResponse = httpRes.isChunked(); + + if (0 < contentOffset) + in.skip(contentOffset); + + int chunkSize = HTTP.getChunkSize(); + byte readBuf[] = new byte[chunkSize]; + long readCnt = 0; + long readSize = (chunkSize < contentLength) ? chunkSize : contentLength; + int readLen = in.read(readBuf, 0, (int)readSize); + while (0 < readLen && readCnt < contentLength) { + if (isChunkedResponse == true) { + String chunSizeBuf = Long.toString(readLen); + out.write(chunSizeBuf.getBytes()); + out.write(HTTP.CRLF.getBytes()); + } + out.write(readBuf, 0, readLen); + if (isChunkedResponse == true) + out.write(HTTP.CRLF.getBytes()); + readCnt += readLen; + readSize = (chunkSize < (contentLength-readCnt)) ? chunkSize : (contentLength-readCnt); + readLen = in.read(readBuf, 0, (int)readSize); + } + + if (isChunkedResponse == true) { + out.write("0".getBytes()); + out.write(HTTP.CRLF.getBytes()); + } + + out.flush(); + } + catch (Exception e) { + //Debug.warning(e); + return false; + } + + return true; + } + + public boolean post(HTTPResponse httpRes, long contentOffset, long contentLength, boolean isOnlyHeader) + { + if (httpRes.hasContentInputStream() == true) + return post(httpRes,httpRes.getContentInputStream(), contentOffset, contentLength, isOnlyHeader); + return post(httpRes,httpRes.getContent(), contentOffset, contentLength, isOnlyHeader); + } +} diff --git a/router/java/src/org/cybergarage/http/HTTPStatus.java b/router/java/src/org/cybergarage/http/HTTPStatus.java new file mode 100644 index 0000000000000000000000000000000000000000..b112b2bd03a81069ee9b39f73ae32cfb410594f9 --- /dev/null +++ b/router/java/src/org/cybergarage/http/HTTPStatus.java @@ -0,0 +1,185 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: HTTPStatus.java +* +* Revision; +* +* 12/17/02 +* - first revision. +* 09/03/03 +* - Added CONTINUE_STATUS. +* 10/20/04 +* - Brent Hills <bhills@openshores.com> +* - 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 0000000000000000000000000000000000000000..fb29ae994fe07ca3cabd5f59487aa1f9c247fd07 --- /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 0000000000000000000000000000000000000000..9a0b2a6d1d42792e54651182051639a1589464ed --- /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<nLists; n++) { + Parameter param = at(n); + if (name.compareTo(param.getName()) == 0) + return param; + } + return null; + } + + public String getValue(String name) + { + Parameter param = getParameter(name); + if (param == null) + return ""; + return param.getValue(); + } +} + diff --git a/router/java/src/org/cybergarage/net/HostInterface.java b/router/java/src/org/cybergarage/net/HostInterface.java new file mode 100644 index 0000000000000000000000000000000000000000..798f33dcac61cdbf01ecc2ce491c763d87492eec --- /dev/null +++ b/router/java/src/org/cybergarage/net/HostInterface.java @@ -0,0 +1,230 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: HostInterface.java +* +* Revision; +* +* 05/12/03 +* - first revision. +* 05/13/03 +* - Added support for IPv6 and loopback address. +* 02/15/04 +* - Added the following methods to set only a interface. +* - setInterface(), getInterfaces(), hasAssignedInterface() +* 06/30/04 +* - Moved the package from org.cybergarage.http to org.cybergarage.net. +* 06/30/04 +* - Theo Beisch <theo.beisch@gmx.de> +* - 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<addrCnt; n++) { + String addr = getHostAddress(n); + if (isIPv4Address(addr) == true) + return true; + } + return false; + } + + public final static boolean hasIPv6Addresses() + { + int addrCnt = getNHostAddresses(); + for (int n=0; n<addrCnt; n++) { + String addr = getHostAddress(n); + if (isIPv6Address(addr) == true) + return true; + } + return false; + } + + //////////////////////////////////////////////// + // hasIPv?Interfaces + //////////////////////////////////////////////// + + public final static String getIPv4Address() + { + int addrCnt = getNHostAddresses(); + for (int n=0; n<addrCnt; n++) { + String addr = getHostAddress(n); + if (isIPv4Address(addr) == true) + return addr; + } + return ""; + } + + public final static String getIPv6Address() + { + int addrCnt = getNHostAddresses(); + for (int n=0; n<addrCnt; n++) { + String addr = getHostAddress(n); + if (isIPv6Address(addr) == true) + return addr; + } + return ""; + } + + //////////////////////////////////////////////// + // getHostURL + //////////////////////////////////////////////// + + public final static String getHostURL(String host, int port, String uri) + { + String hostAddr = host; + if (isIPv6Address(host) == true) + hostAddr = "[" + host + "]"; + return + "http://" + + hostAddr + + ":" + Integer.toString(port) + + uri; + } + +} diff --git a/router/java/src/org/cybergarage/soap/SOAP.java b/router/java/src/org/cybergarage/soap/SOAP.java new file mode 100644 index 0000000000000000000000000000000000000000..dec42f7ed8719f07151b8fa6137531482c755564 --- /dev/null +++ b/router/java/src/org/cybergarage/soap/SOAP.java @@ -0,0 +1,81 @@ +/****************************************************************** +* +* CyberSOAP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SOAP.java +* +* Revision; +* +* 12/11/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.soap; + +import org.cybergarage.xml.*; + +public class SOAP +{ + public static final String ENVELOPE = "Envelope"; + public static final String BODY = "Body"; + public static final String RESPONSE = "Response"; + public static final String FAULT = "Fault"; + public static final String FAULT_CODE = "faultcode"; + public static final String FAULT_STRING = "faultstring"; + public static final String FAULTACTOR = "faultactor"; + public static final String DETAIL = "detail"; + + public static final String RESULTSTATUS = "ResultStatus"; + public static final String UPNP_ERROR = "UPnPError"; + public static final String ERROR_CODE = "errorCode"; + public static final String ERROR_DESCRIPTION = "errorDescription"; + + //public static final String XMLNS = "SOAP-ENV"; + public static final String XMLNS = "s"; + public static final String METHODNS = "u"; + public static final String DELIM = ":"; + + public static final String XMLNS_URL = "http://schemas.xmlsoap.org/soap/envelope/"; + public static final String ENCSTYLE_URL = "http://schemas.xmlsoap.org/soap/encoding/"; + + public static final String CONTENT_TYPE = "text/xml; charset=\"utf-8\""; + public static final String VERSION_HEADER = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; + + //////////////////////////////////////////////// + // createEnvelopeBodyNode + //////////////////////////////////////////////// + + public final static Node createEnvelopeBodyNode() + { + // <Envelope> + 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); + + // <Body> + 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 0000000000000000000000000000000000000000..d07b12d681bf60f0240c252ae1b9d0702af3971b --- /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 <Ralf@Ber.gs>, Inma Marin Lopez <inma@dif.um.es>. +* - Added XML header, <?xml version=\"1.0\"?> to setContent(). +* 05/11/04 +* - Changed the XML header to <?xml version="1.0" encoding="utf-8"?> 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 <Ralf@Ber.gs>, Inma Marin Lopez <inma@dif.um.es>. + 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 0000000000000000000000000000000000000000..fb59f3a21eeb76689b1c5a69a114609d51cb6cc8 --- /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 <Ralf@Ber.gs>, Inma Marin Lopez <inma@dif.um.es>. +* - Added XML header, <?xml version="1.0"?> to setContent(). +* 05/11/04 +* - Changed the XML header to <?xml version="1.0" encoding="utf-8"?> 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 <Ralf@Ber.gs>, Inma Marin Lopez <inma@dif.um.es>. + 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 0000000000000000000000000000000000000000..9cf98a44d471a803715024ef633894ab4116762d --- /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 <sassarol@cefriel.it> +* - 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 <cyberrate@users.sourceforge.net> and Stefano Lenzi <kismet-sl@users.sourceforge.net> +* - 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<nodeCnt; n++) { + Node node = argumentListNode.getNode(n); + if (Argument.isArgumentNode(node) == false) + continue; + Argument argument = new Argument(getServiceNode(), node); + argumentList.add(argument); + } + return argumentList; + } + + public ArgumentList getInputArgumentList() + { + ArgumentList allArgList = getArgumentList(); + int allArgCnt = allArgList.size(); + ArgumentList argList = new ArgumentList(); + for (int n=0; n<allArgCnt; n++) { + Argument arg = allArgList.getArgument(n); + if (arg.isInDirection() == false) + continue; + argList.add(arg); + } + return argList; + } + + public ArgumentList getOutputArgumentList() + { + ArgumentList allArgList = getArgumentList(); + int allArgCnt = allArgList.size(); + ArgumentList argList = new ArgumentList(); + for (int n=0; n<allArgCnt; n++) { + Argument arg = allArgList.getArgument(n); + if (arg.isOutDirection() == false) + continue; + argList.add(arg); + } + return argList; + } + + public Argument getArgument(String name) + { + ArgumentList argList = getArgumentList(); + int nArgs = argList.size(); + for (int n=0; n<nArgs; n++) { + Argument arg = argList.getArgument(n); + String argName = arg.getName(); + if (argName == null) + continue; + if (name.equals(argName) == true) + return arg; + } + return null; + } + + public void setArgumentValues(ArgumentList argList) + { + getArgumentList().set(argList); + } + + public void setArgumentValue(String name, String value) + { + Argument arg = getArgument(name); + if (arg == null) + return; + arg.setValue(value); + } + + public void setArgumentValue(String name, int value) + { + setArgumentValue(name, Integer.toString(value)); + } + + private void clearOutputAgumentValues() + { + ArgumentList allArgList = getArgumentList(); + int allArgCnt = allArgList.size(); + for (int n=0; n<allArgCnt; n++) { + Argument arg = allArgList.getArgument(n); + if (arg.isOutDirection() == false) + continue; + arg.setValue(""); + } + } + + public String getArgumentValue(String name) + { + Argument arg = getArgument(name); + if (arg == null) + return ""; + return arg.getValue(); + } + + public int getArgumentIntegerValue(String name) + { + Argument arg = getArgument(name); + if (arg == null) + return 0; + return arg.getIntegerValue(); + } + + //////////////////////////////////////////////// + // UserData + //////////////////////////////////////////////// + + private ActionData getActionData() + { + Node node = getActionNode(); + ActionData userData = (ActionData)node.getUserData(); + if (userData == null) { + userData = new ActionData(); + node.setUserData(userData); + userData.setNode(node); + } + return userData; + } + + //////////////////////////////////////////////// + // controlAction + //////////////////////////////////////////////// + + public ActionListener getActionListener() + { + return getActionData().getActionListener(); + } + + public void setActionListener(ActionListener listener) + { + getActionData().setActionListener(listener); + } + + public boolean performActionListener(ActionRequest actionReq) + { + ActionListener listener = (ActionListener)getActionListener(); + if (listener == null) + return false; + ActionResponse actionRes = new ActionResponse(); + setStatus(UPnPStatus.INVALID_ACTION); + clearOutputAgumentValues(); + if (listener.actionControlReceived(this) == true) { + actionRes.setResponse(this); + } + else { + UPnPStatus upnpStatus = getStatus(); + actionRes.setFaultResponse(upnpStatus.getCode(), upnpStatus.getDescription()); + } + if (Debug.isOn() == true) + actionRes.print(); + actionReq.post(actionRes); + return true; + } + + //////////////////////////////////////////////// + // ActionControl + //////////////////////////////////////////////// + + private ControlResponse getControlResponse() + { + return getActionData().getControlResponse(); + } + + private void setControlResponse(ControlResponse res) + { + getActionData().setControlResponse(res); + } + + public UPnPStatus getControlStatus() + { + return getControlResponse().getUPnPError(); + } + + //////////////////////////////////////////////// + // postControlAction + //////////////////////////////////////////////// + + public boolean postControlAction() + { + // Thanks for Giordano Sassaroli <sassarol@cefriel.it> (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 <cyberrate@users.sourceforge.net> and Stefano Lenzi <kismet-sl@users.sourceforge.net> (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<nArgs; n++) { + Argument arg = argList.getArgument(n); + String name = arg.getName(); + String value = arg.getValue(); + String dir = arg.getDirection(); + System.out.println(" [" + n + "] = " + dir + ", " + name + ", " + value); + } + } + + //////////////////////////////////////////////// + // 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/ActionList.java b/router/java/src/org/cybergarage/upnp/ActionList.java new file mode 100644 index 0000000000000000000000000000000000000000..6a5a688dc8bc52b2bf0aecf294fb0d60cd79f940 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ActionList.java @@ -0,0 +1,46 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ActionList.java +* +* Revision: +* +* 12/05/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import java.util.*; + +public class ActionList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + private static final long serialVersionUID = 1965922721316119846L; + public final static String ELEM_NAME = "actionList"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public ActionList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public Action getAction(int n) + { + return (Action)get(n); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/AllowedValue.java b/router/java/src/org/cybergarage/upnp/AllowedValue.java new file mode 100644 index 0000000000000000000000000000000000000000..24bc45769279285e271cb97cc3a8fd3e2b0ce341 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/AllowedValue.java @@ -0,0 +1,70 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: AllowedValue.java +* +* Revision: +* +* 03/27/04 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import org.cybergarage.xml.*; + +public class AllowedValue +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "allowedValue"; + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private Node allowedValueNode; + + public Node getAllowedValueNode() + { + return allowedValueNode; + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public AllowedValue(Node node) + { + allowedValueNode = node; + } + + //////////////////////////////////////////////// + // isAllowedValueNode + //////////////////////////////////////////////// + + public static boolean isAllowedValueNode(Node node) + { + return ELEM_NAME.equals(node.getName()); + } + + //////////////////////////////////////////////// + // Value + //////////////////////////////////////////////// + + public void setValue(String value) + { + getAllowedValueNode().setValue(value); + } + + public String getValue() + { + return getAllowedValueNode().getValue(); + } +} diff --git a/router/java/src/org/cybergarage/upnp/AllowedValueList.java b/router/java/src/org/cybergarage/upnp/AllowedValueList.java new file mode 100644 index 0000000000000000000000000000000000000000..da10589962ad3e3ae4658f734c02e7c17df9eaa1 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/AllowedValueList.java @@ -0,0 +1,49 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: AllowedValueList.java +* +* Revision: +* +* 03/27/04 +* - first revision. +* 02/28/05 +* - Changed to use AllowedValue instead of String as the member. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import java.util.*; + +public class AllowedValueList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + private static final long serialVersionUID = 5740394642751180992L; + public final static String ELEM_NAME = "allowedValueList"; + + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public AllowedValueList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public AllowedValue getAllowedValue(int n) + { + return (AllowedValue)get(n); + } + +} diff --git a/router/java/src/org/cybergarage/upnp/AllowedValueRange.java b/router/java/src/org/cybergarage/upnp/AllowedValueRange.java new file mode 100644 index 0000000000000000000000000000000000000000..3c4ba60c1ea6d04f37a093cd5958d15de01ad10c --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/AllowedValueRange.java @@ -0,0 +1,104 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: AllowedValueRange.java +* +* Revision: +* +* 03/27/04 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import org.cybergarage.xml.*; + +public class AllowedValueRange +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "allowedValueRange"; + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private Node allowedValueRangeNode; + + public Node getAllowedValueRangeNode() + { + return allowedValueRangeNode; + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public AllowedValueRange(Node node) + { + allowedValueRangeNode = node; + } + + //////////////////////////////////////////////// + // isAllowedValueRangeNode + //////////////////////////////////////////////// + + public static boolean isAllowedValueRangeNode(Node node) + { + return ELEM_NAME.equals(node.getName()); + } + + //////////////////////////////////////////////// + // minimum + //////////////////////////////////////////////// + + private final static String MINIMUM = "minimum"; + + public void setMinimum(String value) + { + getAllowedValueRangeNode().setNode(MINIMUM, value); + } + + public String getMinimum() + { + return getAllowedValueRangeNode().getNodeValue(MINIMUM); + } + + //////////////////////////////////////////////// + // maximum + //////////////////////////////////////////////// + + private final static String MAXIMUM = "maximum"; + + public void setMaximum(String value) + { + getAllowedValueRangeNode().setNode(MAXIMUM, value); + } + + public String getMaximum() + { + return getAllowedValueRangeNode().getNodeValue(MAXIMUM); + } + + //////////////////////////////////////////////// + // width + //////////////////////////////////////////////// + + private final static String STEP = "step"; + + public void setStep(String value) + { + getAllowedValueRangeNode().setNode(STEP, value); + } + + public String getStep() + { + return getAllowedValueRangeNode().getNodeValue(STEP); + } +} diff --git a/router/java/src/org/cybergarage/upnp/Argument.java b/router/java/src/org/cybergarage/upnp/Argument.java new file mode 100644 index 0000000000000000000000000000000000000000..6a1907551ab3dc0960dfe67334816a7be7e67077 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/Argument.java @@ -0,0 +1,231 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: Argument.java +* +* Revision; +* +* 12/05/02 +* - first revision. +* 03/28/04 +* - Added getRelatedStateVariable(). +* - Changed setRelatedStateVariable() to setRelatedStateVariableName(). +* - Changed getRelatedStateVariable() to getRelatedStateVariableName(). +* - Added getActionNode() and getAction(). +* - Added getServiceNode() and getService(). +* - Added the parent service node to the constructor. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import org.cybergarage.xml.*; + +import org.cybergarage.upnp.xml.*; + +public class Argument +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "argument"; + + public final static String IN = "in"; + public final static String OUT = "out"; + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private Node argumentNode; + private Node serviceNode; + + public Node getArgumentNode() + { + return argumentNode; + } + + private Node getServiceNode() + { + return serviceNode; + } + + public Service getService() + { + return new Service(getServiceNode()); + } + + public Node getActionNode() + { + Node argumentLinstNode = getArgumentNode().getParentNode(); + if (argumentLinstNode == null) + return null; + Node actionNode = argumentLinstNode.getParentNode(); + if (actionNode == null) + return null; + if (Action.isActionNode(actionNode) == false) + return null; + return actionNode; + } + + public Action getAction() + { + return new Action(getServiceNode(), getActionNode()); + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public Argument() + { + argumentNode = new Node(); + serviceNode = null; + } + + public Argument(Node servNode, Node argNode) + { + serviceNode = servNode; + argumentNode = argNode; + } + + public Argument(String name, String value) + { + this(); + setName(name); + setValue(value); + } + + //////////////////////////////////////////////// + // isArgumentNode + //////////////////////////////////////////////// + + public static boolean isArgumentNode(Node node) + { + return Argument.ELEM_NAME.equals(node.getName()); + } + + //////////////////////////////////////////////// + // name + //////////////////////////////////////////////// + + private final static String NAME = "name"; + + public void setName(String value) + { + getArgumentNode().setNode(NAME, value); + } + + public String getName() + { + return getArgumentNode().getNodeValue(NAME); + } + + //////////////////////////////////////////////// + // direction + //////////////////////////////////////////////// + + private final static String DIRECTION = "direction"; + + public void setDirection(String value) + { + getArgumentNode().setNode(DIRECTION, value); + } + + public String getDirection() + { + return getArgumentNode().getNodeValue(DIRECTION); + } + + public boolean isInDirection() + { + String dir = getDirection(); + if (dir == null) + return false; + return dir.equalsIgnoreCase(IN); + } + + public boolean isOutDirection() + { + return !isInDirection(); + } + + //////////////////////////////////////////////// + // relatedStateVariable + //////////////////////////////////////////////// + + private final static String RELATED_STATE_VARIABLE = "relatedStateVariable"; + + public void setRelatedStateVariableName(String value) + { + getArgumentNode().setNode(RELATED_STATE_VARIABLE, value); + } + + public String getRelatedStateVariableName() + { + return getArgumentNode().getNodeValue(RELATED_STATE_VARIABLE); + } + + public StateVariable getRelatedStateVariable() + { + Service service = getService(); + if (service == null) + return null; + String relatedStatVarName = getRelatedStateVariableName(); + return service.getStateVariable(relatedStatVarName); + } + + //////////////////////////////////////////////// + // UserData + //////////////////////////////////////////////// + + private ArgumentData getArgumentData() + { + Node node = getArgumentNode(); + ArgumentData userData = (ArgumentData)node.getUserData(); + if (userData == null) { + userData = new ArgumentData(); + node.setUserData(userData); + userData.setNode(node); + } + return userData; + } + + //////////////////////////////////////////////// + // value + //////////////////////////////////////////////// + + public void setValue(String value) + { + getArgumentData().setValue(value); + } + + public void setValue(int value) + { + setValue(Integer.toString(value)); + } + + public String getValue() + { + return getArgumentData().getValue(); + } + + public int getIntegerValue() + { + String value = getValue(); + try { + return Integer.parseInt(value); + } + catch (Exception e) { + } + return 0; + } + + //////////////////////////////////////////////// + // Related + //////////////////////////////////////////////// +} diff --git a/router/java/src/org/cybergarage/upnp/ArgumentList.java b/router/java/src/org/cybergarage/upnp/ArgumentList.java new file mode 100644 index 0000000000000000000000000000000000000000..95620d281e8cd9697582073e73b33bc953397e49 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ArgumentList.java @@ -0,0 +1,77 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ArgumentList.java +* +* Revision: +* +* 12/05/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import java.util.*; + +public class ArgumentList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + private static final long serialVersionUID = -5412792105767389170L; + public final static String ELEM_NAME = "argumentList"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public ArgumentList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public Argument getArgument(int n) + { + return (Argument)get(n); + } + + public Argument getArgument(String name) + { + int nArgs = size(); + for (int n=0; n<nArgs; n++) { + Argument arg = getArgument(n); + String argName = arg.getName(); + if (argName == null) + continue; + if (argName.equals(name) == true) + return arg; + } + return null; + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public void set(ArgumentList inArgList) + { + int nInArgs = inArgList.size(); + for (int n=0; n<nInArgs; n++) { + Argument inArg = inArgList.getArgument(n); + String inArgName = inArg.getName(); + Argument arg = getArgument(inArgName); + if (arg == null) + continue; + arg.setValue(inArg.getValue()); + } + } +} + diff --git a/router/java/src/org/cybergarage/upnp/ControlPoint.java b/router/java/src/org/cybergarage/upnp/ControlPoint.java new file mode 100644 index 0000000000000000000000000000000000000000..03cc3726478f1d691e0e5e1863732f4221991925 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ControlPoint.java @@ -0,0 +1,917 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: ControlPoint.java +* +* Revision: +* +* 11/18/02 +* - first revision. +* 05/13/03 +* - Changed to create socket threads each local interfaces. +* (HTTP, SSDPNotiry, SSDPSerachResponse) +* 05/28/03 +* - Changed to send m-serach packets from SSDPSearchResponseSocket. +* The socket doesn't bind interface address. +* - SSDPSearchResponsSocketList that binds a port and a interface can't +* send m-serch packets of IPv6 on J2SE v 1.4.1_02 and Redhat 9. +* 07/23/03 +* - Suzan Foster (suislief) +* - Fixed a bug. HOST field was missing. +* 07/29/03 +* - Synchronized when a device is added by the ssdp message. +* 09/08/03 +* - Giordano Sassaroli <sassarol@cefriel.it> +* - 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 <newell@media-rush.com> +* - 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<nRoots; n++) { + Node rootNode = devNodeList.getNode(n); + Device dev = getDevice(rootNode); + if (dev == null) + continue; + devList.add(dev); + } + return devList; + } + + public Device getDevice(String name) + { + int nRoots = devNodeList.size(); + for (int n=0; n<nRoots; n++) { + Node rootNode = devNodeList.getNode(n); + Device dev = getDevice(rootNode); + if (dev == null) + continue; + if (dev.isDevice(name) == true) + return dev; + Device cdev = dev.getDevice(name); + if (cdev != null) + return cdev; + } + return null; + } + + public boolean hasDevice(String name) + { + return (getDevice(name) != null) ? true : false; + } + + private void removeDevice(Node rootNode) + { + // Thanks for Oliver Newell (2004/10/16) + // Invoke device removal listener prior to actual removal so Device node + // remains valid for the duration of the listener (application may want + // to access the node) + Device dev = getDevice(rootNode); + if( dev != null && dev.isRootDevice() ) + performRemoveDeviceListener( dev ); + + devNodeList.remove(rootNode); + } + + private void removeDevice(Device dev) + { + if (dev == null) + return; + removeDevice(dev.getRootNode()); + } + + private void removeDevice(String name) + { + Device dev = getDevice(name); + removeDevice(dev); + } + + private void removeDevice(SSDPPacket packet) + { + if (packet.isByeBye() == false) + return; + String usn = packet.getUSN(); + String udn = USN.getUDN(usn); + removeDevice(udn); + } + + //////////////////////////////////////////////// + // Expired Device + //////////////////////////////////////////////// + + private Disposer deviceDisposer; + private long expiredDeviceMonitoringInterval; + + public void removeExpiredDevices() + { + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + Device dev[] = new Device[devCnt]; + for (int n=0; n<devCnt; n++) + dev[n] = devList.getDevice(n); + for (int n=0; n<devCnt; n++) { + if (dev[n].isExpired() == true) { + Debug.message("Expired device = " + dev[n].getFriendlyName()); + removeDevice(dev[n]); + } + } + } + + public void setExpiredDeviceMonitoringInterval(long interval) + { + expiredDeviceMonitoringInterval = interval; + } + + public long getExpiredDeviceMonitoringInterval() + { + return expiredDeviceMonitoringInterval; + } + + public void setDeviceDisposer(Disposer disposer) + { + deviceDisposer = disposer; + } + + public Disposer getDeviceDisposer() + { + return deviceDisposer; + } + + //////////////////////////////////////////////// + // Notify + //////////////////////////////////////////////// + + private ListenerList deviceNotifyListenerList = new ListenerList(); + + public void addNotifyListener(NotifyListener listener) + { + deviceNotifyListenerList.add(listener); + } + + public void removeNotifyListener(NotifyListener listener) + { + deviceNotifyListenerList.remove(listener); + } + + public void performNotifyListener(SSDPPacket ssdpPacket) + { + int listenerSize = deviceNotifyListenerList.size(); + for (int n=0; n<listenerSize; n++) { + NotifyListener listener = (NotifyListener)deviceNotifyListenerList.get(n); + listener.deviceNotifyReceived(ssdpPacket); + } + } + + //////////////////////////////////////////////// + // SearchResponse + //////////////////////////////////////////////// + + private ListenerList deviceSearchResponseListenerList = new ListenerList(); + + public void addSearchResponseListener(SearchResponseListener listener) + { + deviceSearchResponseListenerList.add(listener); + } + + public void removeSearchResponseListener(SearchResponseListener listener) + { + deviceSearchResponseListenerList.remove(listener); + } + + public void performSearchResponseListener(SSDPPacket ssdpPacket) + { + int listenerSize = deviceSearchResponseListenerList.size(); + for (int n=0; n<listenerSize; n++) { + SearchResponseListener listener = (SearchResponseListener)deviceSearchResponseListenerList.get(n); + listener.deviceSearchResponseReceived(ssdpPacket); + } + } + + ///////////////////////////////////////////////////////////////////// + // Device status changes (device added or removed) + // Applications that support the DeviceChangeListener interface are + // notified immediately when a device is added to, or removed from, + // the control point. + ///////////////////////////////////////////////////////////////////// + + ListenerList deviceChangeListenerList = new ListenerList(); + + public void addDeviceChangeListener(DeviceChangeListener listener) + { + deviceChangeListenerList.add(listener); + } + + public void removeDeviceChangeListener(DeviceChangeListener listener) + { + deviceChangeListenerList.remove(listener); + } + + public void performAddDeviceListener( Device dev ) + { + int listenerSize = deviceChangeListenerList.size(); + for (int n=0; n<listenerSize; n++) { + DeviceChangeListener listener = (DeviceChangeListener)deviceChangeListenerList.get(n); + listener.deviceAdded( dev ); + } + } + + public void performRemoveDeviceListener( Device dev ) + { + int listenerSize = deviceChangeListenerList.size(); + for (int n=0; n<listenerSize; n++) { + DeviceChangeListener listener = (DeviceChangeListener)deviceChangeListenerList.get(n); + listener.deviceRemoved( dev ); + } + } + + //////////////////////////////////////////////// + // SSDPPacket + //////////////////////////////////////////////// + + public void notifyReceived(SSDPPacket packet) + { + if (packet.isRootDevice() == true) { + if (packet.isAlive() == true) + addDevice(packet); + if (packet.isByeBye() == true) + removeDevice(packet); + } + performNotifyListener(packet); + } + + public void searchResponseReceived(SSDPPacket packet) + { + if (packet.isRootDevice() == true) + addDevice(packet); + performSearchResponseListener(packet); + } + + //////////////////////////////////////////////// + // M-SEARCH + //////////////////////////////////////////////// + + private int searchMx = SSDP.DEFAULT_MSEARCH_MX; + + public int getSearchMx() + { + return searchMx; + } + + public void setSearchMx(int mx) + { + searchMx = mx; + } + + public void search(String target, int mx) + { + SSDPSearchRequest msReq = new SSDPSearchRequest(target, mx); + SSDPSearchResponseSocketList ssdpSearchResponseSocketList = getSSDPSearchResponseSocketList(); + ssdpSearchResponseSocketList.post(msReq); + } + + public void search(String target) + { + search(target, SSDP.DEFAULT_MSEARCH_MX); + } + + public void search() + { + search(ST.ROOT_DEVICE, SSDP.DEFAULT_MSEARCH_MX); + } + + + //////////////////////////////////////////////// + // EventSub HTTPServer + //////////////////////////////////////////////// + + private HTTPServerList httpServerList = new HTTPServerList(); + + private HTTPServerList getHTTPServerList() + { + return httpServerList; + } + + public void httpRequestRecieved(HTTPRequest httpReq) + { + if (Debug.isOn() == true) + httpReq.print(); + + // Thanks for Giordano Sassaroli <sassarol@cefriel.it> (09/08/03) + if (httpReq.isNotifyRequest() == true) { + NotifyRequest notifyReq = new NotifyRequest(httpReq); + String uuid = notifyReq.getSID(); + long seq = notifyReq.getSEQ(); + PropertyList props = notifyReq.getPropertyList(); + int propCnt = props.size(); + for (int n = 0; n < propCnt; n++) { + Property prop = props.getProperty(n); + String varName = prop.getName(); + String varValue = prop.getValue(); + performEventListener(uuid, seq, varName, varValue); + } + httpReq.returnOK(); + return; + } + + httpReq.returnBadRequest(); + } + + //////////////////////////////////////////////// + // Event Listener + //////////////////////////////////////////////// + + private ListenerList eventListenerList = new ListenerList(); + + public void addEventListener(EventListener listener) + { + eventListenerList.add(listener); + } + + public void removeEventListener(EventListener listener) + { + eventListenerList.remove(listener); + } + + public void performEventListener(String uuid, long seq, String name, String value) + { + int listenerSize = eventListenerList.size(); + for (int n=0; n<listenerSize; n++) { + EventListener listener = (EventListener)eventListenerList.get(n); + listener.eventNotifyReceived(uuid, seq, name, value); + } + } + + //////////////////////////////////////////////// + // Subscription + //////////////////////////////////////////////// + + private String eventSubURI = DEFAULT_EVENTSUB_URI; + + public String getEventSubURI() + { + return eventSubURI; + } + + public void setEventSubURI(String url) + { + eventSubURI = url; + } + + private String getEventSubCallbackURL(String host) + { + return HostInterface.getHostURL(host, getHTTPPort(), getEventSubURI()); + } + + public boolean subscribe(Service service, long timeout) + { + if (service.isSubscribed() == true) { + String sid = service.getSID(); + return subscribe(service, sid, timeout); + } + + Device rootDev = service.getRootDevice(); + if (rootDev == null) + return false; + String ifAddress = rootDev.getInterfaceAddress(); + SubscriptionRequest subReq = new SubscriptionRequest(); + subReq.setSubscribeRequest(service, getEventSubCallbackURL(ifAddress), timeout); + SubscriptionResponse subRes = subReq.post(); + if (subRes.isSuccessful() == true) { + service.setSID(subRes.getSID()); + service.setTimeout(subRes.getTimeout()); + return true; + + } + service.clearSID(); + return false; + } + + public boolean subscribe(Service service) + { + return subscribe(service, Subscription.INFINITE_VALUE); + } + + public boolean subscribe(Service service, String uuid, long timeout) + { + SubscriptionRequest subReq = new SubscriptionRequest(); + subReq.setRenewRequest(service, uuid, timeout); + if (Debug.isOn() == true) + subReq.print(); + SubscriptionResponse subRes = subReq.post(); + if (Debug.isOn() == true) + subRes.print(); + if (subRes.isSuccessful() == true) { + service.setSID(subRes.getSID()); + service.setTimeout(subRes.getTimeout()); + return true; + } + service.clearSID(); + return false; + } + + public boolean subscribe(Service service, String uuid) + { + return subscribe(service, uuid, Subscription.INFINITE_VALUE); + } + + public boolean isSubscribed(Service service) + { + if (service == null) + return false; + return service.isSubscribed(); + } + + public boolean unsubscribe(Service service) + { + SubscriptionRequest subReq = new SubscriptionRequest(); + subReq.setUnsubscribeRequest(service); + SubscriptionResponse subRes = subReq.post(); + if (subRes.isSuccessful() == true) { + service.clearSID(); + return true; + } + return false; + } + + public void unsubscribe(Device device) + { + ServiceList serviceList = device.getServiceList(); + int serviceCnt = serviceList.size(); + for (int n=0; n<serviceCnt; n++) { + Service service = serviceList.getService(n); + if (service.hasSID() == true) + unsubscribe(service); + } + + DeviceList childDevList = device.getDeviceList(); + int childDevCnt = childDevList.size(); + for (int n=0; n<childDevCnt; n++) { + Device cdev = childDevList.getDevice(n); + unsubscribe(cdev); + } + } + + public void unsubscribe() + { + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n=0; n<devCnt; n++) { + Device dev = devList.getDevice(n); + unsubscribe(dev); + } + } + + //////////////////////////////////////////////// + // getSubscriberService + //////////////////////////////////////////////// + + public Service getSubscriberService(String uuid) + { + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n=0; n<devCnt; n++) { + Device dev = devList.getDevice(n); + Service service = dev.getSubscriberService(uuid); + if (service != null) + return service; + } + return null; + } + + //////////////////////////////////////////////// + // getSubscriberService + //////////////////////////////////////////////// + + public void renewSubscriberService(Device dev, long timeout) + { + ServiceList serviceList = dev.getServiceList(); + int serviceCnt = serviceList.size(); + for (int n=0; n<serviceCnt; n++) { + Service service = serviceList.getService(n); + if (service.isSubscribed() == false) + continue; + String sid = service.getSID(); + boolean isRenewed = subscribe(service, sid, timeout); + if (isRenewed == false) + subscribe(service, timeout); + } + + DeviceList cdevList = dev.getDeviceList(); + int cdevCnt = cdevList.size(); + for (int n=0; n<cdevCnt; n++) { + Device cdev = cdevList.getDevice(n); + renewSubscriberService(cdev, timeout); + } + } + + public void renewSubscriberService(long timeout) + { + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n=0; n<devCnt; n++) { + Device dev = devList.getDevice(n); + renewSubscriberService(dev, timeout); + } + } + + public void renewSubscriberService() + { + renewSubscriberService(Subscription.INFINITE_VALUE); + } + + //////////////////////////////////////////////// + // Subscriber + //////////////////////////////////////////////// + + private RenewSubscriber renewSubscriber; + + public void setRenewSubscriber(RenewSubscriber sub) + { + renewSubscriber = sub; + } + + public RenewSubscriber getRenewSubscriber() + { + return renewSubscriber; + } + + //////////////////////////////////////////////// + // run + //////////////////////////////////////////////// + + public boolean start(String target, int mx) + { + stop(); + + //////////////////////////////////////// + // HTTP Server + //////////////////////////////////////// + + int retryCnt = 0; + int bindPort = getHTTPPort(); + HTTPServerList httpServerList = getHTTPServerList(); + while (httpServerList.open(bindPort) == false) { + retryCnt++; + if (UPnP.SERVER_RETRY_COUNT < retryCnt) + return false; + setHTTPPort(bindPort + 1); + bindPort = getHTTPPort(); + } + httpServerList.addRequestListener(this); + httpServerList.start(); + + //////////////////////////////////////// + // Notify Socket + //////////////////////////////////////// + + SSDPNotifySocketList ssdpNotifySocketList = getSSDPNotifySocketList(); + if (ssdpNotifySocketList.open() == false) + return false; + ssdpNotifySocketList.setControlPoint(this); + ssdpNotifySocketList.start(); + + //////////////////////////////////////// + // SeachResponse Socket + //////////////////////////////////////// + + int ssdpPort = getSSDPPort(); + retryCnt = 0; + SSDPSearchResponseSocketList ssdpSearchResponseSocketList = getSSDPSearchResponseSocketList(); + while (ssdpSearchResponseSocketList.open(ssdpPort) == false) { + retryCnt++; + if (UPnP.SERVER_RETRY_COUNT < retryCnt) + return false; + setSSDPPort(ssdpPort + 1); + ssdpPort = getSSDPPort(); + } + ssdpSearchResponseSocketList.setControlPoint(this); + ssdpSearchResponseSocketList.start(); + + //////////////////////////////////////// + // search root devices + //////////////////////////////////////// + + search(target, mx); + + //////////////////////////////////////// + // Disposer + //////////////////////////////////////// + + Disposer disposer = new Disposer(this); + setDeviceDisposer(disposer); + disposer.start(); + + //////////////////////////////////////// + // Subscriber + //////////////////////////////////////// + + if (isNMPRMode() == true) { + RenewSubscriber renewSub = new RenewSubscriber(this); + setRenewSubscriber(renewSub); + renewSub.start(); + } + + return true; + } + + public boolean start(String target) + { + return start(target, SSDP.DEFAULT_MSEARCH_MX); + } + + public boolean start() + { + return start(ST.ROOT_DEVICE, SSDP.DEFAULT_MSEARCH_MX); + } + + public boolean stop() + { + unsubscribe(); + + SSDPNotifySocketList ssdpNotifySocketList = getSSDPNotifySocketList(); + ssdpNotifySocketList.stop(); + ssdpNotifySocketList.close(); + ssdpNotifySocketList.clear(); + + SSDPSearchResponseSocketList ssdpSearchResponseSocketList = getSSDPSearchResponseSocketList(); + ssdpSearchResponseSocketList.stop(); + ssdpSearchResponseSocketList.close(); + ssdpSearchResponseSocketList.clear(); + + HTTPServerList httpServerList = getHTTPServerList(); + httpServerList.stop(); + httpServerList.close(); + httpServerList.clear(); + + //////////////////////////////////////// + // Disposer + //////////////////////////////////////// + + Disposer disposer = getDeviceDisposer(); + if (disposer != null) { + disposer.stop(); + setDeviceDisposer(null); + } + + //////////////////////////////////////// + // Subscriber + //////////////////////////////////////// + + RenewSubscriber renewSub = getRenewSubscriber(); + if (renewSub != null) { + renewSub.stop(); + setRenewSubscriber(null); + } + + return true; + } + + //////////////////////////////////////////////// + // print + //////////////////////////////////////////////// + + public void print() + { + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + Debug.message("Device Num = " + devCnt); + for (int n=0; n<devCnt; n++) { + Device dev = devList.getDevice(n); + Debug.message("[" + n + "] " + dev.getFriendlyName() + ", " + dev.getLeaseTime() + ", " + dev.getElapsedTime()); + } + } +} diff --git a/router/java/src/org/cybergarage/upnp/Device.java b/router/java/src/org/cybergarage/upnp/Device.java new file mode 100644 index 0000000000000000000000000000000000000000..9dd9c15f30a9418bfb6a030cd61553b977d8d2d3 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/Device.java @@ -0,0 +1,1809 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: Device.java +* +* Revision: +* +* 11/28/02 +* - first revision. +* 02/26/03 +* - URLBase is updated automatically. +* - Description of a root device is returned from the XML node tree. +* 05/13/03 +* - URLBase is updated when the request is received. +* - Changed to create socket threads each local interfaces. +* (HTTP, SSDPSearch) +* 06/17/03 +* - Added notify all state variables when a new subscription is received. +* 06/18/03 +* - Fixed a announce bug when the bind address is null on J2SE v 1.4.1_02 and Redhat 9. +* 09/02/03 +* - Giordano Sassaroli <sassarol@cefriel.it> +* - 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 <newell@media-rush.com> +* - 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 <kismet-sl@users.sourceforge.net> +* - Added getParentDevice(). +* 10/20/04 +* - Brent Hills <bhills@openshores.com> +* - Changed postSearchResponse() to add MYNAME header. +* 11/19/04 +* - Theo Beisch <theo.beisch@gmx.de> +* - 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 <mhakman@dkab.net> +* Changed announce() and byebye() to close the socket after the posting. +* 04/25/05 +* - Thanks for Mikael Hakman <mhakman@dkab.net> +* Changed deviceSearchResponse() answer with USN:UDN::<device-type> when request ST is device type. +* 04/25/05 +* - Thanks for Mikael Hakman <mhakman@dkab.net> +* - Changed getDescriptionData() to add a XML declaration at first line. +* 04/25/05 +* - Thanks for Mikael Hakman <mhakman@dkab.net> +* - 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(); + //<device><deviceList><device> + 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<nNode; n++) { + Node node = devListNode.getNode(n); + if (Device.isDeviceNode(node) == false) + continue; + Device dev = new Device(node); + devList.add(dev); + } + return devList; + } + + public boolean isDevice(String name) + { + if (name == null) + return false; + if (name.endsWith(getUDN()) == true) + return true; + if (name.equals(getFriendlyName()) == true) + return true; + if (name.endsWith(getDeviceType()) == true) + return true; + return false; + } + + public Device getDevice(String name) + { + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n=0; n<devCnt; n++) { + Device dev = devList.getDevice(n); + if (dev.isDevice(name) == true) + return dev; + Device cdev = dev.getDevice(name); + if (cdev != null) + return cdev; + } + return null; + } + + public Device getDeviceByDescriptionURI(String uri) + { + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n=0; n<devCnt; n++) { + Device dev = devList.getDevice(n); + if (dev.isDescriptionURI(uri) == true) + return dev; + Device cdev = dev.getDeviceByDescriptionURI(uri); + if (cdev != null) + return cdev; + } + return null; + } + + //////////////////////////////////////////////// + // serviceList + //////////////////////////////////////////////// + + public ServiceList getServiceList() + { + ServiceList serviceList = new ServiceList(); + Node serviceListNode = getDeviceNode().getNode(ServiceList.ELEM_NAME); + if (serviceListNode == null) + return serviceList; + int nNode = serviceListNode.getNNodes(); + for (int n=0; n<nNode; n++) { + Node node = serviceListNode.getNode(n); + if (Service.isServiceNode(node) == false) + continue; + Service service = new Service(node); + serviceList.add(service); + } + return serviceList; + } + + public Service getService(String name) + { + ServiceList serviceList = getServiceList(); + int serviceCnt = serviceList.size(); + for (int n=0; n<serviceCnt; n++) { + Service service = serviceList.getService(n); + if (service.isService(name) == true) + return service; + } + + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n=0; n<devCnt; n++) { + Device dev = devList.getDevice(n); + Service service = dev.getService(name); + if (service != null) + return service; + } + + return null; + } + + public Service getServiceBySCPDURL(String searchUrl) + { + ServiceList serviceList = getServiceList(); + int serviceCnt = serviceList.size(); + for (int n=0; n<serviceCnt; n++) { + Service service = serviceList.getService(n); + if (service.isSCPDURL(searchUrl) == true) + return service; + } + + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n=0; n<devCnt; n++) { + Device dev = devList.getDevice(n); + Service service = dev.getServiceBySCPDURL(searchUrl); + if (service != null) + return service; + } + + return null; + } + + public Service getServiceByControlURL(String searchUrl) + { + ServiceList serviceList = getServiceList(); + int serviceCnt = serviceList.size(); + for (int n=0; n<serviceCnt; n++) { + Service service = serviceList.getService(n); + if (service.isControlURL(searchUrl) == true) + return service; + } + + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n=0; n<devCnt; n++) { + Device dev = devList.getDevice(n); + Service service = dev.getServiceByControlURL(searchUrl); + if (service != null) + return service; + } + + return null; + } + + public Service getServiceByEventSubURL(String searchUrl) + { + ServiceList serviceList = getServiceList(); + int serviceCnt = serviceList.size(); + for (int n=0; n<serviceCnt; n++) { + Service service = serviceList.getService(n); + if (service.isEventSubURL(searchUrl) == true) + return service; + } + + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n=0; n<devCnt; n++) { + Device dev = devList.getDevice(n); + Service service = dev.getServiceByEventSubURL(searchUrl); + if (service != null) + return service; + } + + return null; + } + + public Service getSubscriberService(String uuid) + { + ServiceList serviceList = getServiceList(); + int serviceCnt = serviceList.size(); + for (int n=0; n<serviceCnt; n++) { + Service service = serviceList.getService(n); + String sid = service.getSID(); + if (uuid.equals(sid) == true) + return service; + } + + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n=0; n<devCnt; n++) { + Device dev = devList.getDevice(n); + Service service = dev.getSubscriberService(uuid); + if (service != null) + return service; + } + + return null; + } + + //////////////////////////////////////////////// + // StateVariable + //////////////////////////////////////////////// + + public StateVariable getStateVariable(String serviceType, String name) + { + if (serviceType == null && name == null) + return null; + + ServiceList serviceList = getServiceList(); + int serviceCnt = serviceList.size(); + for (int n=0; n<serviceCnt; n++) { + Service service = serviceList.getService(n); + // Thanks for Theo Beisch (11/09/04) + if (serviceType != null) { + if (service.getServiceType().equals(serviceType) == false) + continue; + } + StateVariable stateVar = service.getStateVariable(name); + if (stateVar != null) + return stateVar; + } + + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n=0; n<devCnt; n++) { + Device dev = devList.getDevice(n); + StateVariable stateVar = dev.getStateVariable(serviceType, name); + if (stateVar != null) + return stateVar; + } + + return null; + } + + public StateVariable getStateVariable(String name) + { + return getStateVariable(null, name); + } + + //////////////////////////////////////////////// + // Action + //////////////////////////////////////////////// + + public Action getAction(String name) + { + ServiceList serviceList = getServiceList(); + int serviceCnt = serviceList.size(); + for (int n=0; n<serviceCnt; n++) { + Service service = serviceList.getService(n); + ActionList actionList = service.getActionList(); + int actionCnt = actionList.size(); + for (int i=0; i<actionCnt; i++) { + Action action = (Action)actionList.getAction(i); + String actionName = action.getName(); + if (actionName == null) + continue; + if (actionName.equals(name) == true) + return action; + } + } + + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n=0; n<devCnt; n++) { + Device dev = devList.getDevice(n); + Action action = dev.getAction(name); + if (action != null) + return action; + } + + return null; + } + + //////////////////////////////////////////////// + // iconList + //////////////////////////////////////////////// + + public IconList getIconList() + { + IconList iconList = new IconList(); + Node iconListNode = getDeviceNode().getNode(IconList.ELEM_NAME); + if (iconListNode == null) + return iconList; + int nNode = iconListNode.getNNodes(); + for (int n=0; n<nNode; n++) { + Node node = iconListNode.getNode(n); + if (Icon.isIconNode(node) == false) + continue; + Icon icon = new Icon(node); + iconList.add(icon); + } + return iconList; + } + + public Icon getIcon(int n) + { + IconList iconList = getIconList(); + if (n < 0 && (iconList.size()-1) < n) + return null; + return iconList.getIcon(n); + } + + //////////////////////////////////////////////// + // Notify + //////////////////////////////////////////////// + + public String getLocationURL(String host) + { + return HostInterface.getHostURL(host, getHTTPPort(), getDescriptionURI()); + } + + private String getNotifyDeviceNT() + { + if (isRootDevice() == false) + return getUDN(); + return UPNP_ROOTDEVICE; + } + + private String getNotifyDeviceUSN() + { + if (isRootDevice() == false) + return getUDN(); + return getUDN() + "::" + UPNP_ROOTDEVICE; + } + + private String getNotifyDeviceTypeNT() + { + return getDeviceType(); + } + + private String getNotifyDeviceTypeUSN() + { + return getUDN() + "::" + getDeviceType(); + } + + public final static void notifyWait() + { + TimerUtil.waitRandom(DEFAULT_DISCOVERY_WAIT_TIME); + } + + public void announce(String bindAddr) + { + String devLocation = getLocationURL(bindAddr); + + SSDPNotifySocket ssdpSock = new SSDPNotifySocket(bindAddr); + + SSDPNotifyRequest ssdpReq = new SSDPNotifyRequest(); + ssdpReq.setServer(UPnP.getServerName()); + ssdpReq.setLeaseTime(getLeaseTime()); + ssdpReq.setLocation(devLocation); + ssdpReq.setNTS(NTS.ALIVE); + + // uuid:device-UUID(::upnp:rootdevice)* + if (isRootDevice() == true) { + String devNT = getNotifyDeviceNT(); + String devUSN = getNotifyDeviceUSN(); + ssdpReq.setNT(devNT); + ssdpReq.setUSN(devUSN); + ssdpSock.post(ssdpReq); + } + + // uuid:device-UUID::urn:schemas-upnp-org:device:deviceType:v + String devNT = getNotifyDeviceTypeNT(); + String devUSN = getNotifyDeviceTypeUSN(); + ssdpReq.setNT(devNT); + ssdpReq.setUSN(devUSN); + ssdpSock.post(ssdpReq); + + // Thanks for Mikael Hakman (04/25/05) + ssdpSock.close(); + + ServiceList serviceList = getServiceList(); + int serviceCnt = serviceList.size(); + for (int n=0; n<serviceCnt; n++) { + Service service = serviceList.getService(n); + service.announce(bindAddr); + } + + DeviceList childDeviceList = getDeviceList(); + int childDeviceCnt = childDeviceList.size(); + for (int n=0; n<childDeviceCnt; n++) { + Device childDevice = childDeviceList.getDevice(n); + childDevice.announce(bindAddr); + } + } + + public void announce() + { + notifyWait(); + + int nHostAddrs = HostInterface.getNHostAddresses(); + for (int n=0; n<nHostAddrs; n++) { + String bindAddr = HostInterface.getHostAddress(n); + if (bindAddr == null || bindAddr.length() <= 0) + continue; + int ssdpCount = getSSDPAnnounceCount(); + for (int i=0; i<ssdpCount; i++) + announce(bindAddr); + } + } + + public void byebye(String bindAddr) + { + SSDPNotifySocket ssdpSock = new SSDPNotifySocket(bindAddr); + + SSDPNotifyRequest ssdpReq = new SSDPNotifyRequest(); + ssdpReq.setNTS(NTS.BYEBYE); + + // uuid:device-UUID(::upnp:rootdevice)* + if (isRootDevice() == true) { + String devNT = getNotifyDeviceNT(); + String devUSN = getNotifyDeviceUSN(); + ssdpReq.setNT(devNT); + ssdpReq.setUSN(devUSN); + ssdpSock.post(ssdpReq); + } + + // uuid:device-UUID::urn:schemas-upnp-org:device:deviceType:v + String devNT = getNotifyDeviceTypeNT(); + String devUSN = getNotifyDeviceTypeUSN(); + ssdpReq.setNT(devNT); + ssdpReq.setUSN(devUSN); + ssdpSock.post(ssdpReq); + + // Thanks for Mikael Hakman (04/25/05) + ssdpSock.close(); + + ServiceList serviceList = getServiceList(); + int serviceCnt = serviceList.size(); + for (int n=0; n<serviceCnt; n++) { + Service service = serviceList.getService(n); + service.byebye(bindAddr); + } + + DeviceList childDeviceList = getDeviceList(); + int childDeviceCnt = childDeviceList.size(); + for (int n=0; n<childDeviceCnt; n++) { + Device childDevice = childDeviceList.getDevice(n); + childDevice.byebye(bindAddr); + } + } + + public void byebye() + { + int nHostAddrs = HostInterface.getNHostAddresses(); + for (int n=0; n<nHostAddrs; n++) { + String bindAddr = HostInterface.getHostAddress(n); + if (bindAddr == null || bindAddr.length() <= 0) + continue; + int ssdpCount = getSSDPAnnounceCount(); + for (int i=0; i<ssdpCount; i++) + byebye(bindAddr); + } + } + + //////////////////////////////////////////////// + // Search + //////////////////////////////////////////////// + + public boolean postSearchResponse(SSDPPacket ssdpPacket, String st, String usn) + { + String localAddr = ssdpPacket.getLocalAddress(); + Device rootDev = getRootDevice(); + String rootDevLocation = rootDev.getLocationURL(localAddr); + + SSDPSearchResponse ssdpRes = new SSDPSearchResponse(); + ssdpRes.setLeaseTime(getLeaseTime()); + ssdpRes.setDate(Calendar.getInstance()); + ssdpRes.setST(st); + ssdpRes.setUSN(usn); + ssdpRes.setLocation(rootDevLocation); + // Thanks for Brent Hills (10/20/04) + ssdpRes.setMYNAME(getFriendlyName()); + + int mx = ssdpPacket.getMX(); + TimerUtil.waitRandom(mx * 1000); + + String remoteAddr = ssdpPacket.getRemoteAddress(); + int remotePort = ssdpPacket.getRemotePort(); + SSDPSearchResponseSocket ssdpResSock = new SSDPSearchResponseSocket(); + if (Debug.isOn() == true) + ssdpRes.print(); + int ssdpCount = getSSDPAnnounceCount(); + for (int i=0; i<ssdpCount; i++) + ssdpResSock.post(remoteAddr, remotePort, ssdpRes); + + return true; + } + + public void deviceSearchResponse(SSDPPacket ssdpPacket) + { + String ssdpST = ssdpPacket.getST(); + + if (ssdpST == null) + return; + + boolean isRootDevice = isRootDevice(); + + String devUSN = getUDN(); + if (isRootDevice == true) + devUSN += "::" + USN.ROOTDEVICE; + + if (ST.isAllDevice(ssdpST) == true) { + String devNT = getNotifyDeviceNT(); + int repeatCnt = (isRootDevice == true) ? 3 : 2; + for (int n=0; n<repeatCnt; n++) + postSearchResponse(ssdpPacket, devNT, devUSN); + } + else if (ST.isRootDevice(ssdpST) == true) { + if (isRootDevice == true) + postSearchResponse(ssdpPacket, ST.ROOT_DEVICE, devUSN); + } + else if (ST.isUUIDDevice(ssdpST) == true) { + String devUDN = getUDN(); + if (ssdpST.equals(devUDN) == true) + postSearchResponse(ssdpPacket, devUDN, devUSN); + } + else if (ST.isURNDevice(ssdpST) == true) { + String devType= getDeviceType(); + if (ssdpST.equals(devType) == true) { + // Thanks for Mikael Hakman (04/25/05) + devUSN = getUDN() + "::" + devType; + postSearchResponse(ssdpPacket, devType, devUSN); + } + } + + ServiceList serviceList = getServiceList(); + int serviceCnt = serviceList.size(); + for (int n=0; n<serviceCnt; n++) { + Service service = serviceList.getService(n); + service.serviceSearchResponse(ssdpPacket); + } + + DeviceList childDeviceList = getDeviceList(); + int childDeviceCnt = childDeviceList.size(); + for (int n=0; n<childDeviceCnt; n++) { + Device childDevice = childDeviceList.getDevice(n); + childDevice.deviceSearchResponse(ssdpPacket); + } + } + + public void deviceSearchReceived(SSDPPacket ssdpPacket) + { + deviceSearchResponse(ssdpPacket); + } + + //////////////////////////////////////////////// + // HTTP Server + //////////////////////////////////////////////// + + public void setHTTPPort(int port) + { + getDeviceData().setHTTPPort(port); + } + + public int getHTTPPort() + { + return getDeviceData().getHTTPPort(); + } + + public void httpRequestRecieved(HTTPRequest httpReq) + { + if (Debug.isOn() == true) + httpReq.print(); + + if (httpReq.isGetRequest() == true) { + httpGetRequestRecieved(httpReq); + return; + } + if (httpReq.isPostRequest() == true) { + httpPostRequestRecieved(httpReq); + return; + } + + if (httpReq.isSubscribeRequest() == true || httpReq.isUnsubscribeRequest() == true) { + SubscriptionRequest subReq = new SubscriptionRequest(httpReq); + deviceEventSubscriptionRecieved(subReq); + return; + } + + httpReq.returnBadRequest(); + } + + private synchronized byte[] getDescriptionData(String host) + { + if (isNMPRMode() == false) + updateURLBase(host); + Node rootNode = getRootNode(); + if (rootNode == null) + return new byte[0]; + // Thanks for Mikael Hakman (04/25/05) + String desc = new String(); + desc += UPnP.XML_DECLARATION; + desc += "\n"; + desc += rootNode.toString(); + return desc.getBytes(); + } + + private void httpGetRequestRecieved(HTTPRequest httpReq) + { + String uri = httpReq.getURI(); + Debug.message("httpGetRequestRecieved = " + uri); + if (uri == null) { + httpReq.returnBadRequest(); + return; + } + + Device embDev; + Service embService; + + byte fileByte[] = new byte[0]; + if (isDescriptionURI(uri) == true) { + String localAddr = httpReq.getLocalAddress(); + fileByte = getDescriptionData(localAddr); + } + else if ((embDev = getDeviceByDescriptionURI(uri)) != null) { + String localAddr = httpReq.getLocalAddress(); + fileByte = embDev.getDescriptionData(localAddr); + } + else if ((embService = getServiceBySCPDURL(uri)) != null) { + fileByte = embService.getSCPDData(); + } + else { + httpReq.returnBadRequest(); + return; + } + + HTTPResponse httpRes = new HTTPResponse(); + if (FileUtil.isXMLFileName(uri) == true) + httpRes.setContentType(XML.CONTENT_TYPE); + httpRes.setStatusCode(HTTPStatus.OK); + httpRes.setContent(fileByte); + + httpReq.post(httpRes); + } + + private void httpPostRequestRecieved(HTTPRequest httpReq) + { + if (httpReq.isSOAPAction() == true) { + //SOAPRequest soapReq = new SOAPRequest(httpReq); + soapActionRecieved(httpReq); + return; + } + httpReq.returnBadRequest(); + } + + //////////////////////////////////////////////// + // SOAP + //////////////////////////////////////////////// + + private void soapBadActionRecieved(HTTPRequest soapReq) + { + SOAPResponse soapRes = new SOAPResponse(); + soapRes.setStatusCode(HTTPStatus.BAD_REQUEST); + soapReq.post(soapRes); + } + + private void soapActionRecieved(HTTPRequest soapReq) + { + String uri = soapReq.getURI(); + Service ctlService = getServiceByControlURL(uri); + if (ctlService != null) { + ActionRequest crlReq = new ActionRequest(soapReq); + deviceControlRequestRecieved(crlReq, ctlService); + return; + } + soapBadActionRecieved(soapReq); + } + + //////////////////////////////////////////////// + // controlAction + //////////////////////////////////////////////// + + private void deviceControlRequestRecieved(ControlRequest ctlReq, Service service) + { + if (ctlReq.isQueryControl() == true) + deviceQueryControlRecieved(new QueryRequest(ctlReq), service); + else + deviceActionControlRecieved(new ActionRequest(ctlReq), service); + } + + private void invalidActionControlRecieved(ControlRequest ctlReq) + { + ControlResponse actRes = new ActionResponse(); + actRes.setFaultResponse(UPnPStatus.INVALID_ACTION); + ctlReq.post(actRes); + } + + private void deviceActionControlRecieved(ActionRequest ctlReq, Service service) + { + if (Debug.isOn() == true) + ctlReq.print(); + + String actionName = ctlReq.getActionName(); + Action action = service.getAction(actionName); + if (action == null) { + invalidActionControlRecieved(ctlReq); + return; + } + ArgumentList actionArgList = action.getArgumentList(); + ArgumentList reqArgList = ctlReq.getArgumentList(); + actionArgList.set(reqArgList); + if (action.performActionListener(ctlReq) == false) + invalidActionControlRecieved(ctlReq); + } + + private void deviceQueryControlRecieved(QueryRequest ctlReq, Service service) + { + if (Debug.isOn() == true) + ctlReq.print(); + String varName = ctlReq.getVarName(); + if (service.hasStateVariable(varName) == false) { + invalidActionControlRecieved(ctlReq); + return; + } + StateVariable stateVar = getStateVariable(varName); + if (stateVar.performQueryListener(ctlReq) == false) + invalidActionControlRecieved(ctlReq); + } + + //////////////////////////////////////////////// + // eventSubscribe + //////////////////////////////////////////////// + + private void upnpBadSubscriptionRecieved(SubscriptionRequest subReq, int code) + { + SubscriptionResponse subRes = new SubscriptionResponse(); + subRes.setErrorResponse(code); + subReq.post(subRes); + } + + private void deviceEventSubscriptionRecieved(SubscriptionRequest subReq) + { + String uri = subReq.getURI(); + Service service = getServiceByEventSubURL(uri); + if (service == null) { + subReq.returnBadRequest(); + return; + } + if (subReq.hasCallback() == false && subReq.hasSID() == false) { + upnpBadSubscriptionRecieved(subReq, HTTPStatus.PRECONDITION_FAILED); + return; + } + + // UNSUBSCRIBE + if (subReq.isUnsubscribeRequest() == true) { + deviceEventUnsubscriptionRecieved(service, subReq); + return; + } + + // SUBSCRIBE (NEW) + if (subReq.hasCallback() == true) { + deviceEventNewSubscriptionRecieved(service, subReq); + return; + } + + // SUBSCRIBE (RENEW) + if (subReq.hasSID() == true) { + deviceEventRenewSubscriptionRecieved(service, subReq); + return; + } + + upnpBadSubscriptionRecieved(subReq, HTTPStatus.PRECONDITION_FAILED); + } + + private void deviceEventNewSubscriptionRecieved(Service service, SubscriptionRequest subReq) + { + String callback = subReq.getCallback(); + try { + new URL(callback); + } + catch (Exception e) { + upnpBadSubscriptionRecieved(subReq, HTTPStatus.PRECONDITION_FAILED); + return; + } + + long timeOut = subReq.getTimeout(); + String sid = Subscription.createSID(); + + Subscriber sub = new Subscriber(); + sub.setDeliveryURL(callback); + sub.setTimeOut(timeOut); + sub.setSID(sid); + service.addSubscriber(sub); + + SubscriptionResponse subRes = new SubscriptionResponse(); + subRes.setStatusCode(HTTPStatus.OK); + subRes.setSID(sid); + subRes.setTimeout(timeOut); + if (Debug.isOn() == true) + subRes.print(); + subReq.post(subRes); + + if (Debug.isOn() == true) + subRes.print(); + + service.notifyAllStateVariables(); + } + + private void deviceEventRenewSubscriptionRecieved(Service service, SubscriptionRequest subReq) + { + String sid = subReq.getSID(); + Subscriber sub = service.getSubscriber(sid); + + if (sub == null) { + upnpBadSubscriptionRecieved(subReq, HTTPStatus.PRECONDITION_FAILED); + return; + } + + long timeOut = subReq.getTimeout(); + sub.setTimeOut(timeOut); + sub.renew(); + + SubscriptionResponse subRes = new SubscriptionResponse(); + subRes.setStatusCode(HTTPStatus.OK); + subRes.setSID(sid); + subRes.setTimeout(timeOut); + subReq.post(subRes); + + if (Debug.isOn() == true) + subRes.print(); + } + + private void deviceEventUnsubscriptionRecieved(Service service, SubscriptionRequest subReq) + { + String sid = subReq.getSID(); + Subscriber sub = service.getSubscriber(sid); + + if (sub == null) { + upnpBadSubscriptionRecieved(subReq, HTTPStatus.PRECONDITION_FAILED); + return; + } + + service.removeSubscriber(sub); + + SubscriptionResponse subRes = new SubscriptionResponse(); + subRes.setStatusCode(HTTPStatus.OK); + subReq.post(subRes); + + if (Debug.isOn() == true) + subRes.print(); + } + + //////////////////////////////////////////////// + // Thread + //////////////////////////////////////////////// + + private HTTPServerList getHTTPServerList() + { + return getDeviceData().getHTTPServerList(); + } + + private SSDPSearchSocketList getSSDPSearchSocketList() + { + return getDeviceData().getSSDPSearchSocketList(); + } + + private void setAdvertiser(Advertiser adv) + { + getDeviceData().setAdvertiser(adv); + } + + private Advertiser getAdvertiser() + { + return getDeviceData().getAdvertiser(); + } + + public boolean start() + { + stop(true); + + //////////////////////////////////////// + // HTTP Server + //////////////////////////////////////// + + int retryCnt = 0; + int bindPort = getHTTPPort(); + HTTPServerList httpServerList = getHTTPServerList(); + while (httpServerList.open(bindPort) == false) { + retryCnt++; + if (UPnP.SERVER_RETRY_COUNT < retryCnt) + return false; + setHTTPPort(bindPort + 1); + bindPort = getHTTPPort(); + } + httpServerList.addRequestListener(this); + httpServerList.start(); + + //////////////////////////////////////// + // SSDP Seach Socket + //////////////////////////////////////// + + SSDPSearchSocketList ssdpSearchSockList = getSSDPSearchSocketList(); + if (ssdpSearchSockList.open() == false) + return false; + ssdpSearchSockList.addSearchListener(this); + ssdpSearchSockList.start(); + + //////////////////////////////////////// + // Announce + //////////////////////////////////////// + + announce(); + + //////////////////////////////////////// + // Advertiser + //////////////////////////////////////// + + Advertiser adv = new Advertiser(this); + setAdvertiser(adv); + adv.start(); + + return true; + } + + private boolean stop(boolean doByeBye) + { + if (doByeBye == true) + byebye(); + + HTTPServerList httpServerList = getHTTPServerList(); + httpServerList.stop(); + httpServerList.close(); + httpServerList.clear(); + + SSDPSearchSocketList ssdpSearchSockList = getSSDPSearchSocketList(); + ssdpSearchSockList.stop(); + ssdpSearchSockList.close(); + ssdpSearchSockList.clear(); + + Advertiser adv = getAdvertiser(); + if (adv != null) { + adv.stop(); + setAdvertiser(null); + } + + return true; + } + + public boolean stop() + { + return stop(true); + } + + //////////////////////////////////////////////// + // Interface Address + //////////////////////////////////////////////// + + public String getInterfaceAddress() + { + SSDPPacket ssdpPacket = getSSDPPacket(); + if (ssdpPacket == null) + return ""; + return ssdpPacket.getLocalAddress(); + } + + //////////////////////////////////////////////// + // Acion/QueryListener + //////////////////////////////////////////////// + + public void setActionListener(ActionListener listener) + { + ServiceList serviceList = getServiceList(); + int nServices = serviceList.size(); + for (int n=0; n<nServices; n++) { + Service service = serviceList.getService(n); + service.setActionListener(listener); + } + } + + public void setQueryListener(QueryListener listener) + { + ServiceList serviceList = getServiceList(); + int nServices = serviceList.size(); + for (int n=0; n<nServices; n++) { + Service service = serviceList.getService(n); + service.setQueryListener(listener); + } + } + + //////////////////////////////////////////////// + // Acion/QueryListener (includeSubDevices) + //////////////////////////////////////////////// + + // Thanks for Mikael Hakman (04/25/05) + public void setActionListener(ActionListener listener, boolean includeSubDevices) + { + setActionListener(listener); + if (includeSubDevices == true) { + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n = 0; n < devCnt; n++) { + Device dev = devList.getDevice(n); + dev.setActionListener(listener, true); + } + } + } + + // Thanks for Mikael Hakman (04/25/05) + public void setQueryListener(QueryListener listener, boolean includeSubDevices) + { + setQueryListener(listener); + if (includeSubDevices == true) { + DeviceList devList = getDeviceList(); + int devCnt = devList.size(); + for (int n = 0; n < devCnt; n++) { + Device dev = devList.getDevice(n); + dev.setQueryListener(listener, true); + } + } + } + + //////////////////////////////////////////////// + // output + //////////////////////////////////////////////// + +/* + public void output(PrintWriter ps) + { + ps.println("deviceType = " + getDeviceType()); + ps.println("freindlyName = " + getFriendlyName()); + ps.println("presentationURL = " + getPresentationURL()); + + DeviceList devList = getDeviceList(); + ps.println("devList = " + devList.size()); + + ServiceList serviceList = getServiceList(); + ps.println("serviceList = " + serviceList.size()); + + IconList iconList = getIconList(); + ps.println("iconList = " + iconList.size()); + } + + public void print() + { + PrintWriter pr = new PrintWriter(System.out); + output(pr); + pr.flush(); + } +*/ + +} + diff --git a/router/java/src/org/cybergarage/upnp/DeviceList.java b/router/java/src/org/cybergarage/upnp/DeviceList.java new file mode 100644 index 0000000000000000000000000000000000000000..729b2245f45f1657fbd3be48a27c2ae1e92d44a2 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/DeviceList.java @@ -0,0 +1,46 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: DeviceList.java +* +* Revision; +* +* 12/04/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import java.util.*; + +public class DeviceList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + private static final long serialVersionUID = 3773784061607435126L; + public final static String ELEM_NAME = "deviceList"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public DeviceList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public Device getDevice(int n) + { + return (Device)get(n); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/Icon.java b/router/java/src/org/cybergarage/upnp/Icon.java new file mode 100644 index 0000000000000000000000000000000000000000..7566b3117412290b3fbeb635fd11e97f6d0a7c10 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/Icon.java @@ -0,0 +1,136 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: Icon.java +* +* Revision; +* +* 11/28/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import org.cybergarage.xml.*; + +public class Icon +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "icon"; + + //////////////////////////////////////////////// + // Member + //////////////////////////////////////////////// + + private Node iconNode; + + public Node getIconNode() + { + return iconNode; + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public Icon(Node node) + { + iconNode = node; + } + + //////////////////////////////////////////////// + // isIconNode + //////////////////////////////////////////////// + + public static boolean isIconNode(Node node) + { + return Icon.ELEM_NAME.equals(node.getName()); + } + + //////////////////////////////////////////////// + // mimeType + //////////////////////////////////////////////// + + private final static String MIME_TYPE = "mimeType"; + + public void setMimeType(String value) + { + getIconNode().setNode(MIME_TYPE, value); + } + + public String getMimeType() + { + return getIconNode().getNodeValue(MIME_TYPE); + } + + //////////////////////////////////////////////// + // width + //////////////////////////////////////////////// + + private final static String WIDTH = "width"; + + public void setWidth(String value) + { + getIconNode().setNode(WIDTH, value); + } + + public String getWidth() + { + return getIconNode().getNodeValue(WIDTH); + } + + //////////////////////////////////////////////// + // height + //////////////////////////////////////////////// + + private final static String HEIGHT = "height"; + + public void setHeight(String value) + { + getIconNode().setNode(HEIGHT, value); + } + + public String getHeight() + { + return getIconNode().getNodeValue(HEIGHT); + } + + //////////////////////////////////////////////// + // depth + //////////////////////////////////////////////// + + private final static String DEPTH = "depth"; + + public void setDepth(String value) + { + getIconNode().setNode(DEPTH, value); + } + + public String getDepth() + { + return getIconNode().getNodeValue(DEPTH); + } + + //////////////////////////////////////////////// + // URL + //////////////////////////////////////////////// + + private final static String URL = "url"; + + public void setURL(String value) + { + getIconNode().setNode(URL, value); + } + + public String getURL() + { + return getIconNode().getNodeValue(URL); + } +} diff --git a/router/java/src/org/cybergarage/upnp/IconList.java b/router/java/src/org/cybergarage/upnp/IconList.java new file mode 100644 index 0000000000000000000000000000000000000000..2e60de0fc83feef54dccc4a6d266941a0f66522d --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/IconList.java @@ -0,0 +1,46 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: IconList.java +* +* Revision; +* +* 12/04/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import java.util.*; + +public class IconList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + private static final long serialVersionUID = -1097238335037012991L; + public final static String ELEM_NAME = "iconList"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public IconList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public Icon getIcon(int n) + { + return (Icon)get(n); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/Service.java b/router/java/src/org/cybergarage/upnp/Service.java new file mode 100644 index 0000000000000000000000000000000000000000..2febe00df46e7528bfe904138015ec9f06875def --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/Service.java @@ -0,0 +1,747 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: Service.java +* +* Revision; +* +* 11/28/02 +* - first revision. +* 04/12/02 +* - Holmes, Arran C <acholm@essex.ac.uk> +* - Fixed SERVICE_ID constant instead of "serviceId". +* 06/17/03 +* - Added notifyAllStateVariables(). +* 09/03/03 +* - Giordano Sassaroli <sassarol@cefriel.it> +* - 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 <mhakman@dkab.net> +* - 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 <sassarol@cefriel.it> (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<nNode; n++) { + Node node = actionListNode.getNode(n); + if (Action.isActionNode(node) == false) + continue; + Action action = new Action(serviceNode, node); + actionList.add(action); + } + return actionList; + } + + public Action getAction(String actionName) + { + ActionList actionList = getActionList(); + int nActions = actionList.size(); + for (int n=0; n<nActions; n++) { + Action action = actionList.getAction(n); + String name = action.getName(); + if (name == null) + continue; + if (name.equals(actionName) == true) + return action; + } + return null; + } + + //////////////////////////////////////////////// + // serviceStateTable + //////////////////////////////////////////////// + + public ServiceStateTable getServiceStateTable() + { + ServiceStateTable stateTable = new ServiceStateTable(); + Node stateTableNode = getSCPDNode().getNode(ServiceStateTable.ELEM_NAME); + if (stateTableNode == null) + return stateTable; + Node serviceNode = getServiceNode(); + int nNode = stateTableNode.getNNodes(); + for (int n=0; n<nNode; n++) { + Node node = stateTableNode.getNode(n); + if (StateVariable.isStateVariableNode(node) == false) + continue; + StateVariable serviceVar = new StateVariable(serviceNode, node); + stateTable.add(serviceVar); + } + return stateTable; + } + + public StateVariable getStateVariable(String name) + { + ServiceStateTable stateTable = getServiceStateTable(); + int tableSize = stateTable.size(); + for (int n=0; n<tableSize; n++) { + StateVariable var = stateTable.getStateVariable(n); + String varName = var.getName(); + if (varName == null) + continue; + if (varName.equals(name) == true) + return var; + } + return null; + } + + public boolean hasStateVariable(String name) + { + return (getStateVariable(name) != null ) ? true : false; + } + + //////////////////////////////////////////////// + // UserData + //////////////////////////////////////////////// + + public boolean isService(String name) + { + if (name == null) + return false; + if (name.endsWith(getServiceType()) == true) + return true; + if (name.endsWith(getServiceID()) == true) + return true; + return false; + } + + //////////////////////////////////////////////// + // UserData + //////////////////////////////////////////////// + + private ServiceData getServiceData() + { + Node node = getServiceNode(); + ServiceData userData = (ServiceData)node.getUserData(); + if (userData == null) { + userData = new ServiceData(); + node.setUserData(userData); + userData.setNode(node); + } + return userData; + } + + //////////////////////////////////////////////// + // Notify + //////////////////////////////////////////////// + + private String getNotifyServiceTypeNT() + { + return getServiceType(); + } + + private String getNotifyServiceTypeUSN() + { + return getDevice().getUDN() + "::" + getServiceType(); + } + + public void announce(String bindAddr) + { + // uuid:device-UUID::urn:schemas-upnp-org:service:serviceType:v + Device rootDev = getRootDevice(); + String devLocation = rootDev.getLocationURL(bindAddr); + String serviceNT = getNotifyServiceTypeNT(); + String serviceUSN = getNotifyServiceTypeUSN(); + + Device dev = getDevice(); + + SSDPNotifyRequest ssdpReq = new SSDPNotifyRequest(); + ssdpReq.setServer(UPnP.getServerName()); + ssdpReq.setLeaseTime(dev.getLeaseTime()); + ssdpReq.setLocation(devLocation); + ssdpReq.setNTS(NTS.ALIVE); + ssdpReq.setNT(serviceNT); + ssdpReq.setUSN(serviceUSN); + + SSDPNotifySocket ssdpSock = new SSDPNotifySocket(bindAddr); + Device.notifyWait(); + ssdpSock.post(ssdpReq); + } + + public void byebye(String bindAddr) + { + // uuid:device-UUID::urn:schemas-upnp-org:service:serviceType:v + + String devNT = getNotifyServiceTypeNT(); + String devUSN = getNotifyServiceTypeUSN(); + + SSDPNotifyRequest ssdpReq = new SSDPNotifyRequest(); + ssdpReq.setNTS(NTS.BYEBYE); + ssdpReq.setNT(devNT); + ssdpReq.setUSN(devUSN); + + SSDPNotifySocket ssdpSock = new SSDPNotifySocket(bindAddr); + Device.notifyWait(); + ssdpSock.post(ssdpReq); + } + + public boolean serviceSearchResponse(SSDPPacket ssdpPacket) + { + String ssdpST = ssdpPacket.getST(); + + if (ssdpST == null) + return false; + + Device dev = getDevice(); + + String serviceNT = getNotifyServiceTypeNT(); + String serviceUSN = getNotifyServiceTypeUSN(); + + if (ST.isAllDevice(ssdpST) == true) { + dev.postSearchResponse(ssdpPacket, serviceNT, serviceUSN); + } + else if (ST.isURNService(ssdpST) == true) { + String serviceType = getServiceType(); + if (ssdpST.equals(serviceType) == true) + dev.postSearchResponse(ssdpPacket, serviceType, serviceUSN); + } + + return true; + } + + //////////////////////////////////////////////// + // QueryListener + //////////////////////////////////////////////// + + public void setQueryListener(QueryListener queryListener) + { + ServiceStateTable stateTable = getServiceStateTable(); + int tableSize = stateTable.size(); + for (int n=0; n<tableSize; n++) { + StateVariable var = stateTable.getStateVariable(n); + var.setQueryListener(queryListener); + } + } + + //////////////////////////////////////////////// + // Subscription + //////////////////////////////////////////////// + + public SubscriberList getSubscriberList() + { + return getServiceData().getSubscriberList(); + } + + public void addSubscriber(Subscriber sub) + { + getSubscriberList().add(sub); + } + + public void removeSubscriber(Subscriber sub) + { + getSubscriberList().remove(sub); + } + + public Subscriber getSubscriber(String name) + { + SubscriberList subList = getSubscriberList(); + int subListCnt = subList.size(); + for (int n=0; n<subListCnt; n++) { + Subscriber sub = subList.getSubscriber(n); + if (sub == null) + continue; + String sid = sub.getSID(); + if (sid == null) + continue; + if (sid.equals(name) == true) + return sub; + } + return null; + } + + private boolean notify(Subscriber sub, StateVariable stateVar) + { + String varName = stateVar.getName(); + String value = stateVar.getValue(); + + String host = sub.getDeliveryHost(); + int port = sub.getDeliveryPort(); + + NotifyRequest notifyReq = new NotifyRequest(); + notifyReq.setRequest(sub, varName, value); + + HTTPResponse res = notifyReq.post(host, port); + if (res.isSuccessful() == false) + return false; + + sub.incrementNotifyCount(); + + return true; + } + + public void notify(StateVariable stateVar) + { + SubscriberList subList = getSubscriberList(); + int subListCnt; + Subscriber subs[]; + + // Remove expired subscribers. + subListCnt = subList.size(); + subs = new Subscriber[subListCnt]; + for (int n=0; n<subListCnt; n++) + subs[n] = subList.getSubscriber(n); + for (int n=0; n<subListCnt; n++) { + Subscriber sub = subs[n]; + if (sub.isExpired() == true) + removeSubscriber(sub); + } + + // Notify to subscribers. + subListCnt = subList.size(); + subs = new Subscriber[subListCnt]; + for (int n=0; n<subListCnt; n++) + subs[n] = subList.getSubscriber(n); + for (int n=0; n<subListCnt; n++) { + Subscriber sub = subs[n]; + if (notify(sub, stateVar) == false) { + /* Don't remove for NMPR specification. + removeSubscriber(sub); + */ + } + } + } + + public void notifyAllStateVariables() + { + ServiceStateTable stateTable = getServiceStateTable(); + int tableSize = stateTable.size(); + for (int n=0; n<tableSize; n++) { + StateVariable var = stateTable.getStateVariable(n); + if (var.isSendEvents() == true) + notify(var); + } + } + + //////////////////////////////////////////////// + // SID + //////////////////////////////////////////////// + + public String getSID() + { + return getServiceData().getSID(); + } + + public void setSID(String id) + { + getServiceData().setSID(id); + } + + public void clearSID() + { + setSID(""); + setTimeout(0); + } + + public boolean hasSID() + { + return StringUtil.hasData(getSID()); + } + + public boolean isSubscribed() + { + return hasSID(); + } + + //////////////////////////////////////////////// + // Timeout + //////////////////////////////////////////////// + + public long getTimeout() + { + return getServiceData().getTimeout(); + } + + public void setTimeout(long value) + { + getServiceData().setTimeout(value); + } + + //////////////////////////////////////////////// + // AcionListener + //////////////////////////////////////////////// + + public void setActionListener(ActionListener listener) + { + ActionList actionList = getActionList(); + int nActions = actionList.size(); + for (int n=0; n<nActions; n++) { + Action action = actionList.getAction(n); + action.setActionListener(listener); + } + } +} diff --git a/router/java/src/org/cybergarage/upnp/ServiceList.java b/router/java/src/org/cybergarage/upnp/ServiceList.java new file mode 100644 index 0000000000000000000000000000000000000000..0a23154ebffc66331ede6cef2e566fe863a5dba3 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ServiceList.java @@ -0,0 +1,53 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ServiceList.java +* +* Revision; +* +* 12/04/02 +* - first revision. +* 06/18/03 +* - Added caching a ArrayIndexOfBound exception. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import java.util.*; + +public class ServiceList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + private static final long serialVersionUID = 6372904993975135597L; + public final static String ELEM_NAME = "serviceList"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public ServiceList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public Service getService(int n) + { + Object obj = null; + try { + obj = get(n); + } + catch (Exception e) {}; + return (Service)obj; + } +} + diff --git a/router/java/src/org/cybergarage/upnp/ServiceStateTable.java b/router/java/src/org/cybergarage/upnp/ServiceStateTable.java new file mode 100644 index 0000000000000000000000000000000000000000..0f1b7eed3a86cc8d88a112581747c4dce94ce3c8 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ServiceStateTable.java @@ -0,0 +1,46 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ServiceStateTable.java +* +* Revision: +* +* 12/06/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import java.util.*; + +public class ServiceStateTable extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + private static final long serialVersionUID = 7626909231678469365L; + public final static String ELEM_NAME = "serviceStateTable"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public ServiceStateTable() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public StateVariable getStateVariable(int n) + { + return (StateVariable)get(n); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/StateVariable.java b/router/java/src/org/cybergarage/upnp/StateVariable.java new file mode 100644 index 0000000000000000000000000000000000000000..45b5e301ad51a75c1e5ac5d19534def0d7dcf01b --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/StateVariable.java @@ -0,0 +1,374 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: StateVariable.java +* +* Revision; +* +* 12/06/02 +* - first revision. +* 06/17/03 +* - Added setSendEvents(), isSendEvents(). +* - Changed to send a event after check the eventing state using isSendEvents(). +* 01/04/04 +* - Added UPnP status methods. +* 01/06/04 +* - Added the following methods. +* getQueryListener() +* setQueryListener() +* performQueryListener() +* 01/07/04 +* - Added StateVariable() and set(); +* - Changed performQueryListener() to use a copy of the StateVariable. +* 03/27/04 +* - Thanks for Adavy +* - Added getAllowedValueList() and getAllowedValueRange(). +* 05/11/04 +* - Added hasAllowedValueList() and hasAllowedValueRange(). +* 07/09/04 +* - Thanks for Dimas <cyberrate@users.sourceforge.net> and Stefano Lenzi <kismet-sl@users.sourceforge.net> +* - Changed postQuerylAction() to set the status code to the UPnPStatus. +* 11/09/04 +* - Theo Beisch <theo.beisch@gmx.de> +* - Changed StateVariable::setValue() to update and send the event when the value is not equal to the current value. +* 11/19/04 +* - Rick Keiner <rick@emanciple.com> +* - 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<nNode; n++) { + Node node = valueListNode.getNode(n); + if (AllowedValue.isAllowedValueNode(node) == false) + continue; + AllowedValue allowedVal = new AllowedValue(node); + valueList.add(allowedVal); + } + return valueList; + } + + public boolean hasAllowedValueList() + { + AllowedValueList valueList = getAllowedValueList(); + return (0 < valueList.size()) ? true : false; + } + + //////////////////////////////////////////////// + // AllowedValueRange + //////////////////////////////////////////////// + + public AllowedValueRange getAllowedValueRange() + { + Node valueRangeNode = getStateVariableNode().getNode(AllowedValueRange.ELEM_NAME); + if (valueRangeNode == null) + return null; + return new AllowedValueRange(valueRangeNode); + } + + public boolean hasAllowedValueRange() + { + return (getAllowedValueRange() != null) ? true : false; + } + + //////////////////////////////////////////////// + // queryAction + //////////////////////////////////////////////// + + public QueryListener getQueryListener() + { + return getStateVariableData().getQueryListener(); + } + + public void setQueryListener(QueryListener listener) + { + getStateVariableData().setQueryListener(listener); + } + + public boolean performQueryListener(QueryRequest queryReq) + { + QueryListener listener = getQueryListener(); + if (listener == null) + return false; + QueryResponse queryRes = new QueryResponse(); + StateVariable retVar = new StateVariable(); + retVar.set(this); + retVar.setValue(""); + retVar.setStatus(UPnPStatus.INVALID_VAR); + if (listener.queryControlReceived(retVar) == true) { + queryRes.setResponse(retVar); + } + else { + UPnPStatus upnpStatus = retVar.getStatus(); + queryRes.setFaultResponse(upnpStatus.getCode(), upnpStatus.getDescription()); + } + queryReq.post(queryRes); + return true; + } + + //////////////////////////////////////////////// + // ActionControl + //////////////////////////////////////////////// + + public QueryResponse getQueryResponse() + { + return getStateVariableData().getQueryResponse(); + } + + private void setQueryResponse(QueryResponse res) + { + getStateVariableData().setQueryResponse(res); + } + + public UPnPStatus getQueryStatus() + { + return getQueryResponse().getUPnPError(); + } + + //////////////////////////////////////////////// + // ActionControl + //////////////////////////////////////////////// + + public boolean postQuerylAction() + { + QueryRequest queryReq = new QueryRequest(); + queryReq.setRequest(this); + if (Debug.isOn() == true) + queryReq.print(); + QueryResponse queryRes = queryReq.post(); + if (Debug.isOn() == true) + queryRes.print(); + setQueryResponse(queryRes); + // Thanks for Dimas <cyberrate@users.sourceforge.net> and Stefano Lenzi <kismet-sl@users.sourceforge.net> (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 0000000000000000000000000000000000000000..e27a5db40e03669ca1698597b6dac007b04f9730 --- /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 = "<?xml version=\"1.0\"?>"; + + //////////////////////////////////////////////// + // 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 0000000000000000000000000000000000000000..f00ba69210652c72f721e0e38d7d52abc5dee83a --- /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 0000000000000000000000000000000000000000..40f9244b5bf25aa46155c36177ebe8aae65fb0b2 --- /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 0000000000000000000000000000000000000000..e14db4b46c793b95978eeb165deb896966993c02 --- /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<nArgNodes; n++) { + Argument arg = new Argument(); + Node argNode = actNode.getNode(n); + arg.setName(argNode.getName()); + arg.setValue(argNode.getValue()); + argList.add(arg); + } + return argList; + } + + //////////////////////////////////////////////// + // setRequest + //////////////////////////////////////////////// + + public void setRequest(Action action, ArgumentList argList) + { + Service service = action.getService(); + + setRequestHost(service); + + setEnvelopeNode(SOAP.createEnvelopeBodyNode()); + Node envNode = getEnvelopeNode(); + Node bodyNode = getBodyNode(); + Node argNode = createContentNode(service, action, argList); + bodyNode.addNode(argNode); + setContent(envNode); + + String serviceType = service.getServiceType(); + String actionName = action.getName(); + String soapAction = "\"" + + serviceType + + "#" + actionName + + "\""; + setSOAPAction(soapAction); + } + + //////////////////////////////////////////////// + // Contents + //////////////////////////////////////////////// + + private Node createContentNode(Service service, Action action, ArgumentList argList) + { + String actionName = action.getName(); + String serviceType = service.getServiceType(); + + Node actionNode = new Node(); + actionNode.setName(Control.NS, actionName); + actionNode.setNameSpace(Control.NS, serviceType); + + int argListCnt = argList.size(); + for (int n=0; n<argListCnt; n++) { + Argument arg = argList.getArgument(n); + Node argNode = new Node(); + argNode.setName(arg.getName()); + argNode.setValue(arg.getValue()); + actionNode.addNode(argNode); + } + + return actionNode; + } + + //////////////////////////////////////////////// + // post + //////////////////////////////////////////////// + + public ActionResponse post() + { + SOAPResponse soapRes = postMessage(getRequestHost(), getRequestPort()); + return new ActionResponse(soapRes); + } +} + diff --git a/router/java/src/org/cybergarage/upnp/control/ActionResponse.java b/router/java/src/org/cybergarage/upnp/control/ActionResponse.java new file mode 100644 index 0000000000000000000000000000000000000000..04c495a498c4c0735789eec6a68c7168f061e87a --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/control/ActionResponse.java @@ -0,0 +1,120 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ActionResponse.java +* +* Revision; +* +* 01/29/03 +* - first revision. +* 09/02/03 +* - Giordano Sassaroli <sassarol@cefriel.it> +* - 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<nArgs; n++) { + Argument arg = argList.getArgument(n); + if (arg.isOutDirection() == false) + continue; + Node argNode = new Node(); + argNode.setName(arg.getName()); + argNode.setValue(arg.getValue()); + actionNameResNode.addNode(argNode); + } + + return actionNameResNode; + } + + //////////////////////////////////////////////// + // getResponse + //////////////////////////////////////////////// + + private Node getActionResponseNode() + { + Node bodyNode = getBodyNode(); + if (bodyNode == null || bodyNode.hasNodes() == false) + return null; + return bodyNode.getNode(0); + } + + + public ArgumentList getResponse() + { + ArgumentList argList = new ArgumentList(); + + Node resNode = getActionResponseNode(); + if (resNode == null) + return argList; + + int nArgs = resNode.getNNodes(); + for (int n=0; n<nArgs; n++) { + Node node = resNode.getNode(n); + String name = node.getName(); + String value = node.getValue(); + Argument arg = new Argument(name, value); + argList.add(arg); + } + + return argList; + } +} diff --git a/router/java/src/org/cybergarage/upnp/control/Control.java b/router/java/src/org/cybergarage/upnp/control/Control.java new file mode 100644 index 0000000000000000000000000000000000000000..53549096ebe288a88cc8943d460be7c956504372 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/control/Control.java @@ -0,0 +1,29 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: Control.java +* +* Revision; +* +* 01/20/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.control; + +public class Control +{ + public final static String NS = "u"; + public final static String QUERY_SOAPACTION = "urn:schemas-upnp-org:control-1-0#QueryStateVariable"; + public final static String XMLNS = "urn:schemas-upnp-org:control-1-0"; + public final static String QUERY_STATE_VARIABLE = "QueryStateVariable"; + public final static String QUERY_STATE_VARIABLE_RESPONSE = "QueryStateVariableResponse"; + public final static String VAR_NAME = "varName"; + public final static String RETURN = "return"; +} + + diff --git a/router/java/src/org/cybergarage/upnp/control/ControlRequest.java b/router/java/src/org/cybergarage/upnp/control/ControlRequest.java new file mode 100644 index 0000000000000000000000000000000000000000..a605a2afad49f5ecbe72d220fe6f95d0c382ff6f --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/control/ControlRequest.java @@ -0,0 +1,128 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ControlRequest.java +* +* Revision: +* +* 01/29/03 +* - first revision. +* 05/22/03 +* - Giordano Sassaroli <sassarol@cefriel.it> +* - 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 <sassarol@cefriel.it> / 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 <rob.van.den.boomen@philips.com> +* - Fixed to set a URLBase from the SSDP header when the URLBase of the description is null. +* 02/18/04 +* - Andre <andre@antiheld.net> +* - 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 <tsroyale at users.sourceforge.net> +* - 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 <sassarol@cefriel.it> (05/21/03) + setURI(ctrlURL, true); + + // Thanks for Giordano Sassaroli <sassarol@cefriel.it> and Suzan Foster (09/02/03) + // Thanks for Andre <andre@antiheld.net> (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 <rob.van.den.boomen@philips.com> (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 0000000000000000000000000000000000000000..36fcab89d4e88140e1fedc1c660c8ae76ccf8695 --- /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) + { + // <s:Fault> + Node faultNode = new Node(SOAP.XMLNS + SOAP.DELIM + SOAP.FAULT); + + // <faultcode>s:Client</faultcode> + Node faultCodeNode = new Node(SOAP.FAULT_CODE); + faultCodeNode.setValue(SOAP.XMLNS + SOAP.DELIM + FAULT_CODE); + faultNode.addNode(faultCodeNode); + + // <faultstring>UPnPError</faultstring> + Node faultStringNode = new Node(SOAP.FAULT_STRING); + faultStringNode.setValue(FAULT_STRING); + faultNode.addNode(faultStringNode); + + // <detail> + Node detailNode = new Node(SOAP.DETAIL); + faultNode.addNode(detailNode); + + // <UPnPError xmlns="urn:schemas-upnp-org:control-1-0"> + Node upnpErrorNode = new Node(FAULT_STRING); + upnpErrorNode.setAttribute("xmlns", Control.XMLNS); + detailNode.addNode(upnpErrorNode); + + // <errorCode>error code</errorCode> + Node errorCodeNode = new Node(SOAP.ERROR_CODE); + errorCodeNode.setValue(errCode); + upnpErrorNode.addNode(errorCodeNode); + + // <errorDescription>error string</errorDescription> + 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 0000000000000000000000000000000000000000..8af1ea13e30549c7580a67e86be27e6e6343835d --- /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 0000000000000000000000000000000000000000..d222051fc55de62dbbaac40dec8e7d4b62de1f9d --- /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 <sassarol@cefriel.it> +* - 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 0000000000000000000000000000000000000000..75ba30c84e90186e02e91fde6e1b42bd8c6e7768 --- /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 0000000000000000000000000000000000000000..7b8d2e05907caf7372a34d72347871b5f58769d6 --- /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 0000000000000000000000000000000000000000..373fea82d93c55ff64223e6c15fd4d54cad28bfb --- /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 0000000000000000000000000000000000000000..fe42dc5ea6a88210e0b90cda2894f0115ee288f7 --- /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 0000000000000000000000000000000000000000..fc76020ab7fc2d076547e7f0ab608307a92f4718 --- /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 <newell@media-rush.com> +* - 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 0000000000000000000000000000000000000000..e6c0ddb8e9ed3a0b9f28fcad76442bb2d6b297dc --- /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 0000000000000000000000000000000000000000..a08b3a39324c5f3cce6bc9cef8443df3354947c9 --- /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 0000000000000000000000000000000000000000..1e0093f743a5e988c8fce4c27b1a6f4da2849f7b --- /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 0000000000000000000000000000000000000000..1acb0d49058e89b7cf22f776fbdf777794aa72f8 --- /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 0000000000000000000000000000000000000000..7eab5f19cc21a8257472fbfd59e177a63edb2797 --- /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 0000000000000000000000000000000000000000..bac853ea571a5716994e93933ed5765ec5f9dd2d --- /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 0000000000000000000000000000000000000000..dcbfaf8ce8c5560ec5a8fb6668fdf1ca04848281 --- /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 0000000000000000000000000000000000000000..758a37c272c93219cfb3a302e61ab173506d4ba2 --- /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 0000000000000000000000000000000000000000..c87c41a37a131f6e3b2e9eed3b9c6a50def12e6d --- /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 0000000000000000000000000000000000000000..74c704027b94c1a1e7cc73bebc963fc2532841ea --- /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 0000000000000000000000000000000000000000..900b9307cfdde93165b55c30c116bd5f5f74be3d --- /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 0000000000000000000000000000000000000000..c8891e1d21091106f303b148a0afd2258e5c88f3 --- /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 <sassarol@cefriel.it> +* - 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 <sassarol@cefriel.it> +* - 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 <sassarol@cefriel.it> +* - 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 <sassarol@cefriel.it> +* - 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 <sassarol@cefriel.it> (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 <sassarol@cefriel.it> (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 <sassarol@cefriel.it> (09/08/03) + public PropertyList getPropertyList() { + PropertyList properties = new PropertyList(); + Node varSetNode = getEnvelopeNode(); + for (int i = 0; i<varSetNode.getNNodes(); i++){ + Node propNode = varSetNode.getNode(i); + if (propNode == null) + continue; + Property prop = getProperty(propNode.getNode(0)); + properties.add(prop); + } + return properties; + } + +} \ No newline at end of file diff --git a/router/java/src/org/cybergarage/upnp/event/Property.java b/router/java/src/org/cybergarage/upnp/event/Property.java new file mode 100644 index 0000000000000000000000000000000000000000..a9c6d67b68a48874d287787352427b86da2229e2 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/event/Property.java @@ -0,0 +1,65 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: Subscriber.java +* +* Revision; +* +* 01/29/03 +* - first revision. +* 05/22/03 +* - Giordano Sassaroli <sassarol@cefriel.it> +* - 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 0000000000000000000000000000000000000000..f890bb3afa890940d958709ca7d5eed3a6be2770 --- /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 0000000000000000000000000000000000000000..8ade71c721938aa464b32e154ead68a130883704 --- /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 <newell@media-rush.com> +* - 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 0000000000000000000000000000000000000000..0a991204a2cfc95981f052dc3e44bd5a65eee480 --- /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 0000000000000000000000000000000000000000..c03638daad23e6a5906b480aa6e942034618a26b --- /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 0000000000000000000000000000000000000000..a3e11cc651b67813131d69c1e60bf2ce71e85a40 --- /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 <sassarol@cefriel.it> +* - 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 <sassarol@cefriel.it> +* - 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 <markus.thurner@fh-hagenberg.at> (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 <grzegorz.lehmann@dai-labor.de> +* - Stefano Lenzi <kismet-sl@users.sourceforge.net> +* - 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 <sassarol@cefriel.it> (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 <markus.thurner@fh-hagenberg.at> (06/11/2004) + if (urlBaseStr == null || urlBaseStr.length() <= 0) { + Device rootDev = service.getRootDevice(); + if (rootDev != null) + urlBaseStr = rootDev.getLocation(); + } + + // Thanks for Giordano Sassaroli <sassarol@cefriel.it> (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 0000000000000000000000000000000000000000..cf60ecd56286b9e1d3e7fa2d3182b00a909e8037 --- /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 0000000000000000000000000000000000000000..4f6361f59410e412b065c54abab0c4f97ca09e42 --- /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 <theo.beisch@gmx.de> +* - 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 0000000000000000000000000000000000000000..221311c554888397b8892a683f11a92aed599cc9 --- /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 <inma@DIF.UM.ES> +* - 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 <olivern@users.sourceforge.net> +* - 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 0000000000000000000000000000000000000000..8f692355f740715c9c12b1bae0f73a0342d7bb4e --- /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 0000000000000000000000000000000000000000..b2fb3c5c0bdc2cbfef4175e60177edf2db129271 --- /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 0000000000000000000000000000000000000000..6a33af7c3c2d18fd3a4fc3f075fca9a57a9f6925 --- /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 <inma@dif.um.es> +* - Added a multicast filter using the SSDP pakcet. +* 04/20/05 +* - Mikael Hakman <mhakman@dkab.net> +* - 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 0000000000000000000000000000000000000000..d0541510c0d5d440a0531fc6a6b3174f6fa5d13e --- /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<nSockets; n++) { + SSDPNotifySocket sock = getSSDPNotifySocket(n); + sock.setControlPoint(ctrlPoint); + } + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public boolean open() + { + int nHostAddrs = HostInterface.getNHostAddresses(); + for (int n=0; n<nHostAddrs; n++) { + String bindAddr = HostInterface.getHostAddress(n); + SSDPNotifySocket ssdpNotifySocket = new SSDPNotifySocket(bindAddr); + add(ssdpNotifySocket); + } + return true; + } + + public void close() + { + int nSockets = size(); + for (int n=0; n<nSockets; n++) { + SSDPNotifySocket sock = getSSDPNotifySocket(n); + sock.close(); + } + clear(); + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public void start() + { + int nSockets = size(); + for (int n=0; n<nSockets; n++) { + SSDPNotifySocket sock = getSSDPNotifySocket(n); + sock.start(); + } + } + + public void stop() + { + int nSockets = size(); + for (int n=0; n<nSockets; n++) { + SSDPNotifySocket sock = getSSDPNotifySocket(n); + sock.stop(); + } + } + +} + diff --git a/router/java/src/org/cybergarage/upnp/ssdp/SSDPPacket.java b/router/java/src/org/cybergarage/upnp/ssdp/SSDPPacket.java new file mode 100644 index 0000000000000000000000000000000000000000..d2e0f2770566a632f222363193163785cb75f1ab --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/SSDPPacket.java @@ -0,0 +1,243 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: SSDPPacket.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* 05/13/03 +* - Added getLocalAddress(). +* 11/01/04 +* - Theo Beisch <theo.beisch@gmx.de> +* - Fixed isRootDevice() to check the ST header. +* 11/19/04 +* - Theo Beisch <theo.beisch@gmx.de> +* - 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 0000000000000000000000000000000000000000..f40442f18ec2b4b3690041a544a0953d1a18b681 --- /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 0000000000000000000000000000000000000000..31b4ed280ec9178940aabbba7c615c649ed1a5a6 --- /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 <bhills@openshores.com> +* - 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 0000000000000000000000000000000000000000..fbe7cf8123276a3c7a92d9584b044137ec02dabc --- /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 0000000000000000000000000000000000000000..264f5442918ac192776590f23a26f112775937ce --- /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 0000000000000000000000000000000000000000..61668872aa40da48b0f5d8f10db91892a9893edc --- /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 0000000000000000000000000000000000000000..e9a7582449dd4ea9f8879a7944d977dc658c2544 --- /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<nSockets; n++) { + SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n); + sock.setControlPoint(ctrlPoint); + } + } + + //////////////////////////////////////////////// + // get + //////////////////////////////////////////////// + + public SSDPSearchResponseSocket getSSDPSearchResponseSocket(int n) + { + return (SSDPSearchResponseSocket)get(n); + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public boolean open(int port) + { + try { + int nHostAddrs = HostInterface.getNHostAddresses(); + for (int n=0; n<nHostAddrs; n++) { + String bindAddr = HostInterface.getHostAddress(n); + SSDPSearchResponseSocket socket = new SSDPSearchResponseSocket(bindAddr, port); + add(socket); + } + } + catch (Exception e) { + stop(); + close(); + clear(); + return false; + } + return true; + } + + public boolean open() + { + return open(SSDP.PORT); + } + + public void close() + { + int nSockets = size(); + for (int n=0; n<nSockets; n++) { + SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n); + sock.close(); + } + clear(); + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public void start() + { + int nSockets = size(); + for (int n=0; n<nSockets; n++) { + SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n); + sock.start(); + } + } + + public void stop() + { + int nSockets = size(); + for (int n=0; n<nSockets; n++) { + SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n); + sock.stop(); + } + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public boolean post(SSDPSearchRequest req) + { + boolean ret = true; + int nSockets = size(); + for (int n=0; n<nSockets; n++) { + SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n); + String bindAddr = sock.getLocalAddress(); + req.setLocalAddress(bindAddr); + String ssdpAddr = SSDP.ADDRESS; + if (HostInterface.isIPv6Address(bindAddr) == true) + ssdpAddr = SSDP.getIPv6Address(); + //sock.joinGroup(ssdpAddr, SSDP.PORT, bindAddr); + if (sock.post(ssdpAddr, SSDP.PORT, req) == false) + ret = false; + //sock.leaveGroup(ssdpAddr, SSDP.PORT, bindAddr); + } + return ret; + } + +} + diff --git a/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchSocket.java b/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchSocket.java new file mode 100644 index 0000000000000000000000000000000000000000..295e1e143527786aa9a6057e192d10667ecbb35b --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchSocket.java @@ -0,0 +1,118 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: SSDPSearchSocket.java +* +* Revision; +* +* 12/30/02 +* - first revision. +* 05/13/03 +* - Added support for IPv6. +* 05/28/03 +* - Moved post() for SSDPSearchRequest to SSDPResponseSocketList. +* 04/20/05 +* - Mikael Hakman <mhakman@dkab.net> +* - 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<listenerSize; n++) { + SearchListener listener = (SearchListener)deviceSearchListenerList.get(n); + listener.deviceSearchReceived(ssdpPacket); + } + } + + //////////////////////////////////////////////// + // run + //////////////////////////////////////////////// + + private Thread deviceSearchThread = null; + + public void run() + { + Thread thisThread = Thread.currentThread(); + + while (deviceSearchThread == thisThread) { + Thread.yield(); + SSDPPacket packet = receive(); + + // Thanks for Mikael Hakman (04/20/05) + if (packet == null) + continue; + + if (packet.isDiscover() == true) + performSearchListener(packet); + } + } + + public void start() + { + deviceSearchThread = new Thread(this,"UPnP-SSDPSearchSocket"); + deviceSearchThread.setDaemon(true); + deviceSearchThread.start(); + } + + public void stop() + { + // Thanks for Mikael Hakman (04/20/05) + close(); + + deviceSearchThread = null; + } +} + diff --git a/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchSocketList.java b/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchSocketList.java new file mode 100644 index 0000000000000000000000000000000000000000..7fc37241160b33ed0ef0f6368c6101b577aa5e80 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/ssdp/SSDPSearchSocketList.java @@ -0,0 +1,105 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: SSDPSearchSocketList.java +* +* Revision; +* +* 05/08/03 +* - first revision. +* 05/28/03 +* - Moved post() for SSDPSearchRequest to SSDPResponseSocket. +* - Removed open(int). +* +******************************************************************/ + +package org.cybergarage.upnp.ssdp; + +import java.util.*; + +import org.cybergarage.net.*; + +import org.cybergarage.upnp.device.*; + +public class SSDPSearchSocketList extends Vector +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + private static final long serialVersionUID = 4071292828166415028L; + + public SSDPSearchSocketList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public SSDPSearchSocket getSSDPSearchSocket(int n) + { + return (SSDPSearchSocket)get(n); + } + + public void addSearchListener(SearchListener listener) + { + int nServers = size(); + for (int n=0; n<nServers; n++) { + SSDPSearchSocket sock = getSSDPSearchSocket(n); + sock.addSearchListener(listener); + } + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public boolean open() + { + int nHostAddrs = HostInterface.getNHostAddresses(); + for (int n=0; n<nHostAddrs; n++) { + String bindAddr = HostInterface.getHostAddress(n); + SSDPSearchSocket ssdpSearchSocket = new SSDPSearchSocket(bindAddr); + add(ssdpSearchSocket); + } + return true; + } + + public void close() + { + int nSockets = size(); + for (int n=0; n<nSockets; n++) { + SSDPSearchSocket sock = getSSDPSearchSocket(n); + sock.close(); + } + clear(); + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public void start() + { + int nSockets = size(); + for (int n=0; n<nSockets; n++) { + SSDPSearchSocket sock = getSSDPSearchSocket(n); + sock.start(); + } + } + + public void stop() + { + int nSockets = size(); + for (int n=0; n<nSockets; n++) { + SSDPSearchSocket sock = getSSDPSearchSocket(n); + sock.stop(); + } + } + +} + diff --git a/router/java/src/org/cybergarage/upnp/xml/ActionData.java b/router/java/src/org/cybergarage/upnp/xml/ActionData.java new file mode 100644 index 0000000000000000000000000000000000000000..4982f408842e7541ef214637cfed1cdfc749fe1f --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/xml/ActionData.java @@ -0,0 +1,57 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: ActionData.java +* +* Revision; +* +* 03/28/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.xml; + +import org.cybergarage.upnp.control.*; + +public class ActionData extends NodeData +{ + public ActionData() + { + } + + //////////////////////////////////////////////// + // ActionListener + //////////////////////////////////////////////// + + private ActionListener actionListener = null; + + public ActionListener getActionListener() { + return actionListener; + } + + public void setActionListener(ActionListener actionListener) { + this.actionListener = actionListener; + } + + //////////////////////////////////////////////// + // ControlResponse + //////////////////////////////////////////////// + + private ControlResponse ctrlRes = null; + + public ControlResponse getControlResponse() + { + return ctrlRes; + } + + public void setControlResponse(ControlResponse res) + { + ctrlRes = res; + } + +} + diff --git a/router/java/src/org/cybergarage/upnp/xml/ArgumentData.java b/router/java/src/org/cybergarage/upnp/xml/ArgumentData.java new file mode 100644 index 0000000000000000000000000000000000000000..b8605b811f14a742bc92cfb5e816515166433b5d --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/xml/ArgumentData.java @@ -0,0 +1,41 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: ArgumentData.java +* +* Revision; +* +* 02/24/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.xml; + +public class ArgumentData extends NodeData +{ + public ArgumentData() + { + } + + //////////////////////////////////////////////// + // value + //////////////////////////////////////////////// + + private String value = ""; + + public String getValue() + { + return value; + } + + public void setValue(String value) + { + this.value = value; + } + +} + diff --git a/router/java/src/org/cybergarage/upnp/xml/DeviceData.java b/router/java/src/org/cybergarage/upnp/xml/DeviceData.java new file mode 100644 index 0000000000000000000000000000000000000000..dad0a97b9648b00a8e1120753b915e9831a239bd --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/xml/DeviceData.java @@ -0,0 +1,170 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: DeviceData.java +* +* Revision; +* +* 03/28/03 +* - first revision. +* 12/25/03 +* - Added Advertiser functions. +* +******************************************************************/ + +package org.cybergarage.upnp.xml; + +import java.io.*; + +import org.cybergarage.util.*; +import org.cybergarage.http.*; + +import org.cybergarage.upnp.*; +import org.cybergarage.upnp.ssdp.*; +import org.cybergarage.upnp.device.*; + +public class DeviceData extends NodeData +{ + public DeviceData() + { + } + + //////////////////////////////////////////////// + // description + //////////////////////////////////////////////// + + private String descriptionURI = null; + private File descriptionFile = null; + + public File getDescriptionFile() { + return descriptionFile; + } + + public String getDescriptionURI() { + return descriptionURI; + } + + public void setDescriptionFile(File descriptionFile) { + this.descriptionFile = descriptionFile; + } + + public void setDescriptionURI(String descriptionURI) { + this.descriptionURI = descriptionURI; + } + + //////////////////////////////////////////////// + // description + //////////////////////////////////////////////// + + private String location = ""; + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + + //////////////////////////////////////////////// + // LeaseTime + //////////////////////////////////////////////// + + private int leaseTime = Device.DEFAULT_LEASE_TIME; + + public int getLeaseTime() + { + return leaseTime; + } + + public void setLeaseTime(int val) + { + leaseTime = val; + } + + //////////////////////////////////////////////// + // HTTPServer + //////////////////////////////////////////////// + + private HTTPServerList httpServerList = new HTTPServerList(); + + public HTTPServerList getHTTPServerList() { + return httpServerList; + } + + //////////////////////////////////////////////// + // httpPort + //////////////////////////////////////////////// + + private int httpPort = Device.HTTP_DEFAULT_PORT; + + public int getHTTPPort() { + return httpPort; + } + + public void setHTTPPort(int port) { + httpPort = port; + } + + //////////////////////////////////////////////// + // controlActionListenerList + //////////////////////////////////////////////// + + private ListenerList controlActionListenerList = new ListenerList(); + + public ListenerList getControlActionListenerList() { + return controlActionListenerList; + } + +/* + public void setControlActionListenerList(ListenerList controlActionListenerList) { + this.controlActionListenerList = controlActionListenerList; + } +*/ + + //////////////////////////////////////////////// + // SSDPSearchSocket + //////////////////////////////////////////////// + + private SSDPSearchSocketList ssdpSearchSocketList = new SSDPSearchSocketList(); + + public SSDPSearchSocketList getSSDPSearchSocketList() { + return ssdpSearchSocketList; + } + + //////////////////////////////////////////////// + // SSDPPacket + //////////////////////////////////////////////// + + private SSDPPacket ssdpPacket = null; + + public SSDPPacket getSSDPPacket() { + return ssdpPacket; + } + + public void setSSDPPacket(SSDPPacket packet) { + ssdpPacket = packet; + } + + //////////////////////////////////////////////// + // Advertiser + //////////////////////////////////////////////// + + private Advertiser advertiser = null; + + public void setAdvertiser(Advertiser adv) + { + advertiser = adv; + } + + public Advertiser getAdvertiser() + { + return advertiser; + } + + +} + diff --git a/router/java/src/org/cybergarage/upnp/xml/NodeData.java b/router/java/src/org/cybergarage/upnp/xml/NodeData.java new file mode 100644 index 0000000000000000000000000000000000000000..638e404d71dccbba5189a5c665c7cfe8543dfab7 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/xml/NodeData.java @@ -0,0 +1,43 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: ActionData.java +* +* Revision; +* +* 03/28/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.upnp.xml; + +import org.cybergarage.xml.*; + +public class NodeData +{ + public NodeData() + { + setNode(null); + } + + //////////////////////////////////////////////// + // Node + //////////////////////////////////////////////// + + private Node node; + + public void setNode(Node node) + { + this.node = node; + } + + public Node getNode() + { + return node; + } +} + diff --git a/router/java/src/org/cybergarage/upnp/xml/ServiceData.java b/router/java/src/org/cybergarage/upnp/xml/ServiceData.java new file mode 100644 index 0000000000000000000000000000000000000000..bb4dfe979849691e451e42966aee34d4072336c0 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/xml/ServiceData.java @@ -0,0 +1,98 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: ServiceData.java +* +* Revision; +* +* 03/28/03 +* - first revision. +* 01/06/04 +* - Moved setQueryListener() and getQueryListener() to StateVariableData class. +* 03/30/05 +* - Removed setDescriptionURL() and getDescriptionURL(). +* +******************************************************************/ + +package org.cybergarage.upnp.xml; + +import org.cybergarage.util.*; +import org.cybergarage.xml.*; + +import org.cybergarage.upnp.event.*; + +public class ServiceData extends NodeData +{ + public ServiceData() + { + } + + //////////////////////////////////////////////// + // controlActionListenerList + //////////////////////////////////////////////// + + private ListenerList controlActionListenerList = new ListenerList(); + + public ListenerList getControlActionListenerList() { + return controlActionListenerList; + } + + //////////////////////////////////////////////// + // scpdNode + //////////////////////////////////////////////// + + private Node scpdNode = null; + + public Node getSCPDNode() { + return scpdNode; + } + + public void setSCPDNode(Node node) { + scpdNode = node; + } + + //////////////////////////////////////////////// + // SubscriberList + //////////////////////////////////////////////// + + private SubscriberList subscriberList = new SubscriberList(); + + public SubscriberList getSubscriberList() { + return subscriberList; + } + + //////////////////////////////////////////////// + // SID + //////////////////////////////////////////////// + + private String sid = ""; + + public String getSID() { + return sid; + } + + public void setSID(String id) { + sid = id; + } + + //////////////////////////////////////////////// + // Timeout + //////////////////////////////////////////////// + + private long timeout = 0; + + public long getTimeout() + { + return timeout; + } + + public void setTimeout(long value) + { + timeout = value; + } + +} + diff --git a/router/java/src/org/cybergarage/upnp/xml/StateVariableData.java b/router/java/src/org/cybergarage/upnp/xml/StateVariableData.java new file mode 100644 index 0000000000000000000000000000000000000000..c32c7c1ed949929804f22e3633be4d8babeb4999 --- /dev/null +++ b/router/java/src/org/cybergarage/upnp/xml/StateVariableData.java @@ -0,0 +1,73 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File:StateVariableData.java +* +* Revision; +* +* 02/05/03 +* - first revision. +* 01/06/04 +* - Added setQueryListener() and getQueryListener(). +* +******************************************************************/ + +package org.cybergarage.upnp.xml; + +import org.cybergarage.upnp.control.*; + +public class StateVariableData extends NodeData +{ + public StateVariableData() + { + } + + //////////////////////////////////////////////// + // value + //////////////////////////////////////////////// + + private String value = ""; + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + //////////////////////////////////////////////// + // QueryListener + //////////////////////////////////////////////// + + private QueryListener queryListener = null; + + public QueryListener getQueryListener() { + return queryListener; + } + + public void setQueryListener(QueryListener queryListener) { + this.queryListener = queryListener; + } + + //////////////////////////////////////////////// + // QueryResponse + //////////////////////////////////////////////// + + private QueryResponse queryRes = null; + + public QueryResponse getQueryResponse() + { + return queryRes; + } + + public void setQueryResponse(QueryResponse res) + { + queryRes = res; + } + +} + diff --git a/router/java/src/org/cybergarage/util/Debug.java b/router/java/src/org/cybergarage/util/Debug.java new file mode 100644 index 0000000000000000000000000000000000000000..6cd9d939e883ef347272ed92a14ce838a059decc --- /dev/null +++ b/router/java/src/org/cybergarage/util/Debug.java @@ -0,0 +1,51 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: Debug.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.util; + +public final class Debug +{ + public static boolean enabled = false; + + public static final void on() { + enabled = true; + } + public static final void off() { + enabled = false; + } + public static boolean isOn() { + return enabled; + } + public static final void message(String s) { + if (enabled == true) + System.out.println("CyberGarage message : " + s); + } + public static final void message(String m1, String m2) { + if (enabled == true) + System.out.println("CyberGarage message : "); + System.out.println(m1); + System.out.println(m2); + } + public static final void warning(String s) { + System.out.println("CyberGarage warning : " + s); + } + public static final void warning(String m, Exception e) { + System.out.println("CyberGarage warning : " + m + " (" + e.getMessage() + ")"); + } + public static final void warning(Exception e) { + warning(e.getMessage()); + e.printStackTrace(); + } +} \ No newline at end of file diff --git a/router/java/src/org/cybergarage/util/FileUtil.java b/router/java/src/org/cybergarage/util/FileUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..5e9b54a0dd193c3caabcac768fdb8879ff867fbc --- /dev/null +++ b/router/java/src/org/cybergarage/util/FileUtil.java @@ -0,0 +1,77 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: FileUtil.java +* +* Revision: +* +* 01/03/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.util; + +import java.io.*; + +public final class FileUtil +{ + public final static byte[] load(String fileName) + { + try { + FileInputStream fin=new FileInputStream(fileName); + return load(fin); + } + catch (Exception e) { + Debug.warning(e); + return new byte[0]; + } + } + + public final static byte[] load(File file) + { + try { + FileInputStream fin=new FileInputStream(file); + return load(fin); + } + catch (Exception e) { + Debug.warning(e); + return new byte[0]; + } + } + + public final static byte[] load(FileInputStream fin) + { + byte readBuf[] = new byte[512*1024]; + + try { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + + int readCnt = fin.read(readBuf); + while (0 < readCnt) { + bout.write(readBuf, 0, readCnt); + readCnt = fin.read(readBuf); + } + + fin.close(); + + return bout.toByteArray(); + } + catch (Exception e) { + Debug.warning(e); + return new byte[0]; + } + } + + public final static boolean isXMLFileName(String name) + { + if (StringUtil.hasData(name) == false) + return false; + String lowerName = name.toLowerCase(); + return lowerName.endsWith("xml"); + } +} + diff --git a/router/java/src/org/cybergarage/util/ListenerList.java b/router/java/src/org/cybergarage/util/ListenerList.java new file mode 100644 index 0000000000000000000000000000000000000000..afdeff0d65804d322a00d2e8eb55d4b0080438a9 --- /dev/null +++ b/router/java/src/org/cybergarage/util/ListenerList.java @@ -0,0 +1,31 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ListenerList.java +* +* Revision; +* +* 12/30/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.util; + +import java.util.*; + +public class ListenerList extends Vector +{ + private static final long serialVersionUID = 8039231561720446173L; + + public boolean add(Object obj) + { + if (0 <= indexOf(obj)) + return false; + return super.add(obj); + } +} + diff --git a/router/java/src/org/cybergarage/util/Mutex.java b/router/java/src/org/cybergarage/util/Mutex.java new file mode 100644 index 0000000000000000000000000000000000000000..f26351271c317a5906d20e6666dc3072b8f97342 --- /dev/null +++ b/router/java/src/org/cybergarage/util/Mutex.java @@ -0,0 +1,54 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: Mutex.java +* +* Revision: +* +* 06/19/04 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.util; + +public class Mutex +{ + private boolean syncLock; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public Mutex() + { + syncLock = false; + } + + //////////////////////////////////////////////// + // lock + //////////////////////////////////////////////// + + public synchronized void lock() + { + while(syncLock == true) { + try { + wait(); + } + catch (Exception e) { + Debug.warning(e); + }; + } + syncLock = true; + } + + public synchronized void unlock() + { + syncLock = false; + notifyAll(); + } + +} \ No newline at end of file diff --git a/router/java/src/org/cybergarage/util/StringUtil.java b/router/java/src/org/cybergarage/util/StringUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..b59eb929a101c1e7ed08cd2296b582211e69f098 --- /dev/null +++ b/router/java/src/org/cybergarage/util/StringUtil.java @@ -0,0 +1,123 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: FileUtil.java +* +* Revision: +* +* 01/12/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.util; + +public final class StringUtil +{ + public final static boolean hasData(String value) + { + if (value == null) + return false; + if (value.length() <= 0) + return false; + return true; + } + + public final static int toInteger(String value) + { + try { + return Integer.parseInt(value); + } + catch (Exception e) { + Debug.warning(e); + } + return 0; + } + + public final static long toLong(String value) + { + try { + return Long.parseLong(value); + } + catch (Exception e) { + Debug.warning(e); + } + return 0; + } + + public final static int findOf(String str, String chars, int startIdx, int endIdx, int offset, boolean isEqual) + { + if (offset == 0) + return -1; + int charCnt = chars.length(); + int idx = startIdx; + while (true) { + if (0 < offset) { + if (endIdx < idx) + break; + } + else { + if (idx < endIdx) + break; + } + char strc = str.charAt(idx); + int noEqualCnt = 0; + for (int n=0; n<charCnt; n++) { + char charc = chars.charAt(n); + if (isEqual == true) { + if (strc == charc) + return idx; + } + else { + if (strc != charc) + noEqualCnt++; + if (noEqualCnt == charCnt) + return idx; + } + } + idx += offset; + } + return -1; + } + + public final static int findFirstOf(String str, String chars) + { + return findOf(str, chars, 0, (str.length()-1), 1, true); + } + + public final static int findFirstNotOf(String str, String chars) + { + return findOf(str, chars, 0, (str.length()-1), 1, false); + } + + public final static int findLastOf(String str, String chars) + { + return findOf(str, chars, (str.length()-1), 0, -1, true); + } + + public final static int findLastNotOf(String str, String chars) + { + return findOf(str, chars, (str.length()-1), 0, -1, false); + } + + public final static String trim(String trimStr, String trimChars) + { + int spIdx = findFirstNotOf(trimStr, trimChars); + if (spIdx < 0) { + String buf = trimStr; + return buf; + } + String trimStr2 = trimStr.substring(spIdx, trimStr.length()); + spIdx = findLastNotOf(trimStr2, trimChars); + if (spIdx < 0) { + String buf = trimStr2; + return buf; + } + String buf = trimStr2.substring(0, spIdx+1); + return buf; + } +} + diff --git a/router/java/src/org/cybergarage/util/ThreadCore.java b/router/java/src/org/cybergarage/util/ThreadCore.java new file mode 100644 index 0000000000000000000000000000000000000000..b03f2f0c35993b8c2b6d4b1cbac1150439fd11ed --- /dev/null +++ b/router/java/src/org/cybergarage/util/ThreadCore.java @@ -0,0 +1,76 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: Thread.java +* +* Revision: +* +* 01/05/04 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.util; + +public class ThreadCore implements Runnable +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public ThreadCore() + { + } + + //////////////////////////////////////////////// + // Thread + //////////////////////////////////////////////// + + private java.lang.Thread mThreadObject = null; + + public void setThreadObject(java.lang.Thread obj) { + mThreadObject = obj; + } + + public java.lang.Thread getThreadObject() { + return mThreadObject; + } + + public void start() + { + java.lang.Thread threadObject = getThreadObject(); + if (threadObject == null) { + threadObject = new java.lang.Thread(this); + setThreadObject(threadObject); + threadObject.start(); + } + } + + public void run() + { + } + + public boolean isRunnable() + { + return (Thread.currentThread() == getThreadObject()) ? true : false; + } + + public void stop() + { + java.lang.Thread threadObject = getThreadObject(); + if (threadObject != null) { + //threadObject.destroy(); + //threadObject.stop(); + setThreadObject(null); + } + } + + public void restart() + { + stop(); + start(); + } +} diff --git a/router/java/src/org/cybergarage/util/TimerUtil.java b/router/java/src/org/cybergarage/util/TimerUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..58087435bbfcf5a61d6e25e47a9080f527f58178 --- /dev/null +++ b/router/java/src/org/cybergarage/util/TimerUtil.java @@ -0,0 +1,37 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: TimerUtil.java +* +* Revision: +* +* 01/15/03 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.util; + +public final class TimerUtil +{ + public final static void wait(int waitTime) + { + try { + Thread.sleep(waitTime); + } + catch (Exception e) {} + } + + public final static void waitRandom(int time) + { + int waitTime = (int)(Math.random() * (double)time); + try { + Thread.sleep(waitTime); + } + catch (Exception e) {} + } +} + diff --git a/router/java/src/org/cybergarage/xml/Attribute.java b/router/java/src/org/cybergarage/xml/Attribute.java new file mode 100644 index 0000000000000000000000000000000000000000..ce7c16e9e1a306333fcbfe12a504dc3d32415e63 --- /dev/null +++ b/router/java/src/org/cybergarage/xml/Attribute.java @@ -0,0 +1,61 @@ +/****************************************************************** +* +* CyberXML for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: Attribute.java +* +* Revision; +* +* 11/27/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.xml; + +public class Attribute +{ + private String name = new String(); + private String value = new String(); + + public Attribute() + { + } + + public Attribute(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/xml/AttributeList.java b/router/java/src/org/cybergarage/xml/AttributeList.java new file mode 100644 index 0000000000000000000000000000000000000000..8bb85cd016498a54f3df679f448428ded0c9a9ae --- /dev/null +++ b/router/java/src/org/cybergarage/xml/AttributeList.java @@ -0,0 +1,47 @@ +/****************************************************************** +* +* CyberXML for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: AttributeList.java +* +* Revision; +* +* 11/27/02 +* - first revision. +* +******************************************************************/ + +package org.cybergarage.xml; + +import java.util.*; + +public class AttributeList extends Vector +{ + private static final long serialVersionUID = -5516389508555401104L; + + public AttributeList() + { + } + + public Attribute getAttribute(int n) + { + return (Attribute)get(n); + } + + public Attribute getAttribute(String name) + { + if (name == null) + return null; + + int nLists = size(); + for (int n=0; n<nLists; n++) { + Attribute elem = getAttribute(n); + if (name.compareTo(elem.getName()) == 0) + return elem; + } + return null; + } +} + diff --git a/router/java/src/org/cybergarage/xml/Node.java b/router/java/src/org/cybergarage/xml/Node.java new file mode 100644 index 0000000000000000000000000000000000000000..93ca5aa0b38b351edb7b75243ecfb700239e3f40 --- /dev/null +++ b/router/java/src/org/cybergarage/xml/Node.java @@ -0,0 +1,415 @@ +/****************************************************************** +* +* CyberXML for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: Element.java +* +* Revision; +* +* 11/27/02 +* - first revision. +* 11/01/03 +* - Terje Bakken +* - fixed missing escaping of reserved XML characters +* 11/19/04 +* - Theo Beisch <theo.beisch@gmx.de> +* - Added "&" and "\"" "\\" to toXMLString(). +* 11/19/04 +* - Theo Beisch <theo.beisch@gmx.de> +* - Changed XML::output() to use short notation when the tag value is null. +* 12/02/04 +* - Brian Owens <brian@b-owens.com> +* - 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<nIndentLevel; n++) + indentString[n] = '\t' ; + return new String(indentString); + } + + public void outputAttributes(PrintWriter ps) + { + int nAttributes = getNAttributes(); + for (int n=0; n<nAttributes; n++) { + Attribute attr = getAttribute(n); + ps.print(" " + attr.getName() + "=\"" + XML.escapeXMLChars(attr.getValue()) + "\""); + } + } + + public void output(PrintWriter ps, int indentLevel, boolean hasChildNode) + { + String indentString = getIndentLevelString(indentLevel); + + String name = getName(); + String value = getValue(); + + if (hasNodes() == false || hasChildNode == false) { + ps.print(indentString + "<" + name); + outputAttributes(ps); + // Thnaks for Tho Beisch (11/09/04) + if (value == null || value.length() == 0) { + // No value, so use short notation <node /> + ps.println(" />"); + } else { + ps.println(">" + XML.escapeXMLChars(value) + "</" + name + ">"); + } + + return; + } + + ps.print(indentString + "<" + name); + outputAttributes(ps); + ps.println(">"); + + int nChildNodes = getNNodes(); + for (int n=0; n<nChildNodes; n++) { + Node cnode = getNode(n); + cnode.output(ps, indentLevel+1, true); + } + + ps.println(indentString +"</" + name + ">"); + } + + 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 0000000000000000000000000000000000000000..53ac9bce88394beb4a88493a8f7be20f07271f0b --- /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<nLists; n++) { + Node node = getNode(n); + String nodeName = node.getName(); + if (name.compareTo(nodeName) == 0) + return node; + } + return null; + } + + public Node getEndsWith(String name) + { + if (name == null) + return null; + + int nLists = size(); + for (int n=0; n<nLists; n++) { + Node node = getNode(n); + String nodeName = node.getName(); + if (nodeName == null) + continue; + if (nodeName.endsWith(name) == true) + return node; + } + return null; + } +} + diff --git a/router/java/src/org/cybergarage/xml/Parser.java b/router/java/src/org/cybergarage/xml/Parser.java new file mode 100644 index 0000000000000000000000000000000000000000..d93cf48a211b9e1294d69b6b78a3f72971f781fc --- /dev/null +++ b/router/java/src/org/cybergarage/xml/Parser.java @@ -0,0 +1,110 @@ +/****************************************************************** +* +* CyberXML for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: Parser.java +* +* Revision; +* +* 11/26/03 +* - first revision. +* 03/30/05 +* - Change parse(String) to use StringBufferInputStream instead of URL. +* +******************************************************************/ + +package org.cybergarage.xml; + +import java.net.*; +import java.io.*; + +public abstract class Parser +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public Parser() + { + } + + //////////////////////////////////////////////// + // parse + //////////////////////////////////////////////// + + public abstract Node parse(InputStream inStream) throws ParserException; + + //////////////////////////////////////////////// + // parse (URL) + //////////////////////////////////////////////// + + public Node parse(URL locationURL) throws ParserException + { + try { + HttpURLConnection urlCon = (HttpURLConnection)locationURL.openConnection(); + urlCon.setRequestMethod("GET"); + InputStream urlIn = urlCon.getInputStream(); + + Node rootElem = parse(urlIn); + + urlIn.close(); + urlCon.disconnect(); + + return rootElem; + + } catch (Exception e) { + throw new ParserException(e); + } + /* + String host = locationURL.getHost(); + int port = locationURL.getPort(); + String uri = locationURL.getPath(); + HTTPRequest httpReq = new HTTPRequest(); + httpReq.setMethod(HTTP.GET); + httpReq.setURI(uri); + HTTPResponse httpRes = httpReq.post(host, port); + if (httpRes.isSuccessful() == false) + throw new ParserException(locationURL.toString()); + String content = new String(httpRes.getContent()); + StringBufferInputStream strBuf = new StringBufferInputStream(content); + return parse(strBuf); + */ + } + + //////////////////////////////////////////////// + // parse (File) + //////////////////////////////////////////////// + + public Node parse(File descriptionFile) throws ParserException + { + try { + InputStream fileIn = new FileInputStream(descriptionFile); + Node root = parse(fileIn); + fileIn.close(); + return root; + + } catch (Exception e) { + throw new ParserException(e); + } + } + + //////////////////////////////////////////////// + // parse (Memory) + //////////////////////////////////////////////// + + public Node parse(String descr) throws ParserException + { + try { + StringBufferInputStream decrIn = new StringBufferInputStream(descr); + Node root = parse(decrIn); + return root; + } catch (Exception e) { + throw new ParserException(e); + } + } + +} + + diff --git a/router/java/src/org/cybergarage/xml/ParserException.java b/router/java/src/org/cybergarage/xml/ParserException.java new file mode 100644 index 0000000000000000000000000000000000000000..eba4f67937fd8284703c5726726284e8be2fc384 --- /dev/null +++ b/router/java/src/org/cybergarage/xml/ParserException.java @@ -0,0 +1,33 @@ +/****************************************************************** +* +* CyberXML for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ParserException.java +* +* Revision; +* +* 11/27/02 +* - first revision. +* 12/26/03 +* - Changed to a sub class of Exception instead of SAXException. +* +******************************************************************/ + +package org.cybergarage.xml; + +public class ParserException extends Exception +{ + private static final long serialVersionUID = 7443033796560597360L; + + public ParserException(Exception e) + { + super(e); + } + + public ParserException(String s) + { + super(s); + } +} \ No newline at end of file diff --git a/router/java/src/org/cybergarage/xml/XML.java b/router/java/src/org/cybergarage/xml/XML.java new file mode 100644 index 0000000000000000000000000000000000000000..9f62cb9d75cb213b77ef899f846aa8ad0e952d83 --- /dev/null +++ b/router/java/src/org/cybergarage/xml/XML.java @@ -0,0 +1,65 @@ +/****************************************************************** +* +* CyberXML for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: XML.java +* +* Revision; +* +* 01/05/03 +* - first revision. +* 12/15/03 +* - Terje Bakken +* - Added escapeXMLChars() +* +******************************************************************/ + +package org.cybergarage.xml; + +public class XML +{ + public final static String CONTENT_TYPE = "text/xml; charset=\"utf-8\""; + + //////////////////////////////////////////////// + // escapeXMLChars + //////////////////////////////////////////////// + + private final static String escapeXMLChars(String input, boolean quote) + { + StringBuilder out = new StringBuilder(); + if (input == null) + return null; + int oldsize=input.length(); + char[] old=new char[oldsize]; + input.getChars(0,oldsize,old,0); + int selstart = 0; + String entity=null; + for (int i=0;i<oldsize;i++) { + switch (old[i]) { + case '&': entity="&"; break; + case '<': entity="<"; break; + case '>': 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 0000000000000000000000000000000000000000..8de6b06b89869a47ebda8d4bd68800cdafea753e --- /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<attrLen; n++) { + org.w3c.dom.Node attr = attrMap.item(n); + String attrName = attr.getNodeName(); + String attrValue = attr.getNodeValue(); + node.setAttribute(attrName, attrValue); + } + + org.w3c.dom.Node child = domNode.getFirstChild(); + while (child != null) { + parse(node, child, rank+1); + child = child.getNextSibling(); + } + + return node; + } + + public org.cybergarage.xml.Node parse(org.cybergarage.xml.Node parentNode, org.w3c.dom.Node domNode) + { + return parse(parentNode, domNode, 0); + } + + /* (non-Javadoc) + * @see org.cybergarage.xml.Parser#parse(java.io.InputStream) + */ + public Node parse(InputStream inStream) throws ParserException + { + org.cybergarage.xml.Node root = null; + + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + InputSource inSrc = new InputSource(inStream); + Document doc = builder.parse(inSrc); + + org.w3c.dom.Element docElem = doc.getDocumentElement(); + + if (docElem != null) + root = parse(root, docElem); +/* + NodeList rootList = doc.getElementsByTagName("root"); + Debug.message("rootList = " + rootList.getLength()); + + if (0 < rootList.getLength()) + root = parse(root, rootList.item(0)); +*/ + } + catch (Exception e) { + throw new ParserException(e); + } + + return root; + } + +} diff --git a/router/java/src/org/freenetproject/DetectedIP.java b/router/java/src/org/freenetproject/DetectedIP.java new file mode 100644 index 0000000000000000000000000000000000000000..54bec74b76ddadec3016ac17cb4887091330df17 --- /dev/null +++ b/router/java/src/org/freenetproject/DetectedIP.java @@ -0,0 +1,68 @@ +/* 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 org.freenetproject; + +import java.net.InetAddress; + +/** + * Class returned by a FredPluginIPDetector. + * + * Indicates: + * - Whether there is no UDP connectivity at all. + * - Whether there is full inbound IP connectivity. + * - A list of detected public IPs. + */ +public class DetectedIP { + + public final InetAddress publicAddress; + public final short natType; + /** The MTU as advertized by the JVM */ + public int mtu; + // Constants + /** The plugin does not support detecting the NAT type. */ + public static final short NOT_SUPPORTED = 1; + /** Full internet access! */ + public static final short FULL_INTERNET = 2; + /** Full cone NAT. Once we have sent a packet out on a port, any node anywhere can send us + * a packet on that port. The nicest option, but very rare unfortunately. */ + public static final short FULL_CONE_NAT = 3; + /** Restricted cone NAT. Once we have sent a packet out to a specific IP, it can send us + * packets on the port we just used. */ + public static final short RESTRICTED_CONE_NAT = 4; + /** Port restricted cone NAT. Once we have sent a packet to a specific IP+Port, that IP+Port + * can send us packets on the port we just used. */ + public static final short PORT_RESTRICTED_NAT = 5; + /** Symmetric NAT. Uses a separate port number for each IP+port ! Not much hope for symmetric + * to symmetric... */ + public static final short SYMMETRIC_NAT = 6; + /** Symmetric UDP firewall. We are not NATed, but the firewall behaves as if we were. */ + public static final short SYMMETRIC_UDP_FIREWALL = 7; + /** No UDP connectivity at all */ + public static final short NO_UDP = 8; + + public DetectedIP(InetAddress addr, short type) { + this.publicAddress = addr; + this.natType = type; + this.mtu = 1500; + } + + @Override + public boolean equals(Object o) { + if(!(o instanceof DetectedIP)) { + return false; + } + DetectedIP d = (DetectedIP)o; + return ((d.natType == natType) && d.publicAddress.equals(publicAddress)); + } + + @Override + public int hashCode() { + return publicAddress.hashCode() ^ natType; + } + + @Override + public String toString() { + return publicAddress.toString()+":"+natType+":"+mtu; + } +} diff --git a/router/java/src/org/freenetproject/ForwardPort.java b/router/java/src/org/freenetproject/ForwardPort.java new file mode 100644 index 0000000000000000000000000000000000000000..5ddda51606fcd5e668a3b05a6b8118909fb95428 --- /dev/null +++ b/router/java/src/org/freenetproject/ForwardPort.java @@ -0,0 +1,45 @@ +package org.freenetproject; + +/** + * A public Internet Protocol port on the node which needs to be forwarded if the + * node is NATed. + * @author toad + */ +public class ForwardPort { + + /** Name of the interface e.g. "opennet" */ + public final String name; + /** IPv4 vs IPv6? */ + public final boolean isIP6; + /** Protocol number. See constants. */ + public final int protocol; + public static final int PROTOCOL_UDP_IPV4 = 17; + public static final int PROTOCOL_TCP_IPV4 = 6; + /** Port number to forward */ + public final int portNumber; + // We don't currently support binding to a specific internal interface. + // It would be complicated: Different interfaces may be on different LANs, + // and an IGD is normally on only one LAN. + private final int hashCode; + + public ForwardPort(String name, boolean isIP6, int protocol, int portNumber) { + this.name = name; + this.isIP6 = isIP6; + this.protocol = protocol; + this.portNumber = portNumber; + hashCode = name.hashCode() | (isIP6 ? 1 : 0) | protocol | portNumber; + } + + @Override + public int hashCode() { + return hashCode; + } + + @Override + public boolean equals(Object o) { + if(o == this) return true; + if(!(o instanceof ForwardPort)) return false; + ForwardPort f = (ForwardPort) o; + return (f.name.equals(name)) && f.isIP6 == isIP6 && f.protocol == protocol && f.portNumber == portNumber; + } +} diff --git a/router/java/src/org/freenetproject/ForwardPortCallback.java b/router/java/src/org/freenetproject/ForwardPortCallback.java new file mode 100644 index 0000000000000000000000000000000000000000..86240da1fef8915faf0b598fe3bb82d335a19ac5 --- /dev/null +++ b/router/java/src/org/freenetproject/ForwardPortCallback.java @@ -0,0 +1,14 @@ +package org.freenetproject; + +import java.util.Map; + +/** + * Callback called by port forwarding plugins to indicate success or failure. + * @author toad + */ +public interface ForwardPortCallback { + + /** Called to indicate status on one or more forwarded ports. */ + public void portForwardStatus(Map<ForwardPort,ForwardPortStatus> statuses); + +} diff --git a/router/java/src/org/freenetproject/ForwardPortStatus.java b/router/java/src/org/freenetproject/ForwardPortStatus.java new file mode 100644 index 0000000000000000000000000000000000000000..88090da403e6104193ce4e8e237f49f7cc18dc6c --- /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 0000000000000000000000000000000000000000..6fd39288bafb8cfc40f026bbb21184ba6734dd92 --- /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.<a href="XmlPullParserException.html">XmlPullParserException.html</a>; + * import plugins.JabberLinker.org.xmlpull.v1.<a href="XmlPullParserFactory.html">XmlPullParserFactory</a>; + * 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 0000000000000000000000000000000000000000..24a34d2de2849355cab9b374337f03fe17bc0388 --- /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 <a href="http://www.xmlpull.org/">XMLPULL V1 API</a> (visit this website to + * learn more about API and its implementations). + * + * <p>There are following different + * kinds of parser depending on which features are set:<ul> + * <li><b>non-validating</b> parser as defined in XML 1.0 spec when + * FEATURE_PROCESS_DOCDECL is set to true + * <li><b>validating parser</b> as defined in XML 1.0 spec when + * FEATURE_VALIDATION is true (and that implies that FEATURE_PROCESS_DOCDECL is true) + * <li>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 + * <em>no DOCDECL is present</em> in XML documents + * (internal entites can still be defined with defineEntityReplacementText()). + * This mode of operation is intened <b>for operation in constrained environments</b> such as J2ME. + * </ul> + * + * + * <p>There are two key methods: next() and nextToken(). While next() provides + * access to high level parsing events, nextToken() allows access to lower + * level tokens. + * + * <p>The current event state of the parser + * can be determined by calling the + * <a href="#getEventType()">getEventType()</a> method. + * Initially, the parser is in the <a href="#START_DOCUMENT">START_DOCUMENT</a> + * state. + * + * <p>The method <a href="#next()">next()</a> 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 (). + * + * <p>Th following event types are seen by next()<dl> + * <dt><a href="#START_TAG">START_TAG</a><dd> An XML start tag was read. + * <dt><a href="#TEXT">TEXT</a><dd> 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) + * <dt><a href="#END_TAG">END_TAG</a><dd> An end tag was read + * <dt><a href="#END_DOCUMENT">END_DOCUMENT</a><dd> No more events are available + * </dl> + * + * <p>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:<ul> + * <li><b>version</b>: + * getProperty("<a href="http://xmlpull.org/v1/doc/properties.html#xmldecl-version">http://xmlpull.org/v1/doc/properties.html#xmldecl-version</a>") + * returns String ("1.0") or null if XMLDecl was not read or if property is not supported + * <li><b>standalone</b>: + * getProperty("<a href="http://xmlpull.org/v1/doc/features.html#xmldecl-standalone">http://xmlpull.org/v1/doc/features.html#xmldecl-standalone</a>") + * 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" + * <li><b>encoding</b>: obtained from getInputEncoding() + * null if stream had unknown encoding (not set in setInputStream) + * and it was not declared in XMLDecl + * </ul> + * + * A minimal example for using this API may look as follows: + * <pre> + * import java.io.IOException; + * import java.io.StringReader; + * + * import plugins.JabberLinker.org.xmlpull.v1.XmlPullParser; + * import plugins.JabberLinker.org.xmlpull.v1.<a href="XmlPullParserException.html">XmlPullParserException.html</a>; + * import plugins.JabberLinker.org.xmlpull.v1.<a href="XmlPullParserFactory.html">XmlPullParserFactory</a>; + * + * public class SimpleXmlPullApp + * { + * + * public static void main (String args[]) + * throws XmlPullParserException, IOException + * { + * XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); + * factory.setNamespaceAware(true); + * XmlPullParser xpp = factory.newPullParser(); + * + * xpp.<a href="#setInput">setInput</a>( 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.<a href="#getName()">getName()</a>); + * } else if(eventType == XmlPullParser.END_TAG) { + * System.out.println("End tag "+xpp.getName()); + * } else if(eventType == XmlPullParser.TEXT) { + * System.out.println("Text "+xpp.<a href="#getText()">getText()</a>); + * } + * eventType = xpp.next(); + * } + * } + * } + * </pre> + * + * <p>The above example will generate the following output: + * <pre> + * Start document + * Start tag foo + * Text Hello World! + * End tag foo + * </pre> + * + * <p>For more details on API usage, please refer to the + * quick Introduction available at <a href="http://www.xmlpull.org">http://www.xmlpull.org</a> + * + * @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 <a href="http://www-ai.cs.uni-dortmund.de/PERSONAL/haustein.html">Stefan Haustein</a> + * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a> + */ + +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()</a>). + * + * @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. + * <p><strong>NOTE:</strong> calling again + * <a href="#next()">next()</a> or <a href="#nextToken()">nextToken()</a> + * will result in exception being thrown. + * + * @see #next + * @see #nextToken + */ + int END_DOCUMENT = 1; + + /** + * Returned from getEventType(), + * <a href="#next()">next()</a>, <a href="#nextToken()">nextToken()</a> 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 <a href='#FEATURE_PROCESS_NAMESPACES'>namespaces are enabled</a>. + * 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(), <a href="#next()">next()</a>, or + * <a href="#nextToken()">nextToken()</a> 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(). + * <p><strong>Please note:</strong> <a href="#next()">next()</a> will + * accumulate multiple + * events into one TEXT event, skipping IGNORABLE_WHITESPACE, + * PROCESSING_INSTRUCTION and COMMENT events, + * In contrast, <a href="#nextToken()">nextToken()</a> 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 <a href="#nextToken()">nextToken()</a>. + * 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 <a href="#nextToken()">nextToken()</a> + * 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 <a href="#nextToken()">nextToken()</a>). + * 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() + * + * <p><strong>NOTE:</strong> 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 <a href="#nextToken()">nextToken()</a>. + * 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 <a href="#nextToken()">nextToken()</a> 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 <a href="#nextToken()">nextToken()</a> 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. + * <p><strong>NOTE:</strong> 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. + * + * <p><strong>Please note:</strong> 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. + * <p><strong>Please Note:</strong> 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. + * <p>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. + * <p><strong>Please note:</strong> unknown features are + * <strong>always</strong> 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. + * <p><strong>NOTE:</strong> unknown properties are <strong>always</strong> + * 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. + * + * <p><strong>NOTE:</strong> 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 + * <a href="http://xmlpull.org/v1/doc/features.html#detect-encoding">http://xmlpull.org/v1/doc/features.html#detect-encoding</a> + * 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 + * <a href="http://www.w3.org/TR/REC-xml#intern-replacement">XML 1.0 Section 4.5 + * Construction of Internal Entity Replacement Text</a>. + * 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. + * + * <p>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. + * + * <p><b>Please notes:</b> 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. + * + * <p><b>Note:</b> 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). + * + * <p><b>Note:</b> 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. + * + * <p><b>NOTE:</b> 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. + * <p><b>NOTE:</b> to retrieve lsit of namespaces declared in current element:<pre> + * 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); + * // ... + * } + * </pre> + * + * @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. + * <p><b>Please note:</b> 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. + * <p><b>NOTE:</b> 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. + * + * <p>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). + * + * <p>This method is a convenience method for + * + * <pre> + * for (int i = getNamespaceCount(getDepth ())-1; i >= 0; i--) { + * if (getNamespacePrefix(i).equals( prefix )) { + * return getNamespaceUri(i); + * } + * } + * return null; + * </pre> + * + * <p><strong>Please note:</strong> 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 + * <a href="http://www.w3.org/TR/REC-xml-names/#ns-using">Namespaces in XML</a> + * specification. Analogous, the 'xmlns' prefix is resolved to + * <a href="http://www.w3.org/2000/xmlns/">http://www.w3.org/2000/xmlns/</a> + * + * @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. + * + * <pre> + * <!-- outside --> 0 + * <root> 1 + * sometext 1 + * <foobar> 2 + * </foobar> 2 + * </root> 1 + * <!-- outside --> 0 + * </pre> + */ + 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. + * + * <p><b>Please note:</b> 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. + * + * <p><strong>NOTE:</strong> 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. + * + * <p><strong>Please note:</strong> 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. + * <p><b>Please note:</b> 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/>). + * <p><b>NOTE:</b> 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. + * + * <p><strong>NOTE:</strong> if FEATURE_REPORT_NAMESPACE_ATTRIBUTES is set + * then namespace attributes (xmlns:ns='...') must be reported + * with namespace + * <a href="http://www.w3.org/2000/xmlns/">http://www.w3.org/2000/xmlns/</a> + * (visit this URL for description!). + * The default namespace attribute (xmlns="...") will be reported with empty namespace. + * <p><strong>NOTE:</strong>The xml prefix is bound as defined in + * <a href="http://www.w3.org/TR/REC-xml-names/#ns-using">Namespaces in XML</a> + * 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. + * + * <p><strong>NOTE:</strong> attribute value must be normalized + * (including entity replacement text if PROCESS_DOCDECL is false) as described in + * <a href="http://www.w3.org/TR/REC-xml#AVNormalize">XML 1.0 section + * 3.3.3 Attribute-Value Normalization</a> + * + * @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. + * + * <p><strong>NOTE:</strong> attribute value must be normalized + * (including entity replacement text if PROCESS_DOCDECL is false) as described in + * <a href="http://www.w3.org/TR/REC-xml#AVNormalize">XML 1.0 section + * 3.3.3 Attribute-Value Normalization</a> + * + * @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. + * + * <p><b>NOTE:</b> 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. + * + * <p>If special feature + * <a href="http://xmlpull.org/v1/doc/features.html#xml-roundtrip">FEATURE_XML_ROUNDTRIP</a> + * (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 + * <a href="http://www.w3.org/TR/REC-xml#sec-line-ends">XML 1.0 End-of-Line Handling</a> + * and. Also when this feature is enabled exact content of START_TAG, END_TAG, + * DOCDECL and PROCESSING_INSTRUCTION is available. + * + * <p>Here is the list of tokens that can be returned from nextToken() + * and what getText() and getTextCharacters() returns:<dl> + * <dt>START_DOCUMENT<dd>null + * <dt>END_DOCUMENT<dd>null + * <dt>START_TAG<dd>null unless FEATURE_XML_ROUNDTRIP + * enabled and then returns XML tag, ex: <tag attr='val'> + * <dt>END_TAG<dd>null unless FEATURE_XML_ROUNDTRIP + * id enabled and then returns XML tag, ex: </tag> + * <dt>TEXT<dd>return element content. + * <br>Note: that element content may be delivered in multiple consecutive TEXT events. + * <dt>IGNORABLE_WHITESPACE<dd>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. + * <br>Note: that element content may be delevered in multiple consecutive IGNORABLE_WHITESPACE events. + * <dt>CDSECT<dd> + * return text <em>inside</em> CDATA + * (ex. 'fo<o' from <!CDATA[fo<o]]>) + * <dt>PROCESSING_INSTRUCTION<dd> + * 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. + * <dt>COMMENT<dd>return comment content ex. 'foo bar' from <!--foo bar--> + * <dt>ENTITY_REF<dd>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;). + * <br><b>NOTE:</b> this is the only place where value returned from getText() and + * getTextCharacters() <b>are different</b> + * <br><b>NOTE:</b> 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) + * <br><b>NOTE:</b> character entities (ex. &#32;) and standard entities such as + * &amp; &lt; &gt; &quot; &apos; are reported as well + * and are <b>not</b> reported as TEXT tokens but as ENTITY_REF tokens! + * This requirement is added to allow to do roundtrip of XML documents! + * <dt>DOCDECL<dd> + * if FEATURE_XML_ROUNDTRIP is true or PROCESS_DOCDECL is false + * then return what is inside of DOCDECL for example it returns:<pre> + * " titlepage SYSTEM "http://www.foo.bar/dtds/typo.dtd" + * [<!ENTITY % active.links "INCLUDE">]"</pre> + * <p>for input document that contained:<pre> + * <!DOCTYPE titlepage SYSTEM "http://www.foo.bar/dtds/typo.dtd" + * [<!ENTITY % active.links "INCLUDE">]></pre> + * otherwise if FEATURE_XML_ROUNDTRIP is false and PROCESS_DOCDECL is true + * then what is returned is undefined (it may be even null) + * </dd> + * </dl> + * + * <p><strong>NOTE:</strong> 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) + * + * <p><strong>NOTE:</strong> whether returned text of token is end-of-line normalized + * is depending on FEATURE_XML_ROUNDTRIP. + * + * <p><strong>NOTE:</strong> 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. + * + * <p>Essentially it does this + * <pre> + * if (type != getEventType() + * || (namespace != null && !namespace.equals( getNamespace () ) ) + * || (name != null && !name.equals( getName() ) ) ) + * throw new XmlPullParserException( "expected "+ TYPES[ type ]+getPositionDescription()); + * </pre> + */ + 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. + * + * <p>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: <ol> + * <li><tag>foo</tag> + * <li><tag></tag> (which is equivalent to <tag/> + * both input can be parsed with the same code: + * <pre> + * p.nextTag() + * p.requireEvent(p.START_TAG, "", "tag"); + * String content = p.nextText(); + * p.requireEvent(p.END_TAG, "", "tag"); + * </pre> + * This function together with nextTag make it very easy to parse XML that has + * no mixed content. + * + * + * <p>Essentially it does this + * <pre> + * 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); + * } + * </pre> + */ + 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. + * + * <p>essentially it does this + * <pre> + * 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; + * </pre> + */ + 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 0000000000000000000000000000000000000000..0faba7e20230c3eae505b2c31731813748fcbc5b --- /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 <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a> + */ +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 0000000000000000000000000000000000000000..af6c27adb8a5547a70084f2bec045cf1fdd11cdf --- /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. + * + * <br /><strong>NOTE:</strong>In J2SE or J2EE environments, you may want to use + * <code>newInstance(property, classLoaderCtx)</code> + * where first argument is + * <code>System.getProperty(XmlPullParserFactory.PROPERTY_NAME)</code> + * and second is <code>Thread.getContextClassLoader().getClass()</code> . + * + * @see XmlPullParser + * + * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a> + * @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. + * <p><b>NOTE:</b> 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. + * <p><b>NOTE:</b> 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 <string>always</strong> 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. + * + * <p><b>NOTE:</b> 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 0000000000000000000000000000000000000000..8e85e2f0e264e0fefd77e2d13bf9cbcb50434f79 --- /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). + * + * <p><b>PLEASE NOTE:</b> This interface will be part of XmlPull 1.2 API. + * It is included as basis for discussion. It may change in any way. + * + * <p>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): + * <ul> + * <li><em>IllegalArgumentException</em> - for almost all methods to signal that + * argument is illegal + * <li><em>IllegalStateException</em> - 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) + * </ul> + * + * <p><b>NOTE:</b> 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 + * <a href="http://www.xmlpull.org/v1/doc/features.html"> + * http://www.xmlpull.org/v1/doc/features.html</a>. + * + * 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. + * <p><strong>NOTE:</strong> unknown properties are <strong>always</strong> 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 + * <a href="http://www.xmlpull.org/v1/doc/properties.html"> + * http://www.xmlpull.org/v1/doc/properties.html</a>. + * + * 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 + * <p><strong>NOTE:</strong> unknown properties are <string>always</strong> 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. + * <p><b>WARNING</b> 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 <code>xmlns:prefix='namespace'</code> + * (or <code>xmlns:prefix="namespace"</code> depending what character is used + * to quote attribute value). + * + * <p><b>NOTE:</b> this method MUST be called directly before startTag() + * and if anything but startTag() or setPrefix() is called next there will be exception. + * <p><b>NOTE:</b> prefixes "xml" and "xmlns" are already bound + * and can not be redefined see: + * <a href="http://www.w3.org/XML/xml-names-19990114-errata#NE05">Namespaces in XML Errata</a>. + * <p><b>NOTE:</b> 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. + * + * <p><b>NOTE:</b> if the prefix is empty string "" and defualt namespace is bound + * to this prefix then empty string ("") is returned. + * + * <p><b>NOTE:</b> prefixes "xml" and "xmlns" are already bound + * will have values as defined + * <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML specification</a> + */ + 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. + * + * <pre> + * <!-- outside --> 0 + * <root> 1 + * sometext 1 + * <foobar> 2 + * </foobar> 2 + * </root> 1 + * <!-- outside --> 0 + * </pre> + */ + int getDepth(); + + /** + * Returns the namespace URI of the current element as set by startTag(). + * + * <p><b>NOTE:</b> that measn in particaulr that: <ul> + * <li>if there was startTag("", ...) then getNamespace() returns "" + * <li>if there was startTag(null, ...) then getNamespace() returns null + * </ul> + * + * @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. + * <p><b>Background:</b> 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. + // * <br />If there is no prefix defined (prefix == null) for the given namespace, + // * a prefix will be defined automatically. + // * <br />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. + // * <br />If namespace is null then prefix must be null too or IllegalStateException is thrown. + // * <br />If namespace is null then no namespace prefix is printed but just name. + // * <br />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. + // * <br />If there is no prefix defined (prefix == null) for the given namespace, + // * a prefix will be defined automatically. + // * <br />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. + // * <br />If namespace is null then prefix must be null too or IllegalStateException is thrown. + // * <br />If namespace is null then no namespace prefix is printed but just name. + // * <br />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. + // * <br />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. + // * <br />If namespace is null then prefix must be null too or IllegalStateException is thrown. + // * <br />If namespace is null then no namespace prefix is printed but just name. + // * <br />If namespace is empty string then serializer will make sure that + // * default empty namespace is declared (in XML 1.0 xmlns=''). + // * <p><b>Background:</b> in kXML endTag had no arguments, and non matching tags were + // * very difficult to find...</p> + // */ + // 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. + * + * <p><b>NOTE:</b> 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; + +} +