liucongjy commented on code in PR #9951:
URL: https://github.com/apache/seatunnel/pull/9951#discussion_r2443755319


##########
seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/rest/servlet/PageBaseServlet.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.seatunnel.engine.server.rest.servlet;
+
+import com.hazelcast.internal.json.JsonArray;
+import com.hazelcast.internal.json.JsonObject;
+import com.hazelcast.spi.impl.NodeEngineImpl;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.io.IOException;
+import java.util.Map;
+
+public class PageBaseServlet extends BaseServlet {
+    private final String pageParam = "page";
+    private final String rowsParam = "rows";
+
+    public PageBaseServlet(NodeEngineImpl nodeEngine) {
+        super(nodeEngine);
+    }
+
+    protected void writeJsonWithPagination(
+            HttpServletRequest req, HttpServletResponse resp, JsonArray 
jsonArray)
+            throws IOException {
+        int total = jsonArray.size();
+
+        // fetch pagination params, if page exist, then paginate 
data,pagination data format like:
+        // {"data": [], "total": 10}
+        Map<String, String> parameterMap = getParameterMap(req);
+        if (parameterMap != null && parameterMap.containsKey(pageParam)) {
+            int page = Integer.parseInt(parameterMap.get(pageParam));
+            int rows =
+                    parameterMap.get(rowsParam) != null
+                            ? Integer.parseInt(parameterMap.get(rowsParam))
+                            : 10;
+            int start = (page - 1) * rows;
+            JsonArray paginatedArray = new JsonArray();
+            jsonArray
+                    .values()
+                    .subList(start, Math.min(start + rows, total))
+                    .forEach(
+                            t -> {
+                                paginatedArray.add(t);
+                            });

Review Comment:
   The validation been added.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to