com.alibaba.doris.admin.service.main.DorisConfigUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.alibaba.doris.admin.service.main.DorisConfigUtil.java

Source

/**
 * Project: doris.admin.config.service-0.1.0-SNAPSHOT
 * 
 * File Created at 2011-5-13
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.alibaba.doris.admin.service.main;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

/**
 * @author mian.hem
 *
 */
public class DorisConfigUtil {

    /**
     * <p>
     * ?ip????serviceIP
     * ???.
     * <p>
     * request.getRemoteAddr()?ServiceIP
     * <p>
     * ??Http?x-forwarded-forIP??
     * ??IP?
     * <p>
     * ??forwarded-for?Proxy-Client-IPWL-Proxy-Client-IP??
     * 
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = null;

        ip = request.getHeader("x-forwarded-for");
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        String[] temp = StringUtils.split(ip, ',');
        if (temp.length > 1) {
            for (int i = 0; i < temp.length; i++) {// ?unknown
                if (!"unknown".equalsIgnoreCase(temp[i])) {
                    ip = temp[i];
                    break;
                }
            }
        }

        return ip;
    }

}