package com.appengine.auth.annotation;

import com.appengine.auth.model.AuthExcepFactor;
import com.appengine.common.exception.ExcepFactor;
import com.appengine.frame.context.RequestContext;

import java.util.Map;

/**
 * @author sofn
 * @version 1.0 Created at: 2015-10-08 12:03
 */
public enum RateLimitType {
    IP(AuthExcepFactor.E_IP_RATE_LIMIT),
    USER(AuthExcepFactor.E_USER_RATE_LIMIT),
    PARAM(AuthExcepFactor.E_API_RATE_LIMIT);

    ExcepFactor excepFactor;

    RateLimitType(ExcepFactor excepFactor) {
        this.excepFactor = excepFactor;
    }

    public ExcepFactor getExcepFactor() {
        return excepFactor;
    }

    public String getParamString(RequestContext rc) {
        switch (this) {
            case IP:
                return "i_" + rc.getIp();
            case USER:
                return "u_" + rc.getCurrentUid();
            case PARAM:
                return "p_" + getRequestParamString(rc.getOriginRequest().getParameterMap());
            default:
                return "error";
        }
    }

    private String getRequestParamString(Map<String, String[]> parameters) {
        StringBuilder paramBuf = new StringBuilder();

        for (Map.Entry<String, String[]> e : parameters.entrySet()) {
            String key = e.getKey();
            String[] values = e.getValue();
            for (String value : values) {
                paramBuf.append(key).append(":").append(value);
                paramBuf.append("_");
            }
        }
        if (paramBuf.length() > 0 && paramBuf.charAt(paramBuf.length() - 1) == '_') {
            paramBuf.deleteCharAt(paramBuf.length() - 1);
        }
        return paramBuf.toString();
    }


}

@@ -350,20 +350,118 @@
 T),%0A    
-USER
+IPMETHOD(AuthExcepFactor.E_IP_RATE_LIMIT),%0A    USER(AuthExcepFactor.E_USER_RATE_LIMIT),%0A    USERMETHOD
 (AuthExc
@@ -896,41 +896,269 @@
 ase 
-USER:%0A                return %22u_%22
+IPMETHOD:%0A                return %22i_%22 + rc.getOriginRequest().getRequestURI() + rc.getIp();%0A            case USER:%0A                return %22u_%22 + rc.getCurrentUid();%0A            case USERMETHOD:%0A                return %22u_%22 + rc.getOriginRequest().getRequestURI()
  + r

添加两个枚举类型，使接口频率限制精确到方法