Author: fmoga
Date: Wed Nov 3 22:37:11 2010
New Revision: 1030739
URL: http://svn.apache.org/viewvc?rev=1030739&view=rev
Log:
Added websocket url parsing.
Added:
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/ws/
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/ws/Handler.java
(with props)
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/wss/
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/wss/Handler.java
(with props)
Modified:
labs/monsoon/trunk/modules/monsoon-client/pom.xml
labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/URLParser.java
labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/WebSocket.java
labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/WebSocketUtils.java
labs/monsoon/trunk/modules/monsoon-client/src/test/java/org/apache/monsoon/client/WebSocketTest.java
Modified: labs/monsoon/trunk/modules/monsoon-client/pom.xml
URL:
http://svn.apache.org/viewvc/labs/monsoon/trunk/modules/monsoon-client/pom.xml?rev=1030739&r1=1030738&r2=1030739&view=diff
==============================================================================
--- labs/monsoon/trunk/modules/monsoon-client/pom.xml (original)
+++ labs/monsoon/trunk/modules/monsoon-client/pom.xml Wed Nov 3 22:37:11 2010
@@ -30,12 +30,13 @@
<groupId>org.apache.monsoon</groupId>
<artifactId>monsoon-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
- <name>Monsoon Client</name>
+ <name>Apache Monsoon Client</name>
<url>http://labs.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
+
<dependencies>
<dependency>
<groupId>junit</groupId>
Modified:
labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/URLParser.java
URL:
http://svn.apache.org/viewvc/labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/URLParser.java?rev=1030739&r1=1030738&r2=1030739&view=diff
==============================================================================
---
labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/URLParser.java
(original)
+++
labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/URLParser.java
Wed Nov 3 22:37:11 2010
@@ -18,36 +18,94 @@
*/
package org.apache.monsoon.client;
+import java.net.MalformedURLException;
+import java.net.URL;
+
public class URLParser {
- private static final String WS_SCHEME = "ws://";
- private static final String WS_SECURE_SCHEME = "wss://";
+ private static final String WS = "ws";
+ private static final String WSS = "wss";
+ private static final int DEFAULT_WS_PORT = 80;
+ private static final int DEFAULT_WSS_PORT = 443;
+
+ private URL url;
+
+ public URLParser(String websocketUrl) throws WebSocketURLException {
+ WebSocketUtils.checkNull(websocketUrl, WebSocketConstants.URL_NULL);
+ url = parseUrl(websocketUrl);
+ if (!checkScheme() || hasFragment() || !isAsciiHost()
+ || !isAsciiResourceName()) {
+ throw new WebSocketURLException(WebSocketConstants.URL_MALFORMED);
+ }
+ }
- public URLParser(String url) throws WebSocketURLException {
- if (WebSocketUtils.checkNull(url)) {
- throw new IllegalStateException(WebSocketConstants.URL_NULL);
+ private URL parseUrl(String websocketUrl) throws WebSocketURLException {
+ // TODO: resolve URL?
+ URL parsedUrl = null;
+ try {
+ parsedUrl = new URL(websocketUrl);
+ } catch (MalformedURLException e) {
+ throw new WebSocketURLException(e.getMessage(), e);
}
- if (!isValidURL(url)) {
- throw new WebSocketURLException(WebSocketConstants.URL_MALFORMED);
+ return parsedUrl;
+ }
+
+ private boolean checkScheme() {
+ return WS.equals(url.getProtocol().trim().toLowerCase())
+ || WSS.equals(url.getProtocol().trim().toLowerCase());
+ }
+
+ private boolean hasFragment() {
+ return url.getRef() != null;
+ }
+
+ private boolean isAsciiResourceName() {
+ for (char c : getResourceName().toCharArray()) {
+ if (c < '!' || '~' < c) {
+ // not between U+0021 and U+007E
+ return false;
+ }
}
+ return true;
+ }
+
+ private boolean isAsciiHost() {
+ for (char c : getHost().toCharArray())
+ if (c > '~') {
+ // bigger than U+007E
+ return false;
+ }
+ return true;
}
- private boolean isValidURL(String url) {
- return isAbsolute(url) && hasValidScheme(url) && !hasFragment(url);
+ public boolean isSecure() {
+ return WSS.equals(url.getProtocol().trim().toLowerCase());
}
- private boolean isAbsolute(String url) {
- // TODO: improve absolute URL check
- return !url.startsWith("/");
+ public String getHost() {
+ return url.getHost();
}
- private boolean hasValidScheme(String url) {
- // TODO: ensure conversion is made to ASCII lowercase
- return url.toLowerCase().startsWith(WS_SCHEME) ||
url.toLowerCase().startsWith(WS_SECURE_SCHEME);
+ public int getPort() {
+ int port = url.getPort();
+ if (port < 0) {
+ port = DEFAULT_WS_PORT;
+ if (isSecure()) {
+ port = DEFAULT_WSS_PORT;
+ }
+ }
+ return port;
}
- private boolean hasFragment(String url) {
- return url.indexOf('#') != -1;
+ public String getResourceName() {
+ String resourceName = url.getPath();
+ if (resourceName.length() == 0) {
+ resourceName = "/";
+ }
+ if (url.getQuery() != null) {
+ resourceName += "?" + url.getQuery();
+ }
+ return resourceName;
}
}
Modified:
labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/WebSocket.java
URL:
http://svn.apache.org/viewvc/labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/WebSocket.java?rev=1030739&r1=1030738&r2=1030739&view=diff
==============================================================================
---
labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/WebSocket.java
(original)
+++
labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/WebSocket.java
Wed Nov 3 22:37:11 2010
@@ -25,11 +25,17 @@ public class WebSocket {
private static final short CLOSING = 2;
private static final short CLOSED = 3;
+ // W3C spec
private String url;
private String protocol;
private short readyState;
private long bufferedAmount;
+ private boolean secure;
+ private String host;
+ private int port;
+ private String resourceName;
+
public WebSocket(String url) throws WebSocketURLException {
this(url, new String[] {});
}
@@ -38,8 +44,13 @@ public class WebSocket {
this(url, new String[] { protocols });
}
- public WebSocket(String url, String[] protocols) throws
WebSocketURLException {
+ public WebSocket(String url, String[] protocols)
+ throws WebSocketURLException {
URLParser urlParser = new URLParser(url);
+ this.secure = urlParser.isSecure();
+ this.host = urlParser.getHost();
+ this.port = urlParser.getPort();
+ this.resourceName = urlParser.getResourceName();
}
public void send(String data) {
@@ -66,20 +77,19 @@ public class WebSocket {
// TODO
}
- public String getUrl() {
- return url;
+ public boolean isSecure() {
+ return secure;
}
- public String getProtocol() {
- return protocol;
+ public String getHost() {
+ return host;
}
- public short getReadyState() {
- return readyState;
+ public int getPort() {
+ return port;
}
- public long getBufferedAmount() {
- return bufferedAmount;
+ public String getResourceName() {
+ return resourceName;
}
-
}
Modified:
labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/WebSocketUtils.java
URL:
http://svn.apache.org/viewvc/labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/WebSocketUtils.java?rev=1030739&r1=1030738&r2=1030739&view=diff
==============================================================================
---
labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/WebSocketUtils.java
(original)
+++
labs/monsoon/trunk/modules/monsoon-client/src/main/java/org/apache/monsoon/client/WebSocketUtils.java
Wed Nov 3 22:37:11 2010
@@ -20,11 +20,10 @@ package org.apache.monsoon.client;
public class WebSocketUtils {
- public static boolean checkNull(Object object) {
+ public static void checkNull(Object object, String message) {
if (object == null) {
- return true;
+ throw new IllegalArgumentException(message);
}
- return false;
}
private WebSocketUtils() {
Added:
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/ws/Handler.java
URL:
http://svn.apache.org/viewvc/labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/ws/Handler.java?rev=1030739&view=auto
==============================================================================
---
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/ws/Handler.java
(added)
+++
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/ws/Handler.java
Wed Nov 3 22:37:11 2010
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package sun.net.www.protocol.ws;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+/**
+ *
+ */
+public class Handler extends URLStreamHandler {
+
+ /**
+ * {...@inheritdoc}
+ */
+ @Override
+ protected URLConnection openConnection(URL arg0) throws IOException {
+ return null;
+ }
+
+}
Propchange:
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/ws/Handler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/wss/Handler.java
URL:
http://svn.apache.org/viewvc/labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/wss/Handler.java?rev=1030739&view=auto
==============================================================================
---
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/wss/Handler.java
(added)
+++
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/wss/Handler.java
Wed Nov 3 22:37:11 2010
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package sun.net.www.protocol.wss;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+/**
+ *
+ */
+public class Handler extends URLStreamHandler {
+
+ /**
+ * {...@inheritdoc}
+ */
+ @Override
+ protected URLConnection openConnection(URL u) throws IOException {
+ return null;
+ }
+
+}
Propchange:
labs/monsoon/trunk/modules/monsoon-client/src/main/java/sun/net/www/protocol/wss/Handler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
labs/monsoon/trunk/modules/monsoon-client/src/test/java/org/apache/monsoon/client/WebSocketTest.java
URL:
http://svn.apache.org/viewvc/labs/monsoon/trunk/modules/monsoon-client/src/test/java/org/apache/monsoon/client/WebSocketTest.java?rev=1030739&r1=1030738&r2=1030739&view=diff
==============================================================================
---
labs/monsoon/trunk/modules/monsoon-client/src/test/java/org/apache/monsoon/client/WebSocketTest.java
(original)
+++
labs/monsoon/trunk/modules/monsoon-client/src/test/java/org/apache/monsoon/client/WebSocketTest.java
Wed Nov 3 22:37:11 2010
@@ -64,6 +64,14 @@ public class WebSocketTest extends TestC
}
}
+ public void testInvalidScheme2() {
+ try {
+ new WebSocket("http://monsoon.apache.org");
+ fail();
+ } catch (WebSocketURLException e) {
+ }
+ }
+
public void testValidScheme() {
try {
new WebSocket("ws://monsoon.apache.org");
@@ -111,4 +119,160 @@ public class WebSocketTest extends TestC
fail(e.getMessage());
}
}
+
+ public void testSecureWs() {
+ try {
+ WebSocket websocket = new WebSocket(
+ "ws://labs.apache.org:80/monsoon");
+ assertFalse(websocket.isSecure());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testSecureWss() {
+ try {
+ WebSocket websocket = new WebSocket(
+ "wss://labs.apache.org:80/monsoon");
+ assertTrue(websocket.isSecure());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testHost() {
+ try {
+ WebSocket websocket = new WebSocket(
+ "wss://labs.apache.org:1234/monsoon");
+ assertEquals("labs.apache.org", websocket.getHost());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testHost2() {
+ try {
+ WebSocket websocket = new WebSocket("wss://monsoon");
+ assertEquals("monsoon", websocket.getHost());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testWsPredefinedPort() {
+ try {
+ WebSocket websocket = new WebSocket("ws://monsoon.apache.org");
+ assertEquals(80, websocket.getPort());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testWssPredefinedPort() {
+ try {
+ WebSocket websocket = new WebSocket("wss://monsoon.apache.org");
+ assertEquals(443, websocket.getPort());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testWsPort() {
+ try {
+ WebSocket websocket = new WebSocket("ws://monsoon.apache.org:443");
+ assertEquals(443, websocket.getPort());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testWssPort() {
+ try {
+ WebSocket websocket = new WebSocket("wss://monsoon.apache.org:80");
+ assertEquals(80, websocket.getPort());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testResourceWithoutQuery() {
+ try {
+ WebSocket websocket = new
WebSocket("ws://labs.apache.org/monsoon");
+ assertEquals("/monsoon", websocket.getResourceName());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testResourceWithQuery() {
+ try {
+ WebSocket websocket = new WebSocket(
+ "ws://labs.apache.org/monsoon?lab=monsoon");
+ assertEquals("/monsoon?lab=monsoon", websocket.getResourceName());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testEmptyResource() {
+ try {
+ WebSocket websocket = new WebSocket("ws://labs.apache.org");
+ assertEquals("/", websocket.getResourceName());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testEmptyResourceWithPort() {
+ try {
+ WebSocket websocket = new WebSocket("ws://labs.apache.org:8080");
+ assertEquals("/", websocket.getResourceName());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testEmptyResourceWithQuery() {
+ try {
+ WebSocket websocket = new WebSocket(
+ "ws://labs.apache.org:8080/?lab=monsoon");
+ assertEquals("/?lab=monsoon", websocket.getResourceName());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testInvalidQuery() {
+ try {
+ WebSocket websocket = new WebSocket(
+ "ws://labs.apache.org:8080?lab=monsoon");
+ assertEquals("/?lab=monsoon", websocket.getResourceName());
+ } catch (WebSocketURLException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testNonASCIIHost() {
+ try {
+ new WebSocket("ws://labs£.apache.org:8080?lab=monsoon");
+ fail();
+ } catch (WebSocketURLException e) {
+ }
+ }
+
+ public void testNonASCIIResource() {
+ try {
+ new WebSocket("ws://labs.apache£.org:8080?lab=monsoon");
+ fail();
+ } catch (WebSocketURLException e) {
+ }
+ }
+
+ public void testNonASCIIResource2() {
+ try {
+ new WebSocket("ws://labs.apache.org:8080?lab=mons oon");
+ fail();
+ } catch (WebSocketURLException e) {
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]