forked from I2P_Developers/i2p.i2p
Cyberlink for Java v2.1 (2011-09-16) from SVN,
with extra parsers in xml/parsers removed. Diverging from original v1.7 checkin in prep for merging.
This commit is contained in:
@ -1,12 +1,21 @@
|
|||||||
Copyright (C) 2003-2006 Satoshi Konno
|
Copyright (c) 2003-2010, Satoshi Konno
|
||||||
|
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
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.
|
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||||
|
* 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.
|
||||||
|
* Neither the name of the Cyber Garage nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||||
|
|
||||||
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.
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
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.
|
CONTRIBUTORS 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.
|
@ -30,7 +30,7 @@
|
|||||||
|
|
||||||
package org.cybergarage.http;
|
package org.cybergarage.http;
|
||||||
|
|
||||||
import java.net.*;
|
import java.net.URL;
|
||||||
|
|
||||||
public class HTTP
|
public class HTTP
|
||||||
{
|
{
|
||||||
@ -45,6 +45,8 @@ public class HTTP
|
|||||||
public static final String VERSION_11 = "1.1";
|
public static final String VERSION_11 = "1.1";
|
||||||
|
|
||||||
public static final String CRLF = "\r\n";
|
public static final String CRLF = "\r\n";
|
||||||
|
public static final byte CR = '\r';
|
||||||
|
public static final byte LF = '\n';
|
||||||
public static final String TAB = "\t";
|
public static final String TAB = "\t";
|
||||||
|
|
||||||
public static final String SOAP_ACTION = "SOAPACTION";
|
public static final String SOAP_ACTION = "SOAPACTION";
|
||||||
@ -59,19 +61,20 @@ public class HTTP
|
|||||||
|
|
||||||
public static final String DATE = "Date";
|
public static final String DATE = "Date";
|
||||||
public static final String CACHE_CONTROL = "Cache-Control";
|
public static final String CACHE_CONTROL = "Cache-Control";
|
||||||
public static final String NO_CACHE = "no-cache";
|
public static final String NO_CACHE = "no-cache";
|
||||||
public static final String MAX_AGE = "max-age";
|
public static final String MAX_AGE = "max-age";
|
||||||
public static final String CONNECTION = "Connection";
|
public static final String CONNECTION = "Connection";
|
||||||
public static final String CLOSE = "close";
|
public static final String CLOSE = "close";
|
||||||
public static final String KEEP_ALIVE = "Keep-Alive";
|
public static final String KEEP_ALIVE = "Keep-Alive";
|
||||||
public static final String CONTENT_TYPE = "Content-Type";
|
public static final String CONTENT_TYPE = "Content-Type";
|
||||||
|
public static final String CHARSET = "charset";
|
||||||
public static final String CONTENT_LENGTH = "Content-Length";
|
public static final String CONTENT_LENGTH = "Content-Length";
|
||||||
public static final String CONTENT_RANGE = "Content-Range";
|
public static final String CONTENT_RANGE = "Content-Range";
|
||||||
public static final String CONTENT_RANGE_BYTES = "bytes";
|
public static final String CONTENT_RANGE_BYTES = "bytes";
|
||||||
// Thanks for Brent Hills (10/20/04)
|
// Thanks for Brent Hills (10/20/04)
|
||||||
public static final String RANGE = "Range";
|
public static final String RANGE = "Range";
|
||||||
public static final String TRANSFER_ENCODING = "Transfer-Encoding";
|
public static final String TRANSFER_ENCODING = "Transfer-Encoding";
|
||||||
public static final String CHUNKED = "Chunked";
|
public static final String CHUNKED = "Chunked";
|
||||||
public static final String LOCATION = "Location";
|
public static final String LOCATION = "Location";
|
||||||
public static final String SERVER = "Server";
|
public static final String SERVER = "Server";
|
||||||
|
|
||||||
|
@ -18,12 +18,15 @@
|
|||||||
|
|
||||||
package org.cybergarage.http;
|
package org.cybergarage.http;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
|
import java.io.LineNumberReader;
|
||||||
|
import java.io.StringReader;
|
||||||
|
|
||||||
import org.cybergarage.util.*;
|
import org.cybergarage.util.Debug;
|
||||||
|
|
||||||
public class HTTPHeader
|
public class HTTPHeader
|
||||||
{
|
{
|
||||||
|
private static int MAX_LENGTH = 1024;
|
||||||
private String name;
|
private String name;
|
||||||
private String value;
|
private String value;
|
||||||
|
|
||||||
@ -111,9 +114,10 @@ public class HTTPHeader
|
|||||||
}
|
}
|
||||||
|
|
||||||
public final static String getValue(String data, String name)
|
public final static String getValue(String data, String name)
|
||||||
{
|
{
|
||||||
StringReader strReader = new StringReader(data);
|
/* Thanks for Stephan Mehlhase (2010-10-26) */
|
||||||
LineNumberReader lineReader = new LineNumberReader(strReader);
|
StringReader strReader = new StringReader(data);
|
||||||
|
LineNumberReader lineReader = new LineNumberReader(strReader, Math.min(data.length(), MAX_LENGTH));
|
||||||
return getValue(lineReader, name);
|
return getValue(lineReader, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002-2004
|
* Copyright (C) Satoshi Konno 2002-2004
|
||||||
*
|
*
|
||||||
* File: HTTPConnection.java
|
* File: HTTPConnection.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 11/18/02
|
* 11/18/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 09/02/03
|
* 09/02/03
|
||||||
* - Giordano Sassaroli <sassarol@cefriel.it>
|
* - Giordano Sassaroli <sassarol@cefriel.it>
|
||||||
* - Problem : The API is unable to receive responses from the Microsoft UPnP stack
|
* - Problem : The API is unable to receive responses from the Microsoft UPnP stack
|
||||||
@ -59,29 +59,47 @@
|
|||||||
* hasTransferEncoding(), setTransferEncoding(), getTransferEncoding(), isChunked().
|
* hasTransferEncoding(), setTransferEncoding(), getTransferEncoding(), isChunked().
|
||||||
* 03/02/05
|
* 03/02/05
|
||||||
* - Changed post() to suppot chunked stream.
|
* - Changed post() to suppot chunked stream.
|
||||||
|
* 06/11/05
|
||||||
|
* - Added setHost().
|
||||||
|
* 07/07/05
|
||||||
|
* - Lee Peik Feng <pflee@users.sourceforge.net>
|
||||||
|
* - Andrey Ovchar <AOvchar@consultitnow.com>
|
||||||
|
* - Fixed set() to parse the chunk size as a hex string.
|
||||||
|
* 11/02/05
|
||||||
|
* - Changed set() to use BufferedInputStream instead of BufferedReader to
|
||||||
|
* get the content as a byte stream.
|
||||||
|
* 11/06/05
|
||||||
|
* - Added getCharSet().
|
||||||
|
* - Changed getContentString() to return the content string using the charset.
|
||||||
*
|
*
|
||||||
*******************************************************************/
|
*******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.http;
|
package org.cybergarage.http;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.BufferedInputStream;
|
||||||
import java.util.*;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import org.cybergarage.net.*;
|
import java.io.InputStream;
|
||||||
import org.cybergarage.util.*;
|
import java.io.InterruptedIOException;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
import java.util.StringTokenizer;
|
||||||
public class HTTPPacket
|
import java.util.Vector;
|
||||||
{
|
|
||||||
////////////////////////////////////////////////
|
import org.cybergarage.net.HostInterface;
|
||||||
// Constructor
|
import org.cybergarage.util.Debug;
|
||||||
////////////////////////////////////////////////
|
import org.cybergarage.util.StringUtil;
|
||||||
|
|
||||||
public HTTPPacket()
|
public class HTTPPacket
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Constructor
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public HTTPPacket()
|
||||||
{
|
{
|
||||||
setVersion(HTTP.VERSION);
|
setVersion(HTTP.VERSION);
|
||||||
setContentInputStream(null);
|
setContentInputStream(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HTTPPacket(HTTPPacket httpPacket)
|
public HTTPPacket(HTTPPacket httpPacket)
|
||||||
{
|
{
|
||||||
@ -129,12 +147,38 @@ public class HTTPPacket
|
|||||||
// set
|
// set
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private String readLine(BufferedInputStream in)
|
||||||
|
{
|
||||||
|
ByteArrayOutputStream lineBuf = new ByteArrayOutputStream();
|
||||||
|
byte readBuf[] = new byte[1];
|
||||||
|
|
||||||
|
try {
|
||||||
|
int readLen = in.read(readBuf);
|
||||||
|
while (0 < readLen) {
|
||||||
|
if (readBuf[0] == HTTP.LF)
|
||||||
|
break;
|
||||||
|
if (readBuf[0] != HTTP.CR)
|
||||||
|
lineBuf.write(readBuf[0]);
|
||||||
|
readLen = in.read(readBuf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (InterruptedIOException e) {
|
||||||
|
//Ignoring warning because it's a way to break the HTTP connecttion
|
||||||
|
//TODO Create a new level of Logging and log the event
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
Debug.warning(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return lineBuf.toString();
|
||||||
|
}
|
||||||
|
|
||||||
protected boolean set(InputStream in, boolean onlyHeaders)
|
protected boolean set(InputStream in, boolean onlyHeaders)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
BufferedInputStream reader = new BufferedInputStream(in);
|
||||||
|
|
||||||
String firstLine = reader.readLine();
|
String firstLine = readLine(reader);
|
||||||
if (firstLine == null || firstLine.length() <= 0)
|
if (firstLine == null || firstLine.length() <= 0)
|
||||||
return false;
|
return false;
|
||||||
setFirstLine(firstLine);
|
setFirstLine(firstLine);
|
||||||
@ -148,15 +192,15 @@ public class HTTPPacket
|
|||||||
//stream, so the code should check the presence of the actual
|
//stream, so the code should check the presence of the actual
|
||||||
//response in the stream.
|
//response in the stream.
|
||||||
//skip all header lines
|
//skip all header lines
|
||||||
String headerLine = reader.readLine();
|
String headerLine = readLine(reader);
|
||||||
while ((headerLine != null) && (0 < headerLine.length()) ) {
|
while ((headerLine != null) && (0 < headerLine.length()) ) {
|
||||||
HTTPHeader header = new HTTPHeader(headerLine);
|
HTTPHeader header = new HTTPHeader(headerLine);
|
||||||
if (header.hasName() == true)
|
if (header.hasName() == true)
|
||||||
setHeader(header);
|
setHeader(header);
|
||||||
headerLine = reader.readLine();
|
headerLine = readLine(reader);
|
||||||
}
|
}
|
||||||
//look forward another first line
|
//look forward another first line
|
||||||
String actualFirstLine = reader.readLine();
|
String actualFirstLine = readLine(reader);
|
||||||
if ((actualFirstLine != null) && (0 < actualFirstLine.length()) ) {
|
if ((actualFirstLine != null) && (0 < actualFirstLine.length()) ) {
|
||||||
//this is the actual first line
|
//this is the actual first line
|
||||||
setFirstLine(actualFirstLine);
|
setFirstLine(actualFirstLine);
|
||||||
@ -165,12 +209,12 @@ public class HTTPPacket
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String headerLine = reader.readLine();
|
String headerLine = readLine(reader);
|
||||||
while ((headerLine != null) && (0 < headerLine.length()) ) {
|
while ((headerLine != null) && (0 < headerLine.length()) ) {
|
||||||
HTTPHeader header = new HTTPHeader(headerLine);
|
HTTPHeader header = new HTTPHeader(headerLine);
|
||||||
if (header.hasName() == true)
|
if (header.hasName() == true)
|
||||||
setHeader(header);
|
setHeader(header);
|
||||||
headerLine = reader.readLine();
|
headerLine = readLine(reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (onlyHeaders == true) {
|
if (onlyHeaders == true) {
|
||||||
@ -183,19 +227,24 @@ public class HTTPPacket
|
|||||||
long contentLen = 0;
|
long contentLen = 0;
|
||||||
if (isChunkedRequest == true) {
|
if (isChunkedRequest == true) {
|
||||||
try {
|
try {
|
||||||
String chunkSizeLine = reader.readLine();
|
String chunkSizeLine = readLine(reader);
|
||||||
contentLen = Long.parseLong(new String(chunkSizeLine.getBytes(), 0, chunkSizeLine.length()-2));
|
// Thanks for Lee Peik Feng <pflee@users.sourceforge.net> (07/07/05)
|
||||||
|
//contentLen = Long.parseLong(new String(chunkSizeLine.getBytes(), 0, chunkSizeLine.length()-2), 16);
|
||||||
|
contentLen = (chunkSizeLine != null) ? Long.parseLong(chunkSizeLine.trim(), 16) : 0;
|
||||||
}
|
}
|
||||||
catch (Exception e) {};
|
catch (Exception e) {};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
contentLen = getContentLength();
|
contentLen = getContentLength();
|
||||||
|
|
||||||
StringBuilder contentBuf = new StringBuilder();
|
ByteArrayOutputStream contentBuf = new ByteArrayOutputStream();
|
||||||
|
|
||||||
while (0 < contentLen) {
|
while (0 < contentLen) {
|
||||||
int chunkSize = HTTP.getChunkSize();
|
int chunkSize = HTTP.getChunkSize();
|
||||||
char readBuf[] = new char[chunkSize];
|
|
||||||
|
/* Thanks for Stephan Mehlhase (2010-10-26) */
|
||||||
|
byte readBuf[] = new byte[(int) (contentLen > chunkSize ? chunkSize : contentLen)];
|
||||||
|
|
||||||
long readCnt = 0;
|
long readCnt = 0;
|
||||||
while (readCnt < contentLen) {
|
while (readCnt < contentLen) {
|
||||||
try {
|
try {
|
||||||
@ -206,7 +255,7 @@ public class HTTPPacket
|
|||||||
int readLen = reader.read(readBuf, 0, (int)bufReadLen);
|
int readLen = reader.read(readBuf, 0, (int)bufReadLen);
|
||||||
if (readLen < 0)
|
if (readLen < 0)
|
||||||
break;
|
break;
|
||||||
contentBuf.append(new String(readBuf, 0, readLen));
|
contentBuf.write(readBuf, 0, readLen);
|
||||||
readCnt += readLen;
|
readCnt += readLen;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@ -226,8 +275,9 @@ public class HTTPPacket
|
|||||||
} while (skipLen < HTTP.CRLF.length());
|
} while (skipLen < HTTP.CRLF.length());
|
||||||
// read next chunk size
|
// read next chunk size
|
||||||
try {
|
try {
|
||||||
String chunkSizeLine = reader.readLine();
|
String chunkSizeLine = readLine(reader);
|
||||||
contentLen = Long.parseLong(new String(chunkSizeLine.getBytes(), 0, chunkSizeLine.length()-2));
|
// Thanks for Lee Peik Feng <pflee@users.sourceforge.net> (07/07/05)
|
||||||
|
contentLen = Long.parseLong(new String(chunkSizeLine.getBytes(), 0, chunkSizeLine.length()-2), 16);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
contentLen = 0;
|
contentLen = 0;
|
||||||
@ -237,9 +287,7 @@ public class HTTPPacket
|
|||||||
contentLen = 0;
|
contentLen = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Thanks for Ralf G. R. Bergs (02/09/04)
|
setContent(contentBuf.toByteArray(), false);
|
||||||
String contentStr = contentBuf.toString();
|
|
||||||
setContent(contentStr.getBytes(), false);
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
Debug.warning(e);
|
Debug.warning(e);
|
||||||
@ -312,47 +360,47 @@ public class HTTPPacket
|
|||||||
|
|
||||||
public boolean hasFirstLine()
|
public boolean hasFirstLine()
|
||||||
{
|
{
|
||||||
return (0 < firstLine.length()) ? true : false;
|
return (0 < firstLine.length()) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Header
|
// Header
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private Vector httpHeaderList = new Vector();
|
private Vector httpHeaderList = new Vector();
|
||||||
|
|
||||||
public int getNHeaders()
|
public int getNHeaders()
|
||||||
{
|
{
|
||||||
return httpHeaderList.size();
|
return httpHeaderList.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addHeader(HTTPHeader header)
|
public void addHeader(HTTPHeader header)
|
||||||
{
|
{
|
||||||
httpHeaderList.add(header);
|
httpHeaderList.add(header);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addHeader(String name, String value)
|
public void addHeader(String name, String value)
|
||||||
{
|
{
|
||||||
HTTPHeader header = new HTTPHeader(name, value);
|
HTTPHeader header = new HTTPHeader(name, value);
|
||||||
httpHeaderList.add(header);
|
httpHeaderList.add(header);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HTTPHeader getHeader(int n)
|
public HTTPHeader getHeader(int n)
|
||||||
{
|
{
|
||||||
return (HTTPHeader)httpHeaderList.get(n);
|
return (HTTPHeader)httpHeaderList.get(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HTTPHeader getHeader(String name)
|
public HTTPHeader getHeader(String name)
|
||||||
{
|
{
|
||||||
int nHeaders = getNHeaders();
|
int nHeaders = getNHeaders();
|
||||||
for (int n=0; n<nHeaders; n++) {
|
for (int n=0; n<nHeaders; n++) {
|
||||||
HTTPHeader header = getHeader(n);
|
HTTPHeader header = getHeader(n);
|
||||||
String headerName = header.getName();
|
String headerName = header.getName();
|
||||||
if (headerName.equalsIgnoreCase(name) == true)
|
if (headerName.equalsIgnoreCase(name) == true)
|
||||||
return header;
|
return header;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearHeaders()
|
public void clearHeaders()
|
||||||
{
|
{
|
||||||
@ -364,16 +412,16 @@ public class HTTPPacket
|
|||||||
{
|
{
|
||||||
return (getHeader(name) != null) ? true : false;
|
return (getHeader(name) != null) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHeader(String name, String value)
|
public void setHeader(String name, String value)
|
||||||
{
|
{
|
||||||
HTTPHeader header = getHeader(name);
|
HTTPHeader header = getHeader(name);
|
||||||
if (header != null) {
|
if (header != null) {
|
||||||
header.setValue(value);
|
header.setValue(value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
addHeader(name, value);
|
addHeader(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHeader(String name, int value)
|
public void setHeader(String name, int value)
|
||||||
{
|
{
|
||||||
@ -384,19 +432,19 @@ public class HTTPPacket
|
|||||||
{
|
{
|
||||||
setHeader(name, Long.toString(value));
|
setHeader(name, Long.toString(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHeader(HTTPHeader header)
|
public void setHeader(HTTPHeader header)
|
||||||
{
|
{
|
||||||
setHeader(header.getName(), header.getValue());
|
setHeader(header.getName(), header.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHeaderValue(String name)
|
public String getHeaderValue(String name)
|
||||||
{
|
{
|
||||||
HTTPHeader header = getHeader(name);
|
HTTPHeader header = getHeader(name);
|
||||||
if (header == null)
|
if (header == null)
|
||||||
return "";
|
return "";
|
||||||
return header.getValue();
|
return header.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// set*Value
|
// set*Value
|
||||||
@ -431,7 +479,7 @@ public class HTTPPacket
|
|||||||
{
|
{
|
||||||
return getStringHeaderValue(name, "\"", "\"");
|
return getStringHeaderValue(name, "\"", "\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setIntegerHeader(String name, int value)
|
public void setIntegerHeader(String name, int value)
|
||||||
{
|
{
|
||||||
setHeader(name, Integer.toString(value));
|
setHeader(name, Integer.toString(value));
|
||||||
@ -442,13 +490,13 @@ public class HTTPPacket
|
|||||||
setHeader(name, Long.toString(value));
|
setHeader(name, Long.toString(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getIntegerHeaderValue(String name)
|
public int getIntegerHeaderValue(String name)
|
||||||
{
|
{
|
||||||
HTTPHeader header = getHeader(name);
|
HTTPHeader header = getHeader(name);
|
||||||
if (header == null)
|
if (header == null)
|
||||||
return 0;
|
return 0;
|
||||||
return StringUtil.toInteger(header.getValue());
|
return StringUtil.toInteger(header.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getLongHeaderValue(String name)
|
public long getLongHeaderValue(String name)
|
||||||
{
|
{
|
||||||
@ -457,14 +505,14 @@ public class HTTPPacket
|
|||||||
return 0;
|
return 0;
|
||||||
return StringUtil.toLong(header.getValue());
|
return StringUtil.toLong(header.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// getHeader
|
// getHeader
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public String getHeaderString()
|
public String getHeaderString()
|
||||||
{
|
{
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuffer str = new StringBuffer();
|
||||||
|
|
||||||
int nHeaders = getNHeaders();
|
int nHeaders = getNHeaders();
|
||||||
for (int n=0; n<nHeaders; n++) {
|
for (int n=0; n<nHeaders; n++) {
|
||||||
@ -475,48 +523,57 @@ public class HTTPPacket
|
|||||||
return str.toString();
|
return str.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Contents
|
// Contents
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private byte content[] = new byte[0];
|
private byte content[] = new byte[0];
|
||||||
|
|
||||||
public void setContent(byte data[], boolean updateWithContentLength)
|
public void setContent(byte data[], boolean updateWithContentLength)
|
||||||
{
|
{
|
||||||
content = data;
|
content = data;
|
||||||
if (updateWithContentLength == true)
|
if (updateWithContentLength == true)
|
||||||
setContentLength(data.length);
|
setContentLength(data.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setContent(byte data[])
|
public void setContent(byte data[])
|
||||||
{
|
{
|
||||||
setContent(data, true);
|
setContent(data, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setContent(String data, boolean updateWithContentLength)
|
public void setContent(String data, boolean updateWithContentLength)
|
||||||
{
|
{
|
||||||
setContent(data.getBytes(), updateWithContentLength);
|
setContent(data.getBytes(), updateWithContentLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setContent(String data)
|
public void setContent(String data)
|
||||||
{
|
{
|
||||||
setContent(data, true);
|
setContent(data, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte []getContent()
|
public byte []getContent()
|
||||||
{
|
{
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getContentString()
|
public String getContentString()
|
||||||
{
|
{
|
||||||
return new String(content);
|
String charSet = getCharSet();
|
||||||
|
if (charSet == null || charSet.length() <= 0)
|
||||||
|
return new String(content);
|
||||||
|
try {
|
||||||
|
return new String(content, charSet);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Debug.warning(e);
|
||||||
|
}
|
||||||
|
return new String(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasContent()
|
public boolean hasContent()
|
||||||
{
|
{
|
||||||
return (content.length > 0) ? true : false;
|
return (content.length > 0) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Contents (InputStream)
|
// Contents (InputStream)
|
||||||
@ -538,35 +595,61 @@ public class HTTPPacket
|
|||||||
{
|
{
|
||||||
return (contentInput != null) ? true : false;
|
return (contentInput != null) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// ContentType
|
// ContentType
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public void setContentType(String type)
|
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);
|
setHeader(HTTP.CONTENT_TYPE, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getContentLength()
|
public String getContentType()
|
||||||
{
|
{
|
||||||
return getLongHeaderValue(HTTP.CONTENT_LENGTH);
|
return getHeaderValue(HTTP.CONTENT_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Charset
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public String getCharSet()
|
||||||
|
{
|
||||||
|
String contentType = getContentType();
|
||||||
|
if (contentType == null)
|
||||||
|
return "";
|
||||||
|
contentType = contentType.toLowerCase();
|
||||||
|
int charSetIdx = contentType.indexOf(HTTP.CHARSET);
|
||||||
|
if (charSetIdx < 0)
|
||||||
|
return "";
|
||||||
|
int charSetEndIdx = charSetIdx + HTTP.CHARSET.length() + 1;
|
||||||
|
String charSet = new String(contentType.getBytes(), charSetEndIdx, (contentType.length() - charSetEndIdx));
|
||||||
|
if (charSet.length() < 0)
|
||||||
|
return "";
|
||||||
|
if (charSet.charAt(0) == '\"')
|
||||||
|
charSet = charSet.substring(1, (charSet.length() - 1));
|
||||||
|
if (charSet.length() < 0)
|
||||||
|
return "";
|
||||||
|
if (charSet.charAt((charSet.length()-1)) == '\"')
|
||||||
|
charSet = charSet.substring(0, (charSet.length() - 1));
|
||||||
|
return charSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// ContentLength
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public void setContentLength(long len)
|
||||||
|
{
|
||||||
|
setLongHeader(HTTP.CONTENT_LENGTH, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getContentLength()
|
||||||
|
{
|
||||||
|
return getLongHeaderValue(HTTP.CONTENT_LENGTH);
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Connection
|
// Connection
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -642,7 +725,7 @@ public class HTTPPacket
|
|||||||
// Skip bytes
|
// Skip bytes
|
||||||
if (strToken.hasMoreTokens() == false)
|
if (strToken.hasMoreTokens() == false)
|
||||||
return range;
|
return range;
|
||||||
strToken.nextToken(" ");
|
String bytesStr = strToken.nextToken(" ");
|
||||||
// Get first-byte-pos
|
// Get first-byte-pos
|
||||||
if (strToken.hasMoreTokens() == false)
|
if (strToken.hasMoreTokens() == false)
|
||||||
return range;
|
return range;
|
||||||
@ -686,10 +769,10 @@ public class HTTPPacket
|
|||||||
return range[2];
|
return range[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// CacheControl
|
// CacheControl
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public void setCacheControl(String directive)
|
public void setCacheControl(String directive)
|
||||||
{
|
{
|
||||||
setHeader(HTTP.CACHE_CONTROL, directive);
|
setHeader(HTTP.CACHE_CONTROL, directive);
|
||||||
@ -701,29 +784,29 @@ public class HTTPPacket
|
|||||||
setHeader(HTTP.CACHE_CONTROL, strVal);
|
setHeader(HTTP.CACHE_CONTROL, strVal);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCacheControl(int value)
|
public void setCacheControl(int value)
|
||||||
{
|
{
|
||||||
setCacheControl(HTTP.MAX_AGE, value);
|
setCacheControl(HTTP.MAX_AGE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCacheControl()
|
public String getCacheControl()
|
||||||
{
|
{
|
||||||
return getHeaderValue(HTTP.CACHE_CONTROL);
|
return getHeaderValue(HTTP.CACHE_CONTROL);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Server
|
// Server
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public void setServer(String name)
|
public void setServer(String name)
|
||||||
{
|
{
|
||||||
setHeader(HTTP.SERVER, name);
|
setHeader(HTTP.SERVER, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getServer()
|
public String getServer()
|
||||||
{
|
{
|
||||||
return getHeaderValue(HTTP.SERVER);
|
return getHeaderValue(HTTP.SERVER);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Host
|
// Host
|
||||||
@ -737,26 +820,34 @@ public class HTTPPacket
|
|||||||
setHeader(HTTP.HOST, hostAddr + ":" + Integer.toString(port));
|
setHeader(HTTP.HOST, hostAddr + ":" + Integer.toString(port));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setHost(String host)
|
||||||
|
{
|
||||||
|
String hostAddr = host;
|
||||||
|
if (HostInterface.isIPv6Address(host) == true)
|
||||||
|
hostAddr = "[" + host + "]";
|
||||||
|
setHeader(HTTP.HOST, hostAddr);
|
||||||
|
}
|
||||||
|
|
||||||
public String getHost()
|
public String getHost()
|
||||||
{
|
{
|
||||||
return getHeaderValue(HTTP.HOST);
|
return getHeaderValue(HTTP.HOST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Date
|
// Date
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public void setDate(Calendar cal)
|
public void setDate(Calendar cal)
|
||||||
{
|
{
|
||||||
Date date = new Date(cal);
|
Date date = new Date(cal);
|
||||||
setHeader(HTTP.DATE, date.getDateString());
|
setHeader(HTTP.DATE, date.getDateString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDate()
|
public String getDate()
|
||||||
{
|
{
|
||||||
return getHeaderValue(HTTP.DATE);
|
return getHeaderValue(HTTP.DATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Connection
|
// Connection
|
||||||
@ -804,5 +895,5 @@ public class HTTPPacket
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,15 +44,35 @@
|
|||||||
* - Added to check the range of Content-Range request in post().
|
* - Added to check the range of Content-Range request in post().
|
||||||
* 03/02/05
|
* 03/02/05
|
||||||
* - Changed post() to suppot chunked stream.
|
* - Changed post() to suppot chunked stream.
|
||||||
|
* 06/10/05
|
||||||
|
* - Changed post() to add a HOST headedr before the posting.
|
||||||
|
* 07/07/05
|
||||||
|
* - Lee Peik Feng <pflee@users.sourceforge.net>
|
||||||
|
* - Fixed post() to output the chunk size as a hex string.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.http;
|
package org.cybergarage.http;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
import java.net.*;
|
import java.io.InputStream;
|
||||||
import java.util.*;
|
import java.io.OutputStream;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
|
import org.cybergarage.util.Debug;
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* This class rappresnet an HTTP <b>request</b>, and act as HTTP client when it sends the request<br>
|
||||||
|
*
|
||||||
|
* @author Satoshi "skonno" Konno
|
||||||
|
* @author Stefano "Kismet" Lenzi
|
||||||
|
* @version 1.8
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class HTTPRequest extends HTTPPacket
|
public class HTTPRequest extends HTTPPacket
|
||||||
{
|
{
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -61,6 +81,7 @@ public class HTTPRequest extends HTTPPacket
|
|||||||
|
|
||||||
public HTTPRequest()
|
public HTTPRequest()
|
||||||
{
|
{
|
||||||
|
setVersion(HTTP.VERSION_10);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HTTPRequest(InputStream in)
|
public HTTPRequest(InputStream in)
|
||||||
@ -296,7 +317,7 @@ public class HTTPRequest extends HTTPPacket
|
|||||||
|
|
||||||
public String getHeader()
|
public String getHeader()
|
||||||
{
|
{
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuffer str = new StringBuffer();
|
||||||
|
|
||||||
str.append(getFirstLineString());
|
str.append(getFirstLineString());
|
||||||
|
|
||||||
@ -370,6 +391,8 @@ public class HTTPRequest extends HTTPPacket
|
|||||||
{
|
{
|
||||||
HTTPResponse httpRes = new HTTPResponse();
|
HTTPResponse httpRes = new HTTPResponse();
|
||||||
|
|
||||||
|
setHost(host);
|
||||||
|
|
||||||
setConnection((isKeepAlive == true) ? HTTP.KEEP_ALIVE : HTTP.CLOSE);
|
setConnection((isKeepAlive == true) ? HTTP.KEEP_ALIVE : HTTP.CLOSE);
|
||||||
|
|
||||||
boolean isHeaderRequest = isHeadRequest();
|
boolean isHeaderRequest = isHeadRequest();
|
||||||
@ -378,8 +401,11 @@ public class HTTPRequest extends HTTPPacket
|
|||||||
InputStream in = null;
|
InputStream in = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (postSocket == null)
|
if (postSocket == null){
|
||||||
postSocket = new Socket(host, port);
|
// Thanks for Hao Hu
|
||||||
|
postSocket = new Socket();
|
||||||
|
postSocket.connect(new InetSocketAddress(host, port), HTTPServer.DEFAULT_TIMEOUT);
|
||||||
|
}
|
||||||
|
|
||||||
out = postSocket.getOutputStream();
|
out = postSocket.getOutputStream();
|
||||||
PrintStream pout = new PrintStream(out);
|
PrintStream pout = new PrintStream(out);
|
||||||
@ -395,7 +421,8 @@ public class HTTPRequest extends HTTPPacket
|
|||||||
|
|
||||||
if (0 < contentLength) {
|
if (0 < contentLength) {
|
||||||
if (isChunkedRequest == true) {
|
if (isChunkedRequest == true) {
|
||||||
String chunSizeBuf = Long.toString(contentLength);
|
// Thanks for Lee Peik Feng <pflee@users.sourceforge.net> (07/07/05)
|
||||||
|
String chunSizeBuf = Long.toHexString(contentLength);
|
||||||
pout.print(chunSizeBuf);
|
pout.print(chunSizeBuf);
|
||||||
pout.print(HTTP.CRLF);
|
pout.print(HTTP.CRLF);
|
||||||
}
|
}
|
||||||
@ -413,9 +440,14 @@ public class HTTPRequest extends HTTPPacket
|
|||||||
|
|
||||||
in = postSocket.getInputStream();
|
in = postSocket.getInputStream();
|
||||||
httpRes.set(in, isHeaderRequest);
|
httpRes.set(in, isHeaderRequest);
|
||||||
}
|
} catch (SocketException e) {
|
||||||
catch (Exception e) {
|
httpRes.setStatusCode(HTTPStatus.INTERNAL_SERVER_ERROR);
|
||||||
httpRes.setStatusCode(HTTPStatus.INTERNAL_SERVER_ERROR);
|
Debug.warning(e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
//Socket create but without connection
|
||||||
|
//TODO Blacklistening the device
|
||||||
|
httpRes.setStatusCode(HTTPStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
Debug.warning(e);
|
||||||
} finally {
|
} finally {
|
||||||
if (isKeepAlive == false) {
|
if (isKeepAlive == false) {
|
||||||
try {
|
try {
|
||||||
@ -479,7 +511,7 @@ public class HTTPRequest extends HTTPPacket
|
|||||||
|
|
||||||
public String toString()
|
public String toString()
|
||||||
{
|
{
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuffer str = new StringBuffer();
|
||||||
|
|
||||||
str.append(getHeader());
|
str.append(getHeader());
|
||||||
str.append(HTTP.CRLF);
|
str.append(HTTP.CRLF);
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
package org.cybergarage.http;
|
package org.cybergarage.http;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.InputStream;
|
||||||
|
|
||||||
public class HTTPResponse extends HTTPPacket
|
public class HTTPResponse extends HTTPPacket
|
||||||
{
|
{
|
||||||
@ -29,6 +29,7 @@ public class HTTPResponse extends HTTPPacket
|
|||||||
|
|
||||||
public HTTPResponse()
|
public HTTPResponse()
|
||||||
{
|
{
|
||||||
|
setVersion(HTTP.VERSION_11);
|
||||||
setContentType(HTML.CONTENT_TYPE);
|
setContentType(HTML.CONTENT_TYPE);
|
||||||
setServer(HTTPServer.getName());
|
setServer(HTTPServer.getName());
|
||||||
setContent("");
|
setContent("");
|
||||||
@ -84,7 +85,7 @@ public class HTTPResponse extends HTTPPacket
|
|||||||
|
|
||||||
public String getHeader()
|
public String getHeader()
|
||||||
{
|
{
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuffer str = new StringBuffer();
|
||||||
|
|
||||||
str.append(getStatusLineString());
|
str.append(getStatusLineString());
|
||||||
str.append(getHeaderString());
|
str.append(getHeaderString());
|
||||||
@ -98,7 +99,7 @@ public class HTTPResponse extends HTTPPacket
|
|||||||
|
|
||||||
public String toString()
|
public String toString()
|
||||||
{
|
{
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuffer str = new StringBuffer();
|
||||||
|
|
||||||
str.append(getStatusLineString());
|
str.append(getStatusLineString());
|
||||||
str.append(getHeaderString());
|
str.append(getHeaderString());
|
||||||
|
@ -5,36 +5,57 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002-2003
|
* Copyright (C) Satoshi Konno 2002-2003
|
||||||
*
|
*
|
||||||
* File: HTTPServer.java
|
* File: HTTPServer.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 12/12/02
|
* 12/12/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 10/20/03
|
* 10/20/03
|
||||||
* - Improved the HTTP server using multithreading.
|
* - Improved the HTTP server using multithreading.
|
||||||
* 08/27/04
|
* 08/27/04
|
||||||
* - Changed accept() to set a default timeout, HTTP.DEFAULT_TIMEOUT, to the socket.
|
* - Changed accept() to set a default timeout, HTTP.DEFAULT_TIMEOUT, to the socket.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.http;
|
package org.cybergarage.http;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
import java.net.*;
|
import java.net.InetAddress;
|
||||||
|
import java.net.ServerSocket;
|
||||||
import org.cybergarage.util.*;
|
import java.net.Socket;
|
||||||
|
|
||||||
public class HTTPServer implements Runnable
|
import org.cybergarage.util.Debug;
|
||||||
{
|
import org.cybergarage.util.ListenerList;
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Constants
|
/**
|
||||||
////////////////////////////////////////////////
|
*
|
||||||
|
* This class identifies an HTTP over TCP server<br>
|
||||||
|
* The server must be initialized iether by the {@link HTTPServer#open(InetAddress, int)} or the {@link HTTPServer#open(String, int)} method.<br>
|
||||||
|
* Optionally a set of {@link HTTPRequestListener} may be set<br>
|
||||||
|
* The server then can be started or stopped by the method {@link HTTPServer#start()} and {@link HTTPServer#stop()}
|
||||||
|
*
|
||||||
|
* @author Satoshi "skonno" Konno
|
||||||
|
* @author Stefano "Kismet" Lenzi
|
||||||
|
* @version 1.8
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class HTTPServer implements Runnable
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Constants
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public final static String NAME = "CyberHTTP";
|
public final static String NAME = "CyberHTTP";
|
||||||
public final static String VERSION = "1.0";
|
public final static String VERSION = "1.0";
|
||||||
|
|
||||||
public final static int DEFAULT_PORT = 80;
|
public final static int DEFAULT_PORT = 80;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default timeout connection for HTTP comunication
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public final static int DEFAULT_TIMEOUT = DEFAULT_PORT * 1000;
|
||||||
|
|
||||||
public static String getName()
|
public static String getName()
|
||||||
{
|
{
|
||||||
String osName = System.getProperty("os.name");
|
String osName = System.getProperty("os.name");
|
||||||
@ -42,27 +63,33 @@ public class HTTPServer implements Runnable
|
|||||||
return osName + "/" + osVer + " " + NAME + "/" + VERSION;
|
return osName + "/" + osVer + " " + NAME + "/" + VERSION;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public HTTPServer()
|
public HTTPServer()
|
||||||
{
|
{
|
||||||
serverSock = null;
|
serverSock = null;
|
||||||
}
|
|
||||||
|
}
|
||||||
////////////////////////////////////////////////
|
|
||||||
// ServerSocket
|
////////////////////////////////////////////////
|
||||||
////////////////////////////////////////////////
|
// ServerSocket
|
||||||
|
////////////////////////////////////////////////
|
||||||
private ServerSocket serverSock = null;
|
|
||||||
|
private ServerSocket serverSock = null;
|
||||||
private InetAddress bindAddr = null;
|
private InetAddress bindAddr = null;
|
||||||
private int bindPort = 0;
|
private int bindPort = 0;
|
||||||
|
/**
|
||||||
public ServerSocket getServerSock()
|
* Store the current TCP timeout value
|
||||||
{
|
* The variable should be accessed by getter and setter metho
|
||||||
return serverSock;
|
*/
|
||||||
}
|
protected int timeout = DEFAULT_TIMEOUT;
|
||||||
|
|
||||||
|
public ServerSocket getServerSock()
|
||||||
|
{
|
||||||
|
return serverSock;
|
||||||
|
}
|
||||||
|
|
||||||
public String getBindAddress()
|
public String getBindAddress()
|
||||||
{
|
{
|
||||||
@ -76,105 +103,134 @@ public class HTTPServer implements Runnable
|
|||||||
return bindPort;
|
return bindPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// open/close
|
// open/close
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public boolean open(String addr, int port)
|
/**
|
||||||
|
* Get the current socket timeout
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public synchronized int getTimeout() {
|
||||||
|
return timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the current socket timeout
|
||||||
|
* @param longout new timeout
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public synchronized void setTimeout(int timeout) {
|
||||||
|
this.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean open(InetAddress addr,int port){
|
||||||
|
if (serverSock != null)
|
||||||
|
return true;
|
||||||
|
try {
|
||||||
|
serverSock = new ServerSocket(bindPort, 0, bindAddr);
|
||||||
|
}catch (IOException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean open(String addr, int port)
|
||||||
{
|
{
|
||||||
if (serverSock != null)
|
if (serverSock != null)
|
||||||
return true;
|
return true;
|
||||||
try {
|
try {
|
||||||
bindAddr = InetAddress.getByName(addr);
|
bindAddr = InetAddress.getByName(addr);
|
||||||
bindPort = port;
|
bindPort = port;
|
||||||
serverSock = new ServerSocket(bindPort, 0, bindAddr);
|
serverSock = new ServerSocket(bindPort, 0, bindAddr);
|
||||||
serverSock.setSoTimeout(10*1000);
|
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean close()
|
public boolean close()
|
||||||
{
|
{
|
||||||
if (serverSock == null)
|
if (serverSock == null)
|
||||||
return true;
|
return true;
|
||||||
try {
|
try {
|
||||||
serverSock.close();
|
serverSock.close();
|
||||||
serverSock = null;
|
serverSock = null;
|
||||||
bindAddr = null;
|
bindAddr = null;
|
||||||
bindPort = 0;
|
bindPort = 0;
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
Debug.warning(e);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
catch (Exception e) {
|
||||||
}
|
Debug.warning(e);
|
||||||
|
return false;
|
||||||
public Socket accept()
|
}
|
||||||
{
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Socket accept()
|
||||||
|
{
|
||||||
if (serverSock == null)
|
if (serverSock == null)
|
||||||
return null;
|
return null;
|
||||||
try {
|
try {
|
||||||
Socket sock = serverSock.accept();
|
Socket sock = serverSock.accept();
|
||||||
sock.setSoTimeout(HTTP.DEFAULT_PORT * 1000);
|
sock.setSoTimeout(getTimeout());
|
||||||
return sock;
|
return sock;
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOpened()
|
public boolean isOpened()
|
||||||
{
|
{
|
||||||
return (serverSock != null) ? true : false;
|
return (serverSock != null) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// httpRequest
|
// httpRequest
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private ListenerList httpRequestListenerList = new ListenerList();
|
private ListenerList httpRequestListenerList = new ListenerList();
|
||||||
|
|
||||||
public void addRequestListener(HTTPRequestListener listener)
|
public void addRequestListener(HTTPRequestListener listener)
|
||||||
{
|
{
|
||||||
httpRequestListenerList.add(listener);
|
httpRequestListenerList.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeRequestListener(HTTPRequestListener listener)
|
public void removeRequestListener(HTTPRequestListener listener)
|
||||||
{
|
{
|
||||||
httpRequestListenerList.remove(listener);
|
httpRequestListenerList.remove(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void performRequestListener(HTTPRequest httpReq)
|
public void performRequestListener(HTTPRequest httpReq)
|
||||||
{
|
{
|
||||||
int listenerSize = httpRequestListenerList.size();
|
int listenerSize = httpRequestListenerList.size();
|
||||||
for (int n=0; n<listenerSize; n++) {
|
for (int n=0; n<listenerSize; n++) {
|
||||||
HTTPRequestListener listener = (HTTPRequestListener)httpRequestListenerList.get(n);
|
HTTPRequestListener listener = (HTTPRequestListener)httpRequestListenerList.get(n);
|
||||||
listener.httpRequestRecieved(httpReq);
|
listener.httpRequestRecieved(httpReq);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// run
|
// run
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private Thread httpServerThread = null;
|
private Thread httpServerThread = null;
|
||||||
|
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
if (isOpened() == false)
|
if (isOpened() == false)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Thread thisThread = Thread.currentThread();
|
Thread thisThread = Thread.currentThread();
|
||||||
|
|
||||||
while (httpServerThread == thisThread) {
|
while (httpServerThread == thisThread) {
|
||||||
Thread.yield();
|
Thread.yield();
|
||||||
Socket sock;
|
Socket sock;
|
||||||
try {
|
try {
|
||||||
Debug.message("accept ...");
|
Debug.message("accept ...");
|
||||||
sock = accept();
|
sock = accept();
|
||||||
if (sock != null)
|
if (sock != null)
|
||||||
Debug.message("sock = " + sock.getRemoteSocketAddress());
|
Debug.message("sock = " + sock.getRemoteSocketAddress());
|
||||||
@ -186,19 +242,20 @@ public class HTTPServer implements Runnable
|
|||||||
HTTPServerThread httpServThread = new HTTPServerThread(this, sock);
|
HTTPServerThread httpServThread = new HTTPServerThread(this, sock);
|
||||||
httpServThread.start();
|
httpServThread.start();
|
||||||
Debug.message("httpServThread ...");
|
Debug.message("httpServThread ...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean start()
|
public boolean start(){
|
||||||
{
|
StringBuffer name = new StringBuffer("Cyber.HTTPServer/");
|
||||||
httpServerThread = new Thread(this, "UPnP-HTTPServer");
|
name.append(serverSock.getLocalSocketAddress());
|
||||||
|
httpServerThread = new Thread(this,name.toString());
|
||||||
httpServerThread.start();
|
httpServerThread.start();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean stop()
|
public boolean stop()
|
||||||
{
|
{
|
||||||
httpServerThread = null;
|
httpServerThread = null;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,104 +1,133 @@
|
|||||||
/******************************************************************
|
/******************************************************************
|
||||||
*
|
|
||||||
* CyberUPnP for Java
|
|
||||||
*
|
|
||||||
* Copyright (C) Satoshi Konno 2002-2003
|
|
||||||
*
|
*
|
||||||
* File: HTTPServerList.java
|
* CyberUPnP for Java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Copyright (C) Satoshi Konno 2002-2003
|
||||||
*
|
*
|
||||||
* 05/08/03
|
* File: HTTPServerList.java
|
||||||
* - first revision.
|
*
|
||||||
*
|
* Revision;
|
||||||
******************************************************************/
|
*
|
||||||
|
* 05/08/03
|
||||||
|
* - first revision.
|
||||||
|
* 24/03/06
|
||||||
|
* - Stefano Lenzi:added debug information as request by Stephen More
|
||||||
|
*
|
||||||
|
******************************************************************/
|
||||||
|
|
||||||
|
package org.cybergarage.http;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import org.cybergarage.net.HostInterface;
|
||||||
|
import org.cybergarage.upnp.Device;
|
||||||
|
|
||||||
|
public class HTTPServerList extends Vector
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Constructor
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private InetAddress[] binds = null;
|
||||||
|
private int port = Device.HTTP_DEFAULT_PORT;
|
||||||
|
|
||||||
|
public HTTPServerList() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public HTTPServerList(InetAddress[] list, int port) {
|
||||||
|
this.binds = list;
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// 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 int open(){
|
||||||
|
InetAddress[] binds=this.binds;
|
||||||
|
String[] bindAddresses;
|
||||||
|
if(binds!=null){
|
||||||
|
bindAddresses = new String[binds.length];
|
||||||
|
for (int i = 0; i < binds.length; i++) {
|
||||||
|
bindAddresses[i] = binds[i].getHostAddress();
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
int nHostAddrs = HostInterface.getNHostAddresses();
|
||||||
|
bindAddresses = new String[nHostAddrs];
|
||||||
|
for (int n=0; n<nHostAddrs; n++) {
|
||||||
|
bindAddresses[n] = HostInterface.getHostAddress(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int j=0;
|
||||||
|
for (int i = 0; i < bindAddresses.length; i++) {
|
||||||
|
HTTPServer httpServer = new HTTPServer();
|
||||||
|
if((bindAddresses[i]==null) || (httpServer.open(bindAddresses[i], port) == false)) {
|
||||||
|
close();
|
||||||
|
clear();
|
||||||
|
}else{
|
||||||
|
add(httpServer);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean open(int port)
|
||||||
|
{
|
||||||
|
this.port=port;
|
||||||
|
return open()!=0;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
package org.cybergarage.http;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import org.cybergarage.net.*;
|
|
||||||
|
|
||||||
public class HTTPServerList extends Vector
|
|
||||||
{
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Constructor
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 2379889735659369065L;
|
|
||||||
|
|
||||||
public HTTPServerList()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Methods
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public void addRequestListener(HTTPRequestListener listener)
|
|
||||||
{
|
|
||||||
int nServers = size();
|
|
||||||
for (int n=0; n<nServers; n++) {
|
|
||||||
HTTPServer server = getHTTPServer(n);
|
|
||||||
server.addRequestListener(listener);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public HTTPServer getHTTPServer(int n)
|
|
||||||
{
|
|
||||||
return (HTTPServer)get(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// open/close
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public void close()
|
|
||||||
{
|
|
||||||
int nServers = size();
|
|
||||||
for (int n=0; n<nServers; n++) {
|
|
||||||
HTTPServer server = getHTTPServer(n);
|
|
||||||
server.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean open(int port)
|
|
||||||
{
|
|
||||||
int nHostAddrs = HostInterface.getNHostAddresses();
|
|
||||||
for (int n=0; n<nHostAddrs; n++) {
|
|
||||||
String bindAddr = HostInterface.getHostAddress(n);
|
|
||||||
HTTPServer httpServer = new HTTPServer();
|
|
||||||
if (httpServer.open(bindAddr, port) == false) {
|
|
||||||
close();
|
|
||||||
clear();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
add(httpServer);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// start/stop
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public void start()
|
|
||||||
{
|
|
||||||
int nServers = size();
|
|
||||||
for (int n=0; n<nServers; n++) {
|
|
||||||
HTTPServer server = getHTTPServer(n);
|
|
||||||
server.start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stop()
|
|
||||||
{
|
|
||||||
int nServers = size();
|
|
||||||
for (int n=0; n<nServers; n++) {
|
|
||||||
HTTPServer server = getHTTPServer(n);
|
|
||||||
server.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
package org.cybergarage.http;
|
package org.cybergarage.http;
|
||||||
|
|
||||||
import java.net.*;
|
import java.net.Socket;
|
||||||
|
|
||||||
public class HTTPServerThread extends Thread
|
public class HTTPServerThread extends Thread
|
||||||
{
|
{
|
||||||
@ -28,9 +28,9 @@ public class HTTPServerThread extends Thread
|
|||||||
|
|
||||||
public HTTPServerThread(HTTPServer httpServer, Socket sock)
|
public HTTPServerThread(HTTPServer httpServer, Socket sock)
|
||||||
{
|
{
|
||||||
|
super("Cyber.HTTPServerThread");
|
||||||
this.httpServer = httpServer;
|
this.httpServer = httpServer;
|
||||||
this.sock = sock;
|
this.sock = sock;
|
||||||
this.setDaemon(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002-2004
|
* Copyright (C) Satoshi Konno 2002-2004
|
||||||
*
|
*
|
||||||
* File: HTTPSocket.java
|
* File: HTTPSocket.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 12/12/02
|
* 12/12/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 03/11/04
|
* 03/11/04
|
||||||
* - Added the following methods about chunk size.
|
* - Added the following methods about chunk size.
|
||||||
* setChunkSize(), getChunkSize().
|
* setChunkSize(), getChunkSize().
|
||||||
@ -17,26 +17,32 @@
|
|||||||
* - Added a isOnlyHeader to post().
|
* - Added a isOnlyHeader to post().
|
||||||
* 03/02/05
|
* 03/02/05
|
||||||
* - Changed post() to suppot chunked stream.
|
* - Changed post() to suppot chunked stream.
|
||||||
|
* 06/10/05
|
||||||
|
* - Changed post() to add a Date headedr to the HTTPResponse before the posting.
|
||||||
|
* 07/07/05
|
||||||
|
* - Lee Peik Feng <pflee@users.sourceforge.net>
|
||||||
|
* - Fixed post() to output the chunk size as a hex string.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.http;
|
package org.cybergarage.http;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.InputStream;
|
||||||
import java.net.*;
|
import java.io.OutputStream;
|
||||||
import java.util.*;
|
import java.net.Socket;
|
||||||
|
import java.util.Calendar;
|
||||||
public class HTTPSocket
|
|
||||||
{
|
public class HTTPSocket
|
||||||
////////////////////////////////////////////////
|
{
|
||||||
// Constructor
|
////////////////////////////////////////////////
|
||||||
////////////////////////////////////////////////
|
// Constructor
|
||||||
|
////////////////////////////////////////////////
|
||||||
public HTTPSocket(Socket socket)
|
|
||||||
{
|
public HTTPSocket(Socket socket)
|
||||||
setSocket(socket);
|
{
|
||||||
|
setSocket(socket);
|
||||||
open();
|
open();
|
||||||
}
|
}
|
||||||
|
|
||||||
public HTTPSocket(HTTPSocket socket)
|
public HTTPSocket(HTTPSocket socket)
|
||||||
{
|
{
|
||||||
@ -45,11 +51,11 @@ public class HTTPSocket
|
|||||||
setOutputStream(socket.getOutputStream());
|
setOutputStream(socket.getOutputStream());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void finalize()
|
public void finalize()
|
||||||
{
|
{
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Socket
|
// Socket
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -86,27 +92,27 @@ public class HTTPSocket
|
|||||||
|
|
||||||
private InputStream sockIn = null;
|
private InputStream sockIn = null;
|
||||||
private OutputStream sockOut = null;
|
private OutputStream sockOut = null;
|
||||||
|
|
||||||
private void setInputStream(InputStream in)
|
private void setInputStream(InputStream in)
|
||||||
{
|
{
|
||||||
sockIn = in;
|
sockIn = in;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InputStream getInputStream()
|
public InputStream getInputStream()
|
||||||
{
|
{
|
||||||
return sockIn;
|
return sockIn;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setOutputStream(OutputStream out)
|
private void setOutputStream(OutputStream out)
|
||||||
{
|
{
|
||||||
sockOut = out;
|
sockOut = out;
|
||||||
}
|
}
|
||||||
|
|
||||||
private OutputStream getOutputStream()
|
private OutputStream getOutputStream()
|
||||||
{
|
{
|
||||||
return sockOut;
|
return sockOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// open/close
|
// open/close
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -119,6 +125,7 @@ public class HTTPSocket
|
|||||||
sockOut = sock.getOutputStream();
|
sockOut = sock.getOutputStream();
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
|
//TODO Add blacklistening of the UPnP Device
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -140,20 +147,22 @@ public class HTTPSocket
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// post
|
// post
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private boolean post(HTTPResponse httpRes, byte content[], long contentOffset, long contentLength, boolean isOnlyHeader)
|
private boolean post(HTTPResponse httpRes, byte content[], long contentOffset, long contentLength, boolean isOnlyHeader)
|
||||||
{
|
{
|
||||||
|
//TODO Check for bad HTTP agents, this method may be list for IOInteruptedException and for blacklistening
|
||||||
httpRes.setDate(Calendar.getInstance());
|
httpRes.setDate(Calendar.getInstance());
|
||||||
OutputStream out = getOutputStream();
|
|
||||||
|
OutputStream out = getOutputStream();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
httpRes.setContentLength(contentLength);
|
httpRes.setContentLength(contentLength);
|
||||||
|
|
||||||
out.write(httpRes.getHeader().getBytes());
|
out.write(httpRes.getHeader().getBytes());
|
||||||
out.write(HTTP.CRLF.getBytes());
|
out.write(HTTP.CRLF.getBytes());
|
||||||
if (isOnlyHeader == true) {
|
if (isOnlyHeader == true) {
|
||||||
out.flush();
|
out.flush();
|
||||||
return true;
|
return true;
|
||||||
@ -162,7 +171,8 @@ public class HTTPSocket
|
|||||||
boolean isChunkedResponse = httpRes.isChunked();
|
boolean isChunkedResponse = httpRes.isChunked();
|
||||||
|
|
||||||
if (isChunkedResponse == true) {
|
if (isChunkedResponse == true) {
|
||||||
String chunSizeBuf = Long.toString(contentLength);
|
// Thanks for Lee Peik Feng <pflee@users.sourceforge.net> (07/07/05)
|
||||||
|
String chunSizeBuf = Long.toHexString(contentLength);
|
||||||
out.write(chunSizeBuf.getBytes());
|
out.write(chunSizeBuf.getBytes());
|
||||||
out.write(HTTP.CRLF.getBytes());
|
out.write(HTTP.CRLF.getBytes());
|
||||||
}
|
}
|
||||||
@ -174,20 +184,22 @@ public class HTTPSocket
|
|||||||
out.write("0".getBytes());
|
out.write("0".getBytes());
|
||||||
out.write(HTTP.CRLF.getBytes());
|
out.write(HTTP.CRLF.getBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
out.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
//Debug.warning(e);
|
//Debug.warning(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean post(HTTPResponse httpRes, InputStream in, long contentOffset, long contentLength, boolean isOnlyHeader)
|
private boolean post(HTTPResponse httpRes, InputStream in, long contentOffset, long contentLength, boolean isOnlyHeader)
|
||||||
{
|
{
|
||||||
|
//TODO Check for bad HTTP agents, this method may be list for IOInteruptedException and for blacklistening
|
||||||
httpRes.setDate(Calendar.getInstance());
|
httpRes.setDate(Calendar.getInstance());
|
||||||
|
|
||||||
OutputStream out = getOutputStream();
|
OutputStream out = getOutputStream();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -213,7 +225,8 @@ public class HTTPSocket
|
|||||||
int readLen = in.read(readBuf, 0, (int)readSize);
|
int readLen = in.read(readBuf, 0, (int)readSize);
|
||||||
while (0 < readLen && readCnt < contentLength) {
|
while (0 < readLen && readCnt < contentLength) {
|
||||||
if (isChunkedResponse == true) {
|
if (isChunkedResponse == true) {
|
||||||
String chunSizeBuf = Long.toString(readLen);
|
// Thanks for Lee Peik Feng <pflee@users.sourceforge.net> (07/07/05)
|
||||||
|
String chunSizeBuf = Long.toHexString(readLen);
|
||||||
out.write(chunSizeBuf.getBytes());
|
out.write(chunSizeBuf.getBytes());
|
||||||
out.write(HTTP.CRLF.getBytes());
|
out.write(HTTP.CRLF.getBytes());
|
||||||
}
|
}
|
||||||
@ -242,6 +255,7 @@ public class HTTPSocket
|
|||||||
|
|
||||||
public boolean post(HTTPResponse httpRes, long contentOffset, long contentLength, boolean isOnlyHeader)
|
public boolean post(HTTPResponse httpRes, long contentOffset, long contentLength, boolean isOnlyHeader)
|
||||||
{
|
{
|
||||||
|
//TODO Close if Connection != keep-alive
|
||||||
if (httpRes.hasContentInputStream() == true)
|
if (httpRes.hasContentInputStream() == true)
|
||||||
return post(httpRes,httpRes.getContentInputStream(), contentOffset, contentLength, isOnlyHeader);
|
return post(httpRes,httpRes.getContentInputStream(), contentOffset, contentLength, isOnlyHeader);
|
||||||
return post(httpRes,httpRes.getContent(), contentOffset, contentLength, isOnlyHeader);
|
return post(httpRes,httpRes.getContent(), contentOffset, contentLength, isOnlyHeader);
|
||||||
|
@ -25,9 +25,9 @@
|
|||||||
|
|
||||||
package org.cybergarage.http;
|
package org.cybergarage.http;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
import org.cybergarage.util.*;
|
import org.cybergarage.util.Debug;
|
||||||
|
|
||||||
public class HTTPStatus
|
public class HTTPStatus
|
||||||
{
|
{
|
||||||
|
@ -15,12 +15,10 @@
|
|||||||
|
|
||||||
package org.cybergarage.http;
|
package org.cybergarage.http;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Vector;
|
||||||
|
|
||||||
public class ParameterList extends Vector
|
public class ParameterList extends Vector
|
||||||
{
|
{
|
||||||
private static final long serialVersionUID = -6026765325018137641L;
|
|
||||||
|
|
||||||
public ParameterList()
|
public ParameterList()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,16 @@
|
|||||||
|
|
||||||
package org.cybergarage.net;
|
package org.cybergarage.net;
|
||||||
|
|
||||||
import java.net.*;
|
import java.net.Inet4Address;
|
||||||
import java.util.*;
|
import java.net.Inet6Address;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.NetworkInterface;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import org.cybergarage.util.Debug;
|
||||||
|
|
||||||
public class HostInterface
|
public class HostInterface
|
||||||
{
|
{
|
||||||
@ -43,6 +51,9 @@ public class HostInterface
|
|||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private static String ifAddress = "";
|
private static String ifAddress = "";
|
||||||
|
public final static int IPV4_BITMASK = 0x0001;
|
||||||
|
public final static int IPV6_BITMASK = 0x0010;
|
||||||
|
public final static int LOCAL_BITMASK = 0x0100;
|
||||||
|
|
||||||
public final static void setInterface(String ifaddr)
|
public final static void setInterface(String ifaddr)
|
||||||
{
|
{
|
||||||
@ -101,10 +112,62 @@ public class HostInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(Exception e){};
|
catch(Exception e){
|
||||||
|
Debug.warning(e);
|
||||||
|
};
|
||||||
return nHostAddrs;
|
return nHostAddrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param ipfilter
|
||||||
|
* @param interfaces
|
||||||
|
* @return
|
||||||
|
* @since 1.8.0
|
||||||
|
* @author Stefano "Kismet" Lenzi <kismet.sl@gmail.com>
|
||||||
|
*/
|
||||||
|
public final static InetAddress[] getInetAddress(int ipfilter,String[] interfaces){
|
||||||
|
Enumeration nis;
|
||||||
|
if(interfaces!=null){
|
||||||
|
Vector iflist = new Vector();
|
||||||
|
for (int i = 0; i < interfaces.length; i++) {
|
||||||
|
NetworkInterface ni;
|
||||||
|
try {
|
||||||
|
ni = NetworkInterface.getByName(interfaces[i]);
|
||||||
|
} catch (SocketException e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(ni != null) iflist.add(ni);
|
||||||
|
|
||||||
|
}
|
||||||
|
nis = iflist.elements();
|
||||||
|
}else{
|
||||||
|
try {
|
||||||
|
nis = NetworkInterface.getNetworkInterfaces();
|
||||||
|
} catch (SocketException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ArrayList addresses = new ArrayList();
|
||||||
|
while (nis.hasMoreElements()){
|
||||||
|
NetworkInterface ni = (NetworkInterface)nis.nextElement();
|
||||||
|
Enumeration addrs = ni.getInetAddresses();
|
||||||
|
while (addrs.hasMoreElements()) {
|
||||||
|
InetAddress addr = (InetAddress)addrs.nextElement();
|
||||||
|
if(((ipfilter & LOCAL_BITMASK)==0) && addr.isLoopbackAddress())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (((ipfilter & IPV4_BITMASK)!=0) && addr instanceof Inet4Address ) {
|
||||||
|
addresses.add(addr);
|
||||||
|
}else if (((ipfilter & IPV6_BITMASK)!=0)&& addr instanceof InetAddress) {
|
||||||
|
addresses.add(addr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (InetAddress[]) addresses.toArray(new InetAddress[]{});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public final static String getHostAddress(int n)
|
public final static String getHostAddress(int n)
|
||||||
{
|
{
|
||||||
if (hasAssignedInterface() == true)
|
if (hasAssignedInterface() == true)
|
||||||
@ -143,7 +206,9 @@ public class HostInterface
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
InetAddress addr = InetAddress.getByName(host);
|
InetAddress addr = InetAddress.getByName(host);
|
||||||
return (addr instanceof Inet6Address);
|
if (addr instanceof Inet6Address)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
catch (Exception e) {}
|
catch (Exception e) {}
|
||||||
return false;
|
return false;
|
||||||
@ -153,7 +218,9 @@ public class HostInterface
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
InetAddress addr = InetAddress.getByName(host);
|
InetAddress addr = InetAddress.getByName(host);
|
||||||
return (addr instanceof Inet4Address);
|
if (addr instanceof Inet4Address)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
catch (Exception e) {}
|
catch (Exception e) {}
|
||||||
return false;
|
return false;
|
||||||
|
@ -15,7 +15,8 @@
|
|||||||
|
|
||||||
package org.cybergarage.soap;
|
package org.cybergarage.soap;
|
||||||
|
|
||||||
import org.cybergarage.xml.*;
|
import org.cybergarage.xml.Node;
|
||||||
|
import org.cybergarage.xml.Parser;
|
||||||
|
|
||||||
public class SOAP
|
public class SOAP
|
||||||
{
|
{
|
||||||
|
@ -20,11 +20,15 @@
|
|||||||
|
|
||||||
package org.cybergarage.soap;
|
package org.cybergarage.soap;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.ByteArrayInputStream;
|
||||||
|
|
||||||
import org.cybergarage.http.*;
|
import org.cybergarage.http.HTTP;
|
||||||
import org.cybergarage.xml.*;
|
import org.cybergarage.http.HTTPRequest;
|
||||||
import org.cybergarage.util.*;
|
import org.cybergarage.http.HTTPResponse;
|
||||||
|
import org.cybergarage.util.Debug;
|
||||||
|
import org.cybergarage.xml.Node;
|
||||||
|
import org.cybergarage.xml.Parser;
|
||||||
|
import org.cybergarage.xml.ParserException;
|
||||||
|
|
||||||
public class SOAPRequest extends HTTPRequest
|
public class SOAPRequest extends HTTPRequest
|
||||||
{
|
{
|
||||||
@ -172,12 +176,12 @@ public class SOAPRequest extends HTTPRequest
|
|||||||
|
|
||||||
public void print()
|
public void print()
|
||||||
{
|
{
|
||||||
System.out.println(toString());
|
Debug.message(toString());
|
||||||
if (hasContent() == true)
|
if (hasContent() == true)
|
||||||
return;
|
return;
|
||||||
Node rootElem = getRootNode();
|
Node rootElem = getRootNode();
|
||||||
if (rootElem == null)
|
if (rootElem == null)
|
||||||
return;
|
return;
|
||||||
System.out.println(rootElem.toString());
|
Debug.message(rootElem.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002
|
* Copyright (C) Satoshi Konno 2002
|
||||||
*
|
*
|
||||||
* File: SOAPResponse.java
|
* File: SOAPResponse.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 12/17/02
|
* 12/17/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 02/13/04
|
* 02/13/04
|
||||||
* - Ralf G. R. Bergs <Ralf@Ber.gs>, Inma Marin Lopez <inma@dif.um.es>.
|
* - Ralf G. R. Bergs <Ralf@Ber.gs>, Inma Marin Lopez <inma@dif.um.es>.
|
||||||
* - Added XML header, <?xml version="1.0"?> to setContent().
|
* - Added XML header, <?xml version="1.0"?> to setContent().
|
||||||
@ -17,23 +17,25 @@
|
|||||||
* - Changed the XML header to <?xml version="1.0" encoding="utf-8"?> in setContent().
|
* - Changed the XML header to <?xml version="1.0" encoding="utf-8"?> in setContent().
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.soap;
|
package org.cybergarage.soap;
|
||||||
|
|
||||||
import org.cybergarage.http.*;
|
import org.cybergarage.http.HTTPResponse;
|
||||||
import org.cybergarage.xml.*;
|
import org.cybergarage.util.Debug;
|
||||||
|
import org.cybergarage.xml.Node;
|
||||||
public class SOAPResponse extends HTTPResponse
|
import org.cybergarage.xml.XML;
|
||||||
{
|
|
||||||
////////////////////////////////////////////////
|
public class SOAPResponse extends HTTPResponse
|
||||||
// Constructor
|
{
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
// Constructor
|
||||||
public SOAPResponse()
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public SOAPResponse()
|
||||||
{
|
{
|
||||||
setRootNode(SOAP.createEnvelopeBodyNode());
|
setRootNode(SOAP.createEnvelopeBodyNode());
|
||||||
setContentType(XML.CONTENT_TYPE);
|
setContentType(XML.CONTENT_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SOAPResponse(HTTPResponse httpRes)
|
public SOAPResponse(HTTPResponse httpRes)
|
||||||
{
|
{
|
||||||
@ -49,25 +51,25 @@ public class SOAPResponse extends HTTPResponse
|
|||||||
setContentType(XML.CONTENT_TYPE);
|
setContentType(XML.CONTENT_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Node
|
// Node
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private Node rootNode;
|
private Node rootNode;
|
||||||
|
|
||||||
private void setRootNode(Node node)
|
private void setRootNode(Node node)
|
||||||
{
|
{
|
||||||
rootNode = node;
|
rootNode = node;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Node getRootNode()
|
private Node getRootNode()
|
||||||
{
|
{
|
||||||
return rootNode;
|
return rootNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// SOAP Basic
|
// SOAP Basic
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public void setEnvelopeNode(Node node)
|
public void setEnvelopeNode(Node node)
|
||||||
{
|
{
|
||||||
@ -78,88 +80,88 @@ public class SOAPResponse extends HTTPResponse
|
|||||||
{
|
{
|
||||||
return getRootNode();
|
return getRootNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getBodyNode()
|
public Node getBodyNode()
|
||||||
{
|
{
|
||||||
Node envNode = getEnvelopeNode();
|
Node envNode = getEnvelopeNode();
|
||||||
if (envNode == null)
|
if (envNode == null)
|
||||||
return null;
|
return null;
|
||||||
return envNode.getNodeEndsWith(SOAP.BODY);
|
return envNode.getNodeEndsWith(SOAP.BODY);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getMethodResponseNode(String name)
|
public Node getMethodResponseNode(String name)
|
||||||
{
|
{
|
||||||
Node bodyNode = getBodyNode();
|
Node bodyNode = getBodyNode();
|
||||||
if (bodyNode == null)
|
if (bodyNode == null)
|
||||||
return null;
|
return null;
|
||||||
String methodResName = name + SOAP.RESPONSE;
|
String methodResName = name + SOAP.RESPONSE;
|
||||||
return bodyNode.getNodeEndsWith(methodResName);
|
return bodyNode.getNodeEndsWith(methodResName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getFaultNode()
|
public Node getFaultNode()
|
||||||
{
|
{
|
||||||
Node bodyNode = getBodyNode();
|
Node bodyNode = getBodyNode();
|
||||||
if (bodyNode == null)
|
if (bodyNode == null)
|
||||||
return null;
|
return null;
|
||||||
return bodyNode.getNodeEndsWith(SOAP.FAULT);
|
return bodyNode.getNodeEndsWith(SOAP.FAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getFaultCodeNode()
|
public Node getFaultCodeNode()
|
||||||
{
|
{
|
||||||
Node faultNode = getFaultNode();
|
Node faultNode = getFaultNode();
|
||||||
if (faultNode == null)
|
if (faultNode == null)
|
||||||
return null;
|
return null;
|
||||||
return faultNode.getNodeEndsWith(SOAP.FAULT_CODE);
|
return faultNode.getNodeEndsWith(SOAP.FAULT_CODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getFaultStringNode()
|
public Node getFaultStringNode()
|
||||||
{
|
{
|
||||||
Node faultNode = getFaultNode();
|
Node faultNode = getFaultNode();
|
||||||
if (faultNode == null)
|
if (faultNode == null)
|
||||||
return null;
|
return null;
|
||||||
return faultNode.getNodeEndsWith(SOAP.FAULT_STRING);
|
return faultNode.getNodeEndsWith(SOAP.FAULT_STRING);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getFaultActorNode()
|
public Node getFaultActorNode()
|
||||||
{
|
{
|
||||||
Node faultNode = getFaultNode();
|
Node faultNode = getFaultNode();
|
||||||
if (faultNode == null)
|
if (faultNode == null)
|
||||||
return null;
|
return null;
|
||||||
return faultNode.getNodeEndsWith(SOAP.FAULTACTOR);
|
return faultNode.getNodeEndsWith(SOAP.FAULTACTOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getFaultDetailNode()
|
public Node getFaultDetailNode()
|
||||||
{
|
{
|
||||||
Node faultNode = getFaultNode();
|
Node faultNode = getFaultNode();
|
||||||
if (faultNode == null)
|
if (faultNode == null)
|
||||||
return null;
|
return null;
|
||||||
return faultNode.getNodeEndsWith(SOAP.DETAIL);
|
return faultNode.getNodeEndsWith(SOAP.DETAIL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFaultCode()
|
public String getFaultCode()
|
||||||
{
|
{
|
||||||
Node node = getFaultCodeNode();
|
Node node = getFaultCodeNode();
|
||||||
if (node == null)
|
if (node == null)
|
||||||
return "";
|
return "";
|
||||||
return node.getValue();
|
return node.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFaultString()
|
public String getFaultString()
|
||||||
{
|
{
|
||||||
Node node = getFaultStringNode();
|
Node node = getFaultStringNode();
|
||||||
if (node == null)
|
if (node == null)
|
||||||
return "";
|
return "";
|
||||||
return node.getValue();
|
return node.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFaultActor()
|
public String getFaultActor()
|
||||||
{
|
{
|
||||||
Node node = getFaultActorNode();
|
Node node = getFaultActorNode();
|
||||||
if (node == null)
|
if (node == null)
|
||||||
return "";
|
return "";
|
||||||
return node.getValue();
|
return node.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// XML Contents
|
// XML Contents
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -180,12 +182,12 @@ public class SOAPResponse extends HTTPResponse
|
|||||||
|
|
||||||
public void print()
|
public void print()
|
||||||
{
|
{
|
||||||
System.out.println(toString());
|
Debug.message(toString());
|
||||||
if (hasContent() == true)
|
if (hasContent() == true)
|
||||||
return;
|
return;
|
||||||
Node rootElem = getRootNode();
|
Node rootElem = getRootNode();
|
||||||
if (rootElem == null)
|
if (rootElem == null)
|
||||||
return;
|
return;
|
||||||
System.out.println(rootElem.toString());
|
Debug.message(rootElem.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002-2004
|
* Copyright (C) Satoshi Konno 2002-2004
|
||||||
*
|
*
|
||||||
* File: Action.java
|
* File: Action.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 12/05/02
|
* 12/05/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 08/30/03
|
* 08/30/03
|
||||||
* - Gordano Sassaroli <sassarol@cefriel.it>
|
* - Gordano Sassaroli <sassarol@cefriel.it>
|
||||||
* - Problem : When invoking an action that has at least one out parameter, an error message is returned
|
* - Problem : When invoking an action that has at least one out parameter, an error message is returned
|
||||||
@ -22,63 +22,84 @@
|
|||||||
* 07/09/04
|
* 07/09/04
|
||||||
* - Thanks for Dimas <cyberrate@users.sourceforge.net> and Stefano Lenzi <kismet-sl@users.sourceforge.net>
|
* - Thanks for Dimas <cyberrate@users.sourceforge.net> and Stefano Lenzi <kismet-sl@users.sourceforge.net>
|
||||||
* - Changed postControlAction() to set the status code to the UPnPStatus.
|
* - Changed postControlAction() to set the status code to the UPnPStatus.
|
||||||
|
* 04/12/06
|
||||||
|
* - Added setUserData() and getUserData() to set a user original data object.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp;
|
|
||||||
|
|
||||||
import org.cybergarage.xml.*;
|
package org.cybergarage.upnp;
|
||||||
import org.cybergarage.util.*;
|
import java.util.Iterator;
|
||||||
|
|
||||||
import org.cybergarage.upnp.xml.*;
|
import org.cybergarage.upnp.control.ActionListener;
|
||||||
import org.cybergarage.upnp.control.*;
|
import org.cybergarage.upnp.control.ActionRequest;
|
||||||
|
import org.cybergarage.upnp.control.ActionResponse;
|
||||||
public class Action
|
import org.cybergarage.upnp.control.ControlResponse;
|
||||||
{
|
import org.cybergarage.upnp.xml.ActionData;
|
||||||
////////////////////////////////////////////////
|
import org.cybergarage.util.Debug;
|
||||||
// Constants
|
import org.cybergarage.util.Mutex;
|
||||||
////////////////////////////////////////////////
|
import org.cybergarage.xml.Node;
|
||||||
|
|
||||||
public final static String ELEM_NAME = "action";
|
public class Action
|
||||||
|
{
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Member
|
// Constants
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private Node serviceNode;
|
public final static String ELEM_NAME = "action";
|
||||||
private Node actionNode;
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
private Node getServiceNode()
|
// Member
|
||||||
{
|
////////////////////////////////////////////////
|
||||||
return serviceNode;
|
|
||||||
}
|
private Node serviceNode;
|
||||||
|
private Node actionNode;
|
||||||
public Service getService()
|
|
||||||
{
|
private Node getServiceNode()
|
||||||
return new Service(getServiceNode());
|
{
|
||||||
}
|
return serviceNode;
|
||||||
|
}
|
||||||
public Node getActionNode()
|
|
||||||
{
|
public Service getService()
|
||||||
return actionNode;
|
{
|
||||||
}
|
return new Service(getServiceNode());
|
||||||
|
}
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Constructor
|
void setService(Service s){
|
||||||
////////////////////////////////////////////////
|
serviceNode=s.getServiceNode();
|
||||||
|
/*To ensure integrity of the XML structure*/
|
||||||
public Action(Node serviceNode, Node actionNode)
|
Iterator i = getArgumentList().iterator();
|
||||||
{
|
while (i.hasNext()) {
|
||||||
this.serviceNode = serviceNode;
|
Argument arg = (Argument) i.next();
|
||||||
this.actionNode = actionNode;
|
arg.setService(s);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node getActionNode()
|
||||||
|
{
|
||||||
|
return actionNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Constructor
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
public Action(Node serviceNode){
|
||||||
|
//TODO Test
|
||||||
|
this.serviceNode = serviceNode;
|
||||||
|
this.actionNode = new Node(Action.ELEM_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Action(Node serviceNode, Node actionNode)
|
||||||
|
{
|
||||||
|
this.serviceNode = serviceNode;
|
||||||
|
this.actionNode = actionNode;
|
||||||
|
}
|
||||||
|
|
||||||
public Action(Action action)
|
public Action(Action action)
|
||||||
{
|
{
|
||||||
this.serviceNode = action.getServiceNode();
|
this.serviceNode = action.getServiceNode();
|
||||||
this.actionNode = action.getActionNode();
|
this.actionNode = action.getActionNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Mutex
|
// Mutex
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -95,50 +116,67 @@ public class Action
|
|||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// isActionNode
|
// isActionNode
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public static boolean isActionNode(Node node)
|
public static boolean isActionNode(Node node)
|
||||||
{
|
{
|
||||||
return Action.ELEM_NAME.equals(node.getName());
|
return Action.ELEM_NAME.equals(node.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// name
|
// name
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private final static String NAME = "name";
|
private final static String NAME = "name";
|
||||||
|
|
||||||
public void setName(String value)
|
public void setName(String value)
|
||||||
{
|
{
|
||||||
getActionNode().setNode(NAME, value);
|
getActionNode().setNode(NAME, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName()
|
public String getName()
|
||||||
{
|
{
|
||||||
return getActionNode().getNodeValue(NAME);
|
return getActionNode().getNodeValue(NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// argumentList
|
// argumentList
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public ArgumentList getArgumentList()
|
public ArgumentList getArgumentList()
|
||||||
{
|
{
|
||||||
ArgumentList argumentList = new ArgumentList();
|
ArgumentList argumentList = new ArgumentList();
|
||||||
Node argumentListNode = getActionNode().getNode(ArgumentList.ELEM_NAME);
|
Node argumentListNode = getActionNode().getNode(ArgumentList.ELEM_NAME);
|
||||||
if (argumentListNode == null)
|
if (argumentListNode == null)
|
||||||
return argumentList;
|
return argumentList;
|
||||||
int nodeCnt = argumentListNode.getNNodes();
|
int nodeCnt = argumentListNode.getNNodes();
|
||||||
for (int n=0; n<nodeCnt; n++) {
|
for (int n=0; n<nodeCnt; n++) {
|
||||||
Node node = argumentListNode.getNode(n);
|
Node node = argumentListNode.getNode(n);
|
||||||
if (Argument.isArgumentNode(node) == false)
|
if (Argument.isArgumentNode(node) == false)
|
||||||
continue;
|
continue;
|
||||||
Argument argument = new Argument(getServiceNode(), node);
|
Argument argument = new Argument(getServiceNode(), node);
|
||||||
argumentList.add(argument);
|
argumentList.add(argument);
|
||||||
}
|
}
|
||||||
return argumentList;
|
return argumentList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setArgumentList(ArgumentList al){
|
||||||
|
Node argumentListNode = getActionNode().getNode(ArgumentList.ELEM_NAME);
|
||||||
|
if (argumentListNode == null){
|
||||||
|
argumentListNode = new Node(ArgumentList.ELEM_NAME);
|
||||||
|
getActionNode().addNode(argumentListNode);
|
||||||
|
}else{
|
||||||
|
argumentListNode.removeAllNodes();
|
||||||
|
}
|
||||||
|
Iterator i = al.iterator();
|
||||||
|
while (i.hasNext()) {
|
||||||
|
Argument a = (Argument) i.next();
|
||||||
|
a.setService(getService());
|
||||||
|
argumentListNode.addNode(a.getArgumentNode());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArgumentList getInputArgumentList()
|
public ArgumentList getInputArgumentList()
|
||||||
@ -179,15 +217,40 @@ public class Action
|
|||||||
if (argName == null)
|
if (argName == null)
|
||||||
continue;
|
continue;
|
||||||
if (name.equals(argName) == true)
|
if (name.equals(argName) == true)
|
||||||
return arg;
|
return arg;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated You should use one of the following methods instead:<br />
|
||||||
|
* - {@link #setInArgumentValues(ArgumentList)} <br/>
|
||||||
|
* - {@link #setOutArgumentValues(ArgumentList)}
|
||||||
|
*/
|
||||||
public void setArgumentValues(ArgumentList argList)
|
public void setArgumentValues(ArgumentList argList)
|
||||||
{
|
{
|
||||||
getArgumentList().set(argList);
|
getArgumentList().set(argList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param argList
|
||||||
|
* @since 1.8.0
|
||||||
|
*/
|
||||||
|
public void setInArgumentValues(ArgumentList argList)
|
||||||
|
{
|
||||||
|
getArgumentList().setReqArgs(argList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param argList
|
||||||
|
* @since 1.8.0
|
||||||
|
*/
|
||||||
|
public void setOutArgumentValues(ArgumentList argList)
|
||||||
|
{
|
||||||
|
getArgumentList().setResArgs(argList);
|
||||||
|
}
|
||||||
|
|
||||||
public void setArgumentValue(String name, String value)
|
public void setArgumentValue(String name, String value)
|
||||||
{
|
{
|
||||||
@ -323,7 +386,12 @@ public class Action
|
|||||||
if (ctrlRes.isSuccessful() == false)
|
if (ctrlRes.isSuccessful() == false)
|
||||||
return false;
|
return false;
|
||||||
ArgumentList outArgList = ctrlRes.getResponse();
|
ArgumentList outArgList = ctrlRes.getResponse();
|
||||||
actionArgList.set(outArgList);
|
try {
|
||||||
|
actionArgList.setResArgs(outArgList);
|
||||||
|
} catch (IllegalArgumentException ex){
|
||||||
|
setStatus(UPnPStatus.INVALID_ARGS,"Action succesfully delivered but invalid arguments returned.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -367,4 +435,19 @@ public class Action
|
|||||||
return upnpStatus;
|
return upnpStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// userData
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private Object userData = null;
|
||||||
|
|
||||||
|
public void setUserData(Object data)
|
||||||
|
{
|
||||||
|
userData = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getUserData()
|
||||||
|
{
|
||||||
|
return userData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,15 +15,14 @@
|
|||||||
|
|
||||||
package org.cybergarage.upnp;
|
package org.cybergarage.upnp;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Vector;
|
||||||
|
|
||||||
public class ActionList extends Vector
|
public class ActionList extends Vector
|
||||||
{
|
{
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Constants
|
// Constants
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private static final long serialVersionUID = 1965922721316119846L;
|
|
||||||
public final static String ELEM_NAME = "actionList";
|
public final static String ELEM_NAME = "actionList";
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
@ -5,26 +5,26 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002-2004
|
* Copyright (C) Satoshi Konno 2002-2004
|
||||||
*
|
*
|
||||||
* File: AllowedValue.java
|
* File: AllowedValue.java
|
||||||
*
|
*
|
||||||
* Revision:
|
* Revision:
|
||||||
*
|
*
|
||||||
* 03/27/04
|
* 03/27/04
|
||||||
* - first revision.
|
* - first revision.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp;
|
|
||||||
|
|
||||||
import org.cybergarage.xml.*;
|
package org.cybergarage.upnp;
|
||||||
|
|
||||||
public class AllowedValue
|
import org.cybergarage.xml.Node;
|
||||||
{
|
|
||||||
////////////////////////////////////////////////
|
public class AllowedValue
|
||||||
// Constants
|
{
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
// Constants
|
||||||
public final static String ELEM_NAME = "allowedValue";
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public final static String ELEM_NAME = "allowedValue";
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Member
|
// Member
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -45,15 +45,30 @@ public class AllowedValue
|
|||||||
allowedValueNode = node;
|
allowedValueNode = node;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
/**
|
||||||
// isAllowedValueNode
|
* Create an AllowedValue by the value String,
|
||||||
////////////////////////////////////////////////
|
* and will create the Node structure by itself
|
||||||
|
*
|
||||||
public static boolean isAllowedValueNode(Node node)
|
* @param value The value that will be associate to thi object
|
||||||
{
|
*
|
||||||
return ELEM_NAME.equals(node.getName());
|
* @author Stefano "Kismet" Lenzi - kismet-sl@users.sourceforge.net - 2005
|
||||||
}
|
*/
|
||||||
|
public AllowedValue(String value) {
|
||||||
|
|
||||||
|
//TODO Some test are done not stable
|
||||||
|
allowedValueNode = new Node(ELEM_NAME); //better (twa)
|
||||||
|
setValue(value); //better (twa)
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// isAllowedValueNode
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public static boolean isAllowedValueNode(Node node)
|
||||||
|
{
|
||||||
|
return ELEM_NAME.equals(node.getName());
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Value
|
// Value
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
@ -5,29 +5,29 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002-2004
|
* Copyright (C) Satoshi Konno 2002-2004
|
||||||
*
|
*
|
||||||
* File: AllowedValueList.java
|
* File: AllowedValueList.java
|
||||||
*
|
*
|
||||||
* Revision:
|
* Revision:
|
||||||
*
|
*
|
||||||
* 03/27/04
|
* 03/27/04
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 02/28/05
|
* 02/28/05
|
||||||
* - Changed to use AllowedValue instead of String as the member.
|
* - Changed to use AllowedValue instead of String as the member.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp;
|
package org.cybergarage.upnp;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Iterator;
|
||||||
|
import java.util.Vector;
|
||||||
public class AllowedValueList extends Vector
|
|
||||||
{
|
public class AllowedValueList extends Vector
|
||||||
////////////////////////////////////////////////
|
{
|
||||||
// Constants
|
////////////////////////////////////////////////
|
||||||
////////////////////////////////////////////////
|
// Constants
|
||||||
|
////////////////////////////////////////////////
|
||||||
private static final long serialVersionUID = 5740394642751180992L;
|
|
||||||
public final static String ELEM_NAME = "allowedValueList";
|
public final static String ELEM_NAME = "allowedValueList";
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
@ -36,6 +36,14 @@ public class AllowedValueList extends Vector
|
|||||||
public AllowedValueList()
|
public AllowedValueList()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AllowedValueList(String[] values) {
|
||||||
|
for (int i = 0; i < values.length; i++) {
|
||||||
|
add(new AllowedValue(values[i]));
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Methods
|
// Methods
|
||||||
@ -46,4 +54,12 @@ public class AllowedValueList extends Vector
|
|||||||
return (AllowedValue)get(n);
|
return (AllowedValue)get(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isAllowed(String v){
|
||||||
|
for (Iterator i = this.iterator(); i.hasNext();) {
|
||||||
|
AllowedValue av = (AllowedValue) i.next();
|
||||||
|
if(av.getValue().equals(v))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,71 +5,86 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002-2004
|
* Copyright (C) Satoshi Konno 2002-2004
|
||||||
*
|
*
|
||||||
* File: AllowedValueRange.java
|
* File: AllowedValueRange.java
|
||||||
*
|
*
|
||||||
* Revision:
|
* Revision:
|
||||||
*
|
*
|
||||||
* 03/27/04
|
* 03/27/04
|
||||||
* - first revision.
|
* - first revision.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp;
|
package org.cybergarage.upnp;
|
||||||
|
|
||||||
import org.cybergarage.xml.*;
|
import org.cybergarage.xml.Node;
|
||||||
|
|
||||||
public class AllowedValueRange
|
public class AllowedValueRange
|
||||||
{
|
{
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Constants
|
// Constants
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public final static String ELEM_NAME = "allowedValueRange";
|
public final static String ELEM_NAME = "allowedValueRange";
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Member
|
// Member
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private Node allowedValueRangeNode;
|
private Node allowedValueRangeNode;
|
||||||
|
|
||||||
public Node getAllowedValueRangeNode()
|
public Node getAllowedValueRangeNode()
|
||||||
{
|
{
|
||||||
return allowedValueRangeNode;
|
return allowedValueRangeNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public AllowedValueRange(Node node)
|
public AllowedValueRange(Node node)
|
||||||
{
|
{
|
||||||
allowedValueRangeNode = node;
|
allowedValueRangeNode = node;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
public AllowedValueRange(){
|
||||||
// isAllowedValueRangeNode
|
//TODO Test
|
||||||
////////////////////////////////////////////////
|
allowedValueRangeNode = new Node(ELEM_NAME);
|
||||||
|
}
|
||||||
public static boolean isAllowedValueRangeNode(Node node)
|
////////////////////////////////////////////////
|
||||||
{
|
// isAllowedValueRangeNode
|
||||||
return ELEM_NAME.equals(node.getName());
|
////////////////////////////////////////////////
|
||||||
}
|
|
||||||
|
public AllowedValueRange(Number max, Number min, Number step) {
|
||||||
////////////////////////////////////////////////
|
//TODO Test
|
||||||
// minimum
|
allowedValueRangeNode = new Node(ELEM_NAME);
|
||||||
////////////////////////////////////////////////
|
if(max!=null)
|
||||||
|
setMaximum(max.toString());
|
||||||
private final static String MINIMUM = "minimum";
|
if(min!=null)
|
||||||
|
setMinimum(min.toString());
|
||||||
public void setMinimum(String value)
|
if(step!=null)
|
||||||
{
|
setStep(step.toString());
|
||||||
getAllowedValueRangeNode().setNode(MINIMUM, value);
|
}
|
||||||
}
|
|
||||||
|
public static boolean isAllowedValueRangeNode(Node node)
|
||||||
public String getMinimum()
|
{
|
||||||
{
|
return ELEM_NAME.equals(node.getName());
|
||||||
return getAllowedValueRangeNode().getNodeValue(MINIMUM);
|
}
|
||||||
}
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// minimum
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private final static String MINIMUM = "minimum";
|
||||||
|
|
||||||
|
public void setMinimum(String value)
|
||||||
|
{
|
||||||
|
getAllowedValueRangeNode().setNode(MINIMUM, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMinimum()
|
||||||
|
{
|
||||||
|
return getAllowedValueRangeNode().getNodeValue(MINIMUM);
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// maximum
|
// maximum
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -86,19 +101,19 @@ public class AllowedValueRange
|
|||||||
return getAllowedValueRangeNode().getNodeValue(MAXIMUM);
|
return getAllowedValueRangeNode().getNodeValue(MAXIMUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// width
|
// width
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private final static String STEP = "step";
|
private final static String STEP = "step";
|
||||||
|
|
||||||
public void setStep(String value)
|
public void setStep(String value)
|
||||||
{
|
{
|
||||||
getAllowedValueRangeNode().setNode(STEP, value);
|
getAllowedValueRangeNode().setNode(STEP, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getStep()
|
public String getStep()
|
||||||
{
|
{
|
||||||
return getAllowedValueRangeNode().getNodeValue(STEP);
|
return getAllowedValueRangeNode().getNodeValue(STEP);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002
|
* Copyright (C) Satoshi Konno 2002
|
||||||
*
|
*
|
||||||
* File: Argument.java
|
* File: Argument.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 12/05/02
|
* 12/05/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 03/28/04
|
* 03/28/04
|
||||||
* - Added getRelatedStateVariable().
|
* - Added getRelatedStateVariable().
|
||||||
* - Changed setRelatedStateVariable() to setRelatedStateVariableName().
|
* - Changed setRelatedStateVariable() to setRelatedStateVariableName().
|
||||||
@ -17,37 +17,38 @@
|
|||||||
* - Added getActionNode() and getAction().
|
* - Added getActionNode() and getAction().
|
||||||
* - Added getServiceNode() and getService().
|
* - Added getServiceNode() and getService().
|
||||||
* - Added the parent service node to the constructor.
|
* - Added the parent service node to the constructor.
|
||||||
|
* 04/12/06
|
||||||
|
* - Added setUserData() and getUserData() to set a user original data object.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp;
|
|
||||||
|
|
||||||
import org.cybergarage.xml.*;
|
|
||||||
|
|
||||||
import org.cybergarage.upnp.xml.*;
|
package org.cybergarage.upnp;
|
||||||
|
|
||||||
public class Argument
|
import org.cybergarage.upnp.xml.ArgumentData;
|
||||||
{
|
import org.cybergarage.xml.Node;
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Constants
|
public class Argument
|
||||||
////////////////////////////////////////////////
|
{
|
||||||
|
////////////////////////////////////////////////
|
||||||
public final static String ELEM_NAME = "argument";
|
// Constants
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public final static String ELEM_NAME = "argument";
|
||||||
|
|
||||||
public final static String IN = "in";
|
public final static String IN = "in";
|
||||||
public final static String OUT = "out";
|
public final static String OUT = "out";
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Member
|
// Member
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private Node argumentNode;
|
private Node argumentNode;
|
||||||
private Node serviceNode;
|
private Node serviceNode;
|
||||||
|
|
||||||
public Node getArgumentNode()
|
public Node getArgumentNode()
|
||||||
{
|
{
|
||||||
return argumentNode;
|
return argumentNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Node getServiceNode()
|
private Node getServiceNode()
|
||||||
{
|
{
|
||||||
@ -59,6 +60,10 @@ public class Argument
|
|||||||
return new Service(getServiceNode());
|
return new Service(getServiceNode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setService(Service s){
|
||||||
|
s.getServiceNode();
|
||||||
|
}
|
||||||
|
|
||||||
public Node getActionNode()
|
public Node getActionNode()
|
||||||
{
|
{
|
||||||
Node argumentLinstNode = getArgumentNode().getParentNode();
|
Node argumentLinstNode = getArgumentNode().getParentNode();
|
||||||
@ -71,27 +76,32 @@ public class Argument
|
|||||||
return null;
|
return null;
|
||||||
return actionNode;
|
return actionNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Action getAction()
|
public Action getAction()
|
||||||
{
|
{
|
||||||
return new Action(getServiceNode(), getActionNode());
|
return new Action(getServiceNode(), getActionNode());
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public Argument()
|
public Argument()
|
||||||
{
|
{
|
||||||
argumentNode = new Node();
|
argumentNode = new Node(ELEM_NAME);
|
||||||
serviceNode = null;
|
serviceNode = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Argument(Node servNode, Node argNode)
|
public Argument(Node servNode){
|
||||||
|
argumentNode = new Node(ELEM_NAME);
|
||||||
|
serviceNode = servNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Argument(Node servNode, Node argNode)
|
||||||
{
|
{
|
||||||
serviceNode = servNode;
|
serviceNode = servNode;
|
||||||
argumentNode = argNode;
|
argumentNode = argNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Argument(String name, String value)
|
public Argument(String name, String value)
|
||||||
{
|
{
|
||||||
@ -100,46 +110,46 @@ public class Argument
|
|||||||
setValue(value);
|
setValue(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// isArgumentNode
|
// isArgumentNode
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public static boolean isArgumentNode(Node node)
|
public static boolean isArgumentNode(Node node)
|
||||||
{
|
{
|
||||||
return Argument.ELEM_NAME.equals(node.getName());
|
return Argument.ELEM_NAME.equals(node.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// name
|
// name
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private final static String NAME = "name";
|
private final static String NAME = "name";
|
||||||
|
|
||||||
public void setName(String value)
|
public void setName(String value)
|
||||||
{
|
{
|
||||||
getArgumentNode().setNode(NAME, value);
|
getArgumentNode().setNode(NAME, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName()
|
public String getName()
|
||||||
{
|
{
|
||||||
return getArgumentNode().getNodeValue(NAME);
|
return getArgumentNode().getNodeValue(NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// direction
|
// direction
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private final static String DIRECTION = "direction";
|
private final static String DIRECTION = "direction";
|
||||||
|
|
||||||
public void setDirection(String value)
|
public void setDirection(String value)
|
||||||
{
|
{
|
||||||
getArgumentNode().setNode(DIRECTION, value);
|
getArgumentNode().setNode(DIRECTION, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDirection()
|
public String getDirection()
|
||||||
{
|
{
|
||||||
return getArgumentNode().getNodeValue(DIRECTION);
|
return getArgumentNode().getNodeValue(DIRECTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isInDirection()
|
public boolean isInDirection()
|
||||||
{
|
{
|
||||||
@ -153,22 +163,22 @@ public class Argument
|
|||||||
{
|
{
|
||||||
return !isInDirection();
|
return !isInDirection();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// relatedStateVariable
|
// relatedStateVariable
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private final static String RELATED_STATE_VARIABLE = "relatedStateVariable";
|
private final static String RELATED_STATE_VARIABLE = "relatedStateVariable";
|
||||||
|
|
||||||
public void setRelatedStateVariableName(String value)
|
public void setRelatedStateVariableName(String value)
|
||||||
{
|
{
|
||||||
getArgumentNode().setNode(RELATED_STATE_VARIABLE, value);
|
getArgumentNode().setNode(RELATED_STATE_VARIABLE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRelatedStateVariableName()
|
public String getRelatedStateVariableName()
|
||||||
{
|
{
|
||||||
return getArgumentNode().getNodeValue(RELATED_STATE_VARIABLE);
|
return getArgumentNode().getNodeValue(RELATED_STATE_VARIABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public StateVariable getRelatedStateVariable()
|
public StateVariable getRelatedStateVariable()
|
||||||
{
|
{
|
||||||
@ -226,6 +236,18 @@ public class Argument
|
|||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Related
|
// userData
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private Object userData = null;
|
||||||
|
|
||||||
|
public void setUserData(Object data)
|
||||||
|
{
|
||||||
|
userData = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getUserData()
|
||||||
|
{
|
||||||
|
return userData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,77 +1,121 @@
|
|||||||
/******************************************************************
|
/******************************************************************
|
||||||
*
|
|
||||||
* CyberUPnP for Java
|
|
||||||
*
|
|
||||||
* Copyright (C) Satoshi Konno 2002
|
|
||||||
*
|
*
|
||||||
* File: ArgumentList.java
|
* CyberUPnP for Java
|
||||||
*
|
*
|
||||||
* Revision:
|
* Copyright (C) Satoshi Konno 2002
|
||||||
*
|
*
|
||||||
* 12/05/02
|
* File: ArgumentList.java
|
||||||
* - first revision.
|
*
|
||||||
*
|
* Revision:
|
||||||
******************************************************************/
|
*
|
||||||
|
* 12/05/02
|
||||||
|
* - first revision.
|
||||||
|
*
|
||||||
|
******************************************************************/
|
||||||
|
|
||||||
|
package org.cybergarage.upnp;
|
||||||
|
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
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
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set all the Argument which are Input Argoument to the given value in
|
||||||
|
* the argument list
|
||||||
|
*
|
||||||
|
* @param inArgList
|
||||||
|
*/
|
||||||
|
public void setReqArgs(ArgumentList inArgList)
|
||||||
|
{
|
||||||
|
int nArgs = size();
|
||||||
|
for (int n=0; n<nArgs; n++) {
|
||||||
|
Argument arg = getArgument(n);
|
||||||
|
if (arg.isInDirection()){
|
||||||
|
String argName = arg.getName();
|
||||||
|
Argument inArg = inArgList.getArgument(argName);
|
||||||
|
if (inArg == null)
|
||||||
|
throw new IllegalArgumentException("Argument \"" + argName + "\" missing.");
|
||||||
|
arg.setValue(inArg.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set all the Argument which are Output Argoument to the given value in
|
||||||
|
* the argument list
|
||||||
|
*
|
||||||
|
* @param outArgList
|
||||||
|
*/
|
||||||
|
public void setResArgs(ArgumentList outArgList)
|
||||||
|
{
|
||||||
|
int nArgs = size();
|
||||||
|
for (int n=0; n<nArgs; n++) {
|
||||||
|
Argument arg = getArgument(n);
|
||||||
|
if (arg.isOutDirection()){
|
||||||
|
String argName = arg.getName();
|
||||||
|
Argument outArg = outArgList.getArgument(argName);
|
||||||
|
if (outArg == null)
|
||||||
|
throw new IllegalArgumentException("Argument \"" + argName + "\" missing.");
|
||||||
|
arg.setValue(outArg.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
package org.cybergarage.upnp;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class ArgumentList extends Vector
|
|
||||||
{
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Constants
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
private static final long serialVersionUID = -5412792105767389170L;
|
|
||||||
public final static String ELEM_NAME = "argumentList";
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Constructor
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public ArgumentList()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Methods
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public Argument getArgument(int n)
|
|
||||||
{
|
|
||||||
return (Argument)get(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Argument getArgument(String name)
|
|
||||||
{
|
|
||||||
int nArgs = size();
|
|
||||||
for (int n=0; n<nArgs; n++) {
|
|
||||||
Argument arg = getArgument(n);
|
|
||||||
String argName = arg.getName();
|
|
||||||
if (argName == null)
|
|
||||||
continue;
|
|
||||||
if (argName.equals(name) == true)
|
|
||||||
return arg;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Methods
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public void set(ArgumentList inArgList)
|
|
||||||
{
|
|
||||||
int nInArgs = inArgList.size();
|
|
||||||
for (int n=0; n<nInArgs; n++) {
|
|
||||||
Argument inArg = inArgList.getArgument(n);
|
|
||||||
String inArgName = inArg.getName();
|
|
||||||
Argument arg = getArgument(inArgName);
|
|
||||||
if (arg == null)
|
|
||||||
continue;
|
|
||||||
arg.setValue(inArg.getValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002-2004
|
* Copyright (C) Satoshi Konno 2002-2004
|
||||||
*
|
*
|
||||||
* File: ControlPoint.java
|
* File: ControlPoint.java
|
||||||
*
|
*
|
||||||
* Revision:
|
* Revision:
|
||||||
*
|
*
|
||||||
* 11/18/02
|
* 11/18/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 05/13/03
|
* 05/13/03
|
||||||
* - Changed to create socket threads each local interfaces.
|
* - Changed to create socket threads each local interfaces.
|
||||||
* (HTTP, SSDPNotiry, SSDPSerachResponse)
|
* (HTTP, SSDPNotiry, SSDPSerachResponse)
|
||||||
@ -54,37 +54,62 @@
|
|||||||
* the ControlPoint base class adds/removes a UPnP device
|
* the ControlPoint base class adds/removes a UPnP device
|
||||||
* 03/30/05
|
* 03/30/05
|
||||||
* - Changed addDevice() to use Parser::parse(URL).
|
* - Changed addDevice() to use Parser::parse(URL).
|
||||||
|
* 04/12/06
|
||||||
|
* - Added setUserData() and getUserData() to set a user original data object.
|
||||||
*
|
*
|
||||||
*******************************************************************/
|
*******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp;
|
|
||||||
|
|
||||||
import org.cybergarage.net.*;
|
|
||||||
import org.cybergarage.util.*;
|
|
||||||
import org.cybergarage.xml.*;
|
|
||||||
import org.cybergarage.http.*;
|
|
||||||
|
|
||||||
import org.cybergarage.upnp.control.*;
|
package org.cybergarage.upnp;
|
||||||
import org.cybergarage.upnp.ssdp.*;
|
|
||||||
import org.cybergarage.upnp.device.*;
|
|
||||||
import org.cybergarage.upnp.event.*;
|
|
||||||
|
|
||||||
import java.net.*;
|
import java.net.InetAddress;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
public class ControlPoint implements HTTPRequestListener
|
import java.net.URL;
|
||||||
{
|
|
||||||
|
import org.cybergarage.http.HTTPRequest;
|
||||||
|
import org.cybergarage.http.HTTPRequestListener;
|
||||||
|
import org.cybergarage.http.HTTPServerList;
|
||||||
|
import org.cybergarage.net.HostInterface;
|
||||||
|
import org.cybergarage.upnp.control.RenewSubscriber;
|
||||||
|
import org.cybergarage.upnp.device.DeviceChangeListener;
|
||||||
|
import org.cybergarage.upnp.device.Disposer;
|
||||||
|
import org.cybergarage.upnp.device.NotifyListener;
|
||||||
|
import org.cybergarage.upnp.device.ST;
|
||||||
|
import org.cybergarage.upnp.device.SearchResponseListener;
|
||||||
|
import org.cybergarage.upnp.device.USN;
|
||||||
|
import org.cybergarage.upnp.event.EventListener;
|
||||||
|
import org.cybergarage.upnp.event.NotifyRequest;
|
||||||
|
import org.cybergarage.upnp.event.Property;
|
||||||
|
import org.cybergarage.upnp.event.PropertyList;
|
||||||
|
import org.cybergarage.upnp.event.Subscription;
|
||||||
|
import org.cybergarage.upnp.event.SubscriptionRequest;
|
||||||
|
import org.cybergarage.upnp.event.SubscriptionResponse;
|
||||||
|
import org.cybergarage.upnp.ssdp.SSDP;
|
||||||
|
import org.cybergarage.upnp.ssdp.SSDPNotifySocketList;
|
||||||
|
import org.cybergarage.upnp.ssdp.SSDPPacket;
|
||||||
|
import org.cybergarage.upnp.ssdp.SSDPSearchRequest;
|
||||||
|
import org.cybergarage.upnp.ssdp.SSDPSearchResponseSocketList;
|
||||||
|
import org.cybergarage.util.Debug;
|
||||||
|
import org.cybergarage.util.ListenerList;
|
||||||
|
import org.cybergarage.util.Mutex;
|
||||||
|
import org.cybergarage.xml.Node;
|
||||||
|
import org.cybergarage.xml.NodeList;
|
||||||
|
import org.cybergarage.xml.Parser;
|
||||||
|
import org.cybergarage.xml.ParserException;
|
||||||
|
|
||||||
|
public class ControlPoint implements HTTPRequestListener
|
||||||
|
{
|
||||||
private final static int DEFAULT_EVENTSUB_PORT = 8058;
|
private final static int DEFAULT_EVENTSUB_PORT = 8058;
|
||||||
private final static int DEFAULT_SSDP_PORT = 8008;
|
private final static int DEFAULT_SSDP_PORT = 8008;
|
||||||
private final static int DEFAULT_EXPIRED_DEVICE_MONITORING_INTERVAL = 60;
|
private final static int DEFAULT_EXPIRED_DEVICE_MONITORING_INTERVAL = 60;
|
||||||
|
|
||||||
private final static String DEFAULT_EVENTSUB_URI = "/evetSub";
|
private final static String DEFAULT_EVENTSUB_URI = "/evetSub";
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Member
|
// Member
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private SSDPNotifySocketList ssdpNotifySocketList;
|
private SSDPNotifySocketList ssdpNotifySocketList;
|
||||||
private SSDPSearchResponseSocketList ssdpSearchResponseSocketList;
|
private SSDPSearchResponseSocketList ssdpSearchResponseSocketList;
|
||||||
|
|
||||||
private SSDPNotifySocketList getSSDPNotifySocketList()
|
private SSDPNotifySocketList getSSDPNotifySocketList()
|
||||||
{
|
{
|
||||||
@ -105,14 +130,13 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
UPnP.initialize();
|
UPnP.initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public ControlPoint(int ssdpPort, int httpPort)
|
public ControlPoint(int ssdpPort, int httpPort,InetAddress[] binds){
|
||||||
{
|
ssdpNotifySocketList = new SSDPNotifySocketList(binds);
|
||||||
ssdpNotifySocketList = new SSDPNotifySocketList();
|
ssdpSearchResponseSocketList = new SSDPSearchResponseSocketList(binds);
|
||||||
ssdpSearchResponseSocketList = new SSDPSearchResponseSocketList();
|
|
||||||
|
|
||||||
setSSDPPort(ssdpPort);
|
setSSDPPort(ssdpPort);
|
||||||
setHTTPPort(httpPort);
|
setHTTPPort(httpPort);
|
||||||
@ -124,7 +148,11 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
|
|
||||||
setNMPRMode(false);
|
setNMPRMode(false);
|
||||||
setRenewSubscriber(null);
|
setRenewSubscriber(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ControlPoint(int ssdpPort, int httpPort){
|
||||||
|
this(ssdpPort,httpPort,null);
|
||||||
|
}
|
||||||
|
|
||||||
public ControlPoint()
|
public ControlPoint()
|
||||||
{
|
{
|
||||||
@ -179,7 +207,7 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
public void setHTTPPort(int port) {
|
public void setHTTPPort(int port) {
|
||||||
httpPort = port;
|
httpPort = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// NMPR
|
// NMPR
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -196,35 +224,35 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
return nmprMode;
|
return nmprMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Device List
|
// Device List
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private NodeList devNodeList = new NodeList();
|
private NodeList devNodeList = new NodeList();
|
||||||
|
|
||||||
private void addDevice(Node rootNode)
|
private void addDevice(Node rootNode)
|
||||||
{
|
{
|
||||||
devNodeList.add(rootNode);
|
devNodeList.add(rootNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void addDevice(SSDPPacket ssdpPacket)
|
private synchronized void addDevice(SSDPPacket ssdpPacket)
|
||||||
{
|
{
|
||||||
if (ssdpPacket.isRootDevice() == false)
|
if (ssdpPacket.isRootDevice() == false)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
String usn = ssdpPacket.getUSN();
|
String usn = ssdpPacket.getUSN();
|
||||||
String udn = USN.getUDN(usn);
|
String udn = USN.getUDN(usn);
|
||||||
Device dev = getDevice(udn);
|
Device dev = getDevice(udn);
|
||||||
if (dev != null) {
|
if (dev != null) {
|
||||||
dev.setSSDPPacket(ssdpPacket);
|
dev.setSSDPPacket(ssdpPacket);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String location = ssdpPacket.getLocation();
|
String location = ssdpPacket.getLocation();
|
||||||
try {
|
try {
|
||||||
URL locationUrl = new URL(location);
|
URL locationUrl = new URL(location);
|
||||||
Parser parser = UPnP.getXMLParser();
|
Parser parser = UPnP.getXMLParser();
|
||||||
Node rootNode = parser.parse(locationUrl);
|
Node rootNode = parser.parse(locationUrl);
|
||||||
Device rootDev = getDevice(rootNode);
|
Device rootDev = getDevice(rootNode);
|
||||||
if (rootDev == null)
|
if (rootDev == null)
|
||||||
return;
|
return;
|
||||||
@ -237,16 +265,16 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
// control point application must implement the DeviceChangeListener interface
|
// control point application must implement the DeviceChangeListener interface
|
||||||
// to receive the notifications)
|
// to receive the notifications)
|
||||||
performAddDeviceListener( rootDev );
|
performAddDeviceListener( rootDev );
|
||||||
}
|
}
|
||||||
catch (MalformedURLException me) {
|
catch (MalformedURLException me) {
|
||||||
Debug.warning(ssdpPacket.toString());
|
Debug.warning(ssdpPacket.toString());
|
||||||
Debug.warning(me);
|
Debug.warning(me);
|
||||||
}
|
}
|
||||||
catch (ParserException pe) {
|
catch (ParserException pe) {
|
||||||
Debug.warning(ssdpPacket.toString());
|
Debug.warning(ssdpPacket.toString());
|
||||||
Debug.warning(pe);
|
Debug.warning(pe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Device getDevice(Node rootNode)
|
private Device getDevice(Node rootNode)
|
||||||
{
|
{
|
||||||
@ -257,26 +285,26 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
return null;
|
return null;
|
||||||
return new Device(rootNode, devNode);
|
return new Device(rootNode, devNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DeviceList getDeviceList()
|
public DeviceList getDeviceList()
|
||||||
{
|
{
|
||||||
DeviceList devList = new DeviceList();
|
DeviceList devList = new DeviceList();
|
||||||
int nRoots = devNodeList.size();
|
int nRoots = devNodeList.size();
|
||||||
for (int n=0; n<nRoots; n++) {
|
for (int n=0; n<nRoots; n++) {
|
||||||
Node rootNode = devNodeList.getNode(n);
|
Node rootNode = devNodeList.getNode(n);
|
||||||
Device dev = getDevice(rootNode);
|
Device dev = getDevice(rootNode);
|
||||||
if (dev == null)
|
if (dev == null)
|
||||||
continue;
|
continue;
|
||||||
devList.add(dev);
|
devList.add(dev);
|
||||||
}
|
}
|
||||||
return devList;
|
return devList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Device getDevice(String name)
|
public Device getDevice(String name)
|
||||||
{
|
{
|
||||||
int nRoots = devNodeList.size();
|
int nRoots = devNodeList.size();
|
||||||
for (int n=0; n<nRoots; n++) {
|
for (int n=0; n<nRoots; n++) {
|
||||||
Node rootNode = devNodeList.getNode(n);
|
Node rootNode = devNodeList.getNode(n);
|
||||||
Device dev = getDevice(rootNode);
|
Device dev = getDevice(rootNode);
|
||||||
if (dev == null)
|
if (dev == null)
|
||||||
continue;
|
continue;
|
||||||
@ -284,18 +312,18 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
return dev;
|
return dev;
|
||||||
Device cdev = dev.getDevice(name);
|
Device cdev = dev.getDevice(name);
|
||||||
if (cdev != null)
|
if (cdev != null)
|
||||||
return cdev;
|
return cdev;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasDevice(String name)
|
public boolean hasDevice(String name)
|
||||||
{
|
{
|
||||||
return (getDevice(name) != null) ? true : false;
|
return (getDevice(name) != null) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeDevice(Node rootNode)
|
private void removeDevice(Node rootNode)
|
||||||
{
|
{
|
||||||
// Thanks for Oliver Newell (2004/10/16)
|
// Thanks for Oliver Newell (2004/10/16)
|
||||||
// Invoke device removal listener prior to actual removal so Device node
|
// Invoke device removal listener prior to actual removal so Device node
|
||||||
// remains valid for the duration of the listener (application may want
|
// remains valid for the duration of the listener (application may want
|
||||||
@ -304,29 +332,29 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
if( dev != null && dev.isRootDevice() )
|
if( dev != null && dev.isRootDevice() )
|
||||||
performRemoveDeviceListener( dev );
|
performRemoveDeviceListener( dev );
|
||||||
|
|
||||||
devNodeList.remove(rootNode);
|
devNodeList.remove(rootNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeDevice(Device dev)
|
protected void removeDevice(Device dev)
|
||||||
{
|
{
|
||||||
if (dev == null)
|
if (dev == null)
|
||||||
return;
|
return;
|
||||||
removeDevice(dev.getRootNode());
|
removeDevice(dev.getRootNode());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeDevice(String name)
|
protected void removeDevice(String name)
|
||||||
{
|
{
|
||||||
Device dev = getDevice(name);
|
Device dev = getDevice(name);
|
||||||
removeDevice(dev);
|
removeDevice(dev);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeDevice(SSDPPacket packet)
|
private void removeDevice(SSDPPacket packet)
|
||||||
{
|
{
|
||||||
if (packet.isByeBye() == false)
|
if (packet.isByeBye() == false)
|
||||||
return;
|
return;
|
||||||
String usn = packet.getUSN();
|
String usn = packet.getUSN();
|
||||||
String udn = USN.getUDN(usn);
|
String udn = USN.getUDN(usn);
|
||||||
removeDevice(udn);
|
removeDevice(udn);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -353,7 +381,7 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
|
|
||||||
public void setExpiredDeviceMonitoringInterval(long interval)
|
public void setExpiredDeviceMonitoringInterval(long interval)
|
||||||
{
|
{
|
||||||
expiredDeviceMonitoringInterval = interval;
|
expiredDeviceMonitoringInterval = interval;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getExpiredDeviceMonitoringInterval()
|
public long getExpiredDeviceMonitoringInterval()
|
||||||
@ -371,9 +399,9 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
return deviceDisposer;
|
return deviceDisposer;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Notify
|
// Notify
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private ListenerList deviceNotifyListenerList = new ListenerList();
|
private ListenerList deviceNotifyListenerList = new ListenerList();
|
||||||
|
|
||||||
@ -392,10 +420,14 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
int listenerSize = deviceNotifyListenerList.size();
|
int listenerSize = deviceNotifyListenerList.size();
|
||||||
for (int n=0; n<listenerSize; n++) {
|
for (int n=0; n<listenerSize; n++) {
|
||||||
NotifyListener listener = (NotifyListener)deviceNotifyListenerList.get(n);
|
NotifyListener listener = (NotifyListener)deviceNotifyListenerList.get(n);
|
||||||
listener.deviceNotifyReceived(ssdpPacket);
|
try{
|
||||||
|
listener.deviceNotifyReceived(ssdpPacket);
|
||||||
|
}catch(Exception e){
|
||||||
|
Debug.warning("NotifyListener returned an error:", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// SearchResponse
|
// SearchResponse
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -417,7 +449,13 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
int listenerSize = deviceSearchResponseListenerList.size();
|
int listenerSize = deviceSearchResponseListenerList.size();
|
||||||
for (int n=0; n<listenerSize; n++) {
|
for (int n=0; n<listenerSize; n++) {
|
||||||
SearchResponseListener listener = (SearchResponseListener)deviceSearchResponseListenerList.get(n);
|
SearchResponseListener listener = (SearchResponseListener)deviceSearchResponseListenerList.get(n);
|
||||||
listener.deviceSearchResponseReceived(ssdpPacket);
|
try{
|
||||||
|
listener.deviceSearchResponseReceived(ssdpPacket);
|
||||||
|
}catch(Exception e){
|
||||||
|
Debug.warning("SearchResponseListener returned an error:", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -465,10 +503,11 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
public void notifyReceived(SSDPPacket packet)
|
public void notifyReceived(SSDPPacket packet)
|
||||||
{
|
{
|
||||||
if (packet.isRootDevice() == true) {
|
if (packet.isRootDevice() == true) {
|
||||||
if (packet.isAlive() == true)
|
if (packet.isAlive() == true){
|
||||||
addDevice(packet);
|
addDevice(packet);
|
||||||
if (packet.isByeBye() == true)
|
}else if (packet.isByeBye() == true){
|
||||||
removeDevice(packet);
|
removeDevice(packet);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
performNotifyListener(packet);
|
performNotifyListener(packet);
|
||||||
}
|
}
|
||||||
@ -479,12 +518,12 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
addDevice(packet);
|
addDevice(packet);
|
||||||
performSearchResponseListener(packet);
|
performSearchResponseListener(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// M-SEARCH
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
private int searchMx = SSDP.DEFAULT_MSEARCH_MX;
|
////////////////////////////////////////////////
|
||||||
|
// M-SEARCH
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private int searchMx = SSDP.DEFAULT_MSEARCH_MX;
|
||||||
|
|
||||||
public int getSearchMx()
|
public int getSearchMx()
|
||||||
{
|
{
|
||||||
@ -711,7 +750,7 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// getSubscriberService
|
// getSubscriberService
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -769,11 +808,11 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
return renewSubscriber;
|
return renewSubscriber;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// run
|
// run
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public boolean start(String target, int mx)
|
public boolean start(String target, int mx)
|
||||||
{
|
{
|
||||||
stop();
|
stop();
|
||||||
|
|
||||||
@ -803,7 +842,7 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
return false;
|
return false;
|
||||||
ssdpNotifySocketList.setControlPoint(this);
|
ssdpNotifySocketList.setControlPoint(this);
|
||||||
ssdpNotifySocketList.start();
|
ssdpNotifySocketList.start();
|
||||||
|
|
||||||
////////////////////////////////////////
|
////////////////////////////////////////
|
||||||
// SeachResponse Socket
|
// SeachResponse Socket
|
||||||
////////////////////////////////////////
|
////////////////////////////////////////
|
||||||
@ -845,9 +884,9 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
renewSub.start();
|
renewSub.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean start(String target)
|
public boolean start(String target)
|
||||||
{
|
{
|
||||||
return start(target, SSDP.DEFAULT_MSEARCH_MX);
|
return start(target, SSDP.DEFAULT_MSEARCH_MX);
|
||||||
@ -858,10 +897,10 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
return start(ST.ROOT_DEVICE, SSDP.DEFAULT_MSEARCH_MX);
|
return start(ST.ROOT_DEVICE, SSDP.DEFAULT_MSEARCH_MX);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean stop()
|
public boolean stop()
|
||||||
{
|
{
|
||||||
unsubscribe();
|
unsubscribe();
|
||||||
|
|
||||||
SSDPNotifySocketList ssdpNotifySocketList = getSSDPNotifySocketList();
|
SSDPNotifySocketList ssdpNotifySocketList = getSSDPNotifySocketList();
|
||||||
ssdpNotifySocketList.stop();
|
ssdpNotifySocketList.stop();
|
||||||
ssdpNotifySocketList.close();
|
ssdpNotifySocketList.close();
|
||||||
@ -897,9 +936,25 @@ public class ControlPoint implements HTTPRequestListener
|
|||||||
setRenewSubscriber(null);
|
setRenewSubscriber(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// userData
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private Object userData = null;
|
||||||
|
|
||||||
|
public void setUserData(Object data)
|
||||||
|
{
|
||||||
|
userData = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getUserData()
|
||||||
|
{
|
||||||
|
return userData;
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// print
|
// print
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,46 +1,45 @@
|
|||||||
/******************************************************************
|
/******************************************************************
|
||||||
*
|
|
||||||
* CyberUPnP for Java
|
|
||||||
*
|
|
||||||
* Copyright (C) Satoshi Konno 2002
|
|
||||||
*
|
*
|
||||||
* File: DeviceList.java
|
* CyberUPnP for Java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Copyright (C) Satoshi Konno 2002
|
||||||
*
|
*
|
||||||
* 12/04/02
|
* File: DeviceList.java
|
||||||
* - first revision.
|
*
|
||||||
*
|
* Revision;
|
||||||
******************************************************************/
|
*
|
||||||
|
* 12/04/02
|
||||||
|
* - first revision.
|
||||||
|
*
|
||||||
|
******************************************************************/
|
||||||
|
|
||||||
|
package org.cybergarage.upnp;
|
||||||
|
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
package org.cybergarage.upnp;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class DeviceList extends Vector
|
|
||||||
{
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Constants
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 3773784061607435126L;
|
|
||||||
public final static String ELEM_NAME = "deviceList";
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Constructor
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public DeviceList()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Methods
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public Device getDevice(int n)
|
|
||||||
{
|
|
||||||
return (Device)get(n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -10,12 +10,14 @@
|
|||||||
*
|
*
|
||||||
* 11/28/02
|
* 11/28/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
|
* 04/12/06
|
||||||
|
* - Added setUserData() and getUserData() to set a user original data object.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp;
|
package org.cybergarage.upnp;
|
||||||
|
|
||||||
import org.cybergarage.xml.*;
|
import org.cybergarage.xml.Node;
|
||||||
|
|
||||||
public class Icon
|
public class Icon
|
||||||
{
|
{
|
||||||
@ -81,9 +83,21 @@ public class Icon
|
|||||||
getIconNode().setNode(WIDTH, value);
|
getIconNode().setNode(WIDTH, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getWidth()
|
public void setWidth(int value)
|
||||||
{
|
{
|
||||||
return getIconNode().getNodeValue(WIDTH);
|
try {
|
||||||
|
setWidth(Integer.toString(value));
|
||||||
|
}
|
||||||
|
catch (Exception e) {};
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWidth()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(getIconNode().getNodeValue(WIDTH));
|
||||||
|
}
|
||||||
|
catch (Exception e) {};
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -97,9 +111,21 @@ public class Icon
|
|||||||
getIconNode().setNode(HEIGHT, value);
|
getIconNode().setNode(HEIGHT, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHeight()
|
public void setHeight(int value)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
setHeight(Integer.toString(value));
|
||||||
|
}
|
||||||
|
catch (Exception e) {};
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeight()
|
||||||
{
|
{
|
||||||
return getIconNode().getNodeValue(HEIGHT);
|
try {
|
||||||
|
return Integer.parseInt(getIconNode().getNodeValue(HEIGHT));
|
||||||
|
}
|
||||||
|
catch (Exception e) {};
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -132,5 +158,21 @@ public class Icon
|
|||||||
public String getURL()
|
public String getURL()
|
||||||
{
|
{
|
||||||
return getIconNode().getNodeValue(URL);
|
return getIconNode().getNodeValue(URL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// userData
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private Object userData = null;
|
||||||
|
|
||||||
|
public void setUserData(Object data)
|
||||||
|
{
|
||||||
|
userData = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getUserData()
|
||||||
|
{
|
||||||
|
return userData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
package org.cybergarage.upnp;
|
package org.cybergarage.upnp;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Vector;
|
||||||
|
|
||||||
public class IconList extends Vector
|
public class IconList extends Vector
|
||||||
{
|
{
|
||||||
@ -23,7 +23,6 @@ public class IconList extends Vector
|
|||||||
// Constants
|
// Constants
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private static final long serialVersionUID = -1097238335037012991L;
|
|
||||||
public final static String ELEM_NAME = "iconList";
|
public final static String ELEM_NAME = "iconList";
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
19
router/java/src/org/cybergarage/upnp/RootDescription.java
Normal file
19
router/java/src/org/cybergarage/upnp/RootDescription.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package org.cybergarage.upnp;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Stefano "Kismet" Lenzi - kismet-sl@users.sourceforge.net <br>
|
||||||
|
* Copyright (c) 2005
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public interface RootDescription {
|
||||||
|
|
||||||
|
public final String ROOT_ELEMENT = "root";
|
||||||
|
public final String ROOT_ELEMENT_NAMESPACE = "urn:schemas-upnp-org:device-1-0";
|
||||||
|
|
||||||
|
|
||||||
|
public final String SPECVERSION_ELEMENT = "specVersion";
|
||||||
|
public final String MAJOR_ELEMENT = "major";
|
||||||
|
public final String MINOR_ELEMENT = "minor";
|
||||||
|
public final String SERVICE_LIST_ELEMENT = "serviceList";
|
||||||
|
}
|
@ -5,11 +5,11 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002-2003
|
* Copyright (C) Satoshi Konno 2002-2003
|
||||||
*
|
*
|
||||||
* File: Service.java
|
* File: Service.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 11/28/02
|
* 11/28/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 04/12/02
|
* 04/12/02
|
||||||
* - Holmes, Arran C <acholm@essex.ac.uk>
|
* - Holmes, Arran C <acholm@essex.ac.uk>
|
||||||
* - Fixed SERVICE_ID constant instead of "serviceId".
|
* - Fixed SERVICE_ID constant instead of "serviceId".
|
||||||
@ -60,52 +60,99 @@
|
|||||||
* 04/25/05
|
* 04/25/05
|
||||||
* - Thanks for Mikael Hakman <mhakman@dkab.net>
|
* - Thanks for Mikael Hakman <mhakman@dkab.net>
|
||||||
* - Changed getSCPDData() to add a XML declaration at first line.
|
* - Changed getSCPDData() to add a XML declaration at first line.
|
||||||
|
* 06/21/05
|
||||||
|
* - Changed notify() to continue when the subscriber is null.
|
||||||
|
* 04/12/06
|
||||||
|
* - Added setUserData() and getUserData() to set a user original data object.
|
||||||
|
* 09/18/2010 Robin V. <robinsp@gmail.com>
|
||||||
|
* - Fixed getSCPDNode() not to occur recursive http get requests.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp;
|
package org.cybergarage.upnp;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.File;
|
||||||
import java.net.*;
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
import org.cybergarage.http.*;
|
import java.util.Iterator;
|
||||||
import org.cybergarage.xml.*;
|
|
||||||
import org.cybergarage.util.*;
|
import org.cybergarage.http.HTTP;
|
||||||
|
import org.cybergarage.http.HTTPResponse;
|
||||||
|
import org.cybergarage.upnp.control.ActionListener;
|
||||||
|
import org.cybergarage.upnp.control.QueryListener;
|
||||||
|
import org.cybergarage.upnp.device.InvalidDescriptionException;
|
||||||
|
import org.cybergarage.upnp.device.NTS;
|
||||||
|
import org.cybergarage.upnp.device.ST;
|
||||||
|
import org.cybergarage.upnp.event.NotifyRequest;
|
||||||
|
import org.cybergarage.upnp.event.Subscriber;
|
||||||
|
import org.cybergarage.upnp.event.SubscriberList;
|
||||||
|
import org.cybergarage.upnp.ssdp.SSDPNotifyRequest;
|
||||||
|
import org.cybergarage.upnp.ssdp.SSDPNotifySocket;
|
||||||
|
import org.cybergarage.upnp.ssdp.SSDPPacket;
|
||||||
|
import org.cybergarage.upnp.xml.ServiceData;
|
||||||
|
import org.cybergarage.util.Debug;
|
||||||
|
import org.cybergarage.util.Mutex;
|
||||||
|
import org.cybergarage.util.StringUtil;
|
||||||
|
import org.cybergarage.xml.Node;
|
||||||
|
import org.cybergarage.xml.Parser;
|
||||||
|
import org.cybergarage.xml.ParserException;
|
||||||
|
|
||||||
|
public class Service
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Constants
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public final static String ELEM_NAME = "service";
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Member
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private Node serviceNode;
|
||||||
|
|
||||||
|
public Node getServiceNode()
|
||||||
|
{
|
||||||
|
return serviceNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Constructor
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
public static final String SCPD_ROOTNODE="scpd";
|
||||||
|
public static final String SCPD_ROOTNODE_NS="urn:schemas-upnp-org:service-1-0";
|
||||||
|
|
||||||
|
public static final String SPEC_VERSION="specVersion";
|
||||||
|
public static final String MAJOR="major";
|
||||||
|
public static final String MAJOR_VALUE="1";
|
||||||
|
public static final String MINOR="minor";
|
||||||
|
public static final String MINOR_VALUE="0";
|
||||||
|
|
||||||
|
public Service(){
|
||||||
|
this(new Node(ELEM_NAME));
|
||||||
|
|
||||||
|
Node sp = new Node(SPEC_VERSION);
|
||||||
|
|
||||||
|
Node M =new Node(MAJOR);
|
||||||
|
M.setValue(MAJOR_VALUE);
|
||||||
|
sp.addNode(M);
|
||||||
|
|
||||||
|
Node m =new Node(MINOR);
|
||||||
|
m.setValue(MINOR_VALUE);
|
||||||
|
sp.addNode(m);
|
||||||
|
|
||||||
|
//Node scpd = new Node(SCPD_ROOTNODE,SCPD_ROOTNODE_NS); wrong!
|
||||||
|
Node scpd = new Node(SCPD_ROOTNODE); // better (twa)
|
||||||
|
scpd.addAttribute("xmlns",SCPD_ROOTNODE_NS); // better (twa)
|
||||||
|
scpd.addNode(sp);
|
||||||
|
getServiceData().setSCPDNode(scpd);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Service(Node node)
|
||||||
|
{
|
||||||
|
serviceNode = node;
|
||||||
|
}
|
||||||
|
|
||||||
import org.cybergarage.upnp.ssdp.*;
|
|
||||||
import org.cybergarage.upnp.xml.*;
|
|
||||||
import org.cybergarage.upnp.device.*;
|
|
||||||
import org.cybergarage.upnp.control.*;
|
|
||||||
import org.cybergarage.upnp.event.*;
|
|
||||||
|
|
||||||
public class Service
|
|
||||||
{
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Constants
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public final static String ELEM_NAME = "service";
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Member
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
private Node serviceNode;
|
|
||||||
|
|
||||||
public Node getServiceNode()
|
|
||||||
{
|
|
||||||
return serviceNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Constructor
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public Service(Node node)
|
|
||||||
{
|
|
||||||
serviceNode = node;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Mutex
|
// Mutex
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -122,78 +169,78 @@ public class Service
|
|||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// isServiceNode
|
// isServiceNode
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public static boolean isServiceNode(Node node)
|
public static boolean isServiceNode(Node node)
|
||||||
{
|
{
|
||||||
return Service.ELEM_NAME.equals(node.getName());
|
return Service.ELEM_NAME.equals(node.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Device/Root Node
|
// Device/Root Node
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private Node getDeviceNode()
|
private Node getDeviceNode()
|
||||||
{
|
{
|
||||||
Node node = getServiceNode().getParentNode();
|
Node node = getServiceNode().getParentNode();
|
||||||
if (node == null)
|
if (node == null)
|
||||||
return null;
|
return null;
|
||||||
return node.getParentNode();
|
return node.getParentNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Node getRootNode()
|
private Node getRootNode()
|
||||||
{
|
{
|
||||||
return getServiceNode().getRootNode();
|
return getServiceNode().getRootNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Device
|
// Device
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public Device getDevice()
|
public Device getDevice()
|
||||||
{
|
{
|
||||||
return new Device(getRootNode(), getDeviceNode());
|
return new Device(getRootNode(), getDeviceNode());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Device getRootDevice()
|
public Device getRootDevice()
|
||||||
{
|
{
|
||||||
return getDevice().getRootDevice();
|
return getDevice().getRootDevice();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// serviceType
|
// serviceType
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private final static String SERVICE_TYPE = "serviceType";
|
private final static String SERVICE_TYPE = "serviceType";
|
||||||
|
|
||||||
public void setServiceType(String value)
|
public void setServiceType(String value)
|
||||||
{
|
{
|
||||||
getServiceNode().setNode(SERVICE_TYPE, value);
|
getServiceNode().setNode(SERVICE_TYPE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getServiceType()
|
public String getServiceType()
|
||||||
{
|
{
|
||||||
return getServiceNode().getNodeValue(SERVICE_TYPE);
|
return getServiceNode().getNodeValue(SERVICE_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// serviceID
|
// serviceID
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private final static String SERVICE_ID = "serviceId";
|
private final static String SERVICE_ID = "serviceId";
|
||||||
|
|
||||||
public void setServiceID(String value)
|
public void setServiceID(String value)
|
||||||
{
|
{
|
||||||
getServiceNode().setNode(SERVICE_ID, value);
|
getServiceNode().setNode(SERVICE_ID, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getServiceID()
|
public String getServiceID()
|
||||||
{
|
{
|
||||||
return getServiceNode().getNodeValue(SERVICE_ID);
|
return getServiceNode().getNodeValue(SERVICE_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// isURL
|
// isURL
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -213,73 +260,73 @@ public class Service
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// SCPDURL
|
// SCPDURL
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private final static String SCPDURL = "SCPDURL";
|
private final static String SCPDURL = "SCPDURL";
|
||||||
|
|
||||||
public void setSCPDURL(String value)
|
public void setSCPDURL(String value)
|
||||||
{
|
{
|
||||||
getServiceNode().setNode(SCPDURL, value);
|
getServiceNode().setNode(SCPDURL, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSCPDURL()
|
public String getSCPDURL()
|
||||||
{
|
{
|
||||||
return getServiceNode().getNodeValue(SCPDURL);
|
return getServiceNode().getNodeValue(SCPDURL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSCPDURL(String url)
|
public boolean isSCPDURL(String url)
|
||||||
{
|
{
|
||||||
return isURL(getSCPDURL(), url);
|
return isURL(getSCPDURL(), url);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// controlURL
|
// controlURL
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private final static String CONTROL_URL = "controlURL";
|
private final static String CONTROL_URL = "controlURL";
|
||||||
|
|
||||||
public void setControlURL(String value)
|
public void setControlURL(String value)
|
||||||
{
|
{
|
||||||
getServiceNode().setNode(CONTROL_URL, value);
|
getServiceNode().setNode(CONTROL_URL, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getControlURL()
|
public String getControlURL()
|
||||||
{
|
{
|
||||||
return getServiceNode().getNodeValue(CONTROL_URL);
|
return getServiceNode().getNodeValue(CONTROL_URL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isControlURL(String url)
|
public boolean isControlURL(String url)
|
||||||
{
|
{
|
||||||
return isURL(getControlURL(), url);
|
return isURL(getControlURL(), url);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// eventSubURL
|
// eventSubURL
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private final static String EVENT_SUB_URL = "eventSubURL";
|
private final static String EVENT_SUB_URL = "eventSubURL";
|
||||||
|
|
||||||
public void setEventSubURL(String value)
|
public void setEventSubURL(String value)
|
||||||
{
|
{
|
||||||
getServiceNode().setNode(EVENT_SUB_URL, value);
|
getServiceNode().setNode(EVENT_SUB_URL, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getEventSubURL()
|
public String getEventSubURL()
|
||||||
{
|
{
|
||||||
return getServiceNode().getNodeValue(EVENT_SUB_URL);
|
return getServiceNode().getNodeValue(EVENT_SUB_URL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEventSubURL(String url)
|
public boolean isEventSubURL(String url)
|
||||||
{
|
{
|
||||||
return isURL(getEventSubURL(), url);
|
return isURL(getEventSubURL(), url);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// SCPD node
|
// SCPD node
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public boolean loadSCPD(String scpdStr) throws InvalidDescriptionException
|
public boolean loadSCPD(String scpdStr) throws InvalidDescriptionException
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -306,70 +353,101 @@ public class Service
|
|||||||
data.setSCPDNode(scpdNode);
|
data.setSCPDNode(scpdNode);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Node getSCPDNode(URL scpdUrl) throws ParserException
|
/**
|
||||||
{
|
* @since 1.8.0
|
||||||
|
*/
|
||||||
|
public boolean loadSCPD(InputStream input) throws ParserException
|
||||||
|
{
|
||||||
Parser parser = UPnP.getXMLParser();
|
Parser parser = UPnP.getXMLParser();
|
||||||
return parser.parse(scpdUrl);
|
Node scpdNode = parser.parse(input);
|
||||||
|
if (scpdNode == null)
|
||||||
|
return false;
|
||||||
|
ServiceData data = getServiceData();
|
||||||
|
data.setSCPDNode(scpdNode);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setDescriptionURL(String value)
|
||||||
|
{
|
||||||
|
getServiceData().setDescriptionURL(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescriptionURL()
|
||||||
|
{
|
||||||
|
return getServiceData().getDescriptionURL();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Node getSCPDNode(URL scpdUrl) throws ParserException
|
||||||
|
{
|
||||||
|
Parser parser = UPnP.getXMLParser();
|
||||||
|
return parser.parse(scpdUrl);
|
||||||
|
}
|
||||||
|
|
||||||
private Node getSCPDNode(File scpdFile) throws ParserException
|
private Node getSCPDNode(File scpdFile) throws ParserException
|
||||||
{
|
{
|
||||||
Parser parser = UPnP.getXMLParser();
|
Parser parser = UPnP.getXMLParser();
|
||||||
return parser.parse(scpdFile);
|
return parser.parse(scpdFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Node getSCPDNode()
|
private Node getSCPDNode()
|
||||||
{
|
{
|
||||||
ServiceData data = getServiceData();
|
ServiceData data = getServiceData();
|
||||||
Node scpdNode = data.getSCPDNode();
|
Node scpdNode = data.getSCPDNode();
|
||||||
if (scpdNode != null)
|
if (scpdNode != null)
|
||||||
return scpdNode;
|
return scpdNode;
|
||||||
|
|
||||||
String scpdURLStr = getSCPDURL();
|
// Thanks for Jaap (Sep 18, 2010)
|
||||||
try {
|
Device rootDev = getRootDevice();
|
||||||
URL scpdUrl = new URL(scpdURLStr);
|
if (rootDev == null)
|
||||||
scpdNode = getSCPDNode(scpdUrl);
|
return null;
|
||||||
}
|
|
||||||
catch (Exception e1) {
|
String scpdURLStr = getSCPDURL();
|
||||||
Device rootDev = getRootDevice();
|
|
||||||
String urlBaseStr = rootDev.getURLBase();
|
// Thanks for Robin V. (Sep 18, 2010)
|
||||||
// Thanks for Steven Yen (2003/09/03)
|
String rootDevPath = rootDev.getDescriptionFilePath();
|
||||||
if (urlBaseStr == null || urlBaseStr.length() <= 0) {
|
if(rootDevPath!=null) {
|
||||||
String location = rootDev.getLocation();
|
File f;
|
||||||
String locationHost = HTTP.getHost(location);
|
f = new File(rootDevPath.concat(scpdURLStr));
|
||||||
int locationPort = HTTP.getPort(location);
|
|
||||||
urlBaseStr = HTTP.getRequestHostURL(locationHost, locationPort);
|
if(f.exists()) {
|
||||||
}
|
try {
|
||||||
scpdURLStr = HTTP.toRelativeURL(scpdURLStr);
|
scpdNode = getSCPDNode(f);
|
||||||
String newScpdURLStr = urlBaseStr + scpdURLStr;
|
} catch (ParserException e) {
|
||||||
try {
|
// TODO Auto-generated catch block
|
||||||
URL newScpdURL = new URL(newScpdURLStr);
|
e.printStackTrace();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
if(scpdNode!=null) {
|
||||||
}
|
data.setSCPDNode(scpdNode);
|
||||||
|
return scpdNode;
|
||||||
data.setSCPDNode(scpdNode);
|
}
|
||||||
|
}
|
||||||
return scpdNode;
|
}
|
||||||
}
|
|
||||||
|
try {
|
||||||
|
URL scpdUrl = new URL(rootDev.getAbsoluteURL(scpdURLStr));
|
||||||
|
scpdNode = getSCPDNode(scpdUrl);
|
||||||
|
if (scpdNode != null) {
|
||||||
|
data.setSCPDNode(scpdNode);
|
||||||
|
return scpdNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {}
|
||||||
|
|
||||||
|
String newScpdURLStr = rootDev.getDescriptionFilePath() + HTTP.toRelativeURL(scpdURLStr);
|
||||||
|
try {
|
||||||
|
scpdNode = getSCPDNode(new File(newScpdURLStr));
|
||||||
|
return scpdNode;
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Debug.warning(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public byte[] getSCPDData()
|
public byte[] getSCPDData()
|
||||||
{
|
{
|
||||||
Node scpdNode = getSCPDNode();
|
Node scpdNode = getSCPDNode();
|
||||||
@ -383,30 +461,29 @@ public class Service
|
|||||||
return desc.getBytes();
|
return desc.getBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// actionList
|
// actionList
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public ActionList getActionList()
|
public ActionList getActionList()
|
||||||
{
|
{
|
||||||
ActionList actionList = new ActionList();
|
ActionList actionList = new ActionList();
|
||||||
Node scdpNode = getSCPDNode();
|
Node scdpNode = getSCPDNode();
|
||||||
if (scdpNode == null)
|
if (scdpNode == null)
|
||||||
return actionList;
|
return actionList;
|
||||||
Node actionListNode = scdpNode.getNode(ActionList.ELEM_NAME);
|
Node actionListNode = scdpNode.getNode(ActionList.ELEM_NAME);
|
||||||
if (actionListNode == null)
|
if (actionListNode == null)
|
||||||
return actionList;
|
return actionList;
|
||||||
Node serviceNode = getServiceNode();
|
int nNode = actionListNode.getNNodes();
|
||||||
int nNode = actionListNode.getNNodes();
|
for (int n=0; n<nNode; n++) {
|
||||||
for (int n=0; n<nNode; n++) {
|
Node node = actionListNode.getNode(n);
|
||||||
Node node = actionListNode.getNode(n);
|
if (Action.isActionNode(node) == false)
|
||||||
if (Action.isActionNode(node) == false)
|
continue;
|
||||||
continue;
|
Action action = new Action(serviceNode, node);
|
||||||
Action action = new Action(serviceNode, node);
|
actionList.add(action);
|
||||||
actionList.add(action);
|
}
|
||||||
}
|
return actionList;
|
||||||
return actionList;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public Action getAction(String actionName)
|
public Action getAction(String actionName)
|
||||||
{
|
{
|
||||||
@ -422,28 +499,44 @@ public class Service
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
public void addAction(Action a){
|
||||||
// serviceStateTable
|
Iterator i = a.getArgumentList().iterator();
|
||||||
////////////////////////////////////////////////
|
while (i.hasNext()) {
|
||||||
|
Argument arg = (Argument) i.next();
|
||||||
public ServiceStateTable getServiceStateTable()
|
arg.setService(this);
|
||||||
{
|
}
|
||||||
ServiceStateTable stateTable = new ServiceStateTable();
|
|
||||||
Node stateTableNode = getSCPDNode().getNode(ServiceStateTable.ELEM_NAME);
|
Node scdpNode = getSCPDNode();
|
||||||
if (stateTableNode == null)
|
Node actionListNode = scdpNode.getNode(ActionList.ELEM_NAME);
|
||||||
|
if (actionListNode == null){
|
||||||
|
actionListNode = new Node(ActionList.ELEM_NAME);
|
||||||
|
scdpNode.addNode(actionListNode);
|
||||||
|
}
|
||||||
|
actionListNode.addNode(a.getActionNode());
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// serviceStateTable
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public ServiceStateTable getServiceStateTable()
|
||||||
|
{
|
||||||
|
ServiceStateTable stateTable = new ServiceStateTable();
|
||||||
|
Node stateTableNode = getSCPDNode().getNode(ServiceStateTable.ELEM_NAME);
|
||||||
|
if (stateTableNode == null)
|
||||||
return stateTable;
|
return stateTable;
|
||||||
Node serviceNode = getServiceNode();
|
Node serviceNode = getServiceNode();
|
||||||
int nNode = stateTableNode.getNNodes();
|
int nNode = stateTableNode.getNNodes();
|
||||||
for (int n=0; n<nNode; n++) {
|
for (int n=0; n<nNode; n++) {
|
||||||
Node node = stateTableNode.getNode(n);
|
Node node = stateTableNode.getNode(n);
|
||||||
if (StateVariable.isStateVariableNode(node) == false)
|
if (StateVariable.isStateVariableNode(node) == false)
|
||||||
continue;
|
continue;
|
||||||
StateVariable serviceVar = new StateVariable(serviceNode, node);
|
StateVariable serviceVar = new StateVariable(serviceNode, node);
|
||||||
stateTable.add(serviceVar);
|
stateTable.add(serviceVar);
|
||||||
}
|
}
|
||||||
return stateTable;
|
return stateTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
public StateVariable getStateVariable(String name)
|
public StateVariable getStateVariable(String name)
|
||||||
{
|
{
|
||||||
@ -657,6 +750,8 @@ public class Service
|
|||||||
subs[n] = subList.getSubscriber(n);
|
subs[n] = subList.getSubscriber(n);
|
||||||
for (int n=0; n<subListCnt; n++) {
|
for (int n=0; n<subListCnt; n++) {
|
||||||
Subscriber sub = subs[n];
|
Subscriber sub = subs[n];
|
||||||
|
if (sub == null)
|
||||||
|
continue;
|
||||||
if (sub.isExpired() == true)
|
if (sub.isExpired() == true)
|
||||||
removeSubscriber(sub);
|
removeSubscriber(sub);
|
||||||
}
|
}
|
||||||
@ -668,6 +763,8 @@ public class Service
|
|||||||
subs[n] = subList.getSubscriber(n);
|
subs[n] = subList.getSubscriber(n);
|
||||||
for (int n=0; n<subListCnt; n++) {
|
for (int n=0; n<subListCnt; n++) {
|
||||||
Subscriber sub = subs[n];
|
Subscriber sub = subs[n];
|
||||||
|
if (sub == null)
|
||||||
|
continue;
|
||||||
if (notify(sub, stateVar) == false) {
|
if (notify(sub, stateVar) == false) {
|
||||||
/* Don't remove for NMPR specification.
|
/* Don't remove for NMPR specification.
|
||||||
removeSubscriber(sub);
|
removeSubscriber(sub);
|
||||||
@ -744,4 +841,47 @@ public class Service
|
|||||||
action.setActionListener(listener);
|
action.setActionListener(listener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add the StateVariable to the service.<br>
|
||||||
|
* <br>
|
||||||
|
* Note: This method should be used to create a dynamic<br>
|
||||||
|
* Device withtout writing any XML that describe the device<br>.
|
||||||
|
* <br>
|
||||||
|
* Note: that no control for duplicate StateVariable is done.
|
||||||
|
*
|
||||||
|
* @param var StateVariable that will be added
|
||||||
|
*
|
||||||
|
* @author Stefano "Kismet" Lenzi - kismet-sl@users.sourceforge.net - 2005
|
||||||
|
*/
|
||||||
|
public void addStateVariable(StateVariable var) {
|
||||||
|
//TODO Some test are done not stable
|
||||||
|
Node stateTableNode = getSCPDNode().getNode(ServiceStateTable.ELEM_NAME);
|
||||||
|
if (stateTableNode == null){
|
||||||
|
stateTableNode = new Node(ServiceStateTable.ELEM_NAME);
|
||||||
|
/*
|
||||||
|
* Force the node <serviceStateTable> to be the first node inside <scpd>
|
||||||
|
*/
|
||||||
|
//getSCPDNode().insertNode(stateTableNode,0);
|
||||||
|
getSCPDNode().addNode(stateTableNode);
|
||||||
|
}
|
||||||
|
var.setServiceNode(getServiceNode());
|
||||||
|
stateTableNode.addNode(var.getStateVariableNode());
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// userData
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private Object userData = null;
|
||||||
|
|
||||||
|
public void setUserData(Object data)
|
||||||
|
{
|
||||||
|
userData = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getUserData()
|
||||||
|
{
|
||||||
|
return userData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
package org.cybergarage.upnp;
|
package org.cybergarage.upnp;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Vector;
|
||||||
|
|
||||||
public class ServiceList extends Vector
|
public class ServiceList extends Vector
|
||||||
{
|
{
|
||||||
@ -25,7 +25,6 @@ public class ServiceList extends Vector
|
|||||||
// Constants
|
// Constants
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private static final long serialVersionUID = 6372904993975135597L;
|
|
||||||
public final static String ELEM_NAME = "serviceList";
|
public final static String ELEM_NAME = "serviceList";
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
package org.cybergarage.upnp;
|
package org.cybergarage.upnp;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Vector;
|
||||||
|
|
||||||
public class ServiceStateTable extends Vector
|
public class ServiceStateTable extends Vector
|
||||||
{
|
{
|
||||||
@ -23,7 +23,6 @@ public class ServiceStateTable extends Vector
|
|||||||
// Constants
|
// Constants
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private static final long serialVersionUID = 7626909231678469365L;
|
|
||||||
public final static String ELEM_NAME = "serviceStateTable";
|
public final static String ELEM_NAME = "serviceStateTable";
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
@ -5,10 +5,10 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002
|
* Copyright (C) Satoshi Konno 2002
|
||||||
*
|
*
|
||||||
* File: StateVariable.java
|
* File: StateVariable.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 12/06/02
|
* 12/06/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 06/17/03
|
* 06/17/03
|
||||||
* - Added setSendEvents(), isSendEvents().
|
* - Added setSendEvents(), isSendEvents().
|
||||||
@ -39,36 +39,45 @@
|
|||||||
* - Fixed setValue() to compare only when the current value is not null.
|
* - Fixed setValue() to compare only when the current value is not null.
|
||||||
* 02/28/05
|
* 02/28/05
|
||||||
* - Changed getAllowedValueList() to use AllowedValue instead of String as the member.
|
* - Changed getAllowedValueList() to use AllowedValue instead of String as the member.
|
||||||
|
* 04/12/06
|
||||||
|
* - Added setUserData() and getUserData() to set a user original data object.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp;
|
|
||||||
|
|
||||||
import org.cybergarage.xml.*;
|
|
||||||
import org.cybergarage.util.*;
|
|
||||||
|
|
||||||
import org.cybergarage.upnp.control.*;
|
package org.cybergarage.upnp;
|
||||||
import org.cybergarage.upnp.xml.*;
|
import java.util.Iterator;
|
||||||
|
|
||||||
public class StateVariable extends NodeData
|
import org.cybergarage.upnp.control.QueryListener;
|
||||||
{
|
import org.cybergarage.upnp.control.QueryRequest;
|
||||||
////////////////////////////////////////////////
|
import org.cybergarage.upnp.control.QueryResponse;
|
||||||
// Constants
|
import org.cybergarage.upnp.xml.NodeData;
|
||||||
////////////////////////////////////////////////
|
import org.cybergarage.upnp.xml.StateVariableData;
|
||||||
|
import org.cybergarage.util.Debug;
|
||||||
public final static String ELEM_NAME = "stateVariable";
|
import org.cybergarage.xml.Node;
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
public class StateVariable extends NodeData
|
||||||
// Member
|
{
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
// Constants
|
||||||
private Node stateVariableNode;
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public final static String ELEM_NAME = "stateVariable";
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Member
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private Node stateVariableNode;
|
||||||
private Node serviceNode;
|
private Node serviceNode;
|
||||||
|
|
||||||
public Node getServiceNode()
|
public Node getServiceNode()
|
||||||
{
|
{
|
||||||
return serviceNode;
|
return serviceNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setServiceNode(Node n){
|
||||||
|
serviceNode=n;
|
||||||
|
}
|
||||||
|
|
||||||
public Service getService()
|
public Service getService()
|
||||||
{
|
{
|
||||||
@ -77,68 +86,68 @@ public class StateVariable extends NodeData
|
|||||||
return null;
|
return null;
|
||||||
return new Service(serviceNode);
|
return new Service(serviceNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getStateVariableNode()
|
public Node getStateVariableNode()
|
||||||
{
|
{
|
||||||
return stateVariableNode;
|
return stateVariableNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public StateVariable()
|
public StateVariable()
|
||||||
{
|
{
|
||||||
this.serviceNode = null;
|
this.serviceNode = null;
|
||||||
this.stateVariableNode = new Node();
|
this.stateVariableNode = new Node(ELEM_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
public StateVariable(Node serviceNode, Node stateVarNode)
|
public StateVariable(Node serviceNode, Node stateVarNode)
|
||||||
{
|
{
|
||||||
this.serviceNode = serviceNode;
|
this.serviceNode = serviceNode;
|
||||||
this.stateVariableNode = stateVarNode;
|
this.stateVariableNode = stateVarNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// isStateVariableNode
|
// isStateVariableNode
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public static boolean isStateVariableNode(Node node)
|
public static boolean isStateVariableNode(Node node)
|
||||||
{
|
{
|
||||||
return StateVariable.ELEM_NAME.equals(node.getName());
|
return StateVariable.ELEM_NAME.equals(node.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// name
|
// name
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private final static String NAME = "name";
|
private final static String NAME = "name";
|
||||||
|
|
||||||
public void setName(String value)
|
public void setName(String value)
|
||||||
{
|
{
|
||||||
getStateVariableNode().setNode(NAME, value);
|
getStateVariableNode().setNode(NAME, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName()
|
public String getName()
|
||||||
{
|
{
|
||||||
return getStateVariableNode().getNodeValue(NAME);
|
return getStateVariableNode().getNodeValue(NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// dataType
|
// dataType
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private final static String DATATYPE = "dataType";
|
private final static String DATATYPE = "dataType";
|
||||||
|
|
||||||
public void setDataType(String value)
|
public void setDataType(String value)
|
||||||
{
|
{
|
||||||
getStateVariableNode().setNode(DATATYPE, value);
|
getStateVariableNode().setNode(DATATYPE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDataType()
|
public String getDataType()
|
||||||
{
|
{
|
||||||
return getStateVariableNode().getNodeValue(DATATYPE);
|
return getStateVariableNode().getNodeValue(DATATYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// dataType
|
// dataType
|
||||||
@ -238,7 +247,7 @@ public class StateVariable extends NodeData
|
|||||||
AllowedValueList valueList= new AllowedValueList();
|
AllowedValueList valueList= new AllowedValueList();
|
||||||
Node valueListNode = getStateVariableNode().getNode(AllowedValueList.ELEM_NAME);
|
Node valueListNode = getStateVariableNode().getNode(AllowedValueList.ELEM_NAME);
|
||||||
if (valueListNode == null)
|
if (valueListNode == null)
|
||||||
return valueList;
|
return null;
|
||||||
int nNode = valueListNode.getNNodes();
|
int nNode = valueListNode.getNNodes();
|
||||||
for (int n=0; n<nNode; n++) {
|
for (int n=0; n<nNode; n++) {
|
||||||
Node node = valueListNode.getNode(n);
|
Node node = valueListNode.getNode(n);
|
||||||
@ -250,10 +259,41 @@ public class StateVariable extends NodeData
|
|||||||
return valueList;
|
return valueList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method ovverride the value of the AllowedValueList Node<br>
|
||||||
|
* of this object. <br>
|
||||||
|
* <br>
|
||||||
|
* Note: This method should be used to create a dynamic<br>
|
||||||
|
* Device withtout writing any XML that describe the device<br>.
|
||||||
|
* <br>
|
||||||
|
* Note2: The enforce the constraint of the SCPD rule the<br>
|
||||||
|
* AllowedValueList and AllowedValueRange are mutal exclusive<br>
|
||||||
|
* the last set will be the only present<br>
|
||||||
|
*
|
||||||
|
* @param avl The new AllowedValueList
|
||||||
|
*
|
||||||
|
* @author Stefano "Kismet" Lenzi - kismet-sl@users.sourceforge.net - 2005
|
||||||
|
*/
|
||||||
|
public void setAllowedValueList(AllowedValueList avl) {
|
||||||
|
//TODO Some test done not stable
|
||||||
|
getStateVariableNode().removeNode(AllowedValueList.ELEM_NAME);
|
||||||
|
getStateVariableNode().removeNode(AllowedValueRange.ELEM_NAME);
|
||||||
|
Node n = new Node(AllowedValueList.ELEM_NAME);
|
||||||
|
Iterator i=avl.iterator();
|
||||||
|
while (i.hasNext()) {
|
||||||
|
AllowedValue av = (AllowedValue) i.next();
|
||||||
|
//n.addNode(new Node(AllowedValue.ELEM_NAME,av.getValue())); wrong!
|
||||||
|
n.addNode(av.getAllowedValueNode()); //better (twa)
|
||||||
|
}
|
||||||
|
getStateVariableNode().addNode(n);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean hasAllowedValueList()
|
public boolean hasAllowedValueList()
|
||||||
{
|
{
|
||||||
AllowedValueList valueList = getAllowedValueList();
|
AllowedValueList valueList = getAllowedValueList();
|
||||||
return (0 < valueList.size()) ? true : false;
|
return (valueList != null) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -267,6 +307,29 @@ public class StateVariable extends NodeData
|
|||||||
return null;
|
return null;
|
||||||
return new AllowedValueRange(valueRangeNode);
|
return new AllowedValueRange(valueRangeNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method ovverride the value of the AllowedValueRange Node<br>
|
||||||
|
* of this object. <br>
|
||||||
|
* <br>
|
||||||
|
* Note: This method should be used to create a dynamic<br>
|
||||||
|
* Device withtout writing any XML that describe the device<br>.
|
||||||
|
* <br>
|
||||||
|
* Note2: The enforce the constraint of the SCPD rule the<br>
|
||||||
|
* AllowedValueList and AllowedValueRange are mutal exclusive<br>
|
||||||
|
* the last set will be the only present<br>
|
||||||
|
*
|
||||||
|
* @param avr The new AllowedValueRange
|
||||||
|
*
|
||||||
|
* @author Stefano "Kismet" Lenzi - kismet-sl@users.sourceforge.net - 2005
|
||||||
|
*/
|
||||||
|
public void setAllowedValueRange(AllowedValueRange avr){
|
||||||
|
//TODO Some test done not stable
|
||||||
|
getStateVariableNode().removeNode(AllowedValueList.ELEM_NAME);
|
||||||
|
getStateVariableNode().removeNode(AllowedValueRange.ELEM_NAME);
|
||||||
|
getStateVariableNode().addNode(avr.getAllowedValueRangeNode());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public boolean hasAllowedValueRange()
|
public boolean hasAllowedValueRange()
|
||||||
{
|
{
|
||||||
@ -371,4 +434,44 @@ public class StateVariable extends NodeData
|
|||||||
{
|
{
|
||||||
return upnpStatus;
|
return upnpStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final String DEFAULT_VALUE = "defaultValue";
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
/**
|
||||||
|
* Get the value of DefaultValue of this StateVariable
|
||||||
|
*
|
||||||
|
* @author Stefano Lenzi kismet-sl@users.sourceforge.net
|
||||||
|
*/
|
||||||
|
public String getDefaultValue() {
|
||||||
|
return getStateVariableNode().getNodeValue(DEFAULT_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method ovverride the value of the DefaultValue of this object. <br>
|
||||||
|
* <br>
|
||||||
|
* Note: This method should be used to create a dynamic<br>
|
||||||
|
* Device withtout writing any XML that describe the device<br>.
|
||||||
|
*
|
||||||
|
* @param value The new String value
|
||||||
|
*
|
||||||
|
* @author Stefano Lenzi kismet-sl@users.sourceforge.net
|
||||||
|
*/
|
||||||
|
public void setDefaultValue(String value){
|
||||||
|
getStateVariableNode().setNode(DEFAULT_VALUE,value);
|
||||||
|
}
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// userData
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private Object userData = null;
|
||||||
|
|
||||||
|
public void setUserData(Object data)
|
||||||
|
{
|
||||||
|
userData = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getUserData()
|
||||||
|
{
|
||||||
|
return userData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,53 +5,63 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002-2003
|
* Copyright (C) Satoshi Konno 2002-2003
|
||||||
*
|
*
|
||||||
* File: UPnP.java
|
* File: UPnP.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 11/18/02
|
* 11/18/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 05/13/03
|
* 05/13/03
|
||||||
* - Added support for IPv6 and loopback address.
|
* - Added support for IPv6 and loopback address.
|
||||||
* 12/26/03
|
* 12/26/03
|
||||||
* - Added support for XML Parser
|
* - Added support for XML Parser
|
||||||
* 06/18/03
|
* 06/18/03
|
||||||
* - Added INMPR03 and INMPR03_VERSION.
|
* - Added INMPR03 and INMPR03_VERSION.
|
||||||
|
* 04/14/06
|
||||||
|
* - Added some functios about time-to-live, and the default value is 4.
|
||||||
|
* 05/11/09
|
||||||
|
* - Changed loadDefaultXMLParser() to load org.cybergarage.xml.parser.XmlPullParser at first.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp;
|
|
||||||
|
|
||||||
import org.cybergarage.upnp.ssdp.*;
|
package org.cybergarage.upnp;
|
||||||
//import org.cybergarage.util.*;
|
|
||||||
import org.cybergarage.xml.*;
|
|
||||||
import org.cybergarage.xml.parser.*;
|
|
||||||
import org.cybergarage.soap.*;
|
|
||||||
import org.cybergarage.net.*;
|
|
||||||
|
|
||||||
public class UPnP
|
import org.cybergarage.net.HostInterface;
|
||||||
{
|
import org.cybergarage.soap.SOAP;
|
||||||
////////////////////////////////////////////////
|
import org.cybergarage.upnp.ssdp.SSDP;
|
||||||
// Constants
|
import org.cybergarage.util.Debug;
|
||||||
////////////////////////////////////////////////
|
import org.cybergarage.xml.Parser;
|
||||||
|
|
||||||
public final static String NAME = "CyberLink";
|
public class UPnP
|
||||||
public final static String VERSION = "1.7";
|
{
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Constants
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the system properties used to identifies the default XML Parser.<br>
|
||||||
|
* The value of the properties MUST BE the fully qualified class name of<br>
|
||||||
|
* XML Parser which CyberLink should use.
|
||||||
|
*/
|
||||||
|
public final static String XML_CLASS_PROPERTTY="cyberlink.upnp.xml.parser";
|
||||||
|
|
||||||
|
public final static String NAME = "CyberLinkJava";
|
||||||
|
public final static String VERSION = "1.8";
|
||||||
|
|
||||||
public final static int SERVER_RETRY_COUNT = 100;
|
public final static int SERVER_RETRY_COUNT = 100;
|
||||||
public final static int DEFAULT_EXPIRED_DEVICE_EXTRA_TIME = 60;
|
public final static int DEFAULT_EXPIRED_DEVICE_EXTRA_TIME = 60;
|
||||||
|
|
||||||
public final static String getServerName()
|
public final static String getServerName()
|
||||||
{
|
{
|
||||||
String osName = System.getProperty("os.name");
|
String osName = System.getProperty("os.name");
|
||||||
String osVer = System.getProperty("os.version");
|
String osVer = System.getProperty("os.version");
|
||||||
return osName + "/" + osVer + " UPnP/1.0 " + NAME + "/" + VERSION;
|
return osName + "/" + osVer + " UPnP/1.0 " + NAME + "/" + VERSION;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final static String INMPR03 = "INMPR03";
|
public final static String INMPR03 = "INMPR03";
|
||||||
public final static String INMPR03_VERSION = "1.0";
|
public final static String INMPR03_VERSION = "1.0";
|
||||||
public final static int INMPR03_DISCOVERY_OVER_WIRELESS_COUNT = 4;
|
public final static int INMPR03_DISCOVERY_OVER_WIRELESS_COUNT = 4;
|
||||||
|
|
||||||
public final static String XML_DECLARATION = "<?xml version=\"1.0\"?>";
|
public final static String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Enable / Disable
|
// Enable / Disable
|
||||||
@ -193,8 +203,67 @@ public class UPnP
|
|||||||
|
|
||||||
public final static Parser getXMLParser()
|
public final static Parser getXMLParser()
|
||||||
{
|
{
|
||||||
|
if(xmlParser == null){
|
||||||
|
xmlParser = loadDefaultXMLParser();
|
||||||
|
if(xmlParser == null)
|
||||||
|
throw new RuntimeException("No XML parser defined. And unable to laod any. \n" +
|
||||||
|
"Try to invoke UPnP.setXMLParser before UPnP.getXMLParser");
|
||||||
|
SOAP.setXMLParser(xmlParser);
|
||||||
|
}
|
||||||
return xmlParser;
|
return xmlParser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method loads the default XML Parser using the following behavior:
|
||||||
|
* - First if present loads the parsers specified by the system property {@link UPnP#XML_CLASS_PROPERTTY}<br>
|
||||||
|
* - Second by a fall-back technique, it tries to load the XMLParser from one<br>
|
||||||
|
* of the following classes: {@link JaxpParser}, {@link kXML2Parser}, {@link XercesParser}
|
||||||
|
*
|
||||||
|
* @return {@link Parser} which has been loaded successuflly or null otherwise
|
||||||
|
*
|
||||||
|
* @since 1.8.0
|
||||||
|
*/
|
||||||
|
private static Parser loadDefaultXMLParser() {
|
||||||
|
Parser parser = null;
|
||||||
|
|
||||||
|
String[] parserClass = new String[]{
|
||||||
|
System.getProperty(XML_CLASS_PROPERTTY),
|
||||||
|
"org.cybergarage.xml.parser.XmlPullParser",
|
||||||
|
"org.cybergarage.xml.parser.JaxpParser",
|
||||||
|
"org.cybergarage.xml.parser.kXML2Parser",
|
||||||
|
"org.cybergarage.xml.parser.XercesParser"
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int i = 0; i < parserClass.length; i++) {
|
||||||
|
if(parserClass[i]==null)
|
||||||
|
continue;
|
||||||
|
try {
|
||||||
|
parser = (Parser) Class.forName(parserClass[i]).newInstance();
|
||||||
|
return parser;
|
||||||
|
} catch (Throwable e) {
|
||||||
|
Debug.warning("Unable to load "+parserClass[i]+" as XMLParser due to "+e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// TTL
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public final static int DEFAULT_TTL = 4;
|
||||||
|
|
||||||
|
private static int timeToLive = DEFAULT_TTL;
|
||||||
|
|
||||||
|
public final static void setTimeToLive(int value)
|
||||||
|
{
|
||||||
|
timeToLive = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final static int getTimeToLive()
|
||||||
|
{
|
||||||
|
return timeToLive;
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Initialize
|
// Initialize
|
||||||
@ -206,16 +275,14 @@ public class UPnP
|
|||||||
// Interface Option
|
// Interface Option
|
||||||
////////////////////////////
|
////////////////////////////
|
||||||
|
|
||||||
setXMLParser(new JaxpParser());
|
|
||||||
//setXMLParser(new kXML2Parser());
|
//setXMLParser(new kXML2Parser());
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////
|
////////////////////////////
|
||||||
// Interface Option
|
// TimeToLive
|
||||||
////////////////////////////
|
////////////////////////////
|
||||||
/*
|
|
||||||
if (HostInterface.hasIPv6Addresses() == true)
|
setTimeToLive(DEFAULT_TTL);
|
||||||
setEnable(USE_ONLY_IPV6_ADDR);
|
|
||||||
*/
|
|
||||||
|
|
||||||
////////////////////////////
|
////////////////////////////
|
||||||
// Debug Option
|
// Debug Option
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp;
|
package org.cybergarage.upnp;
|
||||||
|
import org.cybergarage.http.HTTPStatus;
|
||||||
|
|
||||||
public class UPnPStatus
|
public class UPnPStatus
|
||||||
{
|
{
|
||||||
@ -39,8 +40,8 @@ public class UPnPStatus
|
|||||||
case INVALID_VAR: return "Invalid Var";
|
case INVALID_VAR: return "Invalid Var";
|
||||||
case PRECONDITION_FAILED: return "Precondition Failed";
|
case PRECONDITION_FAILED: return "Precondition Failed";
|
||||||
case ACTION_FAILED: return "Action Failed";
|
case ACTION_FAILED: return "Action Failed";
|
||||||
|
default: return HTTPStatus.code2String(code);
|
||||||
}
|
}
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
@ -102,6 +102,11 @@ public class ControlResponse extends SOAPResponse
|
|||||||
return faultNode;
|
return faultNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Node createFaultResponseNode(int errCode)
|
||||||
|
{
|
||||||
|
return createFaultResponseNode(errCode, UPnPStatus.code2String(errCode));
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// UPnP Error
|
// UPnP Error
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
@ -74,6 +74,8 @@ public class QueryRequest extends ControlRequest
|
|||||||
{
|
{
|
||||||
Service service = stateVar.getService();
|
Service service = stateVar.getService();
|
||||||
|
|
||||||
|
String ctrlURL = service.getControlURL();
|
||||||
|
|
||||||
setRequestHost(service);
|
setRequestHost(service);
|
||||||
|
|
||||||
setEnvelopeNode(SOAP.createEnvelopeBodyNode());
|
setEnvelopeNode(SOAP.createEnvelopeBodyNode());
|
||||||
|
@ -19,8 +19,6 @@ import java.io.*;
|
|||||||
|
|
||||||
public class InvalidDescriptionException extends Exception
|
public class InvalidDescriptionException extends Exception
|
||||||
{
|
{
|
||||||
private static final long serialVersionUID = -3144583349586910509L;
|
|
||||||
|
|
||||||
public InvalidDescriptionException()
|
public InvalidDescriptionException()
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
|
@ -115,6 +115,7 @@ public class NotifyRequest extends SOAPRequest
|
|||||||
|
|
||||||
public boolean setRequest(Subscriber sub, String varName, String value)
|
public boolean setRequest(Subscriber sub, String varName, String value)
|
||||||
{
|
{
|
||||||
|
String callback = sub.getDeliveryURL();
|
||||||
String sid = sub.getSID();
|
String sid = sub.getSID();
|
||||||
long notifyCnt = sub.getNotifyCount();
|
long notifyCnt = sub.getNotifyCount();
|
||||||
String host = sub.getDeliveryHost();
|
String host = sub.getDeliveryHost();
|
||||||
@ -154,6 +155,19 @@ public class NotifyRequest extends SOAPRequest
|
|||||||
return propSetNode;
|
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@cefriel.it> (09/08/03)
|
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> (09/08/03)
|
||||||
private Property getProperty(Node varNode)
|
private Property getProperty(Node varNode)
|
||||||
{
|
{
|
||||||
|
@ -23,7 +23,6 @@ public class PropertyList extends Vector
|
|||||||
// Constants
|
// Constants
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private static final long serialVersionUID = 8718064210738306226L;
|
|
||||||
public final static String ELEM_NAME = "PropertyList";
|
public final static String ELEM_NAME = "PropertyList";
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
@ -25,8 +25,6 @@ public class SubscriberList extends Vector
|
|||||||
// Constructor
|
// Constructor
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private static final long serialVersionUID = -648427977601494972L;
|
|
||||||
|
|
||||||
public SubscriberList()
|
public SubscriberList()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -68,6 +68,8 @@ public class Subscription
|
|||||||
{
|
{
|
||||||
if (headerValue == null)
|
if (headerValue == null)
|
||||||
return "";
|
return "";
|
||||||
|
if (headerValue.startsWith(Subscription.UUID) == false)
|
||||||
|
return headerValue;
|
||||||
return headerValue.substring(Subscription.UUID.length(), headerValue.length());
|
return headerValue.substring(Subscription.UUID.length(), headerValue.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,12 +43,12 @@ public class SubscriptionRequest extends HTTPRequest
|
|||||||
// Constructor
|
// Constructor
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public SubscriptionRequest()
|
public SubscriptionRequest(){
|
||||||
{
|
setContentLength(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SubscriptionRequest(HTTPRequest httpReq)
|
public SubscriptionRequest(HTTPRequest httpReq){
|
||||||
{
|
this();
|
||||||
set(httpReq);
|
set(httpReq);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002-2004
|
* Copyright (C) Satoshi Konno 2002-2004
|
||||||
*
|
*
|
||||||
* File: HTTPMU.java
|
* File: HTTPMU.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 11/18/02
|
* 11/18/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 09/03/03
|
* 09/03/03
|
||||||
* - Changed to open the socket using setReuseAddress().
|
* - Changed to open the socket using setReuseAddress().
|
||||||
* 12/10/03
|
* 12/10/03
|
||||||
@ -19,39 +19,55 @@
|
|||||||
* 11/19/04
|
* 11/19/04
|
||||||
* - Theo Beisch <theo.beisch@gmx.de>
|
* - Theo Beisch <theo.beisch@gmx.de>
|
||||||
* - Changed send() to set the TTL as 4.
|
* - Changed send() to set the TTL as 4.
|
||||||
|
* 08/23/07
|
||||||
|
* - Thanks for Kazuyuki Shudo
|
||||||
|
* - Changed receive() to throw IOException.
|
||||||
|
* 01/10/08
|
||||||
|
* - Changed getLocalAddress() to return a brank string when the ssdpMultiGroup or ssdpMultiIf is null on Android m3-rc37a.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp.ssdp;
|
|
||||||
|
|
||||||
import java.net.*;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import org.cybergarage.http.*;
|
package org.cybergarage.upnp.ssdp;
|
||||||
import org.cybergarage.util.*;
|
|
||||||
|
import java.net.DatagramPacket;
|
||||||
public class HTTPMUSocket
|
import java.net.MulticastSocket;
|
||||||
{
|
import java.net.Inet4Address;
|
||||||
////////////////////////////////////////////////
|
import java.net.Inet6Address;
|
||||||
// Member
|
import java.net.InetAddress;
|
||||||
////////////////////////////////////////////////
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.NetworkInterface;
|
||||||
private InetSocketAddress ssdpMultiGroup = null;
|
import java.util.Enumeration;
|
||||||
private MulticastSocket ssdpMultiSock = null;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.cybergarage.http.HTTPRequest;
|
||||||
|
import org.cybergarage.upnp.UPnP;
|
||||||
|
import org.cybergarage.util.Debug;
|
||||||
|
|
||||||
|
// Dummy Class for Android m3-rc37a
|
||||||
|
// import org.cybergarage.android.MulticastSocket;
|
||||||
|
|
||||||
|
public class HTTPMUSocket
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Member
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private InetSocketAddress ssdpMultiGroup = null;
|
||||||
|
private MulticastSocket ssdpMultiSock = null;
|
||||||
private NetworkInterface ssdpMultiIf = null;
|
private NetworkInterface ssdpMultiIf = null;
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public HTTPMUSocket()
|
public HTTPMUSocket()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public HTTPMUSocket(String addr, int port, String bindAddr)
|
public HTTPMUSocket(String addr, int port, String bindAddr)
|
||||||
{
|
{
|
||||||
open(addr, port, bindAddr);
|
open(addr, port, bindAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void finalize()
|
protected void finalize()
|
||||||
{
|
{
|
||||||
@ -64,6 +80,8 @@ public class HTTPMUSocket
|
|||||||
|
|
||||||
public String getLocalAddress()
|
public String getLocalAddress()
|
||||||
{
|
{
|
||||||
|
if (ssdpMultiGroup == null || ssdpMultiIf == null)
|
||||||
|
return "";
|
||||||
InetAddress mcastAddr = ssdpMultiGroup.getAddress();
|
InetAddress mcastAddr = ssdpMultiGroup.getAddress();
|
||||||
Enumeration addrs = ssdpMultiIf.getInetAddresses();
|
Enumeration addrs = ssdpMultiIf.getInetAddresses();
|
||||||
while (addrs.hasMoreElements()) {
|
while (addrs.hasMoreElements()) {
|
||||||
@ -76,6 +94,34 @@ public class HTTPMUSocket
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return the destination port for multicast packet
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public int getMulticastPort(){
|
||||||
|
return ssdpMultiGroup.getPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return the source port for multicast packet
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public int getLocalPort(){
|
||||||
|
return ssdpMultiSock.getLocalPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return the opened {@link MulticastSocket}
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public MulticastSocket getSocket(){
|
||||||
|
return ssdpMultiSock;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// MulticastAddr
|
// MulticastAddr
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -90,109 +136,124 @@ public class HTTPMUSocket
|
|||||||
return getMulticastInetAddress().getHostAddress();
|
return getMulticastInetAddress().getHostAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
/**
|
||||||
// open/close
|
* @param addr {@link String} rappresenting the multicast hostname to join into.
|
||||||
////////////////////////////////////////////////
|
* @param port int rappresenting the port to be use poth as source and destination
|
||||||
|
* @param bindAddr {@link InetAddress} which identify the hostname of the interface
|
||||||
public boolean open(String addr, int port, String bindAddr)
|
* to use for sending and recieving multicast packet
|
||||||
{
|
*/
|
||||||
try {
|
public boolean open(String addr,int port, InetAddress bindAddr){
|
||||||
|
try {
|
||||||
ssdpMultiSock = new MulticastSocket(null);
|
ssdpMultiSock = new MulticastSocket(null);
|
||||||
ssdpMultiSock.setReuseAddress(true);
|
ssdpMultiSock.setReuseAddress(true);
|
||||||
InetSocketAddress bindSockAddr = new InetSocketAddress(port);
|
InetSocketAddress bindSockAddr = new InetSocketAddress(port);
|
||||||
ssdpMultiSock.bind(bindSockAddr);
|
ssdpMultiSock.bind(bindSockAddr);
|
||||||
ssdpMultiGroup = new InetSocketAddress(InetAddress.getByName(addr), port);
|
ssdpMultiGroup = new InetSocketAddress(InetAddress.getByName(addr), port);
|
||||||
ssdpMultiIf = NetworkInterface.getByInetAddress(InetAddress.getByName(bindAddr));
|
ssdpMultiIf = NetworkInterface.getByInetAddress(bindAddr);
|
||||||
ssdpMultiSock.joinGroup(ssdpMultiGroup, ssdpMultiIf);
|
ssdpMultiSock.joinGroup(ssdpMultiGroup, ssdpMultiIf);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
Debug.warning(e);
|
Debug.warning(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean close()
|
public boolean open(String addr, int port, String bindAddr)
|
||||||
{
|
{
|
||||||
if (ssdpMultiSock == null)
|
try {
|
||||||
return true;
|
return open(addr,port,InetAddress.getByName(bindAddr));
|
||||||
|
}catch (Exception e) {
|
||||||
try {
|
Debug.warning(e);
|
||||||
ssdpMultiSock.leaveGroup(ssdpMultiGroup, ssdpMultiIf);
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean close()
|
||||||
|
{
|
||||||
|
if (ssdpMultiSock == null)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
ssdpMultiSock.leaveGroup(ssdpMultiGroup, ssdpMultiIf);
|
||||||
|
ssdpMultiSock.close();
|
||||||
ssdpMultiSock = null;
|
ssdpMultiSock = null;
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
//Debug.warning(e);
|
//Debug.warning(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// send
|
// send
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public boolean send(String msg, String bindAddr, int bindPort)
|
public boolean send(String msg, String bindAddr, int bindPort)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
MulticastSocket msock;
|
MulticastSocket msock;
|
||||||
if ((bindAddr) != null && (0 < bindPort)) {
|
if ((bindAddr) != null && (0 < bindPort)) {
|
||||||
msock = new MulticastSocket(null);
|
msock = new MulticastSocket(null);
|
||||||
msock.bind(new InetSocketAddress(bindAddr, bindPort));
|
msock.bind(new InetSocketAddress(bindAddr, bindPort));
|
||||||
}
|
}else{
|
||||||
else
|
|
||||||
msock = new MulticastSocket();
|
msock = new MulticastSocket();
|
||||||
|
}
|
||||||
DatagramPacket dgmPacket = new DatagramPacket(msg.getBytes(), msg.length(), ssdpMultiGroup);
|
DatagramPacket dgmPacket = new DatagramPacket(msg.getBytes(), msg.length(), ssdpMultiGroup);
|
||||||
// Thnaks for Tho Beisch (11/09/04)
|
// Thnaks for Theo Beisch (11/09/04)
|
||||||
msock.setTimeToLive(4);
|
msock.setTimeToLive(UPnP.getTimeToLive());
|
||||||
msock.send(dgmPacket);
|
msock.send(dgmPacket);
|
||||||
msock.close();
|
msock.close();
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
Debug.warning(e);
|
Debug.warning(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean send(String msg)
|
|
||||||
{
|
|
||||||
return send(msg, null, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// post (HTTPRequest)
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public boolean post(HTTPRequest req, String bindAddr, int bindPort)
|
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);
|
return send(req.toString(), bindAddr, bindPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean post(HTTPRequest req)
|
public boolean post(HTTPRequest req)
|
||||||
{
|
{
|
||||||
return send(req.toString(), null, -1);
|
return send(req.toString(), null, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// reveive
|
// reveive
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public SSDPPacket receive()
|
public SSDPPacket receive() throws IOException
|
||||||
{
|
{
|
||||||
byte ssdvRecvBuf[] = new byte[SSDP.RECV_MESSAGE_BUFSIZE];
|
byte ssdvRecvBuf[] = new byte[SSDP.RECV_MESSAGE_BUFSIZE];
|
||||||
SSDPPacket recvPacket = new SSDPPacket(ssdvRecvBuf, ssdvRecvBuf.length);
|
SSDPPacket recvPacket = new SSDPPacket(ssdvRecvBuf, ssdvRecvBuf.length);
|
||||||
recvPacket.setLocalAddress(getLocalAddress());
|
recvPacket.setLocalAddress(getLocalAddress());
|
||||||
try {
|
|
||||||
ssdpMultiSock.receive(recvPacket.getDatagramPacket());
|
// Thanks for Kazuyuki Shudo (08/23/07)
|
||||||
recvPacket.setTimeStamp(System.currentTimeMillis());
|
// Thanks for Stephan Mehlhase (2010-10-26)
|
||||||
}
|
if (ssdpMultiSock != null)
|
||||||
catch (Exception e) {
|
ssdpMultiSock.receive(recvPacket.getDatagramPacket()); // throws IOException
|
||||||
//Debug.warning(e);
|
else
|
||||||
}
|
throw new IOException("Multicast socket has already been closed.");
|
||||||
return recvPacket;
|
|
||||||
}
|
recvPacket.setTimeStamp(System.currentTimeMillis());
|
||||||
|
|
||||||
|
return recvPacket;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,9 +23,12 @@
|
|||||||
|
|
||||||
package org.cybergarage.upnp.ssdp;
|
package org.cybergarage.upnp.ssdp;
|
||||||
|
|
||||||
import java.net.*;
|
import java.net.DatagramPacket;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
import org.cybergarage.util.*;
|
import java.net.InetAddress;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
import org.cybergarage.util.Debug;
|
||||||
|
|
||||||
public class HTTPUSocket
|
public class HTTPUSocket
|
||||||
{
|
{
|
||||||
@ -75,6 +78,15 @@ public class HTTPUSocket
|
|||||||
{
|
{
|
||||||
localAddr = addr;
|
localAddr = addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return {@link DatagramSocket} open for receieving packets
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public DatagramSocket getUDPSocket(){
|
||||||
|
return ssdpUniSock;
|
||||||
|
}
|
||||||
|
|
||||||
public String getLocalAddress()
|
public String getLocalAddress()
|
||||||
{
|
{
|
||||||
@ -107,16 +119,29 @@ public class HTTPUSocket
|
|||||||
close();
|
close();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Bind only using the port without the interface address. (2003/12/12)
|
// Changed to bind the specified address and port for Android v1.6 (2009/10/07)
|
||||||
InetSocketAddress bindSock = new InetSocketAddress(/*InetAddress.getByName(bindAddr), */ bindPort);
|
InetSocketAddress bindInetAddr = new InetSocketAddress(InetAddress.getByName(bindAddr), bindPort);
|
||||||
ssdpUniSock = new DatagramSocket(null);
|
ssdpUniSock = new DatagramSocket(bindInetAddr);
|
||||||
ssdpUniSock.setReuseAddress(true);
|
|
||||||
ssdpUniSock.bind(bindSock);
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
Debug.warning(e);
|
Debug.warning(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
try {
|
||||||
|
// Bind only using the port without the interface address. (2003/12/12)
|
||||||
|
InetSocketAddress bindInetAddr = new InetSocketAddress(bindPort);
|
||||||
|
ssdpUniSock = new DatagramSocket(null);
|
||||||
|
ssdpUniSock.setReuseAddress(true);
|
||||||
|
ssdpUniSock.bind(bindInetAddr);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Debug.warning(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
setLocalAddress(bindAddr);
|
setLocalAddress(bindAddr);
|
||||||
|
|
||||||
|
@ -5,27 +5,49 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002
|
* Copyright (C) Satoshi Konno 2002
|
||||||
*
|
*
|
||||||
* File: SSDP.java
|
* File: SSDP.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 11/18/02
|
* 11/18/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 05/13/03
|
* 05/13/03
|
||||||
* - Added constants for IPv6.
|
* - Added constants for IPv6.
|
||||||
|
* 08/03/05
|
||||||
|
* - Thanks for Stefano Lenzi <kismet-sl at users.sourceforge.net>
|
||||||
|
* and Mikael <mhakman at users.sourceforge.net>
|
||||||
|
* - Fixed getLeaseTime() to parse normally when the value includes extra strings such as white space.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp.ssdp;
|
package org.cybergarage.upnp.ssdp;
|
||||||
|
|
||||||
public class SSDP
|
import org.cybergarage.util.Debug;
|
||||||
{
|
|
||||||
////////////////////////////////////////////////
|
/**
|
||||||
// Constants
|
*
|
||||||
////////////////////////////////////////////////
|
* This class defines constant value related to SSDP.<br>
|
||||||
|
* All the values defined here are complaint to the UPnP Standard
|
||||||
public static final int PORT = 1900;
|
*
|
||||||
|
* @author Satoshi "skonno" Konno
|
||||||
public static final String ADDRESS = "239.255.255.250";
|
* @author Stefano "Kismet" Lenzi
|
||||||
|
* @version 1.0
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class SSDP
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Constants
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default destination port for SSDP multicast messages
|
||||||
|
*/
|
||||||
|
public static final int PORT = 1900;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default IPv4 multicast address for SSDP messages
|
||||||
|
*/
|
||||||
|
public static final String ADDRESS = "239.255.255.250";
|
||||||
|
|
||||||
public static final String IPV6_LINK_LOCAL_ADDRESS = "FF02::C";
|
public static final String IPV6_LINK_LOCAL_ADDRESS = "FF02::C";
|
||||||
public static final String IPV6_SUBNET_ADDRESS = "FF03::C";
|
public static final String IPV6_SUBNET_ADDRESS = "FF03::C";
|
||||||
@ -37,7 +59,7 @@ public class SSDP
|
|||||||
|
|
||||||
public static final void setIPv6Address(String addr)
|
public static final void setIPv6Address(String addr)
|
||||||
{
|
{
|
||||||
IPV6_ADDRESS = addr;
|
IPV6_ADDRESS = addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final String getIPv6Address()
|
public static final String getIPv6Address()
|
||||||
@ -45,9 +67,9 @@ public class SSDP
|
|||||||
return IPV6_ADDRESS;
|
return IPV6_ADDRESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final int DEFAULT_MSEARCH_MX = 3;
|
public static final int DEFAULT_MSEARCH_MX = 3;
|
||||||
|
|
||||||
public static final int RECV_MESSAGE_BUFSIZE = 1024;
|
public static final int RECV_MESSAGE_BUFSIZE = 1024;
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Initialize
|
// Initialize
|
||||||
@ -61,17 +83,27 @@ public class SSDP
|
|||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// LeaseTime
|
// LeaseTime
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public final static int getLeaseTime(String cacheCont)
|
public final static int getLeaseTime(String cacheCont){
|
||||||
{
|
/*
|
||||||
int equIdx = cacheCont.indexOf('=');
|
* Search for max-age keyword instead of equals sign Found value of
|
||||||
int mx = 0;
|
* max-age ends at next comma or end of string
|
||||||
try {
|
*/
|
||||||
String mxStr = new String(cacheCont.getBytes(), equIdx+1, cacheCont.length() - (equIdx+1));
|
int mx = 0;
|
||||||
mx = Integer.parseInt(mxStr);
|
int maxAgeIdx = cacheCont.indexOf("max-age");
|
||||||
}
|
if (maxAgeIdx >= 0) {
|
||||||
catch (Exception e) {}
|
int endIdx = cacheCont.indexOf(',',maxAgeIdx);
|
||||||
return mx;
|
if (endIdx < 0)
|
||||||
|
endIdx = cacheCont.length();
|
||||||
|
try {
|
||||||
|
maxAgeIdx = cacheCont.indexOf("=",maxAgeIdx);
|
||||||
|
String mxStr = cacheCont.substring(maxAgeIdx+1,endIdx).trim();
|
||||||
|
mx = Integer.parseInt(mxStr);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Debug.warning (e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
package org.cybergarage.upnp.ssdp;
|
package org.cybergarage.upnp.ssdp;
|
||||||
|
|
||||||
import org.cybergarage.http.*;
|
import org.cybergarage.http.HTTP;
|
||||||
|
|
||||||
public class SSDPNotifyRequest extends SSDPRequest
|
public class SSDPNotifyRequest extends SSDPRequest
|
||||||
{
|
{
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002-2003
|
* Copyright (C) Satoshi Konno 2002-2003
|
||||||
*
|
*
|
||||||
* File: SSDPNotifySocket.java
|
* File: SSDPNotifySocket.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 11/20/02
|
* 11/20/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 05/13/03
|
* 05/13/03
|
||||||
* - Added support for IPv6.
|
* - Added support for IPv6.
|
||||||
* 02/20/04
|
* 02/20/04
|
||||||
@ -19,27 +19,42 @@
|
|||||||
* - Mikael Hakman <mhakman@dkab.net>
|
* - Mikael Hakman <mhakman@dkab.net>
|
||||||
* - Handle receive() returning null.
|
* - Handle receive() returning null.
|
||||||
* - Added close() in stop().
|
* - Added close() in stop().
|
||||||
|
* 08/23/07
|
||||||
|
* - Thanks for Kazuyuki Shudo
|
||||||
|
* - Changed run() to catch IOException of HTTPMUSocket::receive().
|
||||||
|
* 01/31/08
|
||||||
|
* - Changed start() not to abort when the interface infomation is null on Android m3-rc37a.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp.ssdp;
|
|
||||||
|
|
||||||
import java.net.*;
|
package org.cybergarage.upnp.ssdp;
|
||||||
|
|
||||||
|
import java.net.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.cybergarage.net.*;
|
import org.cybergarage.net.*;
|
||||||
import org.cybergarage.util.*;
|
import org.cybergarage.util.*;
|
||||||
import org.cybergarage.http.*;
|
import org.cybergarage.http.*;
|
||||||
import org.cybergarage.upnp.*;
|
import org.cybergarage.upnp.*;
|
||||||
|
|
||||||
public class SSDPNotifySocket extends HTTPMUSocket implements Runnable
|
/**
|
||||||
|
*
|
||||||
|
* This class identifies a SSDP socket only for <b>notifing packet</b>.<br>
|
||||||
|
*
|
||||||
|
* @author Satoshi "skonno" Konno
|
||||||
|
* @author Stefano "Kismet" Lenzi
|
||||||
|
* @version 1.8
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class SSDPNotifySocket extends HTTPMUSocket implements Runnable
|
||||||
{
|
{
|
||||||
private boolean useIPv6Address;
|
private boolean useIPv6Address;
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public SSDPNotifySocket(String bindAddr)
|
public SSDPNotifySocket(String bindAddr)
|
||||||
{
|
{
|
||||||
String addr = SSDP.ADDRESS;
|
String addr = SSDP.ADDRESS;
|
||||||
useIPv6Address = false;
|
useIPv6Address = false;
|
||||||
@ -48,29 +63,32 @@ public class SSDPNotifySocket extends HTTPMUSocket implements Runnable
|
|||||||
useIPv6Address = true;
|
useIPv6Address = true;
|
||||||
}
|
}
|
||||||
open(addr, SSDP.PORT, bindAddr);
|
open(addr, SSDP.PORT, bindAddr);
|
||||||
setControlPoint(null);
|
setControlPoint(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// ControlPoint
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
private ControlPoint controlPoint = null;
|
|
||||||
|
|
||||||
public void setControlPoint(ControlPoint ctrlp)
|
|
||||||
{
|
|
||||||
this.controlPoint = ctrlp;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ControlPoint getControlPoint()
|
|
||||||
{
|
|
||||||
return controlPoint;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// post (SSDPNotifySocket)
|
// ControlPoint
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private ControlPoint controlPoint = null;
|
||||||
|
|
||||||
|
public void setControlPoint(ControlPoint ctrlp)
|
||||||
|
{
|
||||||
|
this.controlPoint = ctrlp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ControlPoint getControlPoint()
|
||||||
|
{
|
||||||
|
return controlPoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method send a {@link SSDPNotifyRequest} over {@link SSDPNotifySocket}
|
||||||
|
*
|
||||||
|
* @param req the {@link SSDPNotifyRequest} to send
|
||||||
|
* @return true if and only if the trasmission succeced<br>
|
||||||
|
* Because it rely on UDP doesn't mean that it's also recieved
|
||||||
|
*/
|
||||||
public boolean post(SSDPNotifyRequest req)
|
public boolean post(SSDPNotifyRequest req)
|
||||||
{
|
{
|
||||||
String ssdpAddr = SSDP.ADDRESS;
|
String ssdpAddr = SSDP.ADDRESS;
|
||||||
@ -80,21 +98,29 @@ public class SSDPNotifySocket extends HTTPMUSocket implements Runnable
|
|||||||
return post((HTTPRequest)req);
|
return post((HTTPRequest)req);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// run
|
// run
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private Thread deviceNotifyThread = null;
|
private Thread deviceNotifyThread = null;
|
||||||
|
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
Thread thisThread = Thread.currentThread();
|
Thread thisThread = Thread.currentThread();
|
||||||
|
|
||||||
ControlPoint ctrlPoint = getControlPoint();
|
ControlPoint ctrlPoint = getControlPoint();
|
||||||
|
|
||||||
while (deviceNotifyThread == thisThread) {
|
while (deviceNotifyThread == thisThread) {
|
||||||
Thread.yield();
|
Thread.yield();
|
||||||
SSDPPacket packet = receive();
|
|
||||||
|
// Thanks for Kazuyuki Shudo (08/23/07)
|
||||||
|
SSDPPacket packet = null;
|
||||||
|
try {
|
||||||
|
packet = receive();
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Thanks for Mikael Hakman (04/20/05)
|
// Thanks for Mikael Hakman (04/20/05)
|
||||||
if (packet == null)
|
if (packet == null)
|
||||||
@ -104,28 +130,35 @@ public class SSDPNotifySocket extends HTTPMUSocket implements Runnable
|
|||||||
InetAddress maddr = getMulticastInetAddress();
|
InetAddress maddr = getMulticastInetAddress();
|
||||||
InetAddress pmaddr = packet.getHostInetAddress();
|
InetAddress pmaddr = packet.getHostInetAddress();
|
||||||
if (maddr.equals(pmaddr) == false) {
|
if (maddr.equals(pmaddr) == false) {
|
||||||
Debug.warning("Invalidate Multicast Recieved : " + maddr + "," + pmaddr);
|
Debug.warning("Invalidate Multicast Recieved from IP " + maddr + " on " + pmaddr);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
//TODO Must be performed on a different Thread in order to prevent UDP packet losses.
|
||||||
if (ctrlPoint != null)
|
if (ctrlPoint != null)
|
||||||
ctrlPoint.notifyReceived(packet);
|
ctrlPoint.notifyReceived(packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start()
|
public void start(){
|
||||||
{
|
StringBuffer name = new StringBuffer("Cyber.SSDPNotifySocket/");
|
||||||
deviceNotifyThread = new Thread(this, "UPnP-SSDPNotifySocket");
|
String localAddr = this.getLocalAddress();
|
||||||
deviceNotifyThread.setDaemon(true);
|
// localAddr is null on Android m3-rc37a (01/30/08)
|
||||||
deviceNotifyThread.start();
|
if (localAddr != null && 0 < localAddr.length()) {
|
||||||
}
|
name.append(this.getLocalAddress()).append(':');
|
||||||
|
name.append(this.getLocalPort()).append(" -> ");
|
||||||
public void stop()
|
name.append(this.getMulticastAddress()).append(':');
|
||||||
{
|
name.append(this.getMulticastPort());
|
||||||
|
}
|
||||||
|
deviceNotifyThread = new Thread(this,name.toString());
|
||||||
|
deviceNotifyThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop()
|
||||||
|
{
|
||||||
// Thanks for Mikael Hakman (04/20/05)
|
// Thanks for Mikael Hakman (04/20/05)
|
||||||
close();
|
close();
|
||||||
|
|
||||||
deviceNotifyThread = null;
|
deviceNotifyThread = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,106 +1,129 @@
|
|||||||
/******************************************************************
|
/******************************************************************
|
||||||
*
|
|
||||||
* CyberUPnP for Java
|
|
||||||
*
|
|
||||||
* Copyright (C) Satoshi Konno 2002-2003
|
|
||||||
*
|
*
|
||||||
* File: HTTPServerList.java
|
* CyberUPnP for Java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Copyright (C) Satoshi Konno 2002-2003
|
||||||
*
|
*
|
||||||
* 05/11/03
|
* File: HTTPServerList.java
|
||||||
* - first revision.
|
*
|
||||||
*
|
* Revision;
|
||||||
******************************************************************/
|
*
|
||||||
|
* 05/11/03
|
||||||
|
* - first revision.
|
||||||
|
*
|
||||||
|
******************************************************************/
|
||||||
|
|
||||||
|
package org.cybergarage.upnp.ssdp;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import org.cybergarage.net.*;
|
||||||
|
|
||||||
|
import org.cybergarage.upnp.*;
|
||||||
|
|
||||||
|
public class SSDPNotifySocketList extends Vector
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Constructor
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private InetAddress[] binds = null;
|
||||||
|
|
||||||
|
public SSDPNotifySocketList() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param binds The host to bind the service <tt>null</tt> means to bind to default.
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public SSDPNotifySocketList(InetAddress[] binds){
|
||||||
|
this.binds=binds;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// 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(){
|
||||||
|
InetAddress[] binds=this.binds ;
|
||||||
|
String[] bindAddresses;
|
||||||
|
if(binds!=null){
|
||||||
|
bindAddresses = new String[binds.length];
|
||||||
|
for (int i = 0; i < binds.length; i++) {
|
||||||
|
bindAddresses[i] = binds[i].getHostAddress();
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
int nHostAddrs = HostInterface.getNHostAddresses();
|
||||||
|
bindAddresses = new String[nHostAddrs];
|
||||||
|
for (int n=0; n<nHostAddrs; n++) {
|
||||||
|
bindAddresses[n] = HostInterface.getHostAddress(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < bindAddresses.length; i++) {
|
||||||
|
if(bindAddresses[i]!=null){
|
||||||
|
SSDPNotifySocket ssdpNotifySocket = new SSDPNotifySocket(bindAddresses[i]);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
package org.cybergarage.upnp.ssdp;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import org.cybergarage.net.*;
|
|
||||||
|
|
||||||
import org.cybergarage.upnp.*;
|
|
||||||
|
|
||||||
public class SSDPNotifySocketList extends Vector
|
|
||||||
{
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Constructor
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
private static final long serialVersionUID = -7066290881503106399L;
|
|
||||||
|
|
||||||
public SSDPNotifySocketList()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Methods
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public SSDPNotifySocket getSSDPNotifySocket(int n)
|
|
||||||
{
|
|
||||||
return (SSDPNotifySocket)get(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// ControlPoint
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public void setControlPoint(ControlPoint ctrlPoint)
|
|
||||||
{
|
|
||||||
int nSockets = size();
|
|
||||||
for (int n=0; n<nSockets; n++) {
|
|
||||||
SSDPNotifySocket sock = getSSDPNotifySocket(n);
|
|
||||||
sock.setControlPoint(ctrlPoint);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Methods
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public boolean open()
|
|
||||||
{
|
|
||||||
int nHostAddrs = HostInterface.getNHostAddresses();
|
|
||||||
for (int n=0; n<nHostAddrs; n++) {
|
|
||||||
String bindAddr = HostInterface.getHostAddress(n);
|
|
||||||
SSDPNotifySocket ssdpNotifySocket = new SSDPNotifySocket(bindAddr);
|
|
||||||
add(ssdpNotifySocket);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void close()
|
|
||||||
{
|
|
||||||
int nSockets = size();
|
|
||||||
for (int n=0; n<nSockets; n++) {
|
|
||||||
SSDPNotifySocket sock = getSSDPNotifySocket(n);
|
|
||||||
sock.close();
|
|
||||||
}
|
|
||||||
clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Methods
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public void start()
|
|
||||||
{
|
|
||||||
int nSockets = size();
|
|
||||||
for (int n=0; n<nSockets; n++) {
|
|
||||||
SSDPNotifySocket sock = getSSDPNotifySocket(n);
|
|
||||||
sock.start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stop()
|
|
||||||
{
|
|
||||||
int nSockets = size();
|
|
||||||
for (int n=0; n<nSockets; n++) {
|
|
||||||
SSDPNotifySocket sock = getSSDPNotifySocket(n);
|
|
||||||
sock.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
|
|
||||||
package org.cybergarage.upnp.ssdp;
|
package org.cybergarage.upnp.ssdp;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
import org.cybergarage.http.*;
|
import org.cybergarage.http.*;
|
||||||
|
|
||||||
public class SSDPRequest extends HTTPRequest
|
public class SSDPRequest extends HTTPRequest
|
||||||
@ -31,6 +33,11 @@ public class SSDPRequest extends HTTPRequest
|
|||||||
setVersion(HTTP.VERSION_11);
|
setVersion(HTTP.VERSION_11);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SSDPRequest(InputStream in)
|
||||||
|
{
|
||||||
|
super(in);
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// NT
|
// NT
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
@ -24,6 +24,8 @@
|
|||||||
|
|
||||||
package org.cybergarage.upnp.ssdp;
|
package org.cybergarage.upnp.ssdp;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
import org.cybergarage.http.*;
|
import org.cybergarage.http.*;
|
||||||
|
|
||||||
public class SSDPResponse extends HTTPResponse
|
public class SSDPResponse extends HTTPResponse
|
||||||
@ -37,6 +39,11 @@ public class SSDPResponse extends HTTPResponse
|
|||||||
setVersion(HTTP.VERSION_11);
|
setVersion(HTTP.VERSION_11);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SSDPResponse(InputStream in)
|
||||||
|
{
|
||||||
|
super(in);
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// ST (SearchTarget)
|
// ST (SearchTarget)
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -114,7 +121,7 @@ public class SSDPResponse extends HTTPResponse
|
|||||||
|
|
||||||
public String getHeader()
|
public String getHeader()
|
||||||
{
|
{
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuffer str = new StringBuffer();
|
||||||
|
|
||||||
str.append(getStatusLineString());
|
str.append(getStatusLineString());
|
||||||
str.append(getHeaderString());
|
str.append(getHeaderString());
|
||||||
|
@ -5,36 +5,41 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002
|
* Copyright (C) Satoshi Konno 2002
|
||||||
*
|
*
|
||||||
* File: SSDPSearchResponseSocket.java
|
* File: SSDPSearchResponseSocket.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 11/20/02
|
* 11/20/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 05/28/03
|
* 05/28/03
|
||||||
* - Added post() to send a SSDPSearchRequest.
|
* - Added post() to send a SSDPSearchRequest.
|
||||||
|
* 01/31/08
|
||||||
|
* - Changed start() not to abort when the interface infomation is null on Android m3-rc37a.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp.ssdp;
|
package org.cybergarage.upnp.ssdp;
|
||||||
|
|
||||||
|
import java.net.DatagramSocket;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
|
||||||
import org.cybergarage.upnp.*;
|
import org.cybergarage.upnp.*;
|
||||||
|
|
||||||
public class SSDPSearchResponseSocket extends HTTPUSocket implements Runnable
|
public class SSDPSearchResponseSocket extends HTTPUSocket implements Runnable
|
||||||
{
|
{
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public SSDPSearchResponseSocket()
|
public SSDPSearchResponseSocket()
|
||||||
{
|
{
|
||||||
setControlPoint(null);
|
setControlPoint(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SSDPSearchResponseSocket(String bindAddr, int port)
|
public SSDPSearchResponseSocket(String bindAddr, int port)
|
||||||
{
|
{
|
||||||
super(bindAddr, port);
|
super(bindAddr, port);
|
||||||
setControlPoint(null);
|
setControlPoint(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// ControlPoint
|
// ControlPoint
|
||||||
@ -51,49 +56,56 @@ public class SSDPSearchResponseSocket extends HTTPUSocket implements Runnable
|
|||||||
{
|
{
|
||||||
return controlPoint;
|
return controlPoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// run
|
// run
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private Thread deviceSearchResponseThread = null;
|
private Thread deviceSearchResponseThread = null;
|
||||||
|
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
Thread thisThread = Thread.currentThread();
|
Thread thisThread = Thread.currentThread();
|
||||||
|
|
||||||
ControlPoint ctrlPoint = getControlPoint();
|
ControlPoint ctrlPoint = getControlPoint();
|
||||||
|
|
||||||
while (deviceSearchResponseThread == thisThread) {
|
while (deviceSearchResponseThread == thisThread) {
|
||||||
Thread.yield();
|
Thread.yield();
|
||||||
SSDPPacket packet = receive();
|
SSDPPacket packet = receive();
|
||||||
if (packet == null)
|
if (packet == null)
|
||||||
break;
|
break;
|
||||||
if (ctrlPoint != null)
|
if (ctrlPoint != null)
|
||||||
ctrlPoint.searchResponseReceived(packet);
|
ctrlPoint.searchResponseReceived(packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start()
|
public void start() {
|
||||||
{
|
|
||||||
deviceSearchResponseThread = new Thread(this, "UPnP-SSDPSearchResponseSocket");
|
StringBuffer name = new StringBuffer("Cyber.SSDPSearchResponseSocket/");
|
||||||
deviceSearchResponseThread.setDaemon(true);
|
DatagramSocket s = getDatagramSocket();
|
||||||
deviceSearchResponseThread.start();
|
// localAddr is null on Android m3-rc37a (01/30/08)
|
||||||
}
|
InetAddress localAddr = s.getLocalAddress();
|
||||||
|
if (localAddr != null) {
|
||||||
public void stop()
|
name.append(s.getLocalAddress()).append(':');
|
||||||
{
|
name.append(s.getLocalPort());
|
||||||
deviceSearchResponseThread = null;
|
}
|
||||||
}
|
deviceSearchResponseThread = new Thread(this,name.toString());
|
||||||
|
deviceSearchResponseThread.start();
|
||||||
////////////////////////////////////////////////
|
}
|
||||||
// post
|
|
||||||
////////////////////////////////////////////////
|
public void stop()
|
||||||
|
{
|
||||||
public boolean post(String addr, int port, SSDPSearchResponse res)
|
deviceSearchResponseThread = null;
|
||||||
{
|
}
|
||||||
return post(addr, port, res.getHeader());
|
|
||||||
}
|
////////////////////////////////////////////////
|
||||||
|
// post
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public boolean post(String addr, int port, SSDPSearchResponse res)
|
||||||
|
{
|
||||||
|
return post(addr, port, res.getHeader());
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// post
|
// post
|
||||||
@ -104,4 +116,4 @@ public class SSDPSearchResponseSocket extends HTTPUSocket implements Runnable
|
|||||||
return post(addr, port, req.toString());
|
return post(addr, port, req.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,144 +1,166 @@
|
|||||||
/******************************************************************
|
/******************************************************************
|
||||||
*
|
|
||||||
* CyberUPnP for Java
|
|
||||||
*
|
|
||||||
* Copyright (C) Satoshi Konno 2002-2003
|
|
||||||
*
|
*
|
||||||
* File: SSDPSearchResponseSocketList.java
|
* CyberUPnP for Java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Copyright (C) Satoshi Konno 2002-2003
|
||||||
*
|
*
|
||||||
* 05/08/03
|
* File: SSDPSearchResponseSocketList.java
|
||||||
* - first revision.
|
*
|
||||||
* 05/28/03
|
* Revision;
|
||||||
* - Added post() to send a SSDPSearchRequest.
|
*
|
||||||
*
|
* 05/08/03
|
||||||
******************************************************************/
|
* - first revision.
|
||||||
|
* 05/28/03
|
||||||
|
* - Added post() to send a SSDPSearchRequest.
|
||||||
|
*
|
||||||
|
******************************************************************/
|
||||||
|
|
||||||
|
package org.cybergarage.upnp.ssdp;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import org.cybergarage.net.*;
|
||||||
|
|
||||||
|
import org.cybergarage.upnp.*;
|
||||||
|
|
||||||
|
public class SSDPSearchResponseSocketList extends Vector
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Constructor
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private InetAddress[] binds = null;
|
||||||
|
|
||||||
|
public SSDPSearchResponseSocketList() {
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param binds The host to bind.Use <tt>null</tt> for the default behavior
|
||||||
|
*/
|
||||||
|
public SSDPSearchResponseSocketList(InetAddress[] binds) {
|
||||||
|
this.binds = binds;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// ControlPoint
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// 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){
|
||||||
|
InetAddress[] binds=this.binds ;
|
||||||
|
String[] bindAddresses;
|
||||||
|
if(binds!=null){
|
||||||
|
bindAddresses = new String[binds.length];
|
||||||
|
for (int i = 0; i < binds.length; i++) {
|
||||||
|
bindAddresses[i] = binds[i].getHostAddress();
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
int nHostAddrs = HostInterface.getNHostAddresses();
|
||||||
|
bindAddresses = new String[nHostAddrs];
|
||||||
|
for (int n=0; n<nHostAddrs; n++) {
|
||||||
|
bindAddresses[n] = HostInterface.getHostAddress(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
for (int j = 0; j < bindAddresses.length; j++) {
|
||||||
|
SSDPSearchResponseSocket socket = new SSDPSearchResponseSocket(bindAddresses[j], 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
package org.cybergarage.upnp.ssdp;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import org.cybergarage.net.*;
|
|
||||||
|
|
||||||
import org.cybergarage.upnp.*;
|
|
||||||
|
|
||||||
public class SSDPSearchResponseSocketList extends Vector
|
|
||||||
{
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Constructor
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 4509857798038125744L;
|
|
||||||
|
|
||||||
public SSDPSearchResponseSocketList()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// ControlPoint
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public void setControlPoint(ControlPoint ctrlPoint)
|
|
||||||
{
|
|
||||||
int nSockets = size();
|
|
||||||
for (int n=0; n<nSockets; n++) {
|
|
||||||
SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n);
|
|
||||||
sock.setControlPoint(ctrlPoint);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// get
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public SSDPSearchResponseSocket getSSDPSearchResponseSocket(int n)
|
|
||||||
{
|
|
||||||
return (SSDPSearchResponseSocket)get(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Methods
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public boolean open(int port)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
int nHostAddrs = HostInterface.getNHostAddresses();
|
|
||||||
for (int n=0; n<nHostAddrs; n++) {
|
|
||||||
String bindAddr = HostInterface.getHostAddress(n);
|
|
||||||
SSDPSearchResponseSocket socket = new SSDPSearchResponseSocket(bindAddr, port);
|
|
||||||
add(socket);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
stop();
|
|
||||||
close();
|
|
||||||
clear();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean open()
|
|
||||||
{
|
|
||||||
return open(SSDP.PORT);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void close()
|
|
||||||
{
|
|
||||||
int nSockets = size();
|
|
||||||
for (int n=0; n<nSockets; n++) {
|
|
||||||
SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n);
|
|
||||||
sock.close();
|
|
||||||
}
|
|
||||||
clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Methods
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public void start()
|
|
||||||
{
|
|
||||||
int nSockets = size();
|
|
||||||
for (int n=0; n<nSockets; n++) {
|
|
||||||
SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n);
|
|
||||||
sock.start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stop()
|
|
||||||
{
|
|
||||||
int nSockets = size();
|
|
||||||
for (int n=0; n<nSockets; n++) {
|
|
||||||
SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n);
|
|
||||||
sock.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Methods
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public boolean post(SSDPSearchRequest req)
|
|
||||||
{
|
|
||||||
boolean ret = true;
|
|
||||||
int nSockets = size();
|
|
||||||
for (int n=0; n<nSockets; n++) {
|
|
||||||
SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n);
|
|
||||||
String bindAddr = sock.getLocalAddress();
|
|
||||||
req.setLocalAddress(bindAddr);
|
|
||||||
String ssdpAddr = SSDP.ADDRESS;
|
|
||||||
if (HostInterface.isIPv6Address(bindAddr) == true)
|
|
||||||
ssdpAddr = SSDP.getIPv6Address();
|
|
||||||
//sock.joinGroup(ssdpAddr, SSDP.PORT, bindAddr);
|
|
||||||
if (sock.post(ssdpAddr, SSDP.PORT, req) == false)
|
|
||||||
ret = false;
|
|
||||||
//sock.leaveGroup(ssdpAddr, SSDP.PORT, bindAddr);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
* Copyright (C) Satoshi Konno 2002-2003
|
* Copyright (C) Satoshi Konno 2002-2003
|
||||||
*
|
*
|
||||||
* File: SSDPSearchSocket.java
|
* File: SSDPSearchSocket.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 12/30/02
|
* 12/30/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 05/13/03
|
* 05/13/03
|
||||||
* - Added support for IPv6.
|
* - Added support for IPv6.
|
||||||
* 05/28/03
|
* 05/28/03
|
||||||
@ -18,101 +18,177 @@
|
|||||||
* - Mikael Hakman <mhakman@dkab.net>
|
* - Mikael Hakman <mhakman@dkab.net>
|
||||||
* - Added close() in stop().
|
* - Added close() in stop().
|
||||||
* - Added test for null return from receive() in run().
|
* - Added test for null return from receive() in run().
|
||||||
|
* 08/23/07
|
||||||
|
* - Thanks for Kazuyuki Shudo
|
||||||
|
* - Changed run() to catch IOException of HTTPMUSocket::receive().
|
||||||
|
* 01/10/08
|
||||||
|
* - Changed start() not to abort when the interface infomation is null on Android m3-rc37a.
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.upnp.ssdp;
|
package org.cybergarage.upnp.ssdp;
|
||||||
|
|
||||||
|
import java.net.Inet4Address;
|
||||||
|
import java.net.Inet6Address;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.cybergarage.net.*;
|
import org.cybergarage.net.*;
|
||||||
import org.cybergarage.util.*;
|
import org.cybergarage.util.*;
|
||||||
|
|
||||||
import org.cybergarage.upnp.device.*;
|
import org.cybergarage.upnp.device.*;
|
||||||
|
|
||||||
public class SSDPSearchSocket extends HTTPMUSocket implements Runnable
|
public class SSDPSearchSocket extends HTTPMUSocket implements Runnable
|
||||||
{
|
{
|
||||||
public SSDPSearchSocket()
|
private boolean useIPv6Address;
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public SSDPSearchSocket(String bindAddr)
|
////////////////////////////////////////////////
|
||||||
{
|
// Constructor
|
||||||
open(bindAddr);
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param bindAddr The address to bind the service
|
||||||
|
* @param port The port used for accepting message
|
||||||
|
* @param multicast The multicast address to use as destination
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public SSDPSearchSocket(String bindAddr,int port,String multicast){
|
||||||
|
open(bindAddr,multicast);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param bindAddr the binding address for senging multicast packet
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public SSDPSearchSocket(InetAddress bindAddr){
|
||||||
|
if(bindAddr.getAddress().length!=4){
|
||||||
|
this.open((Inet6Address)bindAddr);
|
||||||
|
}else{
|
||||||
|
this.open((Inet4Address)bindAddr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
public boolean open(Inet4Address bindAddr){
|
||||||
|
useIPv6Address = false;
|
||||||
|
return open(SSDP.ADDRESS, SSDP.PORT, bindAddr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean open(Inet6Address bindAddr){
|
||||||
|
useIPv6Address = true;
|
||||||
|
return open(SSDP.getIPv6Address(), SSDP.PORT, bindAddr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean open(String bind,String multicast){
|
||||||
|
if ((HostInterface.isIPv6Address(bind) ) && (HostInterface.isIPv6Address(multicast))){
|
||||||
|
useIPv6Address = true;
|
||||||
|
}else if(HostInterface.isIPv4Address(bind) && (HostInterface.isIPv4Address(multicast))){
|
||||||
|
useIPv6Address = false;
|
||||||
|
}else{
|
||||||
|
throw new IllegalArgumentException("Cannot open a UDP Socket for IPv6 address on IPv4 interface or viceversa");
|
||||||
|
}
|
||||||
|
return open(multicast, SSDP.PORT, bind);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param bindAddr the hostname of the interface to use for senfing multicast packet
|
||||||
|
* @return true if and only if it open the socket
|
||||||
|
* @see {@link SSDP} for default multicast and port destination of the packtes
|
||||||
|
*/
|
||||||
public boolean open(String bindAddr)
|
public boolean open(String bindAddr)
|
||||||
{
|
{
|
||||||
String addr = SSDP.ADDRESS;
|
String addr = SSDP.ADDRESS;
|
||||||
|
useIPv6Address = false;
|
||||||
if (HostInterface.isIPv6Address(bindAddr) == true) {
|
if (HostInterface.isIPv6Address(bindAddr) == true) {
|
||||||
addr = SSDP.getIPv6Address();
|
addr = SSDP.getIPv6Address();
|
||||||
|
useIPv6Address = true;
|
||||||
}
|
}
|
||||||
return open(addr, SSDP.PORT, bindAddr);
|
return open(addr, SSDP.PORT, bindAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// deviceSearch
|
// deviceSearch
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private ListenerList deviceSearchListenerList = new ListenerList();
|
private ListenerList deviceSearchListenerList = new ListenerList();
|
||||||
|
|
||||||
public void addSearchListener(SearchListener listener)
|
public void addSearchListener(SearchListener listener)
|
||||||
{
|
{
|
||||||
deviceSearchListenerList.add(listener);
|
deviceSearchListenerList.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeSearchListener(SearchListener listener)
|
public void removeSearchListener(SearchListener listener)
|
||||||
{
|
{
|
||||||
deviceSearchListenerList.remove(listener);
|
deviceSearchListenerList.remove(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void performSearchListener(SSDPPacket ssdpPacket)
|
public void performSearchListener(SSDPPacket ssdpPacket)
|
||||||
{
|
{
|
||||||
int listenerSize = deviceSearchListenerList.size();
|
int listenerSize = deviceSearchListenerList.size();
|
||||||
for (int n=0; n<listenerSize; n++) {
|
for (int n=0; n<listenerSize; n++) {
|
||||||
SearchListener listener = (SearchListener)deviceSearchListenerList.get(n);
|
SearchListener listener = (SearchListener)deviceSearchListenerList.get(n);
|
||||||
listener.deviceSearchReceived(ssdpPacket);
|
listener.deviceSearchReceived(ssdpPacket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// run
|
// run
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private Thread deviceSearchThread = null;
|
private Thread deviceSearchThread = null;
|
||||||
|
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
Thread thisThread = Thread.currentThread();
|
Thread thisThread = Thread.currentThread();
|
||||||
|
|
||||||
while (deviceSearchThread == thisThread) {
|
while (deviceSearchThread == thisThread) {
|
||||||
Thread.yield();
|
Thread.yield();
|
||||||
SSDPPacket packet = receive();
|
|
||||||
|
// Thanks for Kazuyuki Shudo (08/23/07)
|
||||||
|
SSDPPacket packet = null;
|
||||||
|
try {
|
||||||
|
packet = receive();
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Thanks for Mikael Hakman (04/20/05)
|
// Thanks for Mikael Hakman (04/20/05)
|
||||||
if (packet == null)
|
if (packet == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (packet.isDiscover() == true)
|
//TODO perform delegation with Thread Pooling
|
||||||
performSearchListener(packet);
|
if (packet.isDiscover() == true)
|
||||||
}
|
performSearchListener(packet);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public void start()
|
|
||||||
{
|
public void start() {
|
||||||
deviceSearchThread = new Thread(this,"UPnP-SSDPSearchSocket");
|
StringBuffer name = new StringBuffer("Cyber.SSDPSearchSocket/");
|
||||||
deviceSearchThread.setDaemon(true);
|
String localAddr = this.getLocalAddress();
|
||||||
deviceSearchThread.start();
|
// localAddr is null on Android m3-rc37a (01/30/08)
|
||||||
}
|
if (localAddr != null && 0 < localAddr.length()) {
|
||||||
|
name.append(this.getLocalAddress()).append(':');
|
||||||
public void stop()
|
name.append(this.getLocalPort()).append(" -> ");
|
||||||
|
name.append(this.getMulticastAddress()).append(':');
|
||||||
|
name.append(this.getMulticastPort());
|
||||||
|
}
|
||||||
|
deviceSearchThread = new Thread(this,name.toString());
|
||||||
|
deviceSearchThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop()
|
||||||
{
|
{
|
||||||
// Thanks for Mikael Hakman (04/20/05)
|
// Thanks for Mikael Hakman (04/20/05)
|
||||||
close();
|
close();
|
||||||
|
|
||||||
deviceSearchThread = null;
|
deviceSearchThread = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,105 +1,151 @@
|
|||||||
/******************************************************************
|
/******************************************************************
|
||||||
*
|
|
||||||
* CyberUPnP for Java
|
|
||||||
*
|
|
||||||
* Copyright (C) Satoshi Konno 2002-2003
|
|
||||||
*
|
*
|
||||||
* File: SSDPSearchSocketList.java
|
* CyberUPnP for Java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Copyright (C) Satoshi Konno 2002-2003
|
||||||
*
|
*
|
||||||
* 05/08/03
|
* File: SSDPSearchSocketList.java
|
||||||
* - first revision.
|
*
|
||||||
* 05/28/03
|
* Revision;
|
||||||
* - Moved post() for SSDPSearchRequest to SSDPResponseSocket.
|
*
|
||||||
* - Removed open(int).
|
* 05/08/03
|
||||||
*
|
* - first revision.
|
||||||
******************************************************************/
|
* 05/28/03
|
||||||
|
* - Moved post() for SSDPSearchRequest to SSDPResponseSocket.
|
||||||
|
* - Removed open(int).
|
||||||
|
*
|
||||||
|
******************************************************************/
|
||||||
|
|
||||||
|
package org.cybergarage.upnp.ssdp;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import org.cybergarage.net.HostInterface;
|
||||||
|
import org.cybergarage.upnp.device.SearchListener;
|
||||||
|
|
||||||
|
public class SSDPSearchSocketList extends Vector
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// Constructor
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private InetAddress[] binds = null;
|
||||||
|
private String multicastIPv4 = SSDP.ADDRESS;
|
||||||
|
private String multicastIPv6 = SSDP.getIPv6Address();
|
||||||
|
private int port = SSDP.PORT;
|
||||||
|
|
||||||
|
public SSDPSearchSocketList()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param binds The IP address that we will used for bindind the service
|
||||||
|
*/
|
||||||
|
public SSDPSearchSocketList(InetAddress[] binds) {
|
||||||
|
this.binds = binds;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param binds The IP address that we will used for bindind the service
|
||||||
|
* @param port The port that we will used for bindind the service
|
||||||
|
* @param multicastIPv4 The IPv4 address that we will used for multicast comunication
|
||||||
|
* @param multicastIPv6 The IPv6 address that we will used for multicast comunication
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public SSDPSearchSocketList(InetAddress[] binds,int port, String multicastIPv4, String multicastIPv6) {
|
||||||
|
this.binds = binds;
|
||||||
|
this.port = port;
|
||||||
|
this.multicastIPv4 = multicastIPv4;
|
||||||
|
this.multicastIPv6 = multicastIPv6;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// 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() {
|
||||||
|
InetAddress[] binds=this.binds;
|
||||||
|
String[] bindAddresses;
|
||||||
|
if(binds!=null){
|
||||||
|
bindAddresses = new String[binds.length];
|
||||||
|
for (int i = 0; i < binds.length; i++) {
|
||||||
|
bindAddresses[i] = binds[i].getHostAddress();
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
int nHostAddrs = HostInterface.getNHostAddresses();
|
||||||
|
bindAddresses = new String[nHostAddrs];
|
||||||
|
for (int n=0; n<nHostAddrs; n++) {
|
||||||
|
bindAddresses[n] = HostInterface.getHostAddress(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < bindAddresses.length; i++) {
|
||||||
|
if(bindAddresses[i]!=null){
|
||||||
|
SSDPSearchSocket ssdpSearchSocket;
|
||||||
|
if(HostInterface.isIPv6Address(bindAddresses[i]))
|
||||||
|
ssdpSearchSocket = new SSDPSearchSocket(bindAddresses[i],port ,multicastIPv6 );
|
||||||
|
else
|
||||||
|
ssdpSearchSocket = new SSDPSearchSocket(bindAddresses[i],port,multicastIPv4 );
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
package org.cybergarage.upnp.ssdp;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import org.cybergarage.net.*;
|
|
||||||
|
|
||||||
import org.cybergarage.upnp.device.*;
|
|
||||||
|
|
||||||
public class SSDPSearchSocketList extends Vector
|
|
||||||
{
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Constructor
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 4071292828166415028L;
|
|
||||||
|
|
||||||
public SSDPSearchSocketList()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Methods
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public SSDPSearchSocket getSSDPSearchSocket(int n)
|
|
||||||
{
|
|
||||||
return (SSDPSearchSocket)get(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addSearchListener(SearchListener listener)
|
|
||||||
{
|
|
||||||
int nServers = size();
|
|
||||||
for (int n=0; n<nServers; n++) {
|
|
||||||
SSDPSearchSocket sock = getSSDPSearchSocket(n);
|
|
||||||
sock.addSearchListener(listener);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Methods
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public boolean open()
|
|
||||||
{
|
|
||||||
int nHostAddrs = HostInterface.getNHostAddresses();
|
|
||||||
for (int n=0; n<nHostAddrs; n++) {
|
|
||||||
String bindAddr = HostInterface.getHostAddress(n);
|
|
||||||
SSDPSearchSocket ssdpSearchSocket = new SSDPSearchSocket(bindAddr);
|
|
||||||
add(ssdpSearchSocket);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void close()
|
|
||||||
{
|
|
||||||
int nSockets = size();
|
|
||||||
for (int n=0; n<nSockets; n++) {
|
|
||||||
SSDPSearchSocket sock = getSSDPSearchSocket(n);
|
|
||||||
sock.close();
|
|
||||||
}
|
|
||||||
clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// Methods
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public void start()
|
|
||||||
{
|
|
||||||
int nSockets = size();
|
|
||||||
for (int n=0; n<nSockets; n++) {
|
|
||||||
SSDPSearchSocket sock = getSSDPSearchSocket(n);
|
|
||||||
sock.start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stop()
|
|
||||||
{
|
|
||||||
int nSockets = size();
|
|
||||||
for (int n=0; n<nSockets; n++) {
|
|
||||||
SSDPSearchSocket sock = getSSDPSearchSocket(n);
|
|
||||||
sock.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
package org.cybergarage.upnp.xml;
|
package org.cybergarage.upnp.xml;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
|
||||||
import org.cybergarage.util.*;
|
import org.cybergarage.util.*;
|
||||||
import org.cybergarage.http.*;
|
import org.cybergarage.http.*;
|
||||||
@ -89,10 +90,23 @@ public class DeviceData extends NodeData
|
|||||||
// HTTPServer
|
// HTTPServer
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private HTTPServerList httpServerList = new HTTPServerList();
|
private HTTPServerList httpServerList = null;
|
||||||
|
|
||||||
public HTTPServerList getHTTPServerList() {
|
public HTTPServerList getHTTPServerList() {
|
||||||
return httpServerList;
|
if(this.httpServerList==null){
|
||||||
|
this.httpServerList = new HTTPServerList(this.httpBinds,this.httpPort);
|
||||||
|
}
|
||||||
|
return this.httpServerList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private InetAddress[] httpBinds = null;
|
||||||
|
|
||||||
|
public void setHTTPBindAddress(InetAddress[] inets){
|
||||||
|
this.httpBinds=inets;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InetAddress[] getHTTPBindAddress(){
|
||||||
|
return this.httpBinds;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
@ -129,11 +143,98 @@ public class DeviceData extends NodeData
|
|||||||
// SSDPSearchSocket
|
// SSDPSearchSocket
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private SSDPSearchSocketList ssdpSearchSocketList = new SSDPSearchSocketList();
|
private SSDPSearchSocketList ssdpSearchSocketList = null;
|
||||||
|
private String ssdpMulticastIPv4 = SSDP.ADDRESS;
|
||||||
|
private String ssdpMulticastIPv6 = SSDP.getIPv6Address();
|
||||||
|
private int ssdpPort = SSDP.PORT;
|
||||||
|
private InetAddress[] ssdpBinds = null;
|
||||||
|
|
||||||
public SSDPSearchSocketList getSSDPSearchSocketList() {
|
public SSDPSearchSocketList getSSDPSearchSocketList() {
|
||||||
|
if(this.ssdpSearchSocketList==null){
|
||||||
|
this.ssdpSearchSocketList = new SSDPSearchSocketList(this.ssdpBinds,ssdpPort,ssdpMulticastIPv4,ssdpMulticastIPv6);
|
||||||
|
}
|
||||||
return ssdpSearchSocketList;
|
return ssdpSearchSocketList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param port The port to use for binding the SSDP service.
|
||||||
|
* The port will be used as source port for all SSDP messages
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public void setSSDPPort(int port){
|
||||||
|
this.ssdpPort=port;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return The port used for binding the SSDP service.
|
||||||
|
* The port will be used as source port for all SSDP messages
|
||||||
|
*/
|
||||||
|
public int getSSDPPort(){
|
||||||
|
return this.ssdpPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param inets The <tt>InetAddress</tt> that will be binded for listing this service.
|
||||||
|
* Use <code>null</code> for the default behaviur.
|
||||||
|
* @see {@link UPnP}
|
||||||
|
* @see {@link USSDP}
|
||||||
|
* @see {@link HostInterface}
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public void setSSDPBindAddress(InetAddress[] inets){
|
||||||
|
this.ssdpBinds=inets;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return inets The <tt>InetAddress</tt> that will be binded for this service
|
||||||
|
* <code>null</code> means that defulat behaviur will be used
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public InetAddress[] getSSDPBindAddress(){
|
||||||
|
return this.ssdpBinds;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param ip The IPv4 address used as destination address for Multicast comunication
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public void setMulticastIPv4Address(String ip){
|
||||||
|
this.ssdpMulticastIPv4=ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return The IPv4 address used for Multicast comunication
|
||||||
|
*/
|
||||||
|
public String getMulticastIPv4Address(){
|
||||||
|
return this.ssdpMulticastIPv4;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param ip The IPv6 address used as destination address for Multicast comunication
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public void setMulticastIPv6Address(String ip){
|
||||||
|
this.ssdpMulticastIPv6=ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return The IPv6 address used as destination address for Multicast comunication
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public String getMulticastIPv6Address(){
|
||||||
|
return this.ssdpMulticastIPv6;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// SSDPPacket
|
// SSDPPacket
|
||||||
|
@ -68,6 +68,20 @@ public class ServiceData extends NodeData
|
|||||||
// SID
|
// SID
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
private String descriptionURL = "";
|
||||||
|
|
||||||
|
public String getDescriptionURL() {
|
||||||
|
return descriptionURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescriptionURL(String descriptionURL) {
|
||||||
|
this.descriptionURL = descriptionURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// SID
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
private String sid = "";
|
private String sid = "";
|
||||||
|
|
||||||
public String getSID() {
|
public String getSID() {
|
||||||
|
@ -1,51 +1,81 @@
|
|||||||
/******************************************************************
|
/******************************************************************
|
||||||
*
|
|
||||||
* CyberUtil for Java
|
|
||||||
*
|
|
||||||
* Copyright (C) Satoshi Konno 2002
|
|
||||||
*
|
*
|
||||||
* File: Debug.java
|
* CyberUtil for Java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Copyright (C) Satoshi Konno 2002
|
||||||
*
|
*
|
||||||
* 11/18/02
|
* File: Debug.java
|
||||||
* - first revision.
|
*
|
||||||
*
|
* Revision;
|
||||||
******************************************************************/
|
*
|
||||||
|
* 11/18/02
|
||||||
|
* - first revision.
|
||||||
|
*
|
||||||
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.util;
|
package org.cybergarage.util;
|
||||||
|
|
||||||
public final class Debug
|
import java.io.PrintStream;
|
||||||
{
|
|
||||||
public static boolean enabled = false;
|
public final class Debug{
|
||||||
|
|
||||||
public static final void on() {
|
public static Debug debug = new Debug();
|
||||||
enabled = true;
|
|
||||||
|
private PrintStream out = System.out;
|
||||||
|
|
||||||
|
|
||||||
|
public Debug(){
|
||||||
|
|
||||||
}
|
}
|
||||||
public static final void off() {
|
|
||||||
enabled = false;
|
public synchronized PrintStream getOut() {
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
public static boolean isOn() {
|
public synchronized void setOut(PrintStream out) {
|
||||||
return enabled;
|
this.out = out;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static boolean enabled = false;
|
||||||
|
|
||||||
|
public static Debug getDebug(){
|
||||||
|
return Debug.debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
public static final void message(String s) {
|
||||||
if (enabled == true)
|
if (enabled == true)
|
||||||
System.out.println("CyberGarage message : " + s);
|
Debug.debug.getOut().println("CyberGarage message : " + s);
|
||||||
|
}
|
||||||
|
public static final void message(String m1, String m2) {
|
||||||
|
if (enabled == true)
|
||||||
|
Debug.debug.getOut().println("CyberGarage message : ");
|
||||||
|
Debug.debug.getOut().println(m1);
|
||||||
|
Debug.debug.getOut().println(m2);
|
||||||
}
|
}
|
||||||
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) {
|
public static final void warning(String s) {
|
||||||
System.out.println("CyberGarage warning : " + s);
|
Debug.debug.getOut().println("CyberGarage warning : " + s);
|
||||||
}
|
}
|
||||||
public static final void warning(String m, Exception e) {
|
public static final void warning(String m, Exception e) {
|
||||||
System.out.println("CyberGarage warning : " + m + " (" + e.getMessage() + ")");
|
if(e.getMessage()==null){
|
||||||
}
|
Debug.debug.getOut().println("CyberGarage warning : " + m + " START");
|
||||||
public static final void warning(Exception e) {
|
e.printStackTrace(Debug.debug.getOut());
|
||||||
|
Debug.debug.getOut().println("CyberGarage warning : " + m + " END");
|
||||||
|
}else{
|
||||||
|
Debug.debug.getOut().println("CyberGarage warning : " + m + " (" + e.getMessage() + ")");
|
||||||
|
e.printStackTrace(Debug.debug.getOut());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static final void warning(Exception e) {
|
||||||
warning(e.getMessage());
|
warning(e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace(Debug.debug.getOut());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,9 @@
|
|||||||
|
|
||||||
package org.cybergarage.util;
|
package org.cybergarage.util;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
|
||||||
public final class FileUtil
|
public final class FileUtil
|
||||||
{
|
{
|
||||||
|
@ -15,12 +15,10 @@
|
|||||||
|
|
||||||
package org.cybergarage.util;
|
package org.cybergarage.util;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Vector;
|
||||||
|
|
||||||
public class ListenerList extends Vector
|
public class ListenerList extends Vector
|
||||||
{
|
{
|
||||||
private static final long serialVersionUID = 8039231561720446173L;
|
|
||||||
|
|
||||||
public boolean add(Object obj)
|
public boolean add(Object obj)
|
||||||
{
|
{
|
||||||
if (0 <= indexOf(obj))
|
if (0 <= indexOf(obj))
|
||||||
|
@ -10,6 +10,9 @@
|
|||||||
*
|
*
|
||||||
* 01/05/04
|
* 01/05/04
|
||||||
* - first revision.
|
* - first revision.
|
||||||
|
* 08/23/07
|
||||||
|
* - Thanks for Kazuyuki Shudo
|
||||||
|
* - Changed stop() to stop more safety using Thread::interrupt().
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
@ -43,7 +46,7 @@ public class ThreadCore implements Runnable
|
|||||||
{
|
{
|
||||||
java.lang.Thread threadObject = getThreadObject();
|
java.lang.Thread threadObject = getThreadObject();
|
||||||
if (threadObject == null) {
|
if (threadObject == null) {
|
||||||
threadObject = new java.lang.Thread(this);
|
threadObject = new java.lang.Thread(this,"Cyber.ThreadCore");
|
||||||
setThreadObject(threadObject);
|
setThreadObject(threadObject);
|
||||||
threadObject.start();
|
threadObject.start();
|
||||||
}
|
}
|
||||||
@ -64,6 +67,10 @@ public class ThreadCore implements Runnable
|
|||||||
if (threadObject != null) {
|
if (threadObject != null) {
|
||||||
//threadObject.destroy();
|
//threadObject.destroy();
|
||||||
//threadObject.stop();
|
//threadObject.stop();
|
||||||
|
|
||||||
|
// Thanks for Kazuyuki Shudo (08/23/07)
|
||||||
|
threadObject.interrupt();
|
||||||
|
|
||||||
setThreadObject(null);
|
setThreadObject(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,12 +15,10 @@
|
|||||||
|
|
||||||
package org.cybergarage.xml;
|
package org.cybergarage.xml;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Vector;
|
||||||
|
|
||||||
public class AttributeList extends Vector
|
public class AttributeList extends Vector
|
||||||
{
|
{
|
||||||
private static final long serialVersionUID = -5516389508555401104L;
|
|
||||||
|
|
||||||
public AttributeList()
|
public AttributeList()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -1,415 +1,468 @@
|
|||||||
/******************************************************************
|
/******************************************************************
|
||||||
*
|
*
|
||||||
* CyberXML for Java
|
* CyberXML for Java
|
||||||
*
|
*
|
||||||
* Copyright (C) Satoshi Konno 2002
|
* Copyright (C) Satoshi Konno 2002
|
||||||
*
|
*
|
||||||
* File: Element.java
|
* File: Element.java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Revision;
|
||||||
*
|
*
|
||||||
* 11/27/02
|
* 11/27/02
|
||||||
* - first revision.
|
* - first revision.
|
||||||
* 11/01/03
|
* 11/01/03
|
||||||
* - Terje Bakken
|
* - Terje Bakken
|
||||||
* - fixed missing escaping of reserved XML characters
|
* - fixed missing escaping of reserved XML characters
|
||||||
* 11/19/04
|
* 11/19/04
|
||||||
* - Theo Beisch <theo.beisch@gmx.de>
|
* - Theo Beisch <theo.beisch@gmx.de>
|
||||||
* - Added "&" and "\"" "\\" to toXMLString().
|
* - Added "&" and "\"" "\\" to toXMLString().
|
||||||
* 11/19/04
|
* 11/19/04
|
||||||
* - Theo Beisch <theo.beisch@gmx.de>
|
* - Theo Beisch <theo.beisch@gmx.de>
|
||||||
* - Changed XML::output() to use short notation when the tag value is null.
|
* - Changed XML::output() to use short notation when the tag value is null.
|
||||||
* 12/02/04
|
* 12/02/04
|
||||||
* - Brian Owens <brian@b-owens.com>
|
* - Brian Owens <brian@b-owens.com>
|
||||||
* - Fixed toXMLString() to convert from "'" to "'" instead of "\".
|
* - Fixed toXMLString() to convert from "'" to "'" instead of "\".
|
||||||
*
|
* 11/07/05
|
||||||
******************************************************************/
|
* - Changed toString() to return as utf-8 string.
|
||||||
|
* 02/08/08
|
||||||
package org.cybergarage.xml;
|
* - Added addValue().
|
||||||
|
*
|
||||||
import java.io.ByteArrayOutputStream;
|
******************************************************************/
|
||||||
import java.io.PrintWriter;
|
|
||||||
|
package org.cybergarage.xml;
|
||||||
public class Node
|
|
||||||
{
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.PrintWriter;
|
||||||
public Node()
|
import java.io.UnsupportedEncodingException;
|
||||||
{
|
import java.util.Iterator;
|
||||||
setUserData(null);
|
|
||||||
setParentNode(null);
|
public class Node
|
||||||
}
|
{
|
||||||
|
|
||||||
public Node(String name)
|
/**
|
||||||
{
|
* Create a Node with empty UserData and no Parent Node
|
||||||
this();
|
*
|
||||||
setName(name);
|
*/
|
||||||
}
|
public Node()
|
||||||
|
{
|
||||||
public Node(String ns, String name)
|
setUserData(null);
|
||||||
{
|
setParentNode(null);
|
||||||
this();
|
}
|
||||||
setName(ns, name);
|
|
||||||
}
|
public Node(String name)
|
||||||
|
{
|
||||||
////////////////////////////////////////////////
|
this();
|
||||||
// parent node
|
setName(name);
|
||||||
////////////////////////////////////////////////
|
}
|
||||||
|
|
||||||
private Node parentNode = null;
|
public Node(String ns, String name)
|
||||||
|
{
|
||||||
public void setParentNode(Node node)
|
this();
|
||||||
{
|
setName(ns, name);
|
||||||
parentNode = node;
|
}
|
||||||
}
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
public Node getParentNode()
|
// parent node
|
||||||
{
|
////////////////////////////////////////////////
|
||||||
return parentNode;
|
|
||||||
}
|
private Node parentNode = null;
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
public void setParentNode(Node node)
|
||||||
// root node
|
{
|
||||||
////////////////////////////////////////////////
|
parentNode = node;
|
||||||
|
}
|
||||||
public Node getRootNode()
|
|
||||||
{
|
public Node getParentNode()
|
||||||
Node rootNode = null;
|
{
|
||||||
Node parentNode = getParentNode();
|
return parentNode;
|
||||||
while (parentNode != null) {
|
}
|
||||||
rootNode = parentNode;
|
|
||||||
parentNode = rootNode.getParentNode();
|
////////////////////////////////////////////////
|
||||||
}
|
// root node
|
||||||
return rootNode;
|
////////////////////////////////////////////////
|
||||||
}
|
|
||||||
|
public Node getRootNode()
|
||||||
////////////////////////////////////////////////
|
{
|
||||||
// name
|
Node rootNode = null;
|
||||||
////////////////////////////////////////////////
|
Node parentNode = getParentNode();
|
||||||
|
while (parentNode != null) {
|
||||||
private String name = new String();
|
rootNode = parentNode;
|
||||||
|
parentNode = rootNode.getParentNode();
|
||||||
public void setName(String name)
|
}
|
||||||
{
|
return rootNode;
|
||||||
this.name = name;
|
}
|
||||||
}
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
public void setName(String ns, String name)
|
// name
|
||||||
{
|
////////////////////////////////////////////////
|
||||||
this.name = ns + ":" + name;
|
|
||||||
}
|
private String name = new String();
|
||||||
|
|
||||||
public String getName()
|
public void setName(String name)
|
||||||
{
|
{
|
||||||
return name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isName(String value)
|
public void setName(String ns, String name)
|
||||||
{
|
{
|
||||||
return name.equals(value);
|
this.name = ns + ":" + name;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
public String getName()
|
||||||
// value
|
{
|
||||||
////////////////////////////////////////////////
|
return name;
|
||||||
|
}
|
||||||
private String value = "";
|
|
||||||
|
public boolean isName(String value)
|
||||||
public void setValue(String value)
|
{
|
||||||
{
|
return name.equals(value);
|
||||||
this.value = value;
|
}
|
||||||
}
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
public void setValue(int value)
|
// value
|
||||||
{
|
////////////////////////////////////////////////
|
||||||
setValue(Integer.toString(value));
|
|
||||||
}
|
private String value = new String();
|
||||||
|
|
||||||
public String getValue()
|
public void setValue(String value)
|
||||||
{
|
{
|
||||||
return value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
public void setValue(int value)
|
||||||
// Attribute (Basic)
|
{
|
||||||
////////////////////////////////////////////////
|
setValue(Integer.toString(value));
|
||||||
|
}
|
||||||
private AttributeList attrList = new AttributeList();
|
|
||||||
|
public void addValue(String value)
|
||||||
public int getNAttributes() {
|
{
|
||||||
return attrList.size();
|
if (this.value == null) {
|
||||||
}
|
this.value = value;
|
||||||
|
return;
|
||||||
public Attribute getAttribute(int index) {
|
}
|
||||||
return attrList.getAttribute(index);
|
if (value != null)
|
||||||
}
|
this.value += value;
|
||||||
|
}
|
||||||
public Attribute getAttribute(String name)
|
|
||||||
{
|
public String getValue()
|
||||||
return attrList.getAttribute(name);
|
{
|
||||||
}
|
return value;
|
||||||
|
}
|
||||||
public void addAttribute(Attribute attr) {
|
|
||||||
attrList.add(attr);
|
////////////////////////////////////////////////
|
||||||
}
|
// Attribute (Basic)
|
||||||
|
////////////////////////////////////////////////
|
||||||
public void insertAttributeAt(Attribute attr, int index) {
|
|
||||||
attrList.insertElementAt(attr, index);
|
private AttributeList attrList = new AttributeList();
|
||||||
}
|
|
||||||
|
public int getNAttributes() {
|
||||||
public void addAttribute(String name, String value) {
|
return attrList.size();
|
||||||
Attribute attr = new Attribute(name, value);
|
}
|
||||||
addAttribute(attr);
|
|
||||||
}
|
public Attribute getAttribute(int index) {
|
||||||
|
return attrList.getAttribute(index);
|
||||||
public boolean removeAttribute(Attribute attr) {
|
}
|
||||||
return attrList.remove(attr);
|
|
||||||
}
|
public Attribute getAttribute(String name)
|
||||||
|
{
|
||||||
public boolean removeAttribute(String name) {
|
return attrList.getAttribute(name);
|
||||||
return removeAttribute(getAttribute(name));
|
}
|
||||||
}
|
|
||||||
|
public void addAttribute(Attribute attr) {
|
||||||
public boolean hasAttributes()
|
attrList.add(attr);
|
||||||
{
|
}
|
||||||
if (0 < getNAttributes())
|
|
||||||
return true;
|
public void insertAttributeAt(Attribute attr, int index) {
|
||||||
return false;
|
attrList.insertElementAt(attr, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
public void addAttribute(String name, String value) {
|
||||||
// Attribute (Extention)
|
Attribute attr = new Attribute(name, value);
|
||||||
////////////////////////////////////////////////
|
addAttribute(attr);
|
||||||
|
}
|
||||||
public void setAttribute(String name, String value) {
|
|
||||||
Attribute attr = getAttribute(name);
|
public boolean removeAttribute(Attribute attr) {
|
||||||
if (attr != null) {
|
return attrList.remove(attr);
|
||||||
attr.setValue(value);
|
}
|
||||||
return;
|
|
||||||
}
|
public boolean removeAttribute(String name) {
|
||||||
attr = new Attribute(name, value);
|
return removeAttribute(getAttribute(name));
|
||||||
addAttribute(attr);
|
}
|
||||||
}
|
|
||||||
|
public boolean hasAttributes()
|
||||||
public void setAttribute(String name, int value) {
|
{
|
||||||
setAttribute(name, Integer.toString(value));
|
if (0 < getNAttributes())
|
||||||
}
|
return true;
|
||||||
|
return false;
|
||||||
public String getAttributeValue(String name) {
|
}
|
||||||
Attribute attr = getAttribute(name);
|
|
||||||
if (attr != null)
|
////////////////////////////////////////////////
|
||||||
return attr.getValue();
|
// Attribute (Extention)
|
||||||
return "";
|
////////////////////////////////////////////////
|
||||||
}
|
|
||||||
|
public void setAttribute(String name, String value) {
|
||||||
public int getAttributeIntegerValue(String name) {
|
Attribute attr = getAttribute(name);
|
||||||
String val = getAttributeValue(name);
|
if (attr != null) {
|
||||||
try {
|
attr.setValue(value);
|
||||||
return Integer.parseInt(val);
|
return;
|
||||||
}
|
}
|
||||||
catch (Exception e) {}
|
attr = new Attribute(name, value);
|
||||||
return 0;
|
addAttribute(attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
public void setAttribute(String name, int value) {
|
||||||
// Attribute (xmlns)
|
setAttribute(name, Integer.toString(value));
|
||||||
////////////////////////////////////////////////
|
}
|
||||||
|
|
||||||
public void setNameSpace(String ns, String value)
|
public String getAttributeValue(String name) {
|
||||||
{
|
Attribute attr = getAttribute(name);
|
||||||
setAttribute("xmlns:" + ns, value);
|
if (attr != null)
|
||||||
}
|
return attr.getValue();
|
||||||
|
return "";
|
||||||
////////////////////////////////////////////////
|
}
|
||||||
// Child node
|
|
||||||
////////////////////////////////////////////////
|
public int getAttributeIntegerValue(String name) {
|
||||||
|
String val = getAttributeValue(name);
|
||||||
private NodeList nodeList = new NodeList();
|
try {
|
||||||
|
return Integer.parseInt(val);
|
||||||
public int getNNodes() {
|
}
|
||||||
return nodeList.size();
|
catch (Exception e) {}
|
||||||
}
|
return 0;
|
||||||
|
}
|
||||||
public Node getNode(int index) {
|
|
||||||
return nodeList.getNode(index);
|
////////////////////////////////////////////////
|
||||||
}
|
// Attribute (xmlns)
|
||||||
|
////////////////////////////////////////////////
|
||||||
public Node getNode(String name)
|
|
||||||
{
|
public void setNameSpace(String ns, String value)
|
||||||
return nodeList.getNode(name);
|
{
|
||||||
}
|
setAttribute("xmlns:" + ns, value);
|
||||||
|
}
|
||||||
public Node getNodeEndsWith(String name)
|
|
||||||
{
|
////////////////////////////////////////////////
|
||||||
return nodeList.getEndsWith(name);
|
// Child node
|
||||||
}
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public void addNode(Node node) {
|
private NodeList nodeList = new NodeList();
|
||||||
node.setParentNode(this);
|
|
||||||
nodeList.add(node);
|
public int getNNodes() {
|
||||||
}
|
return nodeList.size();
|
||||||
|
}
|
||||||
public void insertNode(Node node, int index) {
|
|
||||||
node.setParentNode(this);
|
public Node getNode(int index) {
|
||||||
nodeList.insertElementAt(node, index);
|
return nodeList.getNode(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean removeNode(Node node) {
|
public Node getNode(String name)
|
||||||
node.setParentNode(null);
|
{
|
||||||
return nodeList.remove(node);
|
return nodeList.getNode(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean removeNode(String name) {
|
public Node getNodeEndsWith(String name)
|
||||||
return nodeList.remove(getNode(name));
|
{
|
||||||
}
|
return nodeList.getEndsWith(name);
|
||||||
|
}
|
||||||
public void removeAllNodes()
|
|
||||||
{
|
public void addNode(Node node) {
|
||||||
nodeList.clear();
|
node.setParentNode(this);
|
||||||
}
|
nodeList.add(node);
|
||||||
|
}
|
||||||
public boolean hasNodes()
|
|
||||||
{
|
public void insertNode(Node node, int index) {
|
||||||
if (0 < getNNodes())
|
node.setParentNode(this);
|
||||||
return true;
|
nodeList.insertElementAt(node, index);
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
|
public int getIndex(String name){
|
||||||
////////////////////////////////////////////////
|
int index = -1;
|
||||||
// Element (Child Node)
|
for (Iterator i = nodeList.iterator(); i.hasNext();) {
|
||||||
////////////////////////////////////////////////
|
index++;
|
||||||
|
Node n = (Node) i.next();
|
||||||
public void setNode(String name, String value) {
|
if(n.getName().equals(name))
|
||||||
Node node = getNode(name);
|
return index;
|
||||||
if (node != null) {
|
}
|
||||||
node.setValue(value);
|
return index;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
node = new Node(name);
|
public boolean removeNode(Node node) {
|
||||||
node.setValue(value);
|
node.setParentNode(null);
|
||||||
addNode(node);
|
return nodeList.remove(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getNodeValue(String name) {
|
public boolean removeNode(String name) {
|
||||||
Node node = getNode(name);
|
return nodeList.remove(getNode(name));
|
||||||
if (node != null)
|
}
|
||||||
return node.getValue();
|
|
||||||
return "";
|
public void removeAllNodes()
|
||||||
}
|
{
|
||||||
|
nodeList.clear();
|
||||||
////////////////////////////////////////////////
|
}
|
||||||
// userData
|
|
||||||
////////////////////////////////////////////////
|
public boolean hasNodes()
|
||||||
|
{
|
||||||
private Object userData = null;
|
if (0 < getNNodes())
|
||||||
|
return true;
|
||||||
public void setUserData(Object data)
|
return false;
|
||||||
{
|
}
|
||||||
userData = data;
|
|
||||||
}
|
////////////////////////////////////////////////
|
||||||
|
// Element (Child Node)
|
||||||
public Object getUserData()
|
////////////////////////////////////////////////
|
||||||
{
|
|
||||||
return userData;
|
public void setNode(String name, String value) {
|
||||||
}
|
Node node = getNode(name);
|
||||||
|
if (node != null) {
|
||||||
|
node.setValue(value);
|
||||||
////////////////////////////////////////////////
|
return;
|
||||||
// toString
|
}
|
||||||
////////////////////////////////////////////////
|
node = new Node(name);
|
||||||
|
node.setValue(value);
|
||||||
public String getIndentLevelString(int nIndentLevel)
|
addNode(node);
|
||||||
{
|
}
|
||||||
char indentString[] = new char[nIndentLevel];
|
|
||||||
for (int n=0; n<nIndentLevel; n++)
|
public String getNodeValue(String name) {
|
||||||
indentString[n] = '\t' ;
|
Node node = getNode(name);
|
||||||
return new String(indentString);
|
if (node != null)
|
||||||
}
|
return node.getValue();
|
||||||
|
return "";
|
||||||
public void outputAttributes(PrintWriter ps)
|
}
|
||||||
{
|
|
||||||
int nAttributes = getNAttributes();
|
////////////////////////////////////////////////
|
||||||
for (int n=0; n<nAttributes; n++) {
|
// userData
|
||||||
Attribute attr = getAttribute(n);
|
////////////////////////////////////////////////
|
||||||
ps.print(" " + attr.getName() + "=\"" + XML.escapeXMLChars(attr.getValue()) + "\"");
|
|
||||||
}
|
private Object userData = null;
|
||||||
}
|
|
||||||
|
public void setUserData(Object data)
|
||||||
public void output(PrintWriter ps, int indentLevel, boolean hasChildNode)
|
{
|
||||||
{
|
userData = data;
|
||||||
String indentString = getIndentLevelString(indentLevel);
|
}
|
||||||
|
|
||||||
String name = getName();
|
public Object getUserData()
|
||||||
String value = getValue();
|
{
|
||||||
|
return userData;
|
||||||
if (hasNodes() == false || hasChildNode == false) {
|
}
|
||||||
ps.print(indentString + "<" + name);
|
|
||||||
outputAttributes(ps);
|
////////////////////////////////////////////////
|
||||||
// Thnaks for Tho Beisch (11/09/04)
|
// toString
|
||||||
if (value == null || value.length() == 0) {
|
////////////////////////////////////////////////
|
||||||
// No value, so use short notation <node />
|
|
||||||
ps.println(" />");
|
/**
|
||||||
} else {
|
* Inovoke {@link #getIndentLevelString(int, String)} with <code>" "</code> as String
|
||||||
ps.println(">" + XML.escapeXMLChars(value) + "</" + name + ">");
|
*
|
||||||
}
|
* @see #getIndentLevelString(int, String)
|
||||||
|
*/
|
||||||
return;
|
public String getIndentLevelString(int nIndentLevel)
|
||||||
}
|
{
|
||||||
|
return getIndentLevelString(nIndentLevel," ");
|
||||||
ps.print(indentString + "<" + name);
|
}
|
||||||
outputAttributes(ps);
|
|
||||||
ps.println(">");
|
/**
|
||||||
|
*
|
||||||
int nChildNodes = getNNodes();
|
* @param nIndentLevel the level of indentation to produce
|
||||||
for (int n=0; n<nChildNodes; n++) {
|
* @param space the String to use for the intendation
|
||||||
Node cnode = getNode(n);
|
* @since 1.8.0
|
||||||
cnode.output(ps, indentLevel+1, true);
|
* @return an indentation String
|
||||||
}
|
*/
|
||||||
|
public String getIndentLevelString(int nIndentLevel,String space)
|
||||||
ps.println(indentString +"</" + name + ">");
|
{
|
||||||
}
|
StringBuffer indentString = new StringBuffer(nIndentLevel*space.length());
|
||||||
|
for (int n=0; n<nIndentLevel; n++){
|
||||||
public String toString(boolean hasChildNode)
|
indentString.append(space);
|
||||||
{
|
}
|
||||||
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
return indentString.toString();
|
||||||
PrintWriter pr = new PrintWriter(byteOut);
|
}
|
||||||
output(pr, 0, hasChildNode);
|
|
||||||
pr.flush();
|
public void outputAttributes(PrintWriter ps)
|
||||||
return byteOut.toString();
|
{
|
||||||
}
|
int nAttributes = getNAttributes();
|
||||||
|
for (int n=0; n<nAttributes; n++) {
|
||||||
@Override
|
Attribute attr = getAttribute(n);
|
||||||
public String toString()
|
ps.print(" " + attr.getName() + "=\"" + XML.escapeXMLChars(attr.getValue()) + "\"");
|
||||||
{
|
}
|
||||||
return toString(true);
|
}
|
||||||
}
|
|
||||||
|
public void output(PrintWriter ps, int indentLevel, boolean hasChildNode)
|
||||||
public String toXMLString(boolean hasChildNode)
|
{
|
||||||
{
|
String indentString = getIndentLevelString(indentLevel);
|
||||||
String xmlStr = toString();
|
|
||||||
xmlStr = xmlStr.replaceAll("<", "<");
|
String name = getName();
|
||||||
xmlStr = xmlStr.replaceAll(">", ">");
|
String value = getValue();
|
||||||
// Thanks for Theo Beisch (11/09/04)
|
|
||||||
xmlStr = xmlStr.replaceAll("&", "&");
|
if (hasNodes() == false || hasChildNode == false) {
|
||||||
xmlStr = xmlStr.replaceAll("\"", """);
|
ps.print(indentString + "<" + name);
|
||||||
// Thanks for Brian Owens (12/02/04)
|
outputAttributes(ps);
|
||||||
xmlStr = xmlStr.replaceAll("'", "'");
|
// Thnaks for Tho Beisch (11/09/04)
|
||||||
return xmlStr;
|
if (value == null || value.length() == 0) {
|
||||||
}
|
// Not using the short notation <node /> because it cause compatibility trouble
|
||||||
|
ps.println("></" + name + ">");
|
||||||
public String toXMLString()
|
} else {
|
||||||
{
|
ps.println(">" + XML.escapeXMLChars(value) + "</" + name + ">");
|
||||||
return toXMLString(true);
|
}
|
||||||
}
|
|
||||||
|
return;
|
||||||
public void print(boolean hasChildNode)
|
}
|
||||||
{
|
|
||||||
PrintWriter pr = new PrintWriter(System.out);
|
ps.print(indentString + "<" + name);
|
||||||
output(pr, 0, hasChildNode);
|
outputAttributes(ps);
|
||||||
pr.flush();
|
ps.println(">");
|
||||||
}
|
|
||||||
|
int nChildNodes = getNNodes();
|
||||||
public void print()
|
for (int n=0; n<nChildNodes; n++) {
|
||||||
{
|
Node cnode = getNode(n);
|
||||||
print(true);
|
cnode.output(ps, indentLevel+1, true);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
ps.println(indentString +"</" + name + ">");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString(String enc, boolean hasChildNode)
|
||||||
|
{
|
||||||
|
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
||||||
|
PrintWriter pr = new PrintWriter(byteOut);
|
||||||
|
output(pr, 0, hasChildNode);
|
||||||
|
pr.flush();
|
||||||
|
try {
|
||||||
|
if (enc != null && 0 < enc.length())
|
||||||
|
return byteOut.toString(enc);
|
||||||
|
}
|
||||||
|
catch (UnsupportedEncodingException e) {
|
||||||
|
}
|
||||||
|
return byteOut.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
return toString(XML.CHARSET_UTF8, 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -15,12 +15,10 @@
|
|||||||
|
|
||||||
package org.cybergarage.xml;
|
package org.cybergarage.xml;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Vector;
|
||||||
|
|
||||||
public class NodeList extends Vector
|
public class NodeList extends Vector
|
||||||
{
|
{
|
||||||
private static final long serialVersionUID = 1528884682346143213L;
|
|
||||||
|
|
||||||
public NodeList()
|
public NodeList()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -1,110 +1,128 @@
|
|||||||
/******************************************************************
|
/******************************************************************
|
||||||
*
|
|
||||||
* CyberXML for Java
|
|
||||||
*
|
|
||||||
* Copyright (C) Satoshi Konno 2002
|
|
||||||
*
|
*
|
||||||
* File: Parser.java
|
* CyberXML for Java
|
||||||
*
|
*
|
||||||
* Revision;
|
* Copyright (C) Satoshi Konno 2002
|
||||||
*
|
*
|
||||||
* 11/26/03
|
* File: Parser.java
|
||||||
* - first revision.
|
*
|
||||||
* 03/30/05
|
* Revision;
|
||||||
* - Change parse(String) to use StringBufferInputStream instead of URL.
|
*
|
||||||
*
|
* 11/26/2003
|
||||||
******************************************************************/
|
* - first revision.
|
||||||
|
* 03/30/2005
|
||||||
|
* - Change parse(String) to use StringBufferInputStream instead of URL.
|
||||||
|
* 11/11/2009
|
||||||
|
* - Changed Parser::parser() to use ByteArrayInputStream instead of StringBufferInputStream because of bugs in Android v1.6.
|
||||||
|
*
|
||||||
|
******************************************************************/
|
||||||
|
|
||||||
package org.cybergarage.xml;
|
package org.cybergarage.xml;
|
||||||
|
|
||||||
import java.net.*;
|
import java.io.File;
|
||||||
import java.io.*;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
public abstract class Parser
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
import org.cybergarage.http.HTTP;
|
||||||
|
import org.cybergarage.http.HTTPRequest;
|
||||||
|
import org.cybergarage.http.HTTPResponse;
|
||||||
|
|
||||||
|
public abstract class Parser
|
||||||
{
|
{
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public Parser()
|
public Parser()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// parse
|
// parse
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public abstract Node parse(InputStream inStream) throws ParserException;
|
public abstract Node parse(InputStream inStream) throws ParserException;
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// parse (URL)
|
// parse (URL)
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
public Node parse(URL locationURL) throws ParserException
|
public Node parse(URL locationURL) throws ParserException
|
||||||
{
|
{
|
||||||
try {
|
String host = locationURL.getHost();
|
||||||
HttpURLConnection urlCon = (HttpURLConnection)locationURL.openConnection();
|
int port = locationURL.getPort();
|
||||||
urlCon.setRequestMethod("GET");
|
// Thanks for Hao Hu
|
||||||
InputStream urlIn = urlCon.getInputStream();
|
if (port == -1)
|
||||||
|
port = 80;
|
||||||
Node rootElem = parse(urlIn);
|
String uri = locationURL.getPath();
|
||||||
|
|
||||||
urlIn.close();
|
try {
|
||||||
urlCon.disconnect();
|
HttpURLConnection urlCon = (HttpURLConnection)locationURL.openConnection();
|
||||||
|
urlCon.setRequestMethod("GET");
|
||||||
return rootElem;
|
urlCon.setRequestProperty(HTTP.CONTENT_LENGTH,"0");
|
||||||
|
if (host != null)
|
||||||
} catch (Exception e) {
|
urlCon.setRequestProperty(HTTP.HOST, host);
|
||||||
throw new ParserException(e);
|
|
||||||
}
|
InputStream urlIn = urlCon.getInputStream();
|
||||||
/*
|
|
||||||
String host = locationURL.getHost();
|
Node rootElem = parse(urlIn);
|
||||||
int port = locationURL.getPort();
|
|
||||||
String uri = locationURL.getPath();
|
urlIn.close();
|
||||||
HTTPRequest httpReq = new HTTPRequest();
|
urlCon.disconnect();
|
||||||
httpReq.setMethod(HTTP.GET);
|
|
||||||
httpReq.setURI(uri);
|
return rootElem;
|
||||||
HTTPResponse httpRes = httpReq.post(host, port);
|
|
||||||
if (httpRes.isSuccessful() == false)
|
} catch (Exception e) {
|
||||||
throw new ParserException(locationURL.toString());
|
//throw new ParserException(e);
|
||||||
String content = new String(httpRes.getContent());
|
}
|
||||||
StringBufferInputStream strBuf = new StringBufferInputStream(content);
|
|
||||||
return parse(strBuf);
|
HTTPRequest httpReq = new HTTPRequest();
|
||||||
*/
|
httpReq.setMethod(HTTP.GET);
|
||||||
}
|
httpReq.setURI(uri);
|
||||||
|
HTTPResponse httpRes = httpReq.post(host, port);
|
||||||
////////////////////////////////////////////////
|
if (httpRes.isSuccessful() == false)
|
||||||
// parse (File)
|
throw new ParserException("HTTP comunication failed: no answer from peer." +
|
||||||
////////////////////////////////////////////////
|
"Unable to retrive resoure -> "+locationURL.toString());
|
||||||
|
String content = new String(httpRes.getContent());
|
||||||
public Node parse(File descriptionFile) throws ParserException
|
ByteArrayInputStream strBuf = new ByteArrayInputStream(content.getBytes());
|
||||||
{
|
return parse(strBuf);
|
||||||
try {
|
}
|
||||||
InputStream fileIn = new FileInputStream(descriptionFile);
|
|
||||||
Node root = parse(fileIn);
|
////////////////////////////////////////////////
|
||||||
fileIn.close();
|
// parse (File)
|
||||||
return root;
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
} catch (Exception e) {
|
public Node parse(File descriptionFile) throws ParserException
|
||||||
throw new ParserException(e);
|
{
|
||||||
}
|
try {
|
||||||
}
|
InputStream fileIn = new FileInputStream(descriptionFile);
|
||||||
|
Node root = parse(fileIn);
|
||||||
////////////////////////////////////////////////
|
fileIn.close();
|
||||||
// parse (Memory)
|
return root;
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
} catch (Exception e) {
|
||||||
public Node parse(String descr) throws ParserException
|
throw new ParserException(e);
|
||||||
{
|
}
|
||||||
try {
|
}
|
||||||
StringBufferInputStream decrIn = new StringBufferInputStream(descr);
|
|
||||||
Node root = parse(decrIn);
|
////////////////////////////////////////////////
|
||||||
return root;
|
// parse (Memory)
|
||||||
} catch (Exception e) {
|
////////////////////////////////////////////////
|
||||||
throw new ParserException(e);
|
|
||||||
}
|
public Node parse(String descr) throws ParserException
|
||||||
}
|
{
|
||||||
|
try {
|
||||||
}
|
InputStream decrIn = new ByteArrayInputStream(descr.getBytes());
|
||||||
|
Node root = parse(decrIn);
|
||||||
|
return root;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new ParserException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,8 +19,6 @@ package org.cybergarage.xml;
|
|||||||
|
|
||||||
public class ParserException extends Exception
|
public class ParserException extends Exception
|
||||||
{
|
{
|
||||||
private static final long serialVersionUID = 7443033796560597360L;
|
|
||||||
|
|
||||||
public ParserException(Exception e)
|
public ParserException(Exception e)
|
||||||
{
|
{
|
||||||
super(e);
|
super(e);
|
||||||
|
@ -21,6 +21,7 @@ package org.cybergarage.xml;
|
|||||||
public class XML
|
public class XML
|
||||||
{
|
{
|
||||||
public final static String CONTENT_TYPE = "text/xml; charset=\"utf-8\"";
|
public final static String CONTENT_TYPE = "text/xml; charset=\"utf-8\"";
|
||||||
|
public final static String CHARSET_UTF8 = "utf-8";
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// escapeXMLChars
|
// escapeXMLChars
|
||||||
@ -28,9 +29,9 @@ public class XML
|
|||||||
|
|
||||||
private final static String escapeXMLChars(String input, boolean quote)
|
private final static String escapeXMLChars(String input, boolean quote)
|
||||||
{
|
{
|
||||||
StringBuilder out = new StringBuilder();
|
|
||||||
if (input == null)
|
if (input == null)
|
||||||
return null;
|
return null;
|
||||||
|
StringBuffer out = new StringBuffer();
|
||||||
int oldsize=input.length();
|
int oldsize=input.length();
|
||||||
char[] old=new char[oldsize];
|
char[] old=new char[oldsize];
|
||||||
input.getChars(0,oldsize,old,0);
|
input.getChars(0,oldsize,old,0);
|
||||||
@ -61,5 +62,25 @@ public class XML
|
|||||||
{
|
{
|
||||||
return escapeXMLChars(input, true);
|
return escapeXMLChars(input, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// unescapeXMLChars
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public final static String unescapeXMLChars(String input)
|
||||||
|
{
|
||||||
|
if (input == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
String outStr;
|
||||||
|
|
||||||
|
outStr = input.replace("&", "&");
|
||||||
|
outStr = outStr.replace("<", "<");
|
||||||
|
outStr = outStr.replace(">", ">");
|
||||||
|
outStr = outStr.replace("'", "\'");
|
||||||
|
outStr = outStr.replace(""", "\"");
|
||||||
|
|
||||||
|
return outStr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,10 @@
|
|||||||
*
|
*
|
||||||
* 06/15/04
|
* 06/15/04
|
||||||
* - first revision.
|
* - first revision.
|
||||||
|
* 01/08/08
|
||||||
|
* - Fixed parse() not to occur null exception when the NamedNodeMap is null on Android.
|
||||||
|
* 02/08/08
|
||||||
|
* - Change parse() to use Node::addValue() instead of the setValue().
|
||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
@ -50,11 +54,15 @@ public class JaxpParser extends Parser
|
|||||||
|
|
||||||
String domNodeName = domNode.getNodeName();
|
String domNodeName = domNode.getNodeName();
|
||||||
String domNodeValue = domNode.getNodeValue();
|
String domNodeValue = domNode.getNodeValue();
|
||||||
|
NamedNodeMap attrs = domNode.getAttributes();
|
||||||
|
int arrrsLen = (attrs != null) ? attrs.getLength() : 0;
|
||||||
|
|
||||||
// Debug.message("[" + rank + "] ELEM : " + domNodeName + ", " + domNodeValue + ", type = " + domNodeType + ", attrs = " + arrrsLen);
|
// Debug.message("[" + rank + "] ELEM : " + domNodeName + ", " + domNodeValue + ", type = " + domNodeType + ", attrs = " + arrrsLen);
|
||||||
|
|
||||||
if (domNodeType == org.w3c.dom.Node.TEXT_NODE) {
|
if (domNodeType == org.w3c.dom.Node.TEXT_NODE) {
|
||||||
parentNode.setValue(domNodeValue);
|
// Change to use Node::addValue() instead of the setValue(). (2008/02/07)
|
||||||
|
//parentNode.setValue(domNodeValue);
|
||||||
|
parentNode.addValue(domNodeValue);
|
||||||
return parentNode;
|
return parentNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,20 +77,26 @@ public class JaxpParser extends Parser
|
|||||||
parentNode.addNode(node);
|
parentNode.addNode(node);
|
||||||
|
|
||||||
NamedNodeMap attrMap = domNode.getAttributes();
|
NamedNodeMap attrMap = domNode.getAttributes();
|
||||||
int attrLen = attrMap.getLength();
|
if (attrMap != null) {
|
||||||
//Debug.message("attrLen = " + attrLen);
|
int attrLen = attrMap.getLength();
|
||||||
for (int n = 0; n<attrLen; n++) {
|
//Debug.message("attrLen = " + attrLen);
|
||||||
org.w3c.dom.Node attr = attrMap.item(n);
|
for (int n = 0; n<attrLen; n++) {
|
||||||
String attrName = attr.getNodeName();
|
org.w3c.dom.Node attr = attrMap.item(n);
|
||||||
String attrValue = attr.getNodeValue();
|
String attrName = attr.getNodeName();
|
||||||
node.setAttribute(attrName, attrValue);
|
String attrValue = attr.getNodeValue();
|
||||||
|
node.setAttribute(attrName, attrValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
org.w3c.dom.Node child = domNode.getFirstChild();
|
org.w3c.dom.Node child = domNode.getFirstChild();
|
||||||
while (child != null) {
|
if(child==null){
|
||||||
|
node.setValue("");
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
do{
|
||||||
parse(node, child, rank+1);
|
parse(node, child, rank+1);
|
||||||
child = child.getNextSibling();
|
child = child.getNextSibling();
|
||||||
}
|
}while (child != null);
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user