asterixdb-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Till Westmann (Code Review)" <do-not-re...@asterixdb.incubator.apache.org>
Subject Change in asterixdb[master]: ASTERIXDB-1804: support more HTTP verbs
Date Tue, 21 Feb 2017 18:47:09 GMT
Till Westmann has uploaded a new change for review.

  https://asterix-gerrit.ics.uci.edu/1516

Change subject: ASTERIXDB-1804: support more HTTP verbs
......................................................................

ASTERIXDB-1804: support more HTTP verbs

Change-Id: I2d14ce9c3c34d345fe71a44518b1e95b79c37dab
---
M hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/AbstractServlet.java
R hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/BaseRequest.java
M hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/HttpServerHandler.java
M hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/PostRequest.java
M hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/utils/HttpUtil.java
5 files changed, 113 insertions(+), 67 deletions(-)


  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/16/1516/1

diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/AbstractServlet.java
b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/AbstractServlet.java
index 7d24994..93bdb38 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/AbstractServlet.java
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/AbstractServlet.java
@@ -19,11 +19,19 @@
 package org.apache.hyracks.http.server;
 
 import java.util.concurrent.ConcurrentMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import org.apache.hyracks.http.api.IServlet;
 import org.apache.hyracks.http.api.IServletRequest;
+import org.apache.hyracks.http.api.IServletResponse;
+
+import io.netty.handler.codec.http.HttpMethod;
+import io.netty.handler.codec.http.HttpResponseStatus;
 
 public abstract class AbstractServlet implements IServlet {
+    private static final Logger LOGGER = Logger.getLogger(AbstractServlet.class.getName());
+
     protected final String[] paths;
     protected final ConcurrentMap<String, Object> ctx;
     private final int[] trims;
@@ -54,6 +62,55 @@
         return ctx;
     }
 
+    @Override
+    public void handle(IServletRequest request, IServletResponse response) {
+        try {
+            final HttpMethod method = request.getHttpRequest().method();
+            if (HttpMethod.GET.equals(method)) {
+                get(request, response);
+            } else if (HttpMethod.HEAD.equals(method)) {
+                head(request, response);
+            } else if (HttpMethod.POST.equals(method)) {
+                post(request, response);
+            } else if (HttpMethod.PUT.equals(method)) {
+                put(request, response);
+            } else if (HttpMethod.DELETE.equals(method)) {
+                delete(request, response);
+            } else if (HttpMethod.OPTIONS.equals(method)) {
+                options(request, response);
+            } else {
+                response.setStatus(HttpResponseStatus.METHOD_NOT_ALLOWED);
+            }
+        } catch (Exception e) {
+            LOGGER.log(Level.WARNING, "Unhandled exception", e);
+            response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
+        }
+    }
+
+    protected void get(IServletRequest request, IServletResponse response) throws Exception
{
+        response.setStatus(HttpResponseStatus.METHOD_NOT_ALLOWED);
+    }
+
+    protected void head(IServletRequest request, IServletResponse response) throws Exception
{
+        response.setStatus(HttpResponseStatus.METHOD_NOT_ALLOWED);
+    }
+
+    protected void post(IServletRequest request, IServletResponse response) throws Exception
{
+        response.setStatus(HttpResponseStatus.METHOD_NOT_ALLOWED);
+    }
+
+    protected void put(IServletRequest request, IServletResponse response) throws Exception
{
+        response.setStatus(HttpResponseStatus.METHOD_NOT_ALLOWED);
+    }
+
+    protected void delete(IServletRequest request, IServletResponse response) throws Exception
{
+        response.setStatus(HttpResponseStatus.METHOD_NOT_ALLOWED);
+    }
+
+    protected void options(IServletRequest request, IServletResponse response) throws Exception
{
+        response.setStatus(HttpResponseStatus.METHOD_NOT_ALLOWED);
+    }
+
     public String path(IServletRequest request) {
         int trim = -1;
         if (paths.length > 1) {
diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/GetRequest.java
b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/BaseRequest.java
similarity index 74%
rename from hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/GetRequest.java
rename to hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/BaseRequest.java
index e666c0a..d271177 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/GetRequest.java
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/BaseRequest.java
@@ -18,19 +18,25 @@
  */
 package org.apache.hyracks.http.server;
 
+import java.io.IOException;
 import java.util.List;
 import java.util.Map;
 
+import io.netty.handler.codec.http.QueryStringDecoder;
 import org.apache.hyracks.http.api.IServletRequest;
 import org.apache.hyracks.http.server.utils.HttpUtil;
 
 import io.netty.handler.codec.http.FullHttpRequest;
 
-public class GetRequest implements IServletRequest {
-    private final FullHttpRequest request;
-    private final Map<String, List<String>> parameters;
+public class BaseRequest implements IServletRequest {
+    protected final FullHttpRequest request;
+    protected final Map<String, List<String>> parameters;
 
-    public GetRequest(FullHttpRequest request, Map<String, List<String>> parameters)
{
+    public static IServletRequest create(FullHttpRequest request) throws IOException {
+        return new BaseRequest(request, new QueryStringDecoder(request.uri()).parameters());
+    }
+
+    protected BaseRequest(FullHttpRequest request, Map<String, List<String>>
parameters) {
         this.request = request;
         this.parameters = parameters;
     }
diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/HttpServerHandler.java
b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/HttpServerHandler.java
index e23ede4..5b917cd 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/HttpServerHandler.java
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/HttpServerHandler.java
@@ -66,10 +66,6 @@
                 DefaultHttpResponse notFound =
                         new DefaultHttpResponse(request.protocolVersion(), HttpResponseStatus.NOT_FOUND);
                 ctx.write(notFound).addListener(ChannelFutureListener.CLOSE);
-            } else if (request.method() != HttpMethod.GET && request.method() !=
HttpMethod.POST) {
-                DefaultHttpResponse notAllowed =
-                        new DefaultHttpResponse(request.protocolVersion(), HttpResponseStatus.METHOD_NOT_ALLOWED);
-                ctx.write(notAllowed).addListener(ChannelFutureListener.CLOSE);
             } else {
                 handler = new HttpRequestHandler(ctx, servlet, HttpUtil.toServletRequest(request),
chunkSize);
                 server.getExecutor().submit(handler);
diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/PostRequest.java
b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/PostRequest.java
index 29cfd89..1dcb088 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/PostRequest.java
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/PostRequest.java
@@ -18,31 +18,63 @@
  */
 package org.apache.hyracks.http.server;
 
+import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import org.apache.hyracks.http.api.IServletRequest;
 import org.apache.hyracks.http.server.utils.HttpUtil;
 
 import io.netty.handler.codec.http.FullHttpRequest;
+import io.netty.handler.codec.http.QueryStringDecoder;
+import io.netty.handler.codec.http.multipart.Attribute;
+import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
+import io.netty.handler.codec.http.multipart.InterfaceHttpData;
+import io.netty.handler.codec.http.multipart.MixedAttribute;
 
-public class PostRequest implements IServletRequest {
-    private final FullHttpRequest request;
+public class PostRequest extends BaseRequest implements IServletRequest {
+
+    private static final Logger LOGGER = Logger.getLogger(PostRequest.class.getName());
+
     private final List<String> names;
     private final List<String> values;
-    private final Map<String, List<String>> parameters;
 
-    public PostRequest(FullHttpRequest request, Map<String, List<String>> parameters,
List<String> names,
-            List<String> values) {
-        this.request = request;
-        this.parameters = parameters;
-        this.names = names;
-        this.values = values;
+    public static IServletRequest create(FullHttpRequest request) throws IOException {
+        List<String> names = new ArrayList<>();
+        List<String> values = new ArrayList<>();
+        HttpPostRequestDecoder decoder = null;
+        try {
+            decoder = new HttpPostRequestDecoder(request);
+        } catch (Exception e) {
+            //ignore. this means that the body of the POST request does not have key value
pairs
+            LOGGER.log(Level.WARNING, "Failed to decode a post message. Fix the API not to
have queries as POST body",
+                    e);
+        }
+        if (decoder != null) {
+            try {
+                List<InterfaceHttpData> bodyHttpDatas = decoder.getBodyHttpDatas();
+                for (InterfaceHttpData data : bodyHttpDatas) {
+                    if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.Attribute))
{
+                        Attribute attr = (MixedAttribute) data;
+                        names.add(data.getName());
+                        values.add(attr.getValue());
+                    }
+                }
+            } finally {
+                decoder.destroy();
+            }
+        }
+        return new PostRequest(request, new QueryStringDecoder(request.uri()).parameters(),
names, values);
     }
 
-    @Override
-    public FullHttpRequest getHttpRequest() {
-        return request;
+    protected PostRequest(FullHttpRequest request, Map<String, List<String>>
parameters, List<String> names,
+            List<String> values) {
+        super(request, parameters);
+        this.names = names;
+        this.values = values;
     }
 
     @Override
@@ -53,10 +85,5 @@
             }
         }
         return HttpUtil.getParameter(parameters, name);
-    }
-
-    @Override
-    public String getHeader(CharSequence name) {
-        return request.headers().get(name);
     }
 }
diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/utils/HttpUtil.java
b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/utils/HttpUtil.java
index aa9ebc5..5ffaee6 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/utils/HttpUtil.java
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/utils/HttpUtil.java
@@ -19,26 +19,18 @@
 package org.apache.hyracks.http.server.utils;
 
 import java.io.IOException;
-import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.apache.hyracks.http.api.IServletRequest;
 import org.apache.hyracks.http.api.IServletResponse;
-import org.apache.hyracks.http.server.GetRequest;
+import org.apache.hyracks.http.server.BaseRequest;
 import org.apache.hyracks.http.server.PostRequest;
 
 import io.netty.handler.codec.http.FullHttpRequest;
 import io.netty.handler.codec.http.HttpHeaderNames;
 import io.netty.handler.codec.http.HttpMethod;
-import io.netty.handler.codec.http.QueryStringDecoder;
-import io.netty.handler.codec.http.multipart.Attribute;
-import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
-import io.netty.handler.codec.http.multipart.InterfaceHttpData;
-import io.netty.handler.codec.http.multipart.InterfaceHttpData.HttpDataType;
-import io.netty.handler.codec.http.multipart.MixedAttribute;
 
 public class HttpUtil {
     private static final Logger LOGGER = Logger.getLogger(HttpUtil.class.getName());
@@ -80,40 +72,8 @@
         }
     }
 
-    public static IServletRequest post(FullHttpRequest request) throws IOException {
-        List<String> names = new ArrayList<>();
-        List<String> values = new ArrayList<>();
-        HttpPostRequestDecoder decoder = null;
-        try {
-            decoder = new HttpPostRequestDecoder(request);
-        } catch (Exception e) {
-            //ignore. this means that the body of the POST request does not have key value
pairs
-            LOGGER.log(Level.WARNING, "Failed to decode a post message. Fix the API not to
have queries as POST body",
-                    e);
-        }
-        if (decoder != null) {
-            try {
-                List<InterfaceHttpData> bodyHttpDatas = decoder.getBodyHttpDatas();
-                for (InterfaceHttpData data : bodyHttpDatas) {
-                    if (data.getHttpDataType().equals(HttpDataType.Attribute)) {
-                        Attribute attr = (MixedAttribute) data;
-                        names.add(data.getName());
-                        values.add(attr.getValue());
-                    }
-                }
-            } finally {
-                decoder.destroy();
-            }
-        }
-        return new PostRequest(request, new QueryStringDecoder(request.uri()).parameters(),
names, values);
-    }
-
-    public static IServletRequest get(FullHttpRequest request) throws IOException {
-        return new GetRequest(request, new QueryStringDecoder(request.uri()).parameters());
-    }
-
     public static IServletRequest toServletRequest(FullHttpRequest request) throws IOException
{
-        return request.method() == HttpMethod.GET ? HttpUtil.get(request) : HttpUtil.post(request);
+        return request.method() == HttpMethod.POST ? PostRequest.create(request) : BaseRequest.create(request);
     }
 
     public static void setContentType(IServletResponse response, String type, String charset)
throws IOException {

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1516
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2d14ce9c3c34d345fe71a44518b1e95b79c37dab
Gerrit-PatchSet: 1
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <tillw@apache.org>

Mime
View raw message