
ars.invoke.request.StandardRequester Maven / Gradle / Ivy
The newest version!
package ars.invoke.request;
import java.util.Set;
import java.util.Map;
import java.util.Date;
import java.util.UUID;
import java.util.Locale;
import java.util.Collections;
import ars.util.Strings;
import ars.invoke.Router;
import ars.invoke.Channel;
import ars.invoke.Context;
import ars.invoke.Messager;
/**
* 请求对象标准实现
*
* @author wuyongqiang
*/
public class StandardRequester implements Requester {
private static final long serialVersionUID = 1L;
private String id; // 请求标识
private String uri; // 资源地址
private String host; // 客户主机
private Token token; // 请求令牌
private Locale locale; // 客户语言环境
private String client; // 客户端标识
private Requester parent; // 父级请求对象
private transient Channel channel; // 请求通道
private Map parameters; // 请求参数
private Date created = new Date(); // 请求时间
public StandardRequester(Channel channel, Requester parent, Locale locale, String client, String host, Token token,
String uri, Map parameters) {
if (channel == null) {
throw new IllegalArgumentException("Channel must not be null");
}
if (locale == null) {
throw new IllegalArgumentException("Locale must not be null");
}
if (Strings.isEmpty(client)) {
throw new IllegalArgumentException("Client must not be null");
}
if (Strings.isEmpty(host)) {
throw new IllegalArgumentException("Host must not be null");
}
if (Strings.isEmpty(uri)) {
throw new IllegalArgumentException("Uri must not be null");
}
if (parameters == null) {
throw new IllegalArgumentException("Parameters must not be null");
}
this.id = UUID.randomUUID().toString();
this.uri = uri;
this.host = host;
this.token = token;
this.locale = locale;
this.client = client;
this.parent = parent;
this.channel = channel;
this.parameters = Collections.unmodifiableMap(parameters);
}
@Override
public Channel getChannel() {
return this.channel;
}
@Override
public Requester getRoot() {
return this.parent == null ? this : this.parent.getRoot();
}
@Override
public Requester getParent() {
return this.parent;
}
@Override
public Session getSession() {
return this.channel.getContext().getSessionFactory().getSession(this);
}
@Override
public Date getCreated() {
return this.created;
}
@Override
public String getId() {
return this.id;
}
@Override
public String getUri() {
return uri;
}
@Override
public String getUser() {
return this.token == null ? null : this.token.getAudience();
}
@Override
public Token getToken() {
return this.token;
}
@Override
public String getHost() {
return host;
}
@Override
public String getClient() {
return this.client;
}
@Override
public Locale getLocale() {
return locale;
}
@Override
public Set getParameterNames() {
return this.parameters.keySet();
}
@Override
public boolean hasParameter(String key) {
return this.parameters.containsKey(key);
}
@Override
public Object getParameter(String key) {
return this.parameters.get(key);
}
@Override
public Map getParameters() {
return this.parameters;
}
@Override
public Requester build(String uri) {
return this.build(uri, this.parameters);
}
@Override
public Requester build(Map parameters) {
return this.build(this.uri, parameters);
}
@Override
public Requester build(String uri, Map parameters) {
return new StandardRequester(this.channel, this, this.locale, this.client, this.host, this.token, uri,
parameters);
}
@Override
public Object execute() {
Context context = this.channel.getContext();
if (context == null) {
throw new RuntimeException("Context has not been initialize");
}
Router router = context.getRouter();
if (router == null) {
throw new RuntimeException("Router has not been initialize");
}
return router.routing(this);
}
@Override
public Object execute(String uri) {
return this.execute(uri, this.parameters);
}
@Override
public Object execute(Map parameters) {
return this.execute(this.uri, parameters);
}
@Override
public Object execute(String uri, Map parameters) {
return this.build(uri, parameters).execute();
}
@Override
public String format(String key) {
return this.format(key, null, key);
}
@Override
public String format(String key, String text) {
return this.format(key, null, text);
}
@Override
public String format(String key, Object[] args) {
return this.format(key, args, key);
}
@Override
public String format(String key, Object[] args, String text) {
Messager messager = this.channel.getContext().getMessager();
return messager == null ? key : messager.format(this.locale, key, args, text);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy