summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2008-05-26 01:49:31 (EDT)
committerEike Stepper2008-05-26 01:49:31 (EDT)
commitb87a353a1c9dc61d6158a33a217ac3664f6e68a7 (patch)
treefed92e0df6e182f799c9f21ff1110555e641b333
parentd60cb62fd5a015e3f6f2ea1929834f4a14e497a8 (diff)
downloadcdo-b87a353a1c9dc61d6158a33a217ac3664f6e68a7.zip
cdo-b87a353a1c9dc61d6158a33a217ac3664f6e68a7.tar.gz
cdo-b87a353a1c9dc61d6158a33a217ac3664f6e68a7.tar.bz2
[232648] Provide an HTTPConnector
https://bugs.eclipse.org/bugs/show_bug.cgi?id=232648
-rw-r--r--plugins/org.eclipse.net4j.http/plugin.xml8
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPClientConnector.java15
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPConnector.java30
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/Net4jTransportServlet.java1
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/HTTPTest.java75
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/Net4jEchoTestServlet.java72
6 files changed, 197 insertions, 4 deletions
diff --git a/plugins/org.eclipse.net4j.http/plugin.xml b/plugins/org.eclipse.net4j.http/plugin.xml
index 5289704..eb181fe 100644
--- a/plugins/org.eclipse.net4j.http/plugin.xml
+++ b/plugins/org.eclipse.net4j.http/plugin.xml
@@ -40,8 +40,12 @@
point="org.eclipse.equinox.http.registry.servlets">
<servlet
alias="/net4j"
- class="org.eclipse.net4j.internal.http.Net4jTransportServlet$ContainerAware">
- </servlet>
+ class="org.eclipse.net4j.internal.http.Net4jTransportServlet$ContainerAware"/>
+<!--
+ <servlet
+ alias="/net4j/echotest"
+ class="org.eclipse.net4j.internal.http.tests.Net4jEchoTestServlet"/>
+-->
</extension>
</plugin>
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 db1cfa9..299969d 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
@@ -68,6 +68,7 @@ public class HTTPClientConnector extends HTTPConnector
this.url = url;
}
+ @Override
public void multiplexChannel(final IChannel channel)
{
Queue<IBuffer> localQueue = ((InternalChannel)channel).getSendQueue();
@@ -144,7 +145,7 @@ public class HTTPClientConnector extends HTTPConnector
protected void doActivate() throws Exception
{
super.doActivate();
- httpClient = new HttpClient();
+ httpClient = createHTTPClient();
connect();
}
@@ -155,6 +156,16 @@ public class HTTPClientConnector extends HTTPConnector
super.doDeactivate();
}
+ protected HttpClient createHTTPClient()
+ {
+ return new HttpClient();
+ }
+
+ protected PostMethod createHTTPMethod(String url)
+ {
+ return new PostMethod(url);
+ }
+
@Override
protected void registerChannelWithPeer(final int channelID, final short channelIndex, final IProtocol protocol)
throws ConnectorException
@@ -218,7 +229,7 @@ public class HTTPClientConnector extends HTTPConnector
handler.handleOut(out);
out.flush();
byte[] content = baos.toByteArray();
- PostMethod method = new PostMethod(url);
+ PostMethod method = createHTTPMethod(url);
method.setRequestEntity(new ByteArrayRequestEntity(content));
httpClient.executeMethod(method);
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPConnector.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPConnector.java
index d6f7e1e..2441690 100644
--- a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPConnector.java
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPConnector.java
@@ -10,18 +10,31 @@
**************************************************************************/
package org.eclipse.net4j.internal.http;
+import org.eclipse.net4j.buffer.IBuffer;
+import org.eclipse.net4j.channel.IChannel;
import org.eclipse.net4j.http.IHTTPConnector;
+import org.eclipse.net4j.internal.http.bundle.OM;
+import org.eclipse.net4j.internal.util.om.trace.ContextTracer;
import org.eclipse.net4j.util.security.INegotiationContext;
+import org.eclipse.internal.net4j.buffer.Buffer;
+import org.eclipse.internal.net4j.channel.InternalChannel;
import org.eclipse.internal.net4j.connector.Connector;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
/**
* @author Eike Stepper
*/
public abstract class HTTPConnector extends Connector implements IHTTPConnector
{
+ private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, HTTPConnector.class);
+
private String connectorID;
+ private Queue<IBuffer> outputQueue = new ConcurrentLinkedQueue<IBuffer>();
+
public HTTPConnector()
{
}
@@ -36,6 +49,23 @@ public abstract class HTTPConnector extends Connector implements IHTTPConnector
this.connectorID = connectorID;
}
+ public Queue<IBuffer> getOutputQueue()
+ {
+ return outputQueue;
+ }
+
+ public void multiplexChannel(IChannel channel)
+ {
+ Queue<IBuffer> channelQueue = ((InternalChannel)channel).getSendQueue();
+ IBuffer buffer = channelQueue.poll();
+ if (TRACER.isEnabled())
+ {
+ TRACER.trace("Multiplexing " + ((Buffer)buffer).formatContent(true));
+ }
+
+ outputQueue.add(buffer);
+ }
+
@Override
protected INegotiationContext createNegotiationContext()
{
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 833bab4..038f798 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
@@ -112,6 +112,7 @@ public class Net4jTransportServlet extends HttpServlet implements INet4jTranspor
PrintWriter writer = resp.getWriter();
for (IHTTPConnector connector : connectors)
{
+ // TODO Security: Hide connectorID!
writer.write(connector.getConnectorID());
writer.write(":");
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/HTTPTest.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/HTTPTest.java
index eded166..a2d31dc 100644
--- a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/HTTPTest.java
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/HTTPTest.java
@@ -18,6 +18,15 @@ import org.eclipse.net4j.tests.signal.IntRequest;
import org.eclipse.net4j.tests.signal.TestSignalClientProtocolFactory;
import org.eclipse.net4j.tests.signal.TestSignalProtocol;
import org.eclipse.net4j.util.container.IManagedContainer;
+import org.eclipse.net4j.util.io.ExtendedDataInputStream;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.RequestEntity;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
/**
* @author Eike Stepper
@@ -37,6 +46,72 @@ public class HTTPTest extends AbstractTransportTest
return container;
}
+ /**
+ * Result: With the current implementation (HttpClient / Jetty) it's not possible to transfer request data before
+ */
+ public void _testRequestFlush() throws Exception
+ {
+ HttpClient client = new HttpClient();
+ PostMethod method = new PostMethod("http://eike@localhost:8080/net4j/echotest");
+ method.setRequestEntity(new RequestEntity()
+ {
+ public long getContentLength()
+ {
+ return -1;
+ }
+
+ public String getContentType()
+ {
+ return "application/octet-stream";
+ }
+
+ public boolean isRepeatable()
+ {
+ return false;
+ }
+
+ public void writeRequest(OutputStream out) throws IOException
+ {
+ int count = 10;
+ out.write(count);
+ for (int i = 0; i < count; i++)
+ {
+ send(out, i);
+ }
+ }
+
+ private void send(OutputStream out, int b) throws IOException
+ {
+ try
+ {
+ System.out.println("Writing " + b);
+ out.write(b);
+ out.flush();
+ Thread.sleep(1000);
+ }
+ catch (InterruptedException ex)
+ {
+ throw new RuntimeException(ex);
+ }
+ }
+ });
+
+ client.executeMethod(method);
+ InputStream responseBody = method.getResponseBodyAsStream();
+ ExtendedDataInputStream in = new ExtendedDataInputStream(responseBody);
+ int count = in.readInt();
+ for (int i = 0; i < count; i++)
+ {
+ int b = in.readByte();
+ assertEquals(i, b);
+
+ long gap = in.readLong();
+ System.out.println("Gap: " + gap);
+ }
+
+ method.releaseConnection();
+ }
+
public void test1() throws Exception
{
IHTTPConnector connector = getHTTPConnector();
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/Net4jEchoTestServlet.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/Net4jEchoTestServlet.java
new file mode 100644
index 0000000..de8f5d9
--- /dev/null
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/Net4jEchoTestServlet.java
@@ -0,0 +1,72 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.internal.http.tests;
+
+import org.eclipse.net4j.util.io.ExtendedDataInputStream;
+import org.eclipse.net4j.util.io.ExtendedDataOutputStream;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletInputStream;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.io.IOException;
+
+/**
+ * @author Eike Stepper
+ */
+public class Net4jEchoTestServlet extends HttpServlet
+{
+ private static final long serialVersionUID = 1L;
+
+ public Net4jEchoTestServlet()
+ {
+ }
+
+ @Override
+ protected final void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+ {
+ doPost(req, resp);
+ }
+
+ @Override
+ protected final void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+ {
+ doRequest(req, resp);
+ }
+
+ protected void doRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+ {
+ ServletInputStream servletInputStream = req.getInputStream();
+ ExtendedDataInputStream in = new ExtendedDataInputStream(servletInputStream);
+
+ ServletOutputStream servletOutputStream = resp.getOutputStream();
+ ExtendedDataOutputStream out = new ExtendedDataOutputStream(servletOutputStream);
+
+ long lastTime = System.currentTimeMillis();
+ int count = in.readByte();
+ out.writeInt(count);
+ for (int i = 0; i < count; i++)
+ {
+ byte b = in.readByte();
+ long now = System.currentTimeMillis();
+ long gap = now - lastTime;
+ lastTime = now;
+ System.out.println("Gap: " + gap);
+
+ out.writeByte(b);
+ out.writeLong(gap);
+ out.flush();
+ }
+ }
+}