yfsn666 commented on code in PR #10837: URL: https://github.com/apache/inlong/pull/10837#discussion_r1726516317
########## inlong-sort-standalone/sort-standalone-source/src/main/java/org/apache/inlong/sort/standalone/sink/http/DefaultEvent2HttpRequestHandler.java: ########## @@ -0,0 +1,137 @@ +/* + * 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.sort.standalone.sink.http; + +import org.apache.inlong.sort.standalone.channel.ProfileEvent; +import org.apache.inlong.sort.standalone.utils.InlongLoggerFactory; +import org.apache.inlong.sort.standalone.utils.UnescapeHelper; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.slf4j.Logger; + +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class DefaultEvent2HttpRequestHandler implements IEvent2HttpRequestHandler { + + public static final Logger LOG = InlongLoggerFactory.getLogger(DefaultEvent2HttpRequestHandler.class); + private static final ObjectMapper objectMapper = new ObjectMapper(); + + @Override + public HttpRequest parse(HttpSinkContext context, ProfileEvent event) + throws URISyntaxException, JsonProcessingException { + String uid = event.getUid(); + HttpIdConfig idConfig = context.getIdConfig(uid); + if (idConfig == null) { + context.addSendResultMetric(event, context.getTaskName(), false, System.currentTimeMillis()); + return null; + } + // get the delimiter + String delimiter = idConfig.getSeparator(); + char cDelimiter = delimiter.charAt(0); + // for tab separator + byte[] bodyBytes = event.getBody(); + String strContext = new String(bodyBytes, Charset.defaultCharset()); + // unescape + List<String> columnValues = UnescapeHelper.toFiledList(strContext, cDelimiter); + int valueLength = columnValues.size(); + List<String> fieldList = idConfig.getFieldList(); + int columnLength = fieldList.size(); + // get field value + Map<String, String> fieldMap = new HashMap<>(); + for (int i = 0; i < columnLength; ++i) { + String fieldName = fieldList.get(i); + String fieldValue = i < valueLength ? columnValues.get(i) : ""; + fieldMap.put(fieldName, fieldValue); + } + + // build + String uriString = context.getBaseUrl() + idConfig.getPath(); + if (!uriString.startsWith("http://") && !uriString.startsWith("https://")) { Review Comment: fixed. ########## inlong-sort-standalone/sort-standalone-source/src/main/java/org/apache/inlong/sort/standalone/sink/http/DefaultEvent2HttpRequestHandler.java: ########## @@ -0,0 +1,137 @@ +/* + * 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.sort.standalone.sink.http; + +import org.apache.inlong.sort.standalone.channel.ProfileEvent; +import org.apache.inlong.sort.standalone.utils.InlongLoggerFactory; +import org.apache.inlong.sort.standalone.utils.UnescapeHelper; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.slf4j.Logger; + +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class DefaultEvent2HttpRequestHandler implements IEvent2HttpRequestHandler { + + public static final Logger LOG = InlongLoggerFactory.getLogger(DefaultEvent2HttpRequestHandler.class); + private static final ObjectMapper objectMapper = new ObjectMapper(); + + @Override + public HttpRequest parse(HttpSinkContext context, ProfileEvent event) + throws URISyntaxException, JsonProcessingException { + String uid = event.getUid(); + HttpIdConfig idConfig = context.getIdConfig(uid); + if (idConfig == null) { + context.addSendResultMetric(event, context.getTaskName(), false, System.currentTimeMillis()); + return null; + } + // get the delimiter + String delimiter = idConfig.getSeparator(); + char cDelimiter = delimiter.charAt(0); + // for tab separator + byte[] bodyBytes = event.getBody(); + String strContext = new String(bodyBytes, Charset.defaultCharset()); + // unescape + List<String> columnValues = UnescapeHelper.toFiledList(strContext, cDelimiter); + int valueLength = columnValues.size(); + List<String> fieldList = idConfig.getFieldList(); + int columnLength = fieldList.size(); + // get field value + Map<String, String> fieldMap = new HashMap<>(); + for (int i = 0; i < columnLength; ++i) { + String fieldName = fieldList.get(i); + String fieldValue = i < valueLength ? columnValues.get(i) : ""; + fieldMap.put(fieldName, fieldValue); + } + + // build + String uriString = context.getBaseUrl() + idConfig.getPath(); + if (!uriString.startsWith("http://") && !uriString.startsWith("https://")) { + uriString = "http://" + uriString; + } + URI uri = new URI(uriString); + String jsonData; + HttpUriRequest request; + switch (idConfig.getMethod().toUpperCase()) { + case "GET": + String params = fieldMap.entrySet().stream() + .map(entry -> { + try { + return URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.name()) + "=" + + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + }) + .collect(Collectors.joining("&")); + request = new HttpGet(uri + "?" + params); + for (Map.Entry<String, String> entry : idConfig.getHeaders().entrySet()) { + request.setHeader(entry.getKey(), entry.getValue()); + } + request.setHeader("InlongGroupID", idConfig.getInlongGroupId()); Review Comment: fixed. -- 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: commits-unsubscr...@inlong.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org