summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2008-05-24 06:28:25 (EDT)
committerEike Stepper2008-05-24 06:28:25 (EDT)
commit08ab1577204f18b09ec78b60515224209903d974 (patch)
tree34eab014710eae245e8e6868c0ff6d425a2f5bc0
parent72c22256b9c14272547b4bbb83af0e9b6be10e47 (diff)
downloadcdo-08ab1577204f18b09ec78b60515224209903d974.zip
cdo-08ab1577204f18b09ec78b60515224209903d974.tar.gz
cdo-08ab1577204f18b09ec78b60515224209903d974.tar.bz2
[232648] Provide an HTTPConnector
https://bugs.eclipse.org/bugs/show_bug.cgi?id=232648
-rw-r--r--plugins/org.eclipse.net4j.http/META-INF/MANIFEST.MF3
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/IHTTPConnector.java1
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/INet4jTransportServlet.java2
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPAcceptor.java39
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPClientConnector.java54
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPServerConnector.java9
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/Net4jTransportServlet.java4
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/security/Randomizer.java14
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/security/IRandomizer.java6
9 files changed, 120 insertions, 12 deletions
diff --git a/plugins/org.eclipse.net4j.http/META-INF/MANIFEST.MF b/plugins/org.eclipse.net4j.http/META-INF/MANIFEST.MF
index 40694d6..908e440 100644
--- a/plugins/org.eclipse.net4j.http/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.net4j.http/META-INF/MANIFEST.MF
@@ -13,6 +13,7 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.3.0,4.0.0)";resoluti
org.eclipse.net4j;bundle-version="[0.8.0,0.9.0)";visibility:=reexport
Import-Package: javax.servlet;version="2.4.0";resolution:=optional,
javax.servlet.http;version="2.4.0";resolution:=optional,
- org.apache.commons.httpclient;version="3.0.1";resolution:=optional
+ org.apache.commons.httpclient;version="3.0.1";resolution:=optional,
+ org.apache.commons.httpclient.methods;version="3.0.1";resolution:=optional
Export-Package: org.eclipse.net4j.http;version="0.8.0",
org.eclipse.net4j.internal.http;version="0.8.0"
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/IHTTPConnector.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/IHTTPConnector.java
index 393bb0a..4244e5a 100644
--- a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/IHTTPConnector.java
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/IHTTPConnector.java
@@ -17,4 +17,5 @@ import org.eclipse.net4j.connector.IConnector;
*/
public interface IHTTPConnector extends IConnector
{
+ public String getConnectorID();
}
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/INet4jTransportServlet.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/INet4jTransportServlet.java
index 3b7e317..54a7737 100644
--- a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/INet4jTransportServlet.java
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/INet4jTransportServlet.java
@@ -28,6 +28,6 @@ public interface INet4jTransportServlet extends Servlet
*/
public interface RequestHandler
{
- public int connectRequested(String userID);
+ public String connectRequested(String userID);
}
}
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPAcceptor.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPAcceptor.java
index e61cb26..8f957af 100644
--- a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPAcceptor.java
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPAcceptor.java
@@ -14,7 +14,7 @@ import org.eclipse.net4j.http.IHTTPAcceptor;
import org.eclipse.net4j.http.INet4jTransportServlet;
import org.eclipse.net4j.internal.http.bundle.OM;
import org.eclipse.net4j.internal.util.om.trace.ContextTracer;
-import org.eclipse.net4j.util.concurrent.NonBlockingIntCounter;
+import org.eclipse.net4j.util.security.IRandomizer;
import org.eclipse.internal.net4j.acceptor.Acceptor;
@@ -23,16 +23,30 @@ import org.eclipse.internal.net4j.acceptor.Acceptor;
*/
public class HTTPAcceptor extends Acceptor implements IHTTPAcceptor, INet4jTransportServlet.RequestHandler
{
+ public static final int DEFAULT_CONNECTOR_ID_LENGTH = 32;
+
private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, HTTPAcceptor.class);
+ private IRandomizer randomizer;
+
private INet4jTransportServlet servlet;
- private transient NonBlockingIntCounter lastConnectorID = new NonBlockingIntCounter();
+ private int connectorIDLength = DEFAULT_CONNECTOR_ID_LENGTH;
public HTTPAcceptor()
{
}
+ public IRandomizer getRandomizer()
+ {
+ return randomizer;
+ }
+
+ public void setRandomizer(IRandomizer randomizer)
+ {
+ this.randomizer = randomizer;
+ }
+
public INet4jTransportServlet getServlet()
{
return servlet;
@@ -43,9 +57,19 @@ public class HTTPAcceptor extends Acceptor implements IHTTPAcceptor, INet4jTrans
this.servlet = servlet;
}
- public int connectRequested(String userID)
+ public int getConnectorIDLength()
{
- int connectorID = lastConnectorID.getValue() + 1;
+ return connectorIDLength;
+ }
+
+ public void setConnectorIDLength(int connectorIDLength)
+ {
+ this.connectorIDLength = connectorIDLength;
+ }
+
+ public String connectRequested(String userID)
+ {
+ String connectorID = createConnectorID(userID);
System.out.println("HELLO " + userID + " (" + connectorID + ")");
HTTPServerConnector connector = new HTTPServerConnector(this, connectorID);
@@ -62,11 +86,18 @@ public class HTTPAcceptor extends Acceptor implements IHTTPAcceptor, INet4jTrans
return "HTTPAcceptor";
}
+ protected String createConnectorID(String userID)
+ {
+ return randomizer.nextString(connectorIDLength, "0123456789ABCDEF");
+ }
+
@Override
protected void doBeforeActivate() throws Exception
{
super.doBeforeActivate();
+ checkArg(randomizer, "randomizer == null");
checkArg(servlet, "servlet == null");
+ checkArg(connectorIDLength > 0, "Constraint violated: connectorIDLength > 0");
}
@Override
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPClientConnector.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPClientConnector.java
index 6dafdfd..1ff420e 100644
--- a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPClientConnector.java
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPClientConnector.java
@@ -12,7 +12,18 @@ package org.eclipse.net4j.internal.http;
import org.eclipse.net4j.connector.ConnectorLocation;
import org.eclipse.net4j.http.IHTTPConnector;
+import org.eclipse.net4j.http.INet4jTransportServlet;
+import org.eclipse.net4j.util.io.ExtendedDataInputStream;
+import org.eclipse.net4j.util.io.ExtendedDataOutputStream;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
+import org.apache.commons.httpclient.methods.PostMethod;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.text.MessageFormat;
/**
@@ -22,6 +33,10 @@ public class HTTPClientConnector extends HTTPConnector implements IHTTPConnector
{
private String url;
+ private HttpClient httpClient;
+
+ private String connectorID;
+
public HTTPClientConnector()
{
}
@@ -31,6 +46,11 @@ public class HTTPClientConnector extends HTTPConnector implements IHTTPConnector
return ConnectorLocation.CLIENT;
}
+ public String getConnectorID()
+ {
+ return connectorID;
+ }
+
public String getURL()
{
return url;
@@ -58,4 +78,38 @@ public class HTTPClientConnector extends HTTPConnector implements IHTTPConnector
super.doBeforeActivate();
checkArg(url, "url == null");
}
+
+ @Override
+ protected void doActivate() throws Exception
+ {
+ super.doActivate();
+ httpClient = new HttpClient();
+ connect();
+ }
+
+ @Override
+ protected void doDeactivate() throws Exception
+ {
+ httpClient = null;
+ super.doDeactivate();
+ }
+
+ private void connect() throws IOException, HttpException
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ExtendedDataOutputStream out = new ExtendedDataOutputStream(baos);
+ out.writeByte(INet4jTransportServlet.OPCODE_CONNECT);
+ out.writeString(getUserID());
+ out.flush();
+ byte[] content = baos.toByteArray();
+
+ PostMethod method = new PostMethod(url);
+ method.setRequestEntity(new ByteArrayRequestEntity(content));
+
+ httpClient.executeMethod(method);
+ InputStream bodyInputStream = method.getResponseBodyAsStream();
+ ExtendedDataInputStream in = new ExtendedDataInputStream(bodyInputStream);
+ connectorID = in.readString();
+ method.releaseConnection();
+ }
}
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPServerConnector.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPServerConnector.java
index e1c7139..2add06e 100644
--- a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPServerConnector.java
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPServerConnector.java
@@ -21,11 +21,11 @@ public class HTTPServerConnector extends HTTPConnector
{
private HTTPAcceptor acceptor;
- private int connectorID;
+ private String connectorID;
private long lastTraffic = System.currentTimeMillis();
- public HTTPServerConnector(HTTPAcceptor acceptor, int connectorID)
+ public HTTPServerConnector(HTTPAcceptor acceptor, String connectorID)
{
this.acceptor = acceptor;
this.connectorID = connectorID;
@@ -46,6 +46,11 @@ public class HTTPServerConnector extends HTTPConnector
return "agent://connector:" + connectorID;
}
+ public String getConnectorID()
+ {
+ return connectorID;
+ }
+
public long getLastTraffic()
{
return lastTraffic;
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/Net4jTransportServlet.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/Net4jTransportServlet.java
index 59a967d..4151d09 100644
--- a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/Net4jTransportServlet.java
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/Net4jTransportServlet.java
@@ -83,7 +83,7 @@ public class Net4jTransportServlet extends HttpServlet implements INet4jTranspor
IOException
{
String userID = in.readString();
- int connectorID = requestHandler.connectRequested(userID);
- out.writeInt(connectorID);
+ String connectorID = requestHandler.connectRequested(userID);
+ out.writeString(connectorID);
}
}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/security/Randomizer.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/security/Randomizer.java
index 0a58d39..7c9b078 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/security/Randomizer.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/security/Randomizer.java
@@ -100,6 +100,20 @@ public class Randomizer extends Lifecycle implements IRandomizer
secureRandom.nextBytes(bytes);
}
+ public String nextString(int length, String alphabet)
+ {
+ int n = alphabet.length();
+ StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < length; i++)
+ {
+ int pos = nextInt(n);
+ char c = alphabet.charAt(pos);
+ builder.append(c);
+ }
+
+ return builder.toString();
+ }
+
public synchronized void setSeed(byte[] seed)
{
secureRandom.setSeed(seed);
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/security/IRandomizer.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/security/IRandomizer.java
index ca2f7b4..7f3887f 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/security/IRandomizer.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/security/IRandomizer.java
@@ -15,6 +15,8 @@ package org.eclipse.net4j.util.security;
*/
public interface IRandomizer
{
+ public byte[] generateSeed(int numBytes);
+
public boolean nextBoolean();
public double nextDouble();
@@ -29,7 +31,7 @@ public interface IRandomizer
public long nextLong();
- public byte[] generateSeed(int numBytes);
-
public void nextBytes(byte[] bytes);
+
+ public String nextString(int length, String alphabet);
}