fuweng11 commented on code in PR #8075: URL: https://github.com/apache/inlong/pull/8075#discussion_r1201904173
########## inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/utils/HttpContextUtils.java: ########## @@ -0,0 +1,123 @@ +/* + * 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.inlong.manager.web.utils; + +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +/** + * HttpContextUtils + */ +public class HttpContextUtils { + + /** + * Get request params + * @param request + * @return + */ + public static Map<String, String> getParameterMapAll(HttpServletRequest request) { + Enumeration<String> parameters = request.getParameterNames(); + + Map<String, String> params = new HashMap<>(); + while (parameters.hasMoreElements()) { + String parameter = parameters.nextElement(); + String value = request.getParameter(parameter); + params.put(parameter, value); + } + return params; + } + + public static Map<String, String[]> getParameterMap(HttpServletRequest request) { + Map<String, String[]> paramMap = new HashMap<>(); + String queryString = request.getQueryString(); + if (queryString != null && queryString.trim().length() > 0) { + String[] params = queryString.split("&"); Review Comment: Please use List. ########## inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/utils/HttpContextUtils.java: ########## @@ -0,0 +1,123 @@ +/* + * 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.inlong.manager.web.utils; + +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +/** + * HttpContextUtils + */ +public class HttpContextUtils { + + /** + * Get request params + * @param request + * @return + */ + public static Map<String, String> getParameterMapAll(HttpServletRequest request) { + Enumeration<String> parameters = request.getParameterNames(); + + Map<String, String> params = new HashMap<>(); + while (parameters.hasMoreElements()) { + String parameter = parameters.nextElement(); + String value = request.getParameter(parameter); + params.put(parameter, value); + } + return params; + } + + public static Map<String, String[]> getParameterMap(HttpServletRequest request) { + Map<String, String[]> paramMap = new HashMap<>(); + String queryString = request.getQueryString(); + if (queryString != null && queryString.trim().length() > 0) { Review Comment: Suggest use `StringUtils.isNotBlank` ########## inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/utils/InlongRequestWrapper.java: ########## @@ -0,0 +1,145 @@ +/* + * 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.inlong.manager.web.utils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import lombok.extern.slf4j.Slf4j; + +import javax.servlet.ReadListener; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +/** + * Inlong http request wrapper + */ +@Slf4j +public class InlongRequestWrapper extends HttpServletRequestWrapper { + + private static final ObjectMapper mapper = new ObjectMapper(); + + private String bodyParams; + + private Map<String, String[]> params; + + private Map<String, String> headers; + + public InlongRequestWrapper(HttpServletRequest request) { + super(request); + this.bodyParams = HttpContextUtils.getBodyString(request); + this.params = HttpContextUtils.getParameterMap(request); + this.headers = HttpContextUtils.getHeaderMapAll(request); + log.info("body={}, params={}, headers={}", bodyParams, params, headers); Review Comment: Whether info logs are required. Maybe `debug`? ########## inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/utils/HttpContextUtils.java: ########## @@ -0,0 +1,123 @@ +/* + * 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.inlong.manager.web.utils; + +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +/** + * HttpContextUtils + */ +public class HttpContextUtils { + + /** + * Get request params + * @param request + * @return + */ + public static Map<String, String> getParameterMapAll(HttpServletRequest request) { + Enumeration<String> parameters = request.getParameterNames(); + + Map<String, String> params = new HashMap<>(); + while (parameters.hasMoreElements()) { + String parameter = parameters.nextElement(); + String value = request.getParameter(parameter); + params.put(parameter, value); + } + return params; + } + + public static Map<String, String[]> getParameterMap(HttpServletRequest request) { + Map<String, String[]> paramMap = new HashMap<>(); + String queryString = request.getQueryString(); + if (queryString != null && queryString.trim().length() > 0) { + String[] params = queryString.split("&"); + for (int i = 0; i < params.length; i++) { + int splitIndex = params[i].indexOf("="); + if (splitIndex == -1) { + continue; + } + String key = params[i].substring(0, splitIndex); + if (!paramMap.containsKey(key)) { + if (splitIndex < params[i].length()) { + String value = params[i].substring(splitIndex + 1); + paramMap.put(key, new String[]{value}); + } + } + } + } + return paramMap; + } + + public static Map<String, String> getHeaderMapAll(HttpServletRequest request) { + Enumeration<String> headerNames = request.getHeaderNames(); + Map<String, String> headers = new HashMap<>(); + while (headerNames.hasMoreElements()) { + String parameter = headerNames.nextElement(); + String value = request.getHeader(parameter); + headers.put(parameter, value); + } + return headers; + } + + /** + * Get request body + * @param request + * @return + */ + public static String getBodyString(ServletRequest request) { + StringBuilder sb = new StringBuilder(); + InputStream inputStream = null; + BufferedReader reader = null; + try { + inputStream = request.getInputStream(); + reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); Review Comment: Use` try resource` -- 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]
