org.apache.eventmesh.runtime.util.NetUtils Maven / Gradle / Ivy
/*
* 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.eventmesh.runtime.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class NetUtils {
private static final Logger logger = LoggerFactory.getLogger(NetUtils.class);
/**
* Transform the url form string to Map
*
* @param formData
* @return
*/
public static Map formData2Dic(String formData) {
Map result = new HashMap<>();
if (formData == null || formData.trim().length() == 0) {
return result;
}
final String[] items = formData.split("&");
Arrays.stream(items).forEach(item -> {
final String[] keyAndVal = item.split("=");
if (keyAndVal.length == 2) {
try {
final String key = URLDecoder.decode(keyAndVal[0], "utf8");
final String val = URLDecoder.decode(keyAndVal[1], "utf8");
result.put(key, val);
} catch (UnsupportedEncodingException e) {
logger.warn("formData2Dic:param decode failed...", e);
}
}
});
return result;
}
public static String addressToString(List clients) {
if (clients.isEmpty()) {
return "no session had been closed";
}
StringBuilder sb = new StringBuilder();
for (InetSocketAddress addr : clients) {
sb.append(addr).append("|");
}
return sb.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy