You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
6.4 KiB
Java

2 years ago
package cokr.xit.foundation;
import java.util.Locale;
import java.util.Map;
2 years ago
/** () .
*/
public class Access {
2 years ago
protected static final ThreadLocal<Access> current = new ThreadLocal<>();
private String
id,
sessionID,
action,
ipAddress;
private Map<String, String[]> params;
2 years ago
private boolean
newSession,
mobile,
ajaxRequest,
jsonResponse;
private Locale locale;
private static final String
LOCALHOST_V4 = "127.0.0.1",
LOCALHOST_V6 = "0:0:0:0:0:0:0:1";
/** IP .
* @param address IP
* @param localhost
* @return
* <ul><li> IP </li>
* <li> </li>
* </ul>
*/
public static String getClientAddress(String address, String localhost) {
return LOCALHOST_V4.equals(address) || LOCALHOST_V6.equals(address) ? localhost : address;
}
/** .
* @return
*/
public String getId() {
return Assert.ifEmpty(id, id = "access-" + System.currentTimeMillis());
2 years ago
}
/** .
* @return
*/
public String getSessionID() {
return sessionID;
}
/** .
* @param sessionID
* @return Access
*/
public Access setSessionID(String sessionID) {
this.sessionID = sessionID;
return this;
}
/** (: uri url) .
* @return
*/
public String getAction() {
return action;
}
/** (:uri url) .
* @param action
* @return Access
*/
public Access setAction(String action) {
this.action = action;
return this;
}
/** IP .
* @return IP
*/
public String getIpAddress() {
return ipAddress;
}
/** IP .
* @param ipAddress IP
* @return Access
*/
public Access setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
return this;
}
/** .
* @return
*/
public Map<String, String[]> getParams() {
return params;
}
/** .
* @param params
* @return Access
*/
public Access setParams(Map<String, String[]> params) {
this.params = params;
return this;
}
2 years ago
/** Access .
* @return Access
*/
public static Access current() {
return Assert.ifEmpty(current.get(), Access::new);
2 years ago
}
/** Access .
* @return Access
*/
public Access setCurrent() {
current.set(this);
return this;
}
/** Access .
* @return
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
public boolean inNewSession() {
return newSession;
}
/** Access .
* @param newSession
* <ul><li> true</li>
* <li> false</li>
* </ul>
* @return Access
*/
public Access setNewSession(boolean newSession) {
this.newSession = newSession;
return this;
}
/** .
* @return
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
public boolean isMobile() {
return mobile;
}
/** .
* @param mobile
* <ul><li> true</li>
* <li> false</li>
* </ul>
* @return Access
*/
public Access setMobile(boolean mobile) {
this.mobile = mobile;
return this;
}
/** AJAX .
* @return
* <ul><li>AJAX true</li>
* <li> false</li>
* </ul>
*/
public boolean isAjaxRequest() {
return ajaxRequest;
}
/** AJAX .
* @param header HTTP 'X-Requested-With'
* @return Access
*/
public Access setAjaxRequest(String header) {
ajaxRequest = "XMLHttpRequest".equalsIgnoreCase(header);
return this;
}
/** JSON .
* @return
* <ul><li>JSON true</li>
* <li> false</li>
* </ul>
*/
public boolean isJsonResponse() {
return jsonResponse;
}
/** JSON .
* @param header HTTP 'accept'
* @return Access
*/
public Access setJsonResponse(String header) {
this.jsonResponse = !Assert.isEmpty(header) && header.contains("json");
2 years ago
return this;
}
/** Locale .
* @return Locale
*/
public Locale getLocale() {
return locale != null ? locale : Locale.getDefault();
}
/** Locale .
* @param locale Locale
* @return Access
*/
public Access setLocale(Locale locale) {
this.locale = locale;
return this;
}
/** .
* @return
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
public boolean isDebug() {
return "true".equals(System.getProperty("debug"));
}
/** Access .
* @return Access
*/
public static Access release() {
Access access = current.get();
current.remove();
return access;
}
@Override
public String toString() {
return String.format("%s(id:'%s', clientIP:'%s', action:'%s')", getClass().getSimpleName(), getId(), getIpAddress(), getAction());
}
2 years ago
}