hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1807195 - in /httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples: AsyncServerFilterExample.java ClassicServerFilterExample.java
Date Mon, 04 Sep 2017 08:21:14 GMT
Author: olegk
Date: Mon Sep  4 08:21:14 2017
New Revision: 1807195

URL: http://svn.apache.org/viewvc?rev=1807195&view=rev
Log:
Added server filter example sources

Added:
    httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
  (with props)
    httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
  (with props)

Added: httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
URL: http://svn.apache.org/viewvc/httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java?rev=1807195&view=auto
==============================================================================
--- httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
(added)
+++ httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
Mon Sep  4 08:21:14 2017
@@ -0,0 +1,189 @@
+/*
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.http.examples;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.HttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.Message;
+import org.apache.hc.core5.http.impl.bootstrap.AsyncServerBootstrap;
+import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer;
+import org.apache.hc.core5.http.impl.bootstrap.StandardFilters;
+import org.apache.hc.core5.http.message.BasicHttpResponse;
+import org.apache.hc.core5.http.nio.AsyncDataConsumer;
+import org.apache.hc.core5.http.nio.AsyncEntityProducer;
+import org.apache.hc.core5.http.nio.AsyncFilterChain;
+import org.apache.hc.core5.http.nio.AsyncFilterHandler;
+import org.apache.hc.core5.http.nio.AsyncPushProducer;
+import org.apache.hc.core5.http.nio.AsyncRequestConsumer;
+import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
+import org.apache.hc.core5.http.nio.BasicRequestConsumer;
+import org.apache.hc.core5.http.nio.BasicResponseProducer;
+import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
+import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
+import org.apache.hc.core5.http.nio.support.AbstractAsyncServerAuthFilter;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.io.ShutdownType;
+import org.apache.hc.core5.reactor.IOReactorConfig;
+import org.apache.hc.core5.reactor.ListenerEndpoint;
+import org.apache.hc.core5.util.TimeValue;
+
+/**
+ * Example of using asynchronous I/O request filters with an embedded HTTP/1.1 server.
+ */
+public class AsyncServerFilterExample {
+
+    public static void main(String[] args) throws Exception {
+        int port = 8080;
+        if (args.length >= 1) {
+            port = Integer.parseInt(args[0]);
+        }
+
+        IOReactorConfig config = IOReactorConfig.custom()
+                .setSoTimeout(15, TimeUnit.SECONDS)
+                .setTcpNoDelay(true)
+                .build();
+
+        final HttpAsyncServer server = AsyncServerBootstrap.bootstrap()
+                .setIOReactorConfig(config)
+
+                // Replace standard expect-continue handling with a custom auth filter
+
+                .replaceFilter(StandardFilters.EXPECT_CONTINUE.name(), new AbstractAsyncServerAuthFilter<String>(true)
{
+
+                    @Override
+                    protected String parseChallengeResponse(
+                            final String authorizationValue, final HttpContext context) throws
HttpException {
+                        return authorizationValue;
+                    }
+
+                    @Override
+                    protected boolean authenticate(
+                            final String challengeResponse, final HttpContext context) {
+                        return "let me pass".equals(challengeResponse);
+                    }
+
+                    @Override
+                    protected String generateChallenge(
+                            final String challengeResponse, final HttpContext context) {
+                        return "who goes there?";
+                    }
+
+                })
+
+                // Add a custom request filter at the beginning of the processing pipeline
+
+                .addFilterFirst("my-filter", new AsyncFilterHandler() {
+
+                    @Override
+                    public AsyncDataConsumer handle(
+                            final HttpRequest request,
+                            final EntityDetails entityDetails,
+                            final HttpContext context,
+                            final AsyncFilterChain.ResponseTrigger responseTrigger,
+                            final AsyncFilterChain chain) throws HttpException, IOException
{
+                        if (request.getRequestUri().equals("/back-door")) {
+                            responseTrigger.submitResponse(
+                                    new BasicHttpResponse(HttpStatus.SC_OK),
+                                    new BasicAsyncEntityProducer("Welcome", ContentType.TEXT_PLAIN));
+                            return null;
+                        } else {
+                            return chain.proceed(request, entityDetails, context, new AsyncFilterChain.ResponseTrigger()
{
+
+                                @Override
+                                public void sendInformation(
+                                        final HttpResponse response) throws HttpException,
IOException {
+                                    responseTrigger.sendInformation(response);
+                                }
+
+                                @Override
+                                public void submitResponse(
+                                        final HttpResponse response, final AsyncEntityProducer
entityProducer) throws HttpException, IOException {
+                                    response.addHeader("X-Filter", "My-Filter");
+                                    responseTrigger.submitResponse(response, entityProducer);
+                                }
+
+                                @Override
+                                public void pushPromise(
+                                        final HttpRequest promise, final AsyncPushProducer
responseProducer) throws HttpException, IOException {
+                                    responseTrigger.pushPromise(promise, responseProducer);
+                                }
+
+                            });
+                        }
+                    }
+
+                })
+
+                // Application request handler
+
+                .register("*", new AsyncServerRequestHandler<Message<HttpRequest, String>>()
{
+
+                    @Override
+                    public AsyncRequestConsumer<Message<HttpRequest, String>>
prepare(
+                            final HttpRequest request,
+                            final HttpContext context) throws HttpException {
+                        return new BasicRequestConsumer<>(new StringAsyncEntityConsumer());
+                    }
+
+                    @Override
+                    public void handle(
+                            final Message<HttpRequest, String> requestMessage,
+                            final ResponseTrigger responseTrigger,
+                            final HttpContext context) throws HttpException, IOException
{
+                        // do something useful
+                        responseTrigger.submitResponse(new BasicResponseProducer(
+                                HttpStatus.SC_OK,
+                                new BasicAsyncEntityProducer("Hello", ContentType.TEXT_PLAIN)));
+                    }
+                })
+                .create();
+
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+            @Override
+            public void run() {
+                System.out.println("HTTP server shutting down");
+                server.shutdown(ShutdownType.GRACEFUL);
+            }
+        });
+
+        server.start();
+        Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(port));
+        ListenerEndpoint listenerEndpoint = future.get();
+        System.out.print("Listening on " + listenerEndpoint.getAddress());
+        server.awaitShutdown(TimeValue.ofDays(Long.MAX_VALUE));
+    }
+
+}

Propchange: httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
URL: http://svn.apache.org/viewvc/httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java?rev=1807195&view=auto
==============================================================================
--- httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
(added)
+++ httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
Mon Sep  4 08:21:14 2017
@@ -0,0 +1,157 @@
+/*
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.http.examples;
+
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.hc.core5.http.ClassicHttpRequest;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.config.SocketConfig;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.impl.bootstrap.StandardFilters;
+import org.apache.hc.core5.http.io.HttpFilterChain;
+import org.apache.hc.core5.http.io.HttpFilterHandler;
+import org.apache.hc.core5.http.io.HttpRequestHandler;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.io.support.AbstractHttpServerAuthFilter;
+import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.io.ShutdownType;
+import org.apache.hc.core5.util.TimeValue;
+
+/**
+ * Example of using classic I/O request filters with an embedded HTTP/1.1 server.
+ */
+public class ClassicServerFilterExample {
+
+    public static void main(String[] args) throws Exception {
+        int port = 8080;
+        if (args.length >= 1) {
+            port = Integer.parseInt(args[0]);
+        }
+        SocketConfig socketConfig = SocketConfig.custom()
+                .setSoTimeout(15, TimeUnit.SECONDS)
+                .setTcpNoDelay(true)
+                .build();
+
+        final HttpServer server = ServerBootstrap.bootstrap()
+                .setListenerPort(port)
+                .setSocketConfig(socketConfig)
+
+                // Replace standard expect-continue handling with a custom auth filter
+
+                .replaceFilter(StandardFilters.EXPECT_CONTINUE.name(), new AbstractHttpServerAuthFilter<String>(false)
{
+
+                    @Override
+                    protected String parseChallengeResponse(
+                            final String authorizationValue, final HttpContext context) throws
HttpException {
+                        return authorizationValue;
+                    }
+
+                    @Override
+                    protected boolean authenticate(
+                            final String challengeResponse, final HttpContext context) {
+                        return "let me pass".equals(challengeResponse);
+                    }
+
+                    @Override
+                    protected String generateChallenge(
+                            final String challengeResponse, final HttpContext context) {
+                        return "who goes there?";
+                    }
+
+                })
+
+                // Add a custom request filter at the beginning of the processing pipeline
+
+                .addFilterFirst("my-filter", new HttpFilterHandler() {
+
+                    @Override
+                    public void handle(final ClassicHttpRequest request,
+                                       final HttpFilterChain.ResponseTrigger responseTrigger,
+                                       final HttpContext context, HttpFilterChain chain)
throws HttpException, IOException {
+                        if (request.getRequestUri().equals("/back-door")) {
+                            final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK);
+                            response.setEntity(new StringEntity("Welcome", ContentType.TEXT_PLAIN));
+                            responseTrigger.submitResponse(response);
+                        } else {
+                            chain.proceed(request, new HttpFilterChain.ResponseTrigger()
{
+
+                                @Override
+                                public void sendInformation(final ClassicHttpResponse response)
throws HttpException, IOException {
+                                    responseTrigger.sendInformation(response);
+                                }
+
+                                @Override
+                                public void submitResponse(final ClassicHttpResponse response)
throws HttpException, IOException {
+                                    response.addHeader("X-Filter", "My-Filter");
+                                    responseTrigger.submitResponse(response);
+                                }
+
+                            }, context);
+                        }
+                    }
+
+                })
+
+                // Application request handler
+
+                .register("*", new HttpRequestHandler() {
+
+                    @Override
+                    public void handle(
+                            final ClassicHttpRequest request,
+                            final ClassicHttpResponse response,
+                            final HttpContext context) throws HttpException, IOException
{
+                        // do something useful
+                        response.setCode(HttpStatus.SC_OK);
+                        response.setEntity(new StringEntity("Hello"));
+                    }
+
+                })
+                .create();
+
+        server.start();
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+            @Override
+            public void run() {
+                server.shutdown(ShutdownType.GRACEFUL);
+            }
+        });
+        System.out.println("Listening on port " + port);
+
+        server.awaitTermination(TimeValue.ofDays(Long.MAX_VALUE));
+
+    }
+
+}

Propchange: httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/site/httpcomponents-core-5.0.x/httpcore5/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message