incubator-deft-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmee...@apache.org
Subject svn commit: r1243739 - in /incubator/deft/trunk/awf-example-handlers/src/main/java/org: ./ apache/ apache/awf/ apache/awf/example/ apache/awf/example/client/ apache/awf/example/configuration/ apache/awf/example/handler/ apache/awf/example/keyvalue/
Date Mon, 13 Feb 2012 23:22:29 GMT
Author: jmeehan
Date: Mon Feb 13 23:22:28 2012
New Revision: 1243739

URL: http://svn.apache.org/viewvc?rev=1243739&view=rev
Log:
DEFT-198 - Handlers Example Files

Added:
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/AWFServerExample.java
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/client/
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/client/AsynchronousHttpClientGetExample.java
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/client/AsynchronousHttpClientPostExample.java
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/configuration/
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/configuration/AWFConfigurationExample.java
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/BasicHandlerExample.java
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/ClonedHandlerExample.java
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/CookieHandlerExample.java
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/PatternHandlerExample.java
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStore.java
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreClient.java
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreExample.java
    incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreHandler.java

Added: incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/AWFServerExample.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/AWFServerExample.java?rev=1243739&view=auto
==============================================================================
--- incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/AWFServerExample.java
(added)
+++ incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/AWFServerExample.java
Mon Feb 13 23:22:28 2012
@@ -0,0 +1,49 @@
+/*
+ *  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 org.apache.awf.example;
+
+import org.apache.awf.configuration.Configuration;
+import org.apache.awf.io.IOLoop;
+import org.apache.awf.web.HttpServer;
+
+/**
+ * A simple example of how to configure and instantiate a server.
+ */
+public class AWFServerExample {
+
+    public static void main(String[] args) {
+
+        /*
+         * The only required configuration option is the base package, under
+         * which handlers are found. See the sub-package "handler" for examples
+         * and how to view them as part of this example application.
+         */
+        Configuration configuration = new Configuration();
+        configuration.setHandlerPackage("org.apache.awf.example.handler");
+
+        /*
+         * Create an instance of HttpServer with the given Configuration, bind
+         * to a single port and start.
+         */
+        HttpServer server = new HttpServer(configuration);
+        server.listen(8080);
+        IOLoop.INSTANCE.start();
+    }
+}

Added: incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/client/AsynchronousHttpClientGetExample.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/client/AsynchronousHttpClientGetExample.java?rev=1243739&view=auto
==============================================================================
--- incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/client/AsynchronousHttpClientGetExample.java
(added)
+++ incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/client/AsynchronousHttpClientGetExample.java
Mon Feb 13 23:22:28 2012
@@ -0,0 +1,61 @@
+package org.apache.awf.example.client;
+ 
+import org.apache.awf.io.IOLoop;
+import org.apache.awf.io.timeout.Timeout;
+import org.apache.awf.web.AsyncCallback;
+import org.apache.awf.web.AsyncResult;
+import org.apache.awf.web.http.client.AsynchronousHttpClient;
+import org.apache.awf.web.http.client.Response;
+ 
+/**
+ * Example class to demonstrate usage of <code>AsynchronousHttpClient</code>.
+ */
+public class AsynchronousHttpClientGetExample {
+    /**
+     * A simple example of how to use the <code>AsynchronousHttpClient</code>,
+     * taking the following steps:
+     * <ol>
+     * <li>Create an instance of <code>AsyncResult&lt;Response&gt;</code>
and
+     * define the methods <code>onSuccess</code> and <code>onFailure</code>.
+     * This type will become the callback for our operation, and the actions
+     * defined in the method will be executed when that operation succeeds or
+     * fails respectively. In this case, we will detail the exception on failure
+     * and show the response body on success.
+     * <li>Create an instance of <code>AsynchronousHttpClient</code> and
perform
+     * a GET action passing the URL to hit and our previously defined callback
+     * type.
+     * <li>Add time-out value to the current <code>IOLoop</code> of one
second,
+     * and in the definition of the <code>onCallback</code> method tell the
+     * instance to stop listening when this time limit has been reached.
+     * <li>Start the instance.
+     * </ol>
+     * 
+     * @param args
+     *            the arguments to pass; unused.
+     */
+    public static void main(final String[] args) {
+ 
+        final AsyncResult<Response> callback = new AsyncResult<Response>() {
+ 
+            @Override
+            public void onSuccess(final Response response) { System.out.println("RESPONSE:
" + response); }
+
+            @Override
+            public void onFailure(final Throwable caught) { System.out.println("EXCEPTION:
" + caught); }
+        };
+
+        final AsynchronousHttpClient client = new AsynchronousHttpClient();
+        client.get("http://incubator.apache.org/deft/", callback);
+
+        IOLoop.INSTANCE.addTimeout(new Timeout(System.currentTimeMillis() + 1000, new AsyncCallback()
{
+            @Override
+            public void onCallback() {
+                System.out.println("INSTANCE.stop()");
+                IOLoop.INSTANCE.stop();
+            }
+        }));
+
+        System.out.println("INSTANCE.start()");
+        IOLoop.INSTANCE.start();
+    }
+ }
\ No newline at end of file

Added: incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/client/AsynchronousHttpClientPostExample.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/client/AsynchronousHttpClientPostExample.java?rev=1243739&view=auto
==============================================================================
--- incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/client/AsynchronousHttpClientPostExample.java
(added)
+++ incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/client/AsynchronousHttpClientPostExample.java
Mon Feb 13 23:22:28 2012
@@ -0,0 +1,92 @@
+/*
+ *  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 org.apache.awf.example.client;
+
+import org.apache.awf.io.IOLoop;
+import org.apache.awf.io.timeout.Timeout;
+import org.apache.awf.web.AsyncCallback;
+import org.apache.awf.web.AsyncResult;
+import org.apache.awf.web.http.client.AsynchronousHttpClient;
+import org.apache.awf.web.http.client.Request;
+import org.apache.awf.web.http.client.Response;
+import org.apache.awf.web.http.protocol.ContentType;
+import org.apache.awf.web.http.protocol.HttpVerb;
+
+/**
+ * Example class to demonstrate usage of <code>AsynchronousHttpClient</code>.
+ */
+public class AsynchronousHttpClientPostExample {
+
+    /**
+     * A simple example of how to use the <code>AsynchronousHttpClient</code>,
+     * taking the following steps:
+     * <ol>
+     * <li>Create an instance of <code>AsyncResult&lt;Response&gt;</code>
and
+     * define the methods <code>onSuccess</code> and <code>onFailure</code>.
+     * This type will become the callback for our operation, and the actions
+     * defined in the method will be executed when that operation succeeds or
+     * fails respectively. In this case, we will detail the exception on failure
+     * and show the response body on success.
+     * <li>Create a <code>Request</code>, providing target URL and HTTP
method
+     * as well as body and content type information.
+     * <li>Create an instance of <code>AsynchronousHttpClient</code> and
pass
+     * this <code>Request</code> with our previously defined callback.
+     * <li>Add time-out value to the current <code>IOLoop</code> of one
second,
+     * and in the definition of the <code>onCallback</code> method tell the
+     * instance to stop listening when this time limit has been reached.
+     * <li>Start the instance.
+     * </ol>
+     * 
+     * @param args the arguments to pass; unused.
+     */
+    public static void main(final String[] args) {
+
+        final AsyncResult<Response> callback = new AsyncResult<Response>() {
+
+            @Override
+            public void onSuccess(final Response response) {
+                System.out.println("RESPONSE: " + response);
+            }
+
+            @Override
+            public void onFailure(final Throwable caught) {
+                System.out.println("EXCEPTION: " + caught);
+            }
+        };
+
+        Request request = new Request("http://incubator.apache.org/deft/", HttpVerb.POST);
+        request.setBody("paramName=paramValue");
+        request.setContentType(ContentType.APPLICATION_FORM_URLENCODED);
+
+        final AsynchronousHttpClient client = new AsynchronousHttpClient();
+        client.fetch(request, callback);
+
+        IOLoop.INSTANCE.addTimeout(new Timeout(System.currentTimeMillis() + 1000, new AsyncCallback()
{
+            @Override
+            public void onCallback() {
+                System.out.println("INSTANCE.stop()");
+                IOLoop.INSTANCE.stop();
+            }
+        }));
+
+        System.out.println("INSTANCE.start()");
+        IOLoop.INSTANCE.start();
+    }
+}

Added: incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/configuration/AWFConfigurationExample.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/configuration/AWFConfigurationExample.java?rev=1243739&view=auto
==============================================================================
--- incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/configuration/AWFConfigurationExample.java
(added)
+++ incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/configuration/AWFConfigurationExample.java
Mon Feb 13 23:22:28 2012
@@ -0,0 +1,61 @@
+/*
+ *  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 org.apache.awf.example.configuration;
+
+import org.apache.awf.configuration.Configuration;
+import org.apache.awf.io.IOLoop;
+import org.apache.awf.web.HttpServer;
+
+/**
+ * Instantiation of server, describing all configuration options.
+ */
+public class AWFConfigurationExample {
+
+    public static void main(String[] args) {
+
+        /*
+         * The only required configuration option is the base package, under
+         * which handlers are found. See the sub-package "handler" for examples
+         * and how to view them as part of this example application.
+         */
+        Configuration configuration = new Configuration();
+        configuration.setHandlerPackage("org.apache.awf.example.handler");
+
+        /*
+         * The directory from which content is retrieved, which defaults to
+         * "static" where not set.
+         */
+        configuration.setStaticDirectory("myStaticDirectory");
+
+        /*
+         * By default, ETags will not be generated and so this option should be
+         * set if they are required.
+         */
+        configuration.setCreateETags(true);
+
+        /*
+         * Create an instance of HttpServer with the given Configuration, bind
+         * to a single port and start.
+         */
+        HttpServer server = new HttpServer(configuration);
+        server.listen(8080);
+        IOLoop.INSTANCE.start();
+    }
+}

Added: incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/BasicHandlerExample.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/BasicHandlerExample.java?rev=1243739&view=auto
==============================================================================
--- incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/BasicHandlerExample.java
(added)
+++ incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/BasicHandlerExample.java
Mon Feb 13 23:22:28 2012
@@ -0,0 +1,49 @@
+/*
+ *  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 org.apache.awf.example.handler;
+
+import org.apache.awf.annotation.Path;
+import org.apache.awf.example.AWFServerExample;
+import org.apache.awf.web.handler.RequestHandler;
+import org.apache.awf.web.http.HttpRequest;
+import org.apache.awf.web.http.HttpResponse;
+
+/**
+ * This is a basic handler, which does nothing more than write output and associate itself
with a path. The {@link Path}
+ * annotation below defines the path on which this handler is invoked. When executed as part
of the server example:
+ * <p>
+ * <code>http://localhost:8080/</code>
+ * </p>
+ * <p>
+ * The handler will send a simple message to the caller, and nothing more.
+ * </p>
+ * 
+ * @see Path
+ * @see AWFServerExample
+ */
+@Path("/")
+public class BasicHandlerExample extends RequestHandler {
+
+    @Override
+    public void get(HttpRequest request, HttpResponse response) {
+
+        response.write("It works!");
+    }
+}

Added: incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/ClonedHandlerExample.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/ClonedHandlerExample.java?rev=1243739&view=auto
==============================================================================
--- incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/ClonedHandlerExample.java
(added)
+++ incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/ClonedHandlerExample.java
Mon Feb 13 23:22:28 2012
@@ -0,0 +1,60 @@
+/*
+ *  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 org.apache.awf.example.handler;
+
+import java.util.Date;
+
+import org.apache.awf.annotation.Path;
+import org.apache.awf.example.AWFServerExample;
+import org.apache.awf.web.handler.RequestHandler;
+import org.apache.awf.web.http.HttpRequest;
+import org.apache.awf.web.http.HttpResponse;
+
+/**
+ * All <code>RequestHandler</code> implementations are cloned. Where there are
+ * mutable fields, the {@link #clone()} method might be explicitly overridden.
+ * <p>
+ * In the example below, note that the <code>String</code> representation of
+ * each handler differs when written to the <code>HttpResponse</code>.
+ * </p>
+ * 
+ * @see Path
+ * @see AWFServerExample
+ */
+@Path("/clone")
+public class ClonedHandlerExample extends RequestHandler {
+
+    private Date mutableDate = new Date();
+
+    @Override
+    public void get(HttpRequest request, HttpResponse response) {
+
+        response.write("Cloned: " + toString());
+    }
+
+    @Override
+    public Object clone() throws CloneNotSupportedException {
+
+        ClonedHandlerExample handler = (ClonedHandlerExample) super.clone();
+        handler.mutableDate = new Date(mutableDate.getTime());
+
+        return handler;
+    }
+}

Added: incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/CookieHandlerExample.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/CookieHandlerExample.java?rev=1243739&view=auto
==============================================================================
--- incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/CookieHandlerExample.java
(added)
+++ incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/CookieHandlerExample.java
Mon Feb 13 23:22:28 2012
@@ -0,0 +1,61 @@
+/*
+ *  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 org.apache.awf.example.handler;
+
+import java.util.UUID;
+
+import org.apache.awf.annotation.Path;
+import org.apache.awf.example.AWFServerExample;
+import org.apache.awf.web.handler.RequestHandler;
+import org.apache.awf.web.http.HttpRequest;
+import org.apache.awf.web.http.HttpResponse;
+
+/**
+ * This handler demonstrates usage of cookie-related functionality. The {@link Path} annotation
below defines the path
+ * on which this handler is invoked. When executed as part of the server example:
+ * <p>
+ * <code>http://localhost:8080/cookie</code>
+ * </p>
+ * <p>
+ * This handler will set a cookie when a page is first viewed, with a limited lifetime and
unique identifier. Upon
+ * refresh, this cookie will be cleared.
+ * </p>
+ * 
+ * @see Path
+ * @see AWFServerExample
+ */
+@Path("/cookie")
+public class CookieHandlerExample extends RequestHandler {
+
+    @Override
+    public void get(HttpRequest request, HttpResponse response) {
+
+        String sessCookie = request.getCookie("awfssid");
+
+        if (sessCookie != null) {
+            response.write("Cookie awfssid: " + sessCookie);
+            response.clearCookie("awfssid");
+        } else {
+            String ssid = UUID.randomUUID().toString();
+            response.setCookie("awfssid", ssid, 1800);
+            response.write("Set cookie awfssid: " + ssid);
+        }
+    }
+}

Added: incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/PatternHandlerExample.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/PatternHandlerExample.java?rev=1243739&view=auto
==============================================================================
--- incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/PatternHandlerExample.java
(added)
+++ incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/handler/PatternHandlerExample.java
Mon Feb 13 23:22:28 2012
@@ -0,0 +1,55 @@
+/*
+ *  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 org.apache.awf.example.handler;
+
+import java.util.regex.Pattern;
+
+import org.apache.awf.annotation.Path;
+import org.apache.awf.example.AWFServerExample;
+import org.apache.awf.web.handler.RequestHandler;
+import org.apache.awf.web.http.HttpRequest;
+import org.apache.awf.web.http.HttpResponse;
+
+/**
+ * This handler is invoked as a result of the combination of path and Regular
+ * Expression. When executed as part of the server example:
+ * <p>
+ * <code>http://localhost:8080/pattern/123</code> will be found.<br/>
+ * <code>http://localhost:8080/pattern/abc</code> will not be found.
+ * </p>
+ * <p>
+ * In the annotation below, the regular expression is "[0-9]+" which indicates
+ * that any number will be matched. Any pattern as understood by {@link Pattern}
+ * is suitable, and the handler will be invoked only when this pattern is
+ * matched.
+ * </p>
+ * 
+ * @see Path
+ * @see AWFServerExample
+ */
+@Path("/pattern/([0-9]+)")
+public class PatternHandlerExample extends RequestHandler {
+
+    @Override
+    public void get(HttpRequest request, HttpResponse response) {
+
+        response.write("The pattern was parsed successfully.");
+    }
+}

Added: incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStore.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStore.java?rev=1243739&view=auto
==============================================================================
--- incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStore.java
(added)
+++ incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStore.java
Mon Feb 13 23:22:28 2012
@@ -0,0 +1,121 @@
+/*
+ *  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 org.apache.awf.example.keyvalue;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Mocked KeyValueStoreHandler server (accepts a connection and echoes back the
+ * input)
+ * 
+ */
+@SuppressWarnings("serial")
+public class KeyValueStore extends Thread {
+
+    private final static Logger logger = LoggerFactory.getLogger(KeyValueStore.class);
+
+    public static final String HOST = "127.0.0.1";
+    public static final int PORT = 6379;
+
+    private final static Map<String, String> dict = new HashMap<String, String>()
{
+        {
+            put("awf", "kickass");
+        }
+    };
+
+    private ServerSocket serverSocket;
+
+    public KeyValueStore() {
+        logger.debug("Initializing KeyValueStore");
+        initialize();
+    }
+
+    @Override
+    public void run() {
+        try {
+            logger.debug("KeyValueStore waiting for clients...");
+            Socket clientSocket = serverSocket.accept();
+            logger.debug("KeyValueStore client connected...");
+            BufferedWriter os = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
+            BufferedReader is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
+            String input = is.readLine();
+            logger.debug("KeyValueStore received input: {}", input);
+            if (input.split("\\s+").length == 2) {
+                input = input.split("\\s+")[1]; // "GET awf" => "awf"
+            }
+            int sleep = 250;
+            if (input != null) {
+                logger.debug("KeyValueStore server sleeps " + sleep + "ms...");
+                try {
+                    Thread.sleep(sleep);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+                logger.debug("KeyValueStore woke up...");
+                String value = dict.get(input) + "\r\n";
+                os.write(value, 0, value.length());
+                logger.debug("KeyValueStore echoed back: " + value);
+                os.flush();
+            }
+
+            try {
+                Thread.sleep(sleep);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+            { // cleanup
+                try {
+                    if (is != null) {
+                        is.close();
+                    }
+                    if (os != null) {
+                        os.close();
+                    }
+                    if (clientSocket != null) {
+                        clientSocket.close();
+                    }
+                } catch (IOException ignore) {
+                }
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        logger.debug("Closing KeyValueStore");
+    }
+
+    private void initialize() {
+        try {
+            serverSocket = new ServerSocket(PORT);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}

Added: incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreClient.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreClient.java?rev=1243739&view=auto
==============================================================================
--- incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreClient.java
(added)
+++ incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreClient.java
Mon Feb 13 23:22:28 2012
@@ -0,0 +1,79 @@
+/*
+ *  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 org.apache.awf.example.keyvalue;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.SocketChannel;
+
+import org.apache.awf.io.AsynchronousSocket;
+import org.apache.awf.io.IOLoop;
+import org.apache.awf.web.AsyncCallback;
+import org.apache.awf.web.AsyncResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KeyValueStoreClient {
+
+    private final static Logger logger = LoggerFactory.getLogger(KeyValueStoreClient.class);
+
+    private AsynchronousSocket socket;
+    private SocketChannel channel;
+    private final String host;
+    private final int port;
+
+    public KeyValueStoreClient(String host, int port) {
+        this.host = host;
+        this.port = port;
+    }
+
+    public void connect() {
+        try {
+            channel = SocketChannel.open(new InetSocketAddress(host, port));
+            channel.configureBlocking(false);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        socket = new AsynchronousSocket(channel);
+        IOLoop.INSTANCE.addHandler(channel, socket, SelectionKey.OP_READ, ByteBuffer.allocate(1024));
+    }
+
+    public void get(String value, AsyncResult<byte[]> cb) {
+        socket.write("GET awf\r\n".getBytes(), new WriteCallback(cb));
+    }
+
+    private class WriteCallback implements AsyncCallback {
+
+        private final AsyncResult<byte[]> cb;
+
+        public WriteCallback(AsyncResult<byte[]> cb) {
+            this.cb = cb;
+        }
+
+        @Override
+        public void onCallback() {
+            // write is finished. read response from server
+            logger.debug("readUntil: \r\n");
+            socket.readUntil("\r\n".getBytes(), cb);
+        }
+    }
+}

Added: incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreExample.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreExample.java?rev=1243739&view=auto
==============================================================================
--- incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreExample.java
(added)
+++ incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreExample.java
Mon Feb 13 23:22:28 2012
@@ -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 org.apache.awf.example.keyvalue;
+
+import org.apache.awf.configuration.Configuration;
+import org.apache.awf.io.IOLoop;
+import org.apache.awf.web.HttpServer;
+
+public class KeyValueStoreExample {
+
+    private final static int PORT = 8080;
+
+    public static void main(String[] args) {
+
+        Configuration configuration = new Configuration();
+        configuration.setHandlerPackage("org.apache.awf.example.keyvalue");
+
+        HttpServer server = new HttpServer(configuration);
+        server.listen(PORT);
+        IOLoop.INSTANCE.start();
+    }
+}

Added: incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreHandler.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreHandler.java?rev=1243739&view=auto
==============================================================================
--- incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreHandler.java
(added)
+++ incubator/deft/trunk/awf-example-handlers/src/main/java/org/apache/awf/example/keyvalue/KeyValueStoreHandler.java
Mon Feb 13 23:22:28 2012
@@ -0,0 +1,56 @@
+/*
+ *  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 org.apache.awf.example.keyvalue;
+
+import org.apache.awf.annotation.Asynchronous;
+import org.apache.awf.annotation.Path;
+import org.apache.awf.web.AsyncResult;
+import org.apache.awf.web.handler.RequestHandler;
+import org.apache.awf.web.http.HttpRequest;
+import org.apache.awf.web.http.HttpResponse;
+
+/**
+ * 
+ */
+@Path("/kv")
+public class KeyValueStoreHandler extends RequestHandler {
+
+    private final KeyValueStoreClient client = new KeyValueStoreClient(KeyValueStore.HOST,
KeyValueStore.PORT);
+
+    public KeyValueStoreHandler() {
+        new KeyValueStore().start();
+        client.connect();
+    }
+
+    @Override
+    @Asynchronous
+    public void get(HttpRequest request, final HttpResponse response) {
+        client.get("awf", new AsyncResult<byte[]>() {
+            @Override
+            public void onFailure(Throwable caught) { /* ignore */
+            }
+
+            @Override
+            public void onSuccess(byte[] result) {
+                response.write(new String(result)).finish();
+            }
+        });
+    }
+}



Mime
View raw message