1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.util;
16 import java.util.regex.Pattern;
18 import javax.servlet.http.HttpServletRequest;
20 import org.apache.commons.lang3.StringUtils;
23 public class MicroServiceUtil {
24 public static final String PREFIX_PATH = "discover:microservices";
26 private static final Pattern SERVICE_KEY_REGEX_PATTERN =
27 Pattern.compile("discover:microservices:(?<servicename>[^:]+)(:(?<version>[^:]*))");
30 public static String getPrefixedKey(String... paths) {
31 StringBuffer sb = new StringBuffer();
33 sb.append(PREFIX_PATH);
35 for (int i = 0; i < paths.length; i++) {
43 public static String getServiceKey(String serviceName, String version) {
44 return getPrefixedKey(serviceName, version);
47 public static Pattern getServiceKeyRegexPattern() {
48 return SERVICE_KEY_REGEX_PATTERN;
53 public static String getRealIp(HttpServletRequest request) {
54 String ip = request.getHeader("X-Forwarded-For");
55 if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
56 // After the reverse proxy can have multiple IP value for many times, the first IP is
58 int index = ip.indexOf(",");
60 return ip.substring(0, index);
65 ip = request.getHeader("X-Real-IP");
67 if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
72 return request.getRemoteAddr();