Author: nextgens Date: 2007-03-26 00:53:45 +0000 (Mon, 26 Mar 2007) New Revision: 12375
Added: trunk/plugins/UPnP/ trunk/plugins/UPnP/UPnP.java trunk/plugins/UPnP/asl2.0_xmlpull.txt trunk/plugins/UPnP/bsd_clinkjava.txt trunk/plugins/UPnP/gpl.txt trunk/plugins/UPnP/org/ trunk/plugins/UPnP/org/cybergarage/ trunk/plugins/UPnP/org/cybergarage/http/ trunk/plugins/UPnP/org/cybergarage/http/Date.java trunk/plugins/UPnP/org/cybergarage/http/HTML.java trunk/plugins/UPnP/org/cybergarage/http/HTTP.java trunk/plugins/UPnP/org/cybergarage/http/HTTPHeader.java trunk/plugins/UPnP/org/cybergarage/http/HTTPPacket.java trunk/plugins/UPnP/org/cybergarage/http/HTTPRequest.java trunk/plugins/UPnP/org/cybergarage/http/HTTPRequestListener.java trunk/plugins/UPnP/org/cybergarage/http/HTTPResponse.java trunk/plugins/UPnP/org/cybergarage/http/HTTPServer.java trunk/plugins/UPnP/org/cybergarage/http/HTTPServerList.java trunk/plugins/UPnP/org/cybergarage/http/HTTPServerThread.java trunk/plugins/UPnP/org/cybergarage/http/HTTPSocket.java trunk/plugins/UPnP/org/cybergarage/http/HTTPStatus.java trunk/plugins/UPnP/org/cybergarage/http/Parameter.java trunk/plugins/UPnP/org/cybergarage/http/ParameterList.java trunk/plugins/UPnP/org/cybergarage/net/ trunk/plugins/UPnP/org/cybergarage/net/HostInterface.java trunk/plugins/UPnP/org/cybergarage/soap/ trunk/plugins/UPnP/org/cybergarage/soap/SOAP.java trunk/plugins/UPnP/org/cybergarage/soap/SOAPRequest.java trunk/plugins/UPnP/org/cybergarage/soap/SOAPResponse.java trunk/plugins/UPnP/org/cybergarage/upnp/ trunk/plugins/UPnP/org/cybergarage/upnp/Action.java trunk/plugins/UPnP/org/cybergarage/upnp/ActionList.java trunk/plugins/UPnP/org/cybergarage/upnp/AllowedValue.java trunk/plugins/UPnP/org/cybergarage/upnp/AllowedValueList.java trunk/plugins/UPnP/org/cybergarage/upnp/AllowedValueRange.java trunk/plugins/UPnP/org/cybergarage/upnp/Argument.java trunk/plugins/UPnP/org/cybergarage/upnp/ArgumentList.java trunk/plugins/UPnP/org/cybergarage/upnp/ControlPoint.java trunk/plugins/UPnP/org/cybergarage/upnp/Device.java trunk/plugins/UPnP/org/cybergarage/upnp/DeviceList.java trunk/plugins/UPnP/org/cybergarage/upnp/Icon.java trunk/plugins/UPnP/org/cybergarage/upnp/IconList.java trunk/plugins/UPnP/org/cybergarage/upnp/Service.java trunk/plugins/UPnP/org/cybergarage/upnp/ServiceList.java trunk/plugins/UPnP/org/cybergarage/upnp/ServiceStateTable.java trunk/plugins/UPnP/org/cybergarage/upnp/StateVariable.java trunk/plugins/UPnP/org/cybergarage/upnp/UPnP.java trunk/plugins/UPnP/org/cybergarage/upnp/UPnPStatus.java trunk/plugins/UPnP/org/cybergarage/upnp/control/ trunk/plugins/UPnP/org/cybergarage/upnp/control/ActionListener.java trunk/plugins/UPnP/org/cybergarage/upnp/control/ActionRequest.java trunk/plugins/UPnP/org/cybergarage/upnp/control/ActionResponse.java trunk/plugins/UPnP/org/cybergarage/upnp/control/Control.java trunk/plugins/UPnP/org/cybergarage/upnp/control/ControlRequest.java trunk/plugins/UPnP/org/cybergarage/upnp/control/ControlResponse.java trunk/plugins/UPnP/org/cybergarage/upnp/control/QueryListener.java trunk/plugins/UPnP/org/cybergarage/upnp/control/QueryRequest.java trunk/plugins/UPnP/org/cybergarage/upnp/control/QueryResponse.java trunk/plugins/UPnP/org/cybergarage/upnp/control/RenewSubscriber.java trunk/plugins/UPnP/org/cybergarage/upnp/device/ trunk/plugins/UPnP/org/cybergarage/upnp/device/Advertiser.java trunk/plugins/UPnP/org/cybergarage/upnp/device/Description.java trunk/plugins/UPnP/org/cybergarage/upnp/device/DeviceChangeListener.java trunk/plugins/UPnP/org/cybergarage/upnp/device/Disposer.java trunk/plugins/UPnP/org/cybergarage/upnp/device/InvalidDescriptionException.java trunk/plugins/UPnP/org/cybergarage/upnp/device/MAN.java trunk/plugins/UPnP/org/cybergarage/upnp/device/NT.java trunk/plugins/UPnP/org/cybergarage/upnp/device/NTS.java trunk/plugins/UPnP/org/cybergarage/upnp/device/NotifyListener.java trunk/plugins/UPnP/org/cybergarage/upnp/device/ST.java trunk/plugins/UPnP/org/cybergarage/upnp/device/SearchListener.java trunk/plugins/UPnP/org/cybergarage/upnp/device/SearchResponseListener.java trunk/plugins/UPnP/org/cybergarage/upnp/device/USN.java trunk/plugins/UPnP/org/cybergarage/upnp/event/ trunk/plugins/UPnP/org/cybergarage/upnp/event/EventListener.java trunk/plugins/UPnP/org/cybergarage/upnp/event/NotifyRequest.java trunk/plugins/UPnP/org/cybergarage/upnp/event/Property.java trunk/plugins/UPnP/org/cybergarage/upnp/event/PropertyList.java trunk/plugins/UPnP/org/cybergarage/upnp/event/Subscriber.java trunk/plugins/UPnP/org/cybergarage/upnp/event/SubscriberList.java trunk/plugins/UPnP/org/cybergarage/upnp/event/Subscription.java trunk/plugins/UPnP/org/cybergarage/upnp/event/SubscriptionRequest.java trunk/plugins/UPnP/org/cybergarage/upnp/event/SubscriptionResponse.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/HTTPMUSocket.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/HTTPUSocket.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDP.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPNotifyRequest.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPNotifySocket.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPNotifySocketList.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPPacket.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPRequest.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPResponse.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchRequest.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchResponse.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocket.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocketList.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchSocket.java trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchSocketList.java trunk/plugins/UPnP/org/cybergarage/upnp/xml/ trunk/plugins/UPnP/org/cybergarage/upnp/xml/ActionData.java trunk/plugins/UPnP/org/cybergarage/upnp/xml/ArgumentData.java trunk/plugins/UPnP/org/cybergarage/upnp/xml/DeviceData.java trunk/plugins/UPnP/org/cybergarage/upnp/xml/NodeData.java trunk/plugins/UPnP/org/cybergarage/upnp/xml/ServiceData.java trunk/plugins/UPnP/org/cybergarage/upnp/xml/StateVariableData.java trunk/plugins/UPnP/org/cybergarage/util/ trunk/plugins/UPnP/org/cybergarage/util/Debug.java trunk/plugins/UPnP/org/cybergarage/util/FileUtil.java trunk/plugins/UPnP/org/cybergarage/util/ListenerList.java trunk/plugins/UPnP/org/cybergarage/util/Mutex.java trunk/plugins/UPnP/org/cybergarage/util/StringUtil.java trunk/plugins/UPnP/org/cybergarage/util/ThreadCore.java trunk/plugins/UPnP/org/cybergarage/util/TimerUtil.java trunk/plugins/UPnP/org/cybergarage/xml/ trunk/plugins/UPnP/org/cybergarage/xml/Attribute.java trunk/plugins/UPnP/org/cybergarage/xml/AttributeList.java trunk/plugins/UPnP/org/cybergarage/xml/Node.java trunk/plugins/UPnP/org/cybergarage/xml/NodeList.java trunk/plugins/UPnP/org/cybergarage/xml/Parser.java trunk/plugins/UPnP/org/cybergarage/xml/ParserException.java trunk/plugins/UPnP/org/cybergarage/xml/XML.java trunk/plugins/UPnP/org/cybergarage/xml/parser/ trunk/plugins/UPnP/org/cybergarage/xml/parser/JaxpParser.java trunk/plugins/UPnP/org/xmlpull/ trunk/plugins/UPnP/org/xmlpull/v1/ trunk/plugins/UPnP/org/xmlpull/v1/XmlPullParser.java trunk/plugins/UPnP/org/xmlpull/v1/XmlPullParserException.java trunk/plugins/UPnP/org/xmlpull/v1/XmlPullParserFactory.java trunk/plugins/UPnP/org/xmlpull/v1/XmlSerializer.java Log: plugins: UPnP: implement basic stuffs allowing to see the feasability of such a plugin... I've already spent my WE on it, and it doesn't work for me! :( Added: trunk/plugins/UPnP/UPnP.java =================================================================== --- trunk/plugins/UPnP/UPnP.java (rev 0) +++ trunk/plugins/UPnP/UPnP.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,182 @@ +/* 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 plugins.UPnP; + +import java.util.LinkedList; +import plugins.UPnP.org.cybergarage.upnp.ControlPoint; +import plugins.UPnP.org.cybergarage.upnp.Device; +import plugins.UPnP.org.cybergarage.upnp.DeviceList; +import plugins.UPnP.org.cybergarage.upnp.Service; +import plugins.UPnP.org.cybergarage.upnp.ServiceList; +import plugins.UPnP.org.cybergarage.upnp.ServiceStateTable; +import plugins.UPnP.org.cybergarage.upnp.StateVariable; +import plugins.UPnP.org.cybergarage.upnp.device.DeviceChangeListener; +import freenet.pluginmanager.FredPlugin; +import freenet.pluginmanager.FredPluginHTTP; +import freenet.pluginmanager.FredPluginThreadless; +import freenet.pluginmanager.PluginHTTPException; +import freenet.pluginmanager.PluginRespirator; +import freenet.support.api.HTTPRequest; + +/** + * This plugin implements UP&P support on a freenet node. + * + * @author Florent Daignière <nextgens at freenetproject.org> + * + * @see http://www.upnp.org/ + * @see http://en.wikipedia.org/wiki/Universal_Plug_and_Play + * @see http://azureus.sourceforge.net/ + */ +public class UPnP implements FredPluginHTTP, FredPlugin, FredPluginThreadless, DeviceChangeListener { + private ControlPoint upnpControlPoint; + private final LinkedList igdList = new LinkedList(); + + public void runPlugin(PluginRespirator pr) { + upnpControlPoint = new ControlPoint(); + upnpControlPoint.addDeviceChangeListener(this); + upnpControlPoint.start(); + } + + public void terminate() { + upnpControlPoint.stop(); + } + + public void deviceAdded(Device dev ){ + if(!"InternetGatewayDevice".equals(dev.getDeviceType())) + return; + + System.out.println("##################Detected a new gateway ! "+dev.getFriendlyName()); + igdList.add(dev); + } + + public void deviceRemoved(Device dev ){ + igdList.remove(dev); + } + + private void listStateTable(Service serv, StringBuffer 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 listSubServices(Device dev, StringBuffer 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())){ + StateVariable linkStatus = serv.getStateVariable("PhysicalLinkStatus"); + StateVariable wanAccessType = serv.getStateVariable("WANAccessType"); + StateVariable upstreamBW = serv.getStateVariable("Layer1UpstreamMaxBitRate"); + StateVariable downstreamBW = serv.getStateVariable("Layer1DownstreamMaxBitRate"); + + sb.append("WANCommonInterfaceConfig" + + " status:" + linkStatus.getStateVariableData() + + " type:" + wanAccessType.getStateVariableData() + + " upstream:" + upstreamBW.getStateVariableData() + + " downstream:" + downstreamBW.getStateVariableData() + "<br>"); + }else if("urn:schemas-upnp-org:service:WANPPPConnection:1".equals(serv.getServiceType())){ + StateVariable linkStatus = serv.getStateVariable("ConnectionStatus"); + StateVariable uptime = serv.getStateVariable("Uptime"); + StateVariable upstreamBW = serv.getStateVariable("UpstreamMaxBitRate"); + StateVariable downstreamBW = serv.getStateVariable("DownstreamMaxBitRate"); + + sb.append("WANPPPConnection" + + " status:" + linkStatus.getStateVariableData() + + " uptime:" + uptime.getStateVariableData() + + " upstream:" + upstreamBW.getStateVariableData() + + " downstream:" + downstreamBW.getStateVariableData() + "<br>"); + }else if("urn:schemas-upnp-org:service:Layer3Forwarding:1".equals(serv.getServiceType())){ + StateVariable defaultConnectionService = serv.getStateVariable("DefaultConnectionService"); + sb.append("DefaultConnectionService: " +defaultConnectionService.getStateVariableData()); + }else if("urn:schemas-upnp-org:service:WANIPConnection:1".equals(serv.getServiceType())){ + StateVariable linkStatus = serv.getStateVariable("ConnectionStatus"); + StateVariable externalIPAddress = serv.getStateVariable("ExternalIPAddress"); + + sb.append("WANIPConnection" + + " status:" + linkStatus.getStateVariableData() + + " external IP:" + externalIPAddress.getStateVariableData() + "<br>"); + }else if("urn:schemas-upnp-org:service:WANEthernetLinkConfig:1".equals(serv.getServiceType())){ + StateVariable linkStatus = serv.getStateVariable("EthernetLinkStatus"); + + sb.append("WANEthernetLinkConfig" + + " status:" + linkStatus.getStateVariableData() + "<br>"); + }else if("urn:schemas-upnp-org:service:LANHostConfigManagement:1".equals(serv.getServiceType())){ + StateVariable netmask = serv.getStateVariable("SubnetMask"); + StateVariable dnsServers = serv.getStateVariable("DNSServers"); + + sb.append("LANHostConfigManagement" + + " subnetMask:" + netmask + + " dnsServers:" + dnsServers + "<br>"); + }else + sb.append("~~~~~~~ "+serv.getServiceType()); + listStateTable(serv, sb); + sb.append("</div>"); + } + } + + private void listSubDev(String prefix, Device dev, StringBuffer 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 { + StringBuffer sb = new StringBuffer(); + sb.append("<html><body>"); + sb.append(igdList.size() + "<br>"); + DeviceList rootDevList = upnpControlPoint.getDeviceList(); + + for(int i=0; i<rootDevList.size(); i++) { + Device dev = rootDevList.getDevice(i); + if(!"urn:schemas-upnp-org:device:InternetGatewayDevice:1".equals(dev.getDeviceType())) continue; + listSubDev("WANDevice", dev, sb); + + + //Service wanCommonInterfaceConfig = wan.getService("WANCommonInterfaceConfig"); + //sb.append("status:" + wanCommonInterfaceConfig.getStateVariable("PhysicalLinkStatus") + " type:" + wanCommonInterfaceConfig.getStateVariable("WANAccessType") + " upstream:" + wanCommonInterfaceConfig.getStateVariable("Layer1UpstreamMaxBitRate") + " downstream:" + wanCommonInterfaceConfig.getStateVariable("Layer1DownstreamMaxBitRate")); + Device wan = dev.getDevice("WANDevice"); + Device wanConnectionDevice = wan.getDevice("WANConnectionDevice"); + if(wanConnectionDevice == null) break; + System.out.println(wanConnectionDevice.getDeviceList().getDevice(0).getDeviceType()); + Device wanIPConnectionDevice = dev.getDevice("WANIPConnectionDevice"); + if(wanIPConnectionDevice == null) break; + System.out.println(wanIPConnectionDevice.getDeviceList().getDevice(0).getDeviceType()); + + StateVariable type = wanIPConnectionDevice.getStateVariable("ConnectionType"); + StateVariable ip = wanIPConnectionDevice.getStateVariable("ExternalIPAddress"); + + sb.append("[" + i + "] : " + dev.getFriendlyName() + " : "+ type.getValue() + ip.getValue() + "<br>"); + } + + sb.append("</body></html>"); + return sb.toString(); + } + + public String handleHTTPPost(HTTPRequest request) + throws PluginHTTPException { + // TODO Auto-generated method stub + return null; + } + + public String handleHTTPPut(HTTPRequest request) throws PluginHTTPException { + // TODO Auto-generated method stub + return null; + } +} Added: trunk/plugins/UPnP/asl2.0_xmlpull.txt =================================================================== --- trunk/plugins/UPnP/asl2.0_xmlpull.txt (rev 0) +++ trunk/plugins/UPnP/asl2.0_xmlpull.txt 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. Added: trunk/plugins/UPnP/bsd_clinkjava.txt =================================================================== --- trunk/plugins/UPnP/bsd_clinkjava.txt (rev 0) +++ trunk/plugins/UPnP/bsd_clinkjava.txt 2007-03-26 00:53:45 UTC (rev 12375) @@ -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. Added: trunk/plugins/UPnP/gpl.txt =================================================================== --- trunk/plugins/UPnP/gpl.txt (rev 0) +++ trunk/plugins/UPnP/gpl.txt 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + <signature of Ty Coon>, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. Added: trunk/plugins/UPnP/org/cybergarage/http/Date.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/Date.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/Date.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at gmx.de> +* - Fixed the following methods to use HOUR_OF_DAY instead of HOUR. +* getHour(), getDateString() getTimeString() +* - Fixed getInstance() to return GMT instance. +* +******************************************************************/ + +package plugins.UPnP.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)); + } + +} + Added: trunk/plugins/UPnP/org/cybergarage/http/HTML.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/HTML.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/HTML.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,22 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: HTML.java +* +* Revision; +* +* 01/05/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.http; + +public class HTML +{ + public static final String CONTENT_TYPE = "text/html; charset=\"utf-8\""; +} + Added: trunk/plugins/UPnP/org/cybergarage/http/HTTP.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/HTTP.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/HTTP.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at 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 at openshores.com> +* - Added Range and MYNAME; +* +******************************************************************/ + +package plugins.UPnP.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 { + URL url = 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 at 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; + } + +} + Added: trunk/plugins/UPnP/org/cybergarage/http/HTTPHeader.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/HTTPHeader.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/HTTPHeader.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at infotech.monash.edu.au> (05/26/04) +* - Fixed getValue() to compare using String::equals() instead of String::startWidth(). +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.http; + +import java.io.*; + +import plugins.UPnP.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 at 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; + } + } +} Added: trunk/plugins/UPnP/org/cybergarage/http/HTTPPacket.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/HTTPPacket.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/HTTPPacket.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at 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 at 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 at 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 at openshores.com> +* - Changed hasContentRange() to check Content-Range and Range header. +* - Added support for Range header to getContentRange(). +* 02/02/05 +* - Mark Retallack <mretallack at 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 plugins.UPnP.org.cybergarage.http; + +import java.io.*; +import java.util.*; + +import plugins.UPnP.org.cybergarage.net.*; +import plugins.UPnP.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 at 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(); + + StringBuffer contentBuf = new StringBuffer(); + + 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() + { + StringBuffer str = new StringBuffer(); + + 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; + String bytesStr = 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; + } +*/ +} + Added: trunk/plugins/UPnP/org/cybergarage/http/HTTPRequest.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/HTTPRequest.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/HTTPRequest.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,496 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: HTTPRequest.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* 05/23/03 +* - Giordano Sassaroli <sassarol at cefriel.it> +* - Add a relative URL check to setURI(). +* 09/02/03 +* - Giordano Sassaroli <sassarol at 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 at 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 plugins.UPnP.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 at 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 uriLen = uri.length(); + 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() + { + StringBuffer str = new StringBuffer(); + + 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() + { + StringBuffer str = new StringBuffer(); + + str.append(getHeader()); + str.append(HTTP.CRLF); + str.append(getContentString()); + + return str.toString(); + } + + public void print() + { + System.out.println(toString()); + } +} Added: trunk/plugins/UPnP/org/cybergarage/http/HTTPRequestListener.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/HTTPRequestListener.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/HTTPRequestListener.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,21 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: HTTPRequestListener.java +* +* Revision; +* +* 12/13/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.http; + +public interface HTTPRequestListener +{ + public void httpRequestRecieved(HTTPRequest httpReq); +} Added: trunk/plugins/UPnP/org/cybergarage/http/HTTPResponse.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/HTTPResponse.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/HTTPResponse.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.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() + { + StringBuffer str = new StringBuffer(); + + str.append(getStatusLineString()); + str.append(getHeaderString()); + + return str.toString(); + } + + //////////////////////////////////////////////// + // toString + //////////////////////////////////////////////// + + public String toString() + { + StringBuffer str = new StringBuffer(); + + str.append(getStatusLineString()); + str.append(getHeaderString()); + str.append(HTTP.CRLF); + str.append(getContentString()); + + return str.toString(); + } + + public void print() + { + System.out.println(toString()); + } +} Added: trunk/plugins/UPnP/org/cybergarage/http/HTTPServer.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/HTTPServer.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/HTTPServer.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,203 @@ +/****************************************************************** +* +* 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 plugins.UPnP.org.cybergarage.http; + +import java.io.*; +import java.net.*; + +import plugins.UPnP.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); + } + 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); + httpServerThread.start(); + return true; + } + + public boolean stop() + { + httpServerThread = null; + return true; + } +} Added: trunk/plugins/UPnP/org/cybergarage/http/HTTPServerList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/HTTPServerList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/HTTPServerList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,102 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: HTTPServerList.java +* +* Revision; +* +* 05/08/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.http; + +import java.util.*; + +import plugins.UPnP.org.cybergarage.net.*; + +public class HTTPServerList extends Vector +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + 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(); + } + } + +} + Added: trunk/plugins/UPnP/org/cybergarage/http/HTTPServerThread.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/HTTPServerThread.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/HTTPServerThread.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,53 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: HTTPServerThread.java +* +* Revision; +* +* 10/10/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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; + } + + //////////////////////////////////////////////// + // 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(); + } +} Added: trunk/plugins/UPnP/org/cybergarage/http/HTTPSocket.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/HTTPSocket.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/HTTPSocket.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.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); + } +} Added: trunk/plugins/UPnP/org/cybergarage/http/HTTPStatus.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/HTTPStatus.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/HTTPStatus.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at 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 plugins.UPnP.org.cybergarage.http; + +import java.util.*; + +import plugins.UPnP.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); + } + + } +} Added: trunk/plugins/UPnP/org/cybergarage/http/Parameter.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/Parameter.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/Parameter.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,61 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: Parameter.java +* +* Revision; +* +* 02/01/04 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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; + } +} + Added: trunk/plugins/UPnP/org/cybergarage/http/ParameterList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/http/ParameterList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/http/ParameterList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,58 @@ +/****************************************************************** +* +* CyberHTTP for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: ParameterList.java +* +* Revision; +* +* 02/01/04 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.http; + +import java.util.*; + +public class ParameterList extends Vector +{ + 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(); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/net/HostInterface.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/net/HostInterface.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/net/HostInterface.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,234 @@ +/****************************************************************** +* +* 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 plugins.UPnP.org.cybergarage.http to plugins.UPnP.org.cybergarage.net. +* 06/30/04 +* - Theo Beisch <theo.beisch at gmx.de> +* - Changed isUseAddress() to isUsableAddress(). +* +******************************************************************/ + +package plugins.UPnP.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); + if (addr instanceof Inet6Address) + return true; + return false; + } + catch (Exception e) {} + return false; + } + + public final static boolean isIPv4Address(String host) + { + try { + InetAddress addr = InetAddress.getByName(host); + if (addr instanceof Inet4Address) + return true; + return false; + } + 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; + } + +} Added: trunk/plugins/UPnP/org/cybergarage/soap/SOAP.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/soap/SOAP.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/soap/SOAP.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,81 @@ +/****************************************************************** +* +* CyberSOAP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SOAP.java +* +* Revision; +* +* 12/11/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.soap; + +import plugins.UPnP.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; + } +} + Added: trunk/plugins/UPnP/org/cybergarage/soap/SOAPRequest.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/soap/SOAPRequest.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/soap/SOAPRequest.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at Ber.gs>, Inma Marin Lopez <inma at 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 plugins.UPnP.org.cybergarage.soap; + +import java.io.*; + +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.org.cybergarage.xml.*; +import plugins.UPnP.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 at Ber.gs>, Inma Marin Lopez <inma at 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()); + } +} Added: trunk/plugins/UPnP/org/cybergarage/soap/SOAPResponse.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/soap/SOAPResponse.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/soap/SOAPResponse.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at Ber.gs>, Inma Marin Lopez <inma at 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 plugins.UPnP.org.cybergarage.soap; + +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.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 at Ber.gs>, Inma Marin Lopez <inma at 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()); + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/Action.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/Action.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/Action.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at 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 at users.sourceforge.net> and Stefano Lenzi <kismet-sl at users.sourceforge.net> +* - Changed postControlAction() to set the status code to the UPnPStatus. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp; + +import plugins.UPnP.org.cybergarage.xml.*; +import plugins.UPnP.org.cybergarage.util.*; + +import plugins.UPnP.org.cybergarage.upnp.xml.*; +import plugins.UPnP.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 at 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 at users.sourceforge.net> and Stefano Lenzi <kismet-sl at 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; + } + +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/ActionList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ActionList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ActionList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,45 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ActionList.java +* +* Revision: +* +* 12/05/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp; + +import java.util.*; + +public class ActionList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "actionList"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public ActionList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public Action getAction(int n) + { + return (Action)get(n); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/AllowedValue.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/AllowedValue.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/AllowedValue.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,70 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: AllowedValue.java +* +* Revision: +* +* 03/27/04 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp; + +import plugins.UPnP.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(); + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/AllowedValueList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/AllowedValueList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/AllowedValueList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,48 @@ +/****************************************************************** +* +* 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 plugins.UPnP.org.cybergarage.upnp; + +import java.util.*; + +public class AllowedValueList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "allowedValueList"; + + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public AllowedValueList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public AllowedValue getAllowedValue(int n) + { + return (AllowedValue)get(n); + } + +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/AllowedValueRange.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/AllowedValueRange.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/AllowedValueRange.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,104 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: AllowedValueRange.java +* +* Revision: +* +* 03/27/04 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp; + +import plugins.UPnP.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); + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/Argument.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/Argument.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/Argument.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.org.cybergarage.upnp; + +import plugins.UPnP.org.cybergarage.xml.*; + +import plugins.UPnP.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 + //////////////////////////////////////////////// +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/ArgumentList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ArgumentList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ArgumentList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,76 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ArgumentList.java +* +* Revision: +* +* 12/05/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp; + +import java.util.*; + +public class ArgumentList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + 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()); + } + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/ControlPoint.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ControlPoint.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ControlPoint.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at 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 at 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 plugins.UPnP.org.cybergarage.upnp; + +import plugins.UPnP.org.cybergarage.net.*; +import plugins.UPnP.org.cybergarage.util.*; +import plugins.UPnP.org.cybergarage.xml.*; +import plugins.UPnP.org.cybergarage.http.*; + +import plugins.UPnP.org.cybergarage.upnp.control.*; +import plugins.UPnP.org.cybergarage.upnp.ssdp.*; +import plugins.UPnP.org.cybergarage.upnp.device.*; +import plugins.UPnP.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 at 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()); + } + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/Device.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/Device.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/Device.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at 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 at 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 at users.sourceforge.net> +* - Added getParentDevice(). +* 10/20/04 +* - Brent Hills <bhills at openshores.com> +* - Changed postSearchResponse() to add MYNAME header. +* 11/19/04 +* - Theo Beisch <theo.beisch at 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 at dkab.net> +* Changed announce() and byebye() to close the socket after the posting. +* 04/25/05 +* - Thanks for Mikael Hakman <mhakman at dkab.net> +* Changed deviceSearchResponse() answer with USN:UDN::<device-type> when request ST is device type. +* 04/25/05 +* - Thanks for Mikael Hakman <mhakman at dkab.net> +* - Changed getDescriptionData() to add a XML declaration at first line. +* 04/25/05 +* - Thanks for Mikael Hakman <mhakman at dkab.net> +* - Added a new setActionListener() and serQueryListner() to include the sub devices. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp; + +import java.net.*; +import java.io.*; +import java.util.*; + +import plugins.UPnP.org.cybergarage.net.*; +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.org.cybergarage.util.*; +import plugins.UPnP.org.cybergarage.xml.*; +import plugins.UPnP.org.cybergarage.soap.*; + +import plugins.UPnP.org.cybergarage.upnp.ssdp.*; +import plugins.UPnP.org.cybergarage.upnp.device.*; +import plugins.UPnP.org.cybergarage.upnp.control.*; +import plugins.UPnP.org.cybergarage.upnp.event.*; +import plugins.UPnP.org.cybergarage.upnp.xml.*; + +public class Device implements plugins.UPnP.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 { + URL url = 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(); + } +*/ + +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/DeviceList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/DeviceList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/DeviceList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,45 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: DeviceList.java +* +* Revision; +* +* 12/04/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp; + +import java.util.*; + +public class DeviceList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "deviceList"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public DeviceList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public Device getDevice(int n) + { + return (Device)get(n); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/Icon.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/Icon.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/Icon.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,136 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: Icon.java +* +* Revision; +* +* 11/28/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp; + +import plugins.UPnP.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); + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/IconList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/IconList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/IconList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,45 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: IconList.java +* +* Revision; +* +* 12/04/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp; + +import java.util.*; + +public class IconList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "iconList"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public IconList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public Icon getIcon(int n) + { + return (Icon)get(n); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/Service.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/Service.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/Service.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,749 @@ +/****************************************************************** +* +* 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 at essex.ac.uk> +* - Fixed SERVICE_ID constant instead of "serviceId". +* 06/17/03 +* - Added notifyAllStateVariables(). +* 09/03/03 +* - Giordano Sassaroli <sassarol at 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 at dkab.net> +* - Changed getSCPDData() to add a XML declaration at first line. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp; + +import java.io.*; +import java.net.*; + +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.org.cybergarage.xml.*; +import plugins.UPnP.org.cybergarage.util.*; + +import plugins.UPnP.org.cybergarage.upnp.ssdp.*; +import plugins.UPnP.org.cybergarage.upnp.xml.*; +import plugins.UPnP.org.cybergarage.upnp.device.*; +import plugins.UPnP.org.cybergarage.upnp.control.*; +import plugins.UPnP.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 at 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() + { + Node serviceNode = getServiceNode(); + 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(); + String bindAddr = sub.getInterfaceAddress(); + + 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); + } + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/ServiceList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ServiceList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ServiceList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,52 @@ +/****************************************************************** +* +* 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 plugins.UPnP.org.cybergarage.upnp; + +import java.util.*; + +public class ServiceList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + 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; + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/ServiceStateTable.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ServiceStateTable.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ServiceStateTable.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,45 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ServiceStateTable.java +* +* Revision: +* +* 12/06/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp; + +import java.util.*; + +public class ServiceStateTable extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "serviceStateTable"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public ServiceStateTable() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public StateVariable getStateVariable(int n) + { + return (StateVariable)get(n); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/StateVariable.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/StateVariable.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/StateVariable.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,375 @@ +/****************************************************************** +* +* 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 at users.sourceforge.net> and Stefano Lenzi <kismet-sl at users.sourceforge.net> +* - Changed postQuerylAction() to set the status code to the UPnPStatus. +* 11/09/04 +* - Theo Beisch <theo.beisch at 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 at 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 plugins.UPnP.org.cybergarage.upnp; + +import plugins.UPnP.org.cybergarage.xml.*; +import plugins.UPnP.org.cybergarage.util.*; + +import plugins.UPnP.org.cybergarage.upnp.control.*; +import plugins.UPnP.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; + Node serviceNode = getServiceNode(); + 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 at users.sourceforge.net> and Stefano Lenzi <kismet-sl at 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; + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/UPnP.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/UPnP.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/UPnP.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.org.cybergarage.upnp; + +import plugins.UPnP.org.cybergarage.upnp.ssdp.*; +//import plugins.UPnP.org.cybergarage.util.*; +import plugins.UPnP.org.cybergarage.xml.*; +import plugins.UPnP.org.cybergarage.xml.parser.*; +import plugins.UPnP.org.cybergarage.soap.*; +import plugins.UPnP.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 + } + +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/UPnPStatus.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/UPnPStatus.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/UPnPStatus.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.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; + } + +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/control/ActionListener.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/control/ActionListener.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/control/ActionListener.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,23 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ActionListener.java +* +* Revision; +* +* 01/16/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.control; + +import plugins.UPnP.org.cybergarage.upnp.*; + +public interface ActionListener +{ + public boolean actionControlReceived(Action action); +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/control/ActionRequest.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/control/ActionRequest.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/control/ActionRequest.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.org.cybergarage.upnp.control; + +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.org.cybergarage.xml.*; +import plugins.UPnP.org.cybergarage.soap.*; + +import plugins.UPnP.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); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/control/ActionResponse.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/control/ActionResponse.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/control/ActionResponse.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at cefriel.it> +* - Problem : Action Responses do not contain the mandatory header field EXT +* - Error : ActionResponse class does not set the EXT header +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.control; + +import plugins.UPnP.org.cybergarage.upnp.*; +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.org.cybergarage.soap.*; +import plugins.UPnP.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; + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/control/Control.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/control/Control.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/control/Control.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,29 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: Control.java +* +* Revision; +* +* 01/20/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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"; +} + + Added: trunk/plugins/UPnP/org/cybergarage/upnp/control/ControlRequest.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/control/ControlRequest.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/control/ControlRequest.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at 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 at 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 at philips.com> +* - Fixed to set a URLBase from the SSDP header when the URLBase of the description is null. +* 02/18/04 +* - Andre <andre at 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 plugins.UPnP.org.cybergarage.upnp.control; + +import java.net.*; + +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.org.cybergarage.soap.*; + +import plugins.UPnP.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 at cefriel.it> (05/21/03) + setURI(ctrlURL, true); + + // Thanks for Giordano Sassaroli <sassarol at cefriel.it> and Suzan Foster (09/02/03) + // Thanks for Andre <andre at 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 at 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); + } + +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/control/ControlResponse.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/control/ControlResponse.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/control/ControlResponse.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,173 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ControlResponse.java +* +* Revision; +* +* 01/29/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.control; + +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.org.cybergarage.xml.*; +import plugins.UPnP.org.cybergarage.soap.*; + +import plugins.UPnP.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; + } + + private Node createFaultResponseNode(int errCode) + { + return createFaultResponseNode(errCode, UPnPStatus.code2String(errCode)); + } + + //////////////////////////////////////////////// + // 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; + } + +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/control/QueryListener.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/control/QueryListener.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/control/QueryListener.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.org.cybergarage.upnp.control; + +import plugins.UPnP.org.cybergarage.upnp.*; + +public interface QueryListener +{ + public boolean queryControlReceived(StateVariable stateVar); +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/control/QueryRequest.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/control/QueryRequest.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/control/QueryRequest.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,119 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: QueryRequest.java +* +* Revision; +* +* 01/29/03 +* - first revision. +* 09/02/03 +* - Giordano Sassaroli <sassarol at cefriel.it> +* - Error : redundant code, the setRequest method in QueryRequest invokes setURI even if after a couple of rows setRequestHost is invoked +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.control; + +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.org.cybergarage.xml.*; +import plugins.UPnP.org.cybergarage.soap.*; + +import plugins.UPnP.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(); + + String ctrlURL = service.getControlURL(); + + 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); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/control/QueryResponse.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/control/QueryResponse.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/control/QueryResponse.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,97 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: QueryResponse.java +* +* Revision; +* +* 01/30/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.control; + +import plugins.UPnP.org.cybergarage.upnp.*; +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.org.cybergarage.soap.*; +import plugins.UPnP.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; + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/control/RenewSubscriber.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/control/RenewSubscriber.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/control/RenewSubscriber.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,65 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: RenewSubscriber.java +* +* Revision: +* +* 07/07/04 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.control; + +import plugins.UPnP.org.cybergarage.util.*; +import plugins.UPnP.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(); + } + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/device/Advertiser.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/device/Advertiser.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/device/Advertiser.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.org.cybergarage.upnp.device; + +import plugins.UPnP.org.cybergarage.util.*; +import plugins.UPnP.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(); + } + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/device/Description.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/device/Description.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/device/Description.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,24 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: MAN.java +* +* Revision; +* +* 12/30/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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"; +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/device/DeviceChangeListener.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/device/DeviceChangeListener.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/device/DeviceChangeListener.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,27 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: DeviceChangeListener.java +* +* Revision; +* +* 09/12/04 +* - Oliver Newell <newell at media-rush.com> +* - Added this class to allow ControlPoint applications to +* be notified when the ControlPoint base class adds/removes +* a UPnP device +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.device; + +import plugins.UPnP.org.cybergarage.upnp.Device; + +public interface DeviceChangeListener +{ + public void deviceAdded( Device dev ); + public void deviceRemoved( Device dev ); +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/device/Disposer.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/device/Disposer.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/device/Disposer.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,65 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: Disposer.java +* +* Revision: +* +* 01/05/04 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.device; + +import plugins.UPnP.org.cybergarage.upnp.*; +import plugins.UPnP.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(); + } + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/device/InvalidDescriptionException.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/device/InvalidDescriptionException.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/device/InvalidDescriptionException.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,41 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: InvalidDescriptionException.java +* +* Revision; +* +* 12/26/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.device; + +import java.io.*; + +public class InvalidDescriptionException extends Exception +{ + 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()); + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/device/MAN.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/device/MAN.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/device/MAN.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,31 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: MAN.java +* +* Revision; +* +* 12/30/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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 + "\""); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/device/NT.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/device/NT.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/device/NT.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,30 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: NT.java +* +* Revision; +* +* 12/09/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/device/NTS.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/device/NTS.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/device/NTS.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,38 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: NTS.java +* +* Revision; +* +* 12/09/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/device/NotifyListener.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/device/NotifyListener.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/device/NotifyListener.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,23 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: DeviceNotifyListener.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.device; + +import plugins.UPnP.org.cybergarage.upnp.ssdp.*; + +public interface NotifyListener +{ + public void deviceNotifyReceived(SSDPPacket ssdpPacket); +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/device/ST.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/device/ST.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/device/ST.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,71 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: ST.java +* +* Revision; +* +* 01/07/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/device/SearchListener.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/device/SearchListener.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/device/SearchListener.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,23 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SearchListener.java +* +* Revision; +* +* 11/18/02b +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.device; + +import plugins.UPnP.org.cybergarage.upnp.ssdp.*; + +public interface SearchListener +{ + public void deviceSearchReceived(SSDPPacket ssdpPacket); +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/device/SearchResponseListener.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/device/SearchResponseListener.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/device/SearchResponseListener.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,23 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SearchResponseListener.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.device; + +import plugins.UPnP.org.cybergarage.upnp.ssdp.*; + +public interface SearchResponseListener +{ + public void deviceSearchResponseReceived(SSDPPacket ssdpPacket); +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/device/USN.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/device/USN.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/device/USN.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,40 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: USN.java +* +* Revision; +* +* 12/09/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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(); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/event/EventListener.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/event/EventListener.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/event/EventListener.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,21 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: EventListener.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.event; + +public interface EventListener +{ + public void eventNotifyReceived(String uuid, long seq, String varName, String value); +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/event/NotifyRequest.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/event/NotifyRequest.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/event/NotifyRequest.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,201 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SOAPRequest.java +* +* Revision; +* +* 12/11/02 +* - first revision. +* 05/22/03 +* - Giordano Sassaroli <sassarol at 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 at 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 at 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 at 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 plugins.UPnP.org.cybergarage.upnp.event; + +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.org.cybergarage.xml.*; +import plugins.UPnP.org.cybergarage.soap.*; + +import plugins.UPnP.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 callback = sub.getDeliveryURL(); + 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 at 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; + } + + private Node getVariableNode() + { + Node rootNode = getEnvelopeNode(); + if (rootNode == null) + return null; + if (rootNode.hasNodes() == false) + return null; + Node propNode = rootNode.getNode(0); + if (propNode.hasNodes() == false) + return null; + return propNode.getNode(0); + } + + // Thanks for Giordano Sassaroli <sassarol at 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 at 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 Added: trunk/plugins/UPnP/org/cybergarage/upnp/event/Property.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/event/Property.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/event/Property.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at 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 plugins.UPnP.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; + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/event/PropertyList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/event/PropertyList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/event/PropertyList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,45 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: PropertyList.java +* +* Revision; +* +* 09/08/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.event; + +import java.util.*; + +public class PropertyList extends Vector +{ + //////////////////////////////////////////////// + // Constants + //////////////////////////////////////////////// + + public final static String ELEM_NAME = "PropertyList"; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public PropertyList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public Property getProperty(int n) + { + return (Property)get(n); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/event/Subscriber.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/event/Subscriber.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/event/Subscriber.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at media-rush.com> +* - Added support the intinite time and fixed a bug in isExpired(). +* +******************************************************************/ + +package plugins.UPnP.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); + } + +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/event/SubscriberList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/event/SubscriberList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/event/SubscriberList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,46 @@ +/****************************************************************** +* +* 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 plugins.UPnP.org.cybergarage.upnp.event; + +import java.util.*; + +public class SubscriberList extends Vector +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SubscriberList() + { + } + + //////////////////////////////////////////////// + // Methods + //////////////////////////////////////////////// + + public Subscriber getSubscriber(int n) + { + Object obj = null; + try { + obj = get(n); + } + catch (Exception e) {} + return (Subscriber)obj; + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/event/Subscription.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/event/Subscription.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/event/Subscription.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,75 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: ST.java +* +* Revision; +* +* 01/31/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.event; + +import plugins.UPnP.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()); + } + +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/event/SubscriptionRequest.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/event/SubscriptionRequest.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/event/SubscriptionRequest.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at 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 at 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 at 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 at dai-labor.de> +* - Stefano Lenzi <kismet-sl at users.sourceforge.net> +* - Fixed getSID() to loop between getSID() and hasSID(); +* +********************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.event; + +import plugins.UPnP.org.cybergarage.http.*; + +import plugins.UPnP.org.cybergarage.upnp.*; +import plugins.UPnP.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 at 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 at 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 at 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); + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/event/SubscriptionResponse.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/event/SubscriptionResponse.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/event/SubscriptionResponse.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,84 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SubscriptionResponse.java +* +* Revision; +* +* 01/29/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.event; + +import plugins.UPnP.org.cybergarage.upnp.*; +import plugins.UPnP.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)); + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/HTTPMUSocket.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/HTTPMUSocket.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/HTTPMUSocket.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at gmx.de> +* - Changed send() to set the TTL as 4. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.ssdp; + +import java.net.*; +import java.util.*; + +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.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; + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/HTTPUSocket.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/HTTPUSocket.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/HTTPUSocket.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at 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 at users.sourceforge.net> +* - Added to set a current timestamp when the packet are received. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.ssdp; + +import java.net.*; + +import plugins.UPnP.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; + } +*/ +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDP.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDP.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDP.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.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; + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPNotifyRequest.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPNotifyRequest.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPNotifyRequest.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,31 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SSDPMSearchRequest.java +* +* Revision; +* +* 01/14/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.ssdp; + +import plugins.UPnP.org.cybergarage.http.*; + +public class SSDPNotifyRequest extends SSDPRequest +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SSDPNotifyRequest() + { + setMethod(HTTP.NOTIFY); + setURI("*"); + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPNotifySocket.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPNotifySocket.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPNotifySocket.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,130 @@ +/****************************************************************** +* +* 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 at dif.um.es> +* - Added a multicast filter using the SSDP pakcet. +* 04/20/05 +* - Mikael Hakman <mhakman at dkab.net> +* - Handle receive() returning null. +* - Added close() in stop(). +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.ssdp; + +import java.net.*; + +import plugins.UPnP.org.cybergarage.net.*; +import plugins.UPnP.org.cybergarage.util.*; +import plugins.UPnP.org.cybergarage.http.*; +import plugins.UPnP.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); + deviceNotifyThread.start(); + } + + public void stop() + { + // Thanks for Mikael Hakman (04/20/05) + close(); + + deviceNotifyThread = null; + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPNotifySocketList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPNotifySocketList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPNotifySocketList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,104 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: HTTPServerList.java +* +* Revision; +* +* 05/11/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.ssdp; + +import java.util.*; + +import plugins.UPnP.org.cybergarage.net.*; + +import plugins.UPnP.org.cybergarage.upnp.*; + +public class SSDPNotifySocketList extends Vector +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + 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(); + } + } + +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPPacket.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPPacket.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPPacket.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at gmx.de> +* - Fixed isRootDevice() to check the ST header. +* 11/19/04 +* - Theo Beisch <theo.beisch at gmx.de> +* - Changed getRemoteAddress() to return the adresss instead of the host name. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.ssdp; + +import java.net.*; + +import plugins.UPnP.org.cybergarage.http.*; + +import plugins.UPnP.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()); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPRequest.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPRequest.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPRequest.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.org.cybergarage.upnp.ssdp; + +import plugins.UPnP.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); + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPResponse.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPResponse.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPResponse.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 at openshores.com> +* - Added setMYNAME() and getMYNAME(). +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.ssdp; + +import plugins.UPnP.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() + { + StringBuffer str = new StringBuffer(); + + str.append(getStatusLineString()); + str.append(getHeaderString()); + str.append(HTTP.CRLF); // for Intel UPnP control points. + + return str.toString(); + } + +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchRequest.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchRequest.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchRequest.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,61 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SSDPMSearchRequest.java +* +* Revision; +* +* 11/19/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.ssdp; + +import plugins.UPnP.org.cybergarage.net.*; +import plugins.UPnP.org.cybergarage.http.*; + +import plugins.UPnP.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); + } + +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchResponse.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchResponse.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchResponse.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,35 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: SSDPSearchResponse.java +* +* Revision; +* +* 01/14/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.ssdp; + +import plugins.UPnP.org.cybergarage.http.*; + +import plugins.UPnP.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, ""); + } +} Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocket.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocket.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocket.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,106 @@ +/****************************************************************** +* +* 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 plugins.UPnP.org.cybergarage.upnp.ssdp; + +import plugins.UPnP.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); + 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()); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocketList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocketList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchResponseSocketList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,142 @@ +/****************************************************************** +* +* 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 plugins.UPnP.org.cybergarage.upnp.ssdp; + +import java.util.*; + +import plugins.UPnP.org.cybergarage.net.*; + +import plugins.UPnP.org.cybergarage.upnp.*; + +public class SSDPSearchResponseSocketList extends Vector +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + 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; + } + +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchSocket.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchSocket.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchSocket.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,125 @@ +/****************************************************************** +* +* 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 at dkab.net> +* - Added close() in stop(). +* - Added test for null return from receive() in run(). +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.ssdp; + +import plugins.UPnP.org.cybergarage.net.*; +import plugins.UPnP.org.cybergarage.util.*; + +import plugins.UPnP.org.cybergarage.upnp.device.*; + +public class SSDPSearchSocket extends HTTPMUSocket implements Runnable +{ + private boolean useIPv6Address; + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public SSDPSearchSocket() + { + } + + public SSDPSearchSocket(String bindAddr) + { + open(bindAddr); + } + + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + public boolean open(String bindAddr) + { + String addr = SSDP.ADDRESS; + useIPv6Address = false; + if (HostInterface.isIPv6Address(bindAddr) == true) { + addr = SSDP.getIPv6Address(); + useIPv6Address = true; + } + 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); + deviceSearchThread.start(); + } + + public void stop() + { + // Thanks for Mikael Hakman (04/20/05) + close(); + + deviceSearchThread = null; + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchSocketList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchSocketList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/ssdp/SSDPSearchSocketList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,103 @@ +/****************************************************************** +* +* 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 plugins.UPnP.org.cybergarage.upnp.ssdp; + +import java.util.*; + +import plugins.UPnP.org.cybergarage.net.*; + +import plugins.UPnP.org.cybergarage.upnp.device.*; + +public class SSDPSearchSocketList extends Vector +{ + //////////////////////////////////////////////// + // Constructor + //////////////////////////////////////////////// + + 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(); + } + } + +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/xml/ActionData.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/xml/ActionData.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/xml/ActionData.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,57 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: ActionData.java +* +* Revision; +* +* 03/28/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.xml; + +import plugins.UPnP.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; + } + +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/xml/ArgumentData.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/xml/ArgumentData.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/xml/ArgumentData.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,41 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: ArgumentData.java +* +* Revision; +* +* 02/24/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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; + } + +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/xml/DeviceData.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/xml/DeviceData.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/xml/DeviceData.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.org.cybergarage.upnp.xml; + +import java.io.*; + +import plugins.UPnP.org.cybergarage.util.*; +import plugins.UPnP.org.cybergarage.http.*; + +import plugins.UPnP.org.cybergarage.upnp.*; +import plugins.UPnP.org.cybergarage.upnp.ssdp.*; +import plugins.UPnP.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; + } + + +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/xml/NodeData.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/xml/NodeData.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/xml/NodeData.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,43 @@ +/****************************************************************** +* +* CyberUPnP for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: ActionData.java +* +* Revision; +* +* 03/28/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.upnp.xml; + +import plugins.UPnP.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; + } +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/xml/ServiceData.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/xml/ServiceData.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/xml/ServiceData.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.org.cybergarage.upnp.xml; + +import plugins.UPnP.org.cybergarage.util.*; +import plugins.UPnP.org.cybergarage.xml.*; + +import plugins.UPnP.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; + } + +} + Added: trunk/plugins/UPnP/org/cybergarage/upnp/xml/StateVariableData.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/upnp/xml/StateVariableData.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/upnp/xml/StateVariableData.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.org.cybergarage.upnp.xml; + +import plugins.UPnP.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; + } + +} + Added: trunk/plugins/UPnP/org/cybergarage/util/Debug.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/util/Debug.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/util/Debug.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,51 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: Debug.java +* +* Revision; +* +* 11/18/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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 Added: trunk/plugins/UPnP/org/cybergarage/util/FileUtil.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/util/FileUtil.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/util/FileUtil.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,77 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: FileUtil.java +* +* Revision: +* +* 01/03/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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"); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/util/ListenerList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/util/ListenerList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/util/ListenerList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,29 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: ListenerList.java +* +* Revision; +* +* 12/30/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.util; + +import java.util.*; + +public class ListenerList extends Vector +{ + public boolean add(Object obj) + { + if (0 <= indexOf(obj)) + return false; + return super.add(obj); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/util/Mutex.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/util/Mutex.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/util/Mutex.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,54 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: Mutex.java +* +* Revision: +* +* 06/19/04 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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 Added: trunk/plugins/UPnP/org/cybergarage/util/StringUtil.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/util/StringUtil.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/util/StringUtil.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,123 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: FileUtil.java +* +* Revision: +* +* 01/12/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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; + } +} + Added: trunk/plugins/UPnP/org/cybergarage/util/ThreadCore.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/util/ThreadCore.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/util/ThreadCore.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,76 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002-2004 +* +* File: Thread.java +* +* Revision: +* +* 01/05/04 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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(); + } +} Added: trunk/plugins/UPnP/org/cybergarage/util/TimerUtil.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/util/TimerUtil.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/util/TimerUtil.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,37 @@ +/****************************************************************** +* +* CyberUtil for Java +* +* Copyright (C) Satoshi Konno 2002-2003 +* +* File: TimerUtil.java +* +* Revision: +* +* 01/15/03 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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) {} + } +} + Added: trunk/plugins/UPnP/org/cybergarage/xml/Attribute.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/xml/Attribute.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/xml/Attribute.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,61 @@ +/****************************************************************** +* +* CyberXML for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: Attribute.java +* +* Revision; +* +* 11/27/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.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; + } +} + Added: trunk/plugins/UPnP/org/cybergarage/xml/AttributeList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/xml/AttributeList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/xml/AttributeList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,45 @@ +/****************************************************************** +* +* CyberXML for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: AttributeList.java +* +* Revision; +* +* 11/27/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.xml; + +import java.util.*; + +public class AttributeList extends Vector +{ + 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; + } +} + Added: trunk/plugins/UPnP/org/cybergarage/xml/Node.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/xml/Node.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/xml/Node.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,413 @@ +/****************************************************************** +* +* 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 at gmx.de> +* - Added "&" and "\"" "\\" to toXMLString(). +* 11/19/04 +* - Theo Beisch <theo.beisch at gmx.de> +* - Changed XML::output() to use short notation when the tag value is null. +* 12/02/04 +* - Brian Owens <brian at b-owens.com> +* - Fixed toXMLString() to convert from "'" to "'" instead of "\". +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.xml; + +import java.io.*; + +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 = new String(); + + 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(); + } + + 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); + } +} Added: trunk/plugins/UPnP/org/cybergarage/xml/NodeList.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/xml/NodeList.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/xml/NodeList.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,63 @@ +/****************************************************************** +* +* CyberXML for Java +* +* Copyright (C) Satoshi Konno 2002 +* +* File: NodeList.java +* +* Revision; +* +* 11/27/02 +* - first revision. +* +******************************************************************/ + +package plugins.UPnP.org.cybergarage.xml; + +import java.util.*; + +public class NodeList extends Vector +{ + 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; + } +} + Added: trunk/plugins/UPnP/org/cybergarage/xml/Parser.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/xml/Parser.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/xml/Parser.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.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); + } + } + +} + + Added: trunk/plugins/UPnP/org/cybergarage/xml/ParserException.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/xml/ParserException.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/xml/ParserException.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,31 @@ +/****************************************************************** +* +* 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 plugins.UPnP.org.cybergarage.xml; + +public class ParserException extends Exception +{ + public ParserException(Exception e) + { + super(e); + } + + public ParserException(String s) + { + super(s); + } +} \ No newline at end of file Added: trunk/plugins/UPnP/org/cybergarage/xml/XML.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/xml/XML.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/xml/XML.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.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) + { + StringBuffer out = new StringBuffer(); + 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); + } +} + Added: trunk/plugins/UPnP/org/cybergarage/xml/parser/JaxpParser.java =================================================================== --- trunk/plugins/UPnP/org/cybergarage/xml/parser/JaxpParser.java (rev 0) +++ trunk/plugins/UPnP/org/cybergarage/xml/parser/JaxpParser.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.org.cybergarage.xml.parser; + +import java.io.InputStream; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import plugins.UPnP.org.cybergarage.xml.Node; +import plugins.UPnP.org.cybergarage.xml.Parser; +import plugins.UPnP.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 plugins.UPnP.org.cybergarage.xml.Node parse(plugins.UPnP.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; + + plugins.UPnP.org.cybergarage.xml.Node node = new plugins.UPnP.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 plugins.UPnP.org.cybergarage.xml.Node parse(plugins.UPnP.org.cybergarage.xml.Node parentNode, org.w3c.dom.Node domNode) + { + return parse(parentNode, domNode, 0); + } + + /* (non-Javadoc) + * @see plugins.UPnP.org.cybergarage.xml.Parser#parse(java.io.InputStream) + */ + public Node parse(InputStream inStream) throws ParserException + { + plugins.UPnP.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; + } + +} Added: trunk/plugins/UPnP/org/xmlpull/v1/XmlPullParser.java =================================================================== --- trunk/plugins/UPnP/org/xmlpull/v1/XmlPullParser.java (rev 0) +++ trunk/plugins/UPnP/org/xmlpull/v1/XmlPullParser.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.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; + +} + Added: trunk/plugins/UPnP/org/xmlpull/v1/XmlPullParserException.java =================================================================== --- trunk/plugins/UPnP/org/xmlpull/v1/XmlPullParserException.java (rev 0) +++ trunk/plugins/UPnP/org/xmlpull/v1/XmlPullParserException.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.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(); + } + } + } + +} + Added: trunk/plugins/UPnP/org/xmlpull/v1/XmlPullParserFactory.java =================================================================== --- trunk/plugins/UPnP/org/xmlpull/v1/XmlPullParserFactory.java (rev 0) +++ trunk/plugins/UPnP/org/xmlpull/v1/XmlPullParserFactory.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -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 plugins.UPnP.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 StringBuffer issues = new StringBuffer (); + + 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 StringBuffer issues = new StringBuffer (); + + 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 StringBuffer sb = new StringBuffer(); + + 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; + } +} + + Added: trunk/plugins/UPnP/org/xmlpull/v1/XmlSerializer.java =================================================================== --- trunk/plugins/UPnP/org/xmlpull/v1/XmlSerializer.java (rev 0) +++ trunk/plugins/UPnP/org/xmlpull/v1/XmlSerializer.java 2007-03-26 00:53:45 UTC (rev 12375) @@ -0,0 +1,326 @@ +package plugins.UPnP.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; + +} +
