diff options
Diffstat (limited to 'src/main/java/org/onap/msb/sdk/discovery/util')
4 files changed, 447 insertions, 0 deletions
diff --git a/src/main/java/org/onap/msb/sdk/discovery/util/HttpClientUtil.java b/src/main/java/org/onap/msb/sdk/discovery/util/HttpClientUtil.java new file mode 100644 index 0000000..b3bcf0b --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/util/HttpClientUtil.java @@ -0,0 +1,182 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. and others. + * + * Licensed 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.onap.msb.sdk.discovery.util; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; + +import org.apache.http.NameValuePair; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; +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.utils.URLEncodedUtils; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; +import org.onap.msb.sdk.discovery.common.RouteConst; +import org.onap.msb.sdk.discovery.common.RouteException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HttpClientUtil { + + private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class); + + public static String httpPostWithJSON(String url, String params) throws RouteException { + String result = null; + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(url); + httpPost.addHeader("Content-type", "application/json; charset=utf-8"); + httpPost.setHeader("Accept", "application/json"); + httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8"))); + try { + CloseableHttpResponse res = httpClient.execute(httpPost); + result = EntityUtils.toString(res.getEntity()); + if (res.getStatusLine().getStatusCode() != RouteConst.SC_POST_OK) { + throw new RouteException(result, "SERVICE_GET_ERR"); + } + res.close(); + } catch (IOException e) { + String errorMsg = url + ":httpPostWithJSON connect faild"; + throwsRouteException(errorMsg, e, "POST_CONNECT_FAILD"); + } finally { + try { + httpClient.close(); + } catch (IOException e) { + String errorMsg = url + ":close httpClient faild"; + throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD"); + } + } + + return result; + + } + + public static void delete(String url, String parameter) throws RouteException { + String result = null; + String baseUrl; + if (parameter != null) { + List<NameValuePair> params = new ArrayList<NameValuePair>(); + params.add(new BasicNameValuePair("serviceName", parameter)); + baseUrl = url + "?" + URLEncodedUtils.format(params, "UTF-8"); + } else { + baseUrl = url; + } + + CloseableHttpClient httpClient = HttpClients.createDefault();; + try { + + HttpDelete httpDelete = new HttpDelete(baseUrl); + CloseableHttpResponse res = httpClient.execute(httpDelete); + + if (res.getStatusLine().getStatusCode() != RouteConst.SC_DEL_OK) { + throw new RouteException(EntityUtils.toString(res.getEntity()), "SERVICE_DELETE_ERR"); + } + + res.close(); + } catch (IOException e) { + String errorMsg = baseUrl + ":delete connect faild"; + throwsRouteException(errorMsg, e, "DELETE_CONNECT_FAILD"); + } finally { + try { + httpClient.close(); + } catch (IOException e) { + String errorMsg = baseUrl + ":close httpClient faild"; + throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD"); + } + } + + + } + + public static String httpGet(String url) throws RouteException { + String result = null; + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet(url); + httpGet.addHeader("Content-type", "application/json; charset=utf-8"); + httpGet.setHeader("Accept", "application/json"); + try { + CloseableHttpResponse res = httpClient.execute(httpGet); + result = EntityUtils.toString(res.getEntity()); + if (res.getStatusLine().getStatusCode() != RouteConst.SC_OK) { + throw new RouteException(result, "SERVICE_GET_ERR"); + } + res.close(); + } catch (ClientProtocolException e) { + String errorMsg = url + ":httpGetWithJSON connect faild"; + throwsRouteException(errorMsg, e, "GET_CONNECT_FAILD"); + } catch (IOException e) { + String errorMsg = url + ":httpGetWithJSON connect faild"; + throwsRouteException(errorMsg, e, "GET_CONNECT_FAILD"); + } finally { + try { + httpClient.close(); + } catch (IOException e) { + String errorMsg = url + ":close httpClient faild"; + throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD"); + } + } + + return result; + + } + + + public static String httpPutWithJSON(String url, String params) throws RouteException { + String result = null; + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPut httpPut = new HttpPut(url); + httpPut.addHeader("Content-type", "application/json; charset=utf-8"); + httpPut.setHeader("Accept", "application/json"); + httpPut.setEntity(new StringEntity(params, Charset.forName("UTF-8"))); + try { + CloseableHttpResponse res = httpClient.execute(httpPut); + result = EntityUtils.toString(res.getEntity()); + if (res.getStatusLine().getStatusCode() != RouteConst.SC_POST_OK) { + throw new RouteException(result, "SERVICE_GET_ERR"); + } + res.close(); + } catch (IOException e) { + String errorMsg = url + ":httpPostWithJSON connect faild"; + throwsRouteException(errorMsg, e, "POST_CONNECT_FAILD"); + } finally { + try { + httpClient.close(); + } catch (IOException e) { + String errorMsg = url + ":close httpClient faild"; + throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD"); + } + } + + return result; + + } + + + + private static void throwsRouteException(String errorMsg, Exception e, String errorCode) + throws RouteException { + String msg = errorMsg + ".errorMsg:" + e.getMessage(); + logger.error(msg); + throw new RouteException(errorMsg, errorCode); + } + +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/util/JacksonJsonUtil.java b/src/main/java/org/onap/msb/sdk/discovery/util/JacksonJsonUtil.java new file mode 100644 index 0000000..382dc64 --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/util/JacksonJsonUtil.java @@ -0,0 +1,132 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. and others. + * + * Licensed 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. + ******************************************************************************/ +/** + * Copyright (C) 2015 ZTE, Inc. and others. All rights reserved. (ZTE) + * + * Licensed 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.onap.msb.sdk.discovery.util; + +import org.onap.msb.sdk.discovery.common.RouteException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JacksonJsonUtil { + + private static final Logger logger = LoggerFactory.getLogger(JacksonJsonUtil.class); + + private static ObjectMapper mapper; + + /** + * 获取ObjectMapper实例 + * + * @param createNew 方式:true,新实例;false,存在的mapper实例 + * @return + */ + public static synchronized ObjectMapper getMapperInstance() { + if (mapper == null) { + mapper = new ObjectMapper(); + } + return mapper; + } + + /** + * 将java对象转换成json字符串 + * + * @param obj 准备转换的对象 + * @return json字符串 + * @throws Exception + */ + public static String beanToJson(Object obj) throws RouteException { + String json = null; + try { + ObjectMapper objectMapper = getMapperInstance(); + json = objectMapper.writeValueAsString(obj); + } catch (Exception e) { + String errorMsg = "Class beanToJson faild"; + throwsRouteException(errorMsg, e, "BEAN_TO_JSON_FAILD"); + } + return json; + } + + + + /** + * 将json字符串转换成java对象 + * + * @param json 准备转换的json字符串 + * @param cls 准备转换的类 + * @return + * @throws Exception + */ + public static Object jsonToBean(String json, Class<?> cls) throws RouteException { + Object vo = null; + try { + ObjectMapper objectMapper = getMapperInstance(); + + + vo = objectMapper.readValue(json, cls); + + } catch (Exception e) { + String errorMsg = cls + " JsonTobean faild:" + e.getMessage(); + throwsRouteException(errorMsg, e, "JSON_TO_BEAN_FAILD"); + } + return vo; + } + + + /** + * 将json字符串转换成java集合对象 + * + * @param json 准备转换的json字符串 + * @param cls 准备转换的类 + * @return + * @throws Exception + */ + + public static <T> T jsonToListBean(String json, TypeReference<T> valueTypeRef) { + try { + + ObjectMapper objectMapper = getMapperInstance(); + + + return objectMapper.readValue(json, valueTypeRef); + + } catch (Exception e) { + String errorMsg = " JsonTobean faild:" + e.getMessage(); + logger.error(errorMsg); + } + return null; + } + + + + private static void throwsRouteException(String errorMsg, Exception e, String errorCode) + throws RouteException { + String msg = errorMsg + ".errorMsg:" + e.getMessage(); + logger.error(msg); + throw new RouteException(errorMsg, errorCode); + } +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/util/MsbUtil.java b/src/main/java/org/onap/msb/sdk/discovery/util/MsbUtil.java new file mode 100644 index 0000000..2361d9f --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/util/MsbUtil.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. and others. + * + * Licensed 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.onap.msb.sdk.discovery.util; + +import java.util.HashSet; +import java.util.Set; + +import org.apache.commons.lang3.StringUtils; +import org.onap.msb.sdk.discovery.entity.Node; +import org.onap.msb.sdk.discovery.entity.NodeInfo; + +/** + * @ClassName: MsbUtil + * @Description: TODO(msb功能工具方法类) + * @author tanghua10186366 + * @date 2017年6月26日 + * + */ +public class MsbUtil { + + + /** + * @Title getConsulServiceName + * @Description TODO(通过服务名和命名空间组装conusl存储名,用于服务变化监听) + * @param serviceName + * @param namespace + * @return String + */ + public static String getConsulServiceName(String serviceName, String namespace) { + if (StringUtils.isEmpty(namespace)) { + return serviceName; + } else { + return serviceName + "-" + namespace; + } + } + + public static Set<NodeInfo> getLbNodes(Node lbNode) { + Set<NodeInfo> nodes = new HashSet<NodeInfo>(); + NodeInfo nodeInfo = new NodeInfo(); + nodeInfo.setIp(lbNode.getIp()); + nodeInfo.setPort(lbNode.getPort()); + nodes.add(nodeInfo); + + return nodes; + } + +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/util/RegExpTestUtil.java b/src/main/java/org/onap/msb/sdk/discovery/util/RegExpTestUtil.java new file mode 100644 index 0000000..9afeb7c --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/util/RegExpTestUtil.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. and others. + * + * Licensed 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.onap.msb.sdk.discovery.util; + +import java.util.regex.Pattern; + +public class RegExpTestUtil { + + public static boolean serviceNameRegExpTest(String serviceName) { + + String serviceNameReg = "^([0-9a-zA-Z]|-|_)*$"; + return Pattern.matches(serviceNameReg, serviceName); + + } + + + public static boolean hostRegExpTest(String host) { + + String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)" + ":(\\d{1,5})$"; + return Pattern.matches(hostReg, host); + + } + + public static boolean ipRegExpTest(String ip) { + + String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"; + return Pattern.matches(hostReg, ip); + + } + + public static boolean portRegExpTest(String port) { + + String hostReg = "^\\d{1,5}$"; + return Pattern.matches(hostReg, port); + + } + + public static boolean versionRegExpTest(String version) { + + String versionReg = "^v\\d+(\\.\\d+)?$"; + return Pattern.matches(versionReg, version); + + } + + public static boolean urlRegExpTest(String url) { + if (url.equals("/")) + return true; + String urlReg = "^\\/.*((?!\\/).)$"; + return Pattern.matches(urlReg, url); + + } + + + + public static void main(String[] args) { + System.out.println(urlRegExpTest("/")); + } +} |