Skip to content

Commit

Permalink
this solves #13
Browse files Browse the repository at this point in the history
  • Loading branch information
LODYGENSKY Oleg committed Jun 29, 2017
1 parent c1528d3 commit e3a5a96
Showing 1 changed file with 55 additions and 37 deletions.
92 changes: 55 additions & 37 deletions src/xtremweb/communications/URI.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private URI() {
public URI(final String value) throws URISyntaxException {
this();
fromString(value);
checkHostAndScheme();
}

/**
Expand All @@ -127,6 +128,7 @@ public URI(final Attributes attrs) {
*/
public URI(final String server, final UID uid) throws URISyntaxException {
this(server, -1, uid);
checkHostAndScheme();
}

/**
Expand All @@ -145,6 +147,7 @@ public URI(final String server, final int port, final UID uid) throws URISyntaxE
uri = new java.net.URI(Connection.xwScheme() + Connection.getSchemeSeparator() + server
+ (port > 0 ? ":" + port : "") + (uid != null ? "/" + uid.toString() : ""));
uri.normalize();
checkHostAndScheme();
}

/**
Expand All @@ -155,16 +158,28 @@ public URI(final String server, final int port, final UID uid) throws URISyntaxE
* is the input stream
* @exception IOException
* is thrown on XML parsing error
* @throws URISyntaxException
*/
public URI(final DataInputStream in) throws IOException, SAXException {
public URI(final DataInputStream in) throws IOException, SAXException, URISyntaxException {
this();
final XMLReader reader = new XMLReader(this);
try {
try (final XMLReader reader = new XMLReader(this)) {
reader.read(in);
} catch (final InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
checkHostAndScheme();
}
/**
* This checks the presence of server and scheme
* @throws URISyntaxException if server or scheme is null
*/
private void checkHostAndScheme() throws URISyntaxException {
if (uri == null) {
return;
}
if (!isFile() && ((getScheme() == null) || (getHost() == null))) {
throw new URISyntaxException(uri.toString(), "host or scheme not set");
}
}

/**
Expand Down Expand Up @@ -194,21 +209,19 @@ public int hashCode() {
* This returns a java.rmi.server.UID String representation
*/
public UID getUID() {
try {
final String uidStr = getPath().substring(1, getPath().length());
return new UID(uidStr);
} catch (final Exception e) {
if (getPath() == null) {
return null;
}
return null;
final String uidStr = getPath().substring(1, getPath().length());
return new UID(uidStr);
}

/**
* This returns the host part of this URI
*/
public String getHost() {
try {
if(uri != null) {
return uri.getHost();
} catch (final NullPointerException e) {
}
return null;
}
Expand All @@ -217,16 +230,17 @@ public String getHost() {
* This returns the port part of this URI
*/
public int getPort() {
return uri.getPort();
if(uri != null) {
return uri.getPort();
}
return -1;
}

/**
* This returns the scheme part of this URI
*/
public String getScheme() {
try {
if(uri != null) {
return uri.getScheme();
} catch (final NullPointerException e) {
}
return null;
}
Expand All @@ -235,9 +249,8 @@ public String getScheme() {
* This returns the path part of this URI
*/
public String getPath() {
try {
if(uri != null) {
return uri.getPath();
} catch (final NullPointerException e) {
}
return null;
}
Expand All @@ -252,7 +265,7 @@ public boolean isFile() {
if (getScheme() == null) {
return false;
}
return (getScheme().compareToIgnoreCase(Connection.fileScheme()) == 0);
return (getScheme() == null ? false : getScheme().compareToIgnoreCase(Connection.fileScheme()) == 0);
}

/**
Expand All @@ -262,7 +275,7 @@ public boolean isFile() {
* @see xtremweb.communications.Connection#xwScheme()
*/
public boolean isXtremWeb() {
return (getScheme().compareToIgnoreCase(Connection.xwScheme()) == 0);
return (getScheme() == null ? false : getScheme().compareToIgnoreCase(Connection.xwScheme()) == 0);
}

/**
Expand All @@ -272,9 +285,11 @@ public boolean isXtremWeb() {
* @see xtremweb.communications.Connection#atticScheme()
*/
public boolean isAttic() {
URL url = null;
if(uri == null) {
return false;
}
try {
url = new URL(uri.toString());
final URL url = new URL(uri.toString());
return (url.getProtocol().compareToIgnoreCase(Connection.atticScheme()) == 0);
} catch (final Exception e) {
}
Expand All @@ -288,9 +303,11 @@ public boolean isAttic() {
* @see xtremweb.communications.Connection#httpScheme()
*/
public boolean isHttp() {
URL url = null;
if(uri == null) {
return false;
}
try {
url = new URL(uri.toString());
final URL url = new URL(uri.toString());
return (url.getProtocol().compareToIgnoreCase(Connection.httpScheme()) == 0);
} catch (final Exception e) {
}
Expand All @@ -305,9 +322,11 @@ public boolean isHttp() {
* @since 7.3.2
*/
public boolean isHttps() {
URL url = null;
if(uri == null) {
return false;
}
try {
url = new URL(uri.toString());
final URL url = new URL(uri.toString());
return (url.getProtocol().compareToIgnoreCase(Connection.httpsScheme()) == 0);
} catch (final Exception e) {
}
Expand All @@ -331,9 +350,8 @@ public String toString() {
@Override
public String toString(final boolean csv) {
try {
String ret = uri.toString().replaceAll("&", "&");
ret = ret.replaceAll("&", "&");
return ret;
final String ret = uri.toString().replaceAll("&", "&");
return ret.replaceAll("&", "&");
} catch (final NullPointerException e) {
}
return null;
Expand All @@ -351,14 +369,9 @@ public final void fromString(final String value) throws URISyntaxException {
if (value == null) {
throw new URISyntaxException("string is null", "");
}
String v = value.replaceAll("&", "&");
v = v.replaceAll("&", "&");
try {
uri = new java.net.URI(v);
uri.normalize();
} finally {
v = null;
}
final String v = value.replaceAll("&", "&");
uri = new java.net.URI(v.replaceAll("&", "&"));
uri.normalize();
}

/**
Expand Down Expand Up @@ -428,7 +441,12 @@ public static void main(final String[] argv) {
if (uri.isFile() == true) {
System.out.println(uri.getPath() + " exists = " + new File(uri.getPath()).exists());
}

try {
uri.checkHostAndScheme();
}
catch(Exception e) {
e.printStackTrace();
}
final Hashtable<URI, String> cache = new Hashtable<>();

for (int i = 0; i < 10; i++) {
Expand Down

0 comments on commit e3a5a96

Please sign in to comment.