Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleanup

  • Loading branch information...
commit 342c7cc226b6861aa1704912a526717c9e556943 1 parent 41b59d1
Robert Newson authored
View
10 pom.xml
@@ -28,11 +28,6 @@
<version>1.0</version>
</dependency>
<dependency>
- <groupId>com.ning</groupId>
- <artifactId>async-http-client</artifactId>
- <version>1.6.3</version>
- </dependency>
- <dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-embedded</artifactId>
<version>${jetty-version}</version>
@@ -53,6 +48,11 @@
<version>20090211</version>
</dependency>
<dependency>
+ <groupId>org.codehaus.jackson</groupId>
+ <artifactId>jackson-mapper-asl</artifactId>
+ <version>1.7.6</version>
+ </dependency>
+ <dependency>
<groupId>rhino</groupId>
<artifactId>js</artifactId>
<version>1.7R2</version>
View
64 src/main/java/com/github/rnewson/couchdb/lucene/HC.java
@@ -6,14 +6,18 @@
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
-import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
+import java.util.Arrays;
+import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
@@ -27,6 +31,7 @@
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.nio.DefaultClientIOEventDispatch;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
+import org.apache.http.impl.nio.reactor.SSLSetupHandler;
import org.apache.http.impl.nio.ssl.SSLClientIOEventDispatch;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.nio.ContentDecoder;
@@ -39,6 +44,7 @@
import org.apache.http.nio.protocol.NHttpRequestExecutionHandler;
import org.apache.http.nio.reactor.ConnectingIOReactor;
import org.apache.http.nio.reactor.IOEventDispatch;
+import org.apache.http.nio.reactor.IOSession;
import org.apache.http.nio.reactor.SessionRequest;
import org.apache.http.nio.reactor.SessionRequestCallback;
import org.apache.http.params.CoreConnectionPNames;
@@ -55,12 +61,38 @@
import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;
import org.apache.http.util.EntityUtils;
+import org.codehaus.jackson.map.ObjectMapper;
public class HC {
+ private static class MySSLSetupHandler implements SSLSetupHandler {
+
+ public void initalize(SSLEngine sslengine, HttpParams params)
+ throws SSLException {
+ System.err.println(Arrays.toString(sslengine
+ .getEnabledCipherSuites()));
+
+ // TODO make configurable in ini file
+ sslengine.setEnabledProtocols(new String[] { "TLSv1" });
+
+ // TODO make configurable in ini file
+ sslengine.setEnabledCipherSuites(new String[] {
+ "TLS_RSA_WITH_AES_256_CBC_SHA",
+ "TLS_DHE_RSA_WITH_AES_256_CBC_SHA",
+ "TLS_DHE_DSS_WITH_AES_256_CBC_SHA" });
+ }
+
+ public void verify(IOSession iosession, SSLSession sslsession)
+ throws SSLException {
+ }
+
+ }
+
private static class MyConsumingNHttpEntity implements ConsumingNHttpEntity {
private final ByteBuffer buffer = ByteBuffer.allocate(4096);
+ private final ObjectMapper mapper = new ObjectMapper();
+ private String remainder = "";
public void consumeContent() throws IOException {
}
@@ -72,8 +104,20 @@ public void consumeContent(final ContentDecoder decoder,
buffer.flip();
final CharsetDecoder charsetDecoder = Charset.forName("US-ASCII")
.newDecoder();
- final CharBuffer charBuffer = charsetDecoder.decode(buffer);
- System.out.println(charBuffer);
+ final String str = charsetDecoder.decode(buffer).toString();
+ final String[] changes = str.split("\n");
+ for (int i = 0, len = changes.length - 1; i < len; i++) {
+ final String line = i == 0 ? remainder + changes[i]
+ : changes[i];
+ if (line.startsWith("{") && line.endsWith("}")) {
+ System.out.print("valid : ");
+ } else {
+ System.out.print("invalid: ");
+ }
+ System.out.println(line);
+ }
+ remainder = changes[changes.length-1];
+
}
public void finish() throws IOException {
@@ -120,7 +164,7 @@ public void writeTo(final OutputStream outstream) throws IOException {
}
- static class EventLogger implements EventListener {
+ private static class EventLogger implements EventListener {
public void connectionClosed(final NHttpConnection conn) {
System.out.println("Connection closed: " + conn);
@@ -226,7 +270,8 @@ public HttpRequest submitRequest(final HttpContext context) {
}
- static class MySessionRequestCallback implements SessionRequestCallback {
+ private static class MySessionRequestCallback implements
+ SessionRequestCallback {
private final CountDownLatch requestCount;
@@ -269,8 +314,8 @@ public static void main(final String[] args) throws Exception {
params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true);
params.setParameter(CoreProtocolPNames.USER_AGENT, "HttpComponents/1.1");
- final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(2,
- params);
+ final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(
+ Runtime.getRuntime().availableProcessors(), params);
final HttpProcessor httpproc = new ImmutableHttpProcessor(
new HttpRequestInterceptor[] { new RequestContent(),
@@ -281,11 +326,12 @@ public static void main(final String[] args) throws Exception {
// I/O event and main threads
final CountDownLatch requestCount = new CountDownLatch(1);
- final boolean ssl = true;
+ final boolean ssl = false;
final SSLContext sslcontext;
if (ssl) {
sslcontext = SSLContext.getInstance("TLS");
+ // TODO allow toggle between default trust and custom file.
sslcontext
.init(null, new TrustManager[] { new BlindTrust() }, null);
} else {
@@ -299,7 +345,7 @@ public static void main(final String[] args) throws Exception {
final IOEventDispatch ioEventDispatch;
if (ssl) {
ioEventDispatch = new SSLClientIOEventDispatch(handler, sslcontext,
- params);
+ new MySSLSetupHandler(), params);
} else {
ioEventDispatch = new DefaultClientIOEventDispatch(handler, params);
}
View
75 src/main/java/com/github/rnewson/couchdb/lucene/Ning.java
@@ -1,75 +0,0 @@
-package com.github.rnewson.couchdb.lucene;
-
-import org.json.JSONObject;
-
-import com.ning.http.client.AsyncHandler;
-import com.ning.http.client.AsyncHttpClient;
-import com.ning.http.client.AsyncHttpClientConfig;
-import com.ning.http.client.AsyncHttpClientConfig.Builder;
-import com.ning.http.client.AsyncHttpProvider;
-import com.ning.http.client.HttpResponseBodyPart;
-import com.ning.http.client.HttpResponseHeaders;
-import com.ning.http.client.HttpResponseStatus;
-import com.ning.http.client.Response;
-import com.ning.http.client.providers.jdk.JDKAsyncHttpProvider;
-
-public class Ning {
-
- private static class MyHandler implements AsyncHandler<Response> {
-
- private String url;
-
- public MyHandler(final String url) {
- this.url = url;
- }
-
- public STATE onBodyPartReceived(final HttpResponseBodyPart part) throws Exception {
- final String line = new String(part.getBodyPartBytes(), "UTF-8");
- if ("\n".equals(line)) {
- System.err.println(url + ": heartbeat");
- return STATE.CONTINUE;
- } else {
- final JSONObject json = new JSONObject(line);
- if (json.has("last_seq")) {
- System.err.println(url + ": response ended at sequence " + json.getLong("last_seq"));
- return STATE.ABORT;
- }
- if (json.has("error")) {
- System.err.println(url + ": response encountered error: " + json.getString("reason"));
- return STATE.ABORT;
- }
- System.out.println(url + ": " + json);
- return STATE.CONTINUE;
- }
- }
-
- public Response onCompleted() throws Exception {
- System.out.println(url + " complete");
- return null;
- }
-
- public STATE onHeadersReceived(final HttpResponseHeaders headers) throws Exception {
- return STATE.CONTINUE;
- }
-
- public STATE onStatusReceived(final HttpResponseStatus status) throws Exception {
- System.err.println(url + ": " + status.getStatusCode());
- return status.getStatusCode() == 200 ? STATE.CONTINUE : STATE.ABORT;
- }
-
- public void onThrowable(final Throwable t) {
- System.err.println(url + " threw;");
- t.printStackTrace();
- }
-
- }
-
- public static void main(final String[] args) throws Exception {
- AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
- for (int i = 0; i < args.length; i++) {
- asyncHttpClient.prepareGet(args[i] + "/_changes?feed=continuous&heartbeat=65000").execute(
- new MyHandler(args[i]));
- }
- }
-
-}
View
121 src/main/java/com/github/rnewson/couchdb/lucene/SSLNio.java
@@ -1,121 +0,0 @@
-package com.github.rnewson.couchdb.lucene;
-
-import java.io.IOException;
-import java.net.InetSocketAddress;
-import java.nio.ByteBuffer;
-import java.nio.CharBuffer;
-import java.nio.channels.SelectionKey;
-import java.nio.channels.Selector;
-import java.nio.channels.SocketChannel;
-import java.nio.charset.Charset;
-import java.nio.charset.CharsetDecoder;
-import java.security.cert.CertificateException;
-import java.security.cert.X509Certificate;
-import java.util.Iterator;
-import java.util.Scanner;
-import java.util.Set;
-
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.SSLEngine;
-import javax.net.ssl.SSLEngineResult;
-import javax.net.ssl.SSLEngineResult.HandshakeStatus;
-import javax.net.ssl.SSLEngineResult.Status;
-import javax.net.ssl.SSLSession;
-import javax.net.ssl.TrustManager;
-import javax.net.ssl.X509TrustManager;
-
-public class SSLNio {
- private static class BlindTrust implements X509TrustManager {
-
- public void checkClientTrusted(X509Certificate[] arg0, String arg1)
- throws CertificateException {
- }
-
- public void checkServerTrusted(X509Certificate[] arg0, String arg1)
- throws CertificateException {
- }
-
- public X509Certificate[] getAcceptedIssuers() {
- return new X509Certificate[0];
- }
-
- }
-
- public static void main(String[] args) throws Exception {
- final SSLContext context = SSLContext.getInstance("TLS");
- context.init(null, new TrustManager[] { new BlindTrust() }, null);
-
- final SSLEngine engine = context.createSSLEngine();
- engine.setUseClientMode(true);
- engine.beginHandshake();
-
- SSLSession session = engine.getSession();
- int appBufferMax = session.getApplicationBufferSize();
- int netBufferMax = session.getPacketBufferSize();
-
- final ByteBuffer clientIn = ByteBuffer.allocate(appBufferMax + 50);
- final ByteBuffer clientOut = ByteBuffer
- .wrap("GET /db1/_changes?feed=continuous\r\n\r\n".getBytes());
- final ByteBuffer cTOs = ByteBuffer.allocate(netBufferMax);
- final ByteBuffer sTOc = ByteBuffer.allocate(netBufferMax);
-
- final SocketChannel channel = SocketChannel.open(new InetSocketAddress(
- "localhost", 6984));
-
- final Selector selector = Selector.open();
- channel.configureBlocking(false);
- channel.register(selector,
- SelectionKey.OP_READ | SelectionKey.OP_WRITE, session);
-
- SSLEngineResult result = null;
-
- while (true) {
- selector.select();
- final Set<SelectionKey> keys = selector.selectedKeys();
- for (final Iterator<SelectionKey> i = keys.iterator(); i.hasNext();) {
- final SelectionKey key = i.next();
- i.remove();
-
- result = engine.wrap(clientOut, cTOs);
- runDelegatedTasks(engine);
- cTOs.flip();
- channel.write(cTOs);
- cTOs.compact();
-
- channel.read(sTOc);
- sTOc.flip();
- result = engine.unwrap(sTOc, clientIn);
- runDelegatedTasks(engine);
- sTOc.compact();
-
- if (result.getHandshakeStatus() == HandshakeStatus.NOT_HANDSHAKING
- && result.getStatus() == Status.OK) {
- clientIn.flip();
- final Scanner scanner = new Scanner(clientIn);
- System.out.print(decode(clientIn));
- clientIn.compact();
- }
-
- if (result.getStatus() == Status.CLOSED) {
- channel.close();
- }
- }
- }
- }
-
- private static String decode(final ByteBuffer in) throws IOException {
- final CharsetDecoder decoder = Charset.forName("US-ASCII").newDecoder();
- final CharBuffer buf = decoder.decode(in);
- return new String(buf.array());
- }
-
- private static void runDelegatedTasks(final SSLEngine engine) {
- if (engine.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
- Runnable runnable;
- while ((runnable = engine.getDelegatedTask()) != null) {
- runnable.run();
- }
- }
- }
-
-}
Please sign in to comment.
Something went wrong with that request. Please try again.