use of app.hongs.CoreConfig in project HongsCORE by ihongs.
the class WBAction method inWeb.
@Action("web/create")
public void inWeb(ActionHelper helper) throws HongsException {
CoreConfig cc = CoreConfig.getInstance("oauth2");
String appId = cc.getProperty("oauth2.wb.web.app.id");
String appSk = cc.getProperty("oauth2.wb.web.app.key");
String rurl = cc.getProperty("oauth2.wb.wap.bak.url");
String code = helper.getParameter("code");
if (appId == null || appSk == null) {
helper.error500("Not support this mode");
}
Map info = getUserInfo(code, appId, appSk, rurl);
String opnId = (String) info.get("opnid");
String name = (String) info.get("name");
String head = (String) info.get("head");
AuthKit.openSign(helper, "wb", appId, opnId, name, head, System.currentTimeMillis());
ConnKit.redirect(helper);
}
use of app.hongs.CoreConfig in project HongsCORE by ihongs.
the class ActionHelper method getRequestPart.
/**
* 解析 multipart/form-data 数据, 处理上传
* @return
*/
final Map getRequestPart() {
CoreConfig conf = CoreConfig.getInstance();
// 是否仅登录用户可上传
if (conf.getProperty("core.upload.auth.needs", false) && null == getSessibute(Cnst.UID_SES)) {
throw new HongsExpedient(0x1100, "Multipart is not supported");
}
String x;
Set<String> allowTypes = null;
x = conf.getProperty("fore.upload.allow.types", null);
if (x != null) {
allowTypes = new HashSet(Arrays.asList(x.split(",")));
}
Set<String> denyTypes = null;
x = conf.getProperty("fore.upload.deny.types", null);
if (x != null) {
denyTypes = new HashSet(Arrays.asList(x.split(",")));
}
Set<String> allowExtns = null;
x = conf.getProperty("fore.upload.allow.extns", null);
if (x != null) {
allowExtns = new HashSet(Arrays.asList(x.split(",")));
}
Set<String> denyExtns = null;
x = conf.getProperty("fore.upload.deny.extns", null);
if (x != null) {
denyExtns = new HashSet(Arrays.asList(x.split(",")));
}
try {
Map rd = new HashMap();
Map ud = new HashMap();
for (Part part : request.getParts()) {
long size = part.getSize();
String name = part.getName();
String type = part.getContentType();
String subn = part.getSubmittedFileName();
String extn = subn;
// 无类型的普通参数已在外部处理
if (name == null || type == null || extn == null) {
continue;
}
// 在修改的操作中表示将其置为空
if (size == 0) {
if (null == request.getParameter(name)) {
Dict.setParam(rd, null, name);
}
continue;
}
// 检查类型
int pos = type.indexOf(',');
if (pos != -1) {
type = type.substring(0, pos);
}
if (allowTypes != null && !allowTypes.contains(type)) {
throw new HongsExpedient(0x1100, "Type '" + type + "' is not allowed");
}
if (denyTypes != null && denyTypes.contains(type)) {
throw new HongsExpedient(0x1100, "Type '" + type + "' is denied");
}
// 检查扩展
pos = extn.lastIndexOf('.');
if (pos == -1) {
extn = extn.substring(1 + pos);
} else {
extn = "";
}
if (allowExtns != null && !allowExtns.contains(extn)) {
throw new HongsExpedient(0x1100, "Type '" + extn + "' is not allowed");
}
if (denyExtns != null && denyExtns.contains(extn)) {
throw new HongsExpedient(0x1100, "Type '" + extn + "' is denied");
}
/**
* 临时存储文件
* 不再需要暂存
* 可以直接利用 Part 继续向下传递
*/
/*
String id = Core.getUniqueId();
String temp = path + File.separator + id + ".tmp";
String tenp = path + File.separator + id + ".tnp";
subn = subn.replaceAll("[\\r\\n\\\\/]", ""); // 清理非法字符: 换行和路径分隔符
subn = subn + "\r\n" + type + "\r\n" + size; // 拼接存储信息: 名称和类型及大小
try (
InputStream xmin = part.getInputStream();
FileOutputStream mout = new FileOutputStream(temp);
FileOutputStream nout = new FileOutputStream(tenp);
) {
byte[] nts = subn.getBytes("UTF-8");
byte[] buf = new byte[1024];
int cnt ;
while((cnt = xmin.read(buf)) != -1) {
mout.write(buf, 0, cnt);
}
nout.write(nts);
}
Dict.setParam( rd , id , name );
*/
Dict.setValue(ud, part, name, null);
Dict.setParam(rd, part, name);
}
// 记录在应用里以便有需要时候还可读取原始值
setAttribute(Cnst.UPLOAD_ATTR, ud);
return rd;
} catch (IllegalStateException e) {
// 上传受限, 如大小超标
throw new HongsExpedient(0x1100, e);
} catch (ServletException e) {
throw new HongsExpedient(0x1110, e);
} catch (IOException e) {
throw new HongsExpedient(0x1110, e);
}
}
use of app.hongs.CoreConfig in project HongsCORE by ihongs.
the class ActionDriver method doLaunch.
private void doLaunch(Core core, ActionHelper hlpr, HttpServletRequest req, HttpServletResponse rsp) throws ServletException {
Core.ACTION_TIME.set(System.currentTimeMillis());
Core.CLIENT_ADDR.set(getClientAddr(req));
Core.ACTION_NAME.set(getOriginPath(req).substring(1));
CoreConfig conf = core.get(CoreConfig.class);
Core.ACTION_ZONE.set(conf.getProperty("core.timezone.default", "GMT+8"));
if (conf.getProperty("core.timezone.probing", false)) {
/**
* 时区可以记录到Session/Cookies里
*/
String sess = conf.getProperty("core.timezone.session", "zone");
String zone = (String) hlpr.getSessibute(sess);
if (zone == null || zone.length() == 0) {
zone = (String) hlpr.getCookibute(sess);
if (zone == null || zone.length() == 0) {
zone = req.getHeader(/*Cur*/
"Timezone");
}
}
/**
* 过滤一下避免错误时区
*/
if (zone != null) {
zone = TimeZone.getTimeZone(zone).getID();
// if (zone != null) {
Core.ACTION_ZONE.set(zone);
// }
}
}
Core.ACTION_LANG.set(conf.getProperty("core.language.default", "zh_CN"));
if (conf.getProperty("core.language.probing", false)) {
/**
* 语言可以记录到Session/Cookies里
*/
String sess = conf.getProperty("core.language.session", "lang");
String lang = (String) hlpr.getSessibute(sess);
if (lang == null || lang.length() == 0) {
lang = (String) hlpr.getCookibute(sess);
if (lang == null || lang.length() == 0) {
lang = req.getHeader("Accept-Language");
}
}
/**
* 检查是否是支持的语言
*/
if (lang != null) {
lang = CoreLocale.getAcceptLanguage(lang);
if (lang != null) {
Core.ACTION_LANG.set(lang);
}
}
}
if (!hlpr.getResponse().isCommitted()) {
/**
* 输出特定的服务器信息
*/
String pb;
pb = conf.getProperty("core.powered.by");
if (pb != null && pb.length() != 0) {
rsp.setHeader("X-Powered-By", pb);
}
pb = conf.getProperty("core.service.by");
if (pb != null && pb.length() != 0) {
rsp.setHeader("Server", pb);
}
}
}
use of app.hongs.CoreConfig in project HongsCORE by ihongs.
the class ActionDriver method doFinish.
private void doFinish(Core core, ActionHelper hlpr, HttpServletRequest req) {
try {
if (0 < Core.DEBUG && 8 != (8 & Core.DEBUG)) {
/**
* 提取必要的客户相关标识
* 以便判断用户和模拟登录
*/
req = hlpr.getRequest();
HttpSession ses = req.getSession(false);
Object uid = hlpr.getSessibute(Cnst.UID_SES);
String mem;
if (ses == null) {
mem = "-";
} else {
mem = ses.getId();
}
if (uid != null) {
mem += " " + uid;
}
long time = System.currentTimeMillis() - Core.ACTION_TIME.get();
StringBuilder sb = new StringBuilder("...");
sb.append("\r\n\tACTION_NAME : ").append(Core.ACTION_NAME.get()).append("\r\n\tACTION_TIME : ").append(Core.ACTION_TIME.get()).append("\r\n\tACTION_LANG : ").append(Core.ACTION_LANG.get()).append("\r\n\tACTION_ZONE : ").append(Core.ACTION_ZONE.get()).append("\r\n\tMethod : ").append(req.getMethod()).append("\r\n\tMember : ").append(mem).append("\r\n\tObjects : ").append(core.toString()).append("\r\n\tRuntime : ").append(Tool.humanTime(time));
/**
* 显示请求报头及输入输出
* 这对调试程序非常有帮助
*/
CoreConfig cf = CoreConfig.getInstance();
if (cf.getProperty("core.debug.action.request", false)) {
Map rd = null;
try {
rd = hlpr.getRequestData();
} catch (HongsExpedient ex) {
CoreLogger.debug(ex.getMessage());
}
if (rd != null && !rd.isEmpty()) {
sb.append("\r\n\tRequest : ").append(Tool.indent(Data.toString(rd)).substring(1));
}
}
if (cf.getProperty("core.debug.action.results", false)) {
Map xd = hlpr.getResponseData();
if (xd == null) {
xd = (Map) req.getAttribute(Cnst.RESP_ATTR);
}
if (xd != null && !xd.isEmpty()) {
sb.append("\r\n\tResults : ").append(Tool.indent(Data.toString(xd)).substring(1));
}
}
if (cf.getProperty("core.debug.action.session", false) && ses != null) {
Map map = new HashMap();
Enumeration<String> nms = ses.getAttributeNames();
while (nms.hasMoreElements()) {
String nme = nms.nextElement();
map.put(nme, ses.getAttribute(nme));
}
if (!map.isEmpty()) {
sb.append("\r\n\tSession : ").append(Tool.indent(Data.toString(map)).substring(1));
}
}
if (cf.getProperty("core.debug.action.context", false)) {
Map map = new HashMap();
Enumeration<String> nms = req.getAttributeNames();
while (nms.hasMoreElements()) {
String nme = nms.nextElement();
map.put(nme, req.getAttribute(nme));
}
if (!map.isEmpty()) {
sb.append("\r\n\tContext : ").append(Tool.indent(Data.toString(map)).substring(1));
}
}
if (cf.getProperty("core.debug.action.headers", false)) {
Map map = new HashMap();
Enumeration<String> nms = req.getHeaderNames();
while (nms.hasMoreElements()) {
String nme = nms.nextElement();
map.put(nme, req.getHeader(nme));
}
if (!map.isEmpty()) {
sb.append("\r\n\tHeaders : ").append(Tool.indent(Data.toString(map)).substring(1));
}
}
if (cf.getProperty("core.debug.action.cookies", false)) {
Map map = new HashMap();
Cookie[] cks = req.getCookies();
for (Cookie cke : cks) {
map.put(cke.getName(), cke.getValue());
}
if (!map.isEmpty()) {
sb.append("\r\n\tCookies : ").append(Tool.indent(Data.toString(map)).substring(1));
}
}
CoreLogger.debug(sb.toString());
}
// 删除上传的临时文件
Map<String, List<Part>> ud = Synt.asMap(hlpr.getAttribute(Cnst.UPLOAD_ATTR));
if (ud != null) {
for (List<Part> pa : ud.values()) {
for (Part pr : pa) {
try {
pr.delete();
} catch (IOException ex) {
CoreLogger.error(ex);
}
}
}
}
} finally {
// 销毁此周期内的对象
try {
core.close();
} catch (Error e) {
CoreLogger.error(e);
} catch (Exception e) {
CoreLogger.error(e);
}
req.removeAttribute(Core.class.getName());
Core.THREAD_CORE.remove();
Core.ACTION_TIME.remove();
Core.ACTION_ZONE.remove();
Core.ACTION_LANG.remove();
Core.ACTION_NAME.remove();
}
}
use of app.hongs.CoreConfig in project HongsCORE by ihongs.
the class ServerCmdlet method start.
@Cmdlet("start")
public static void start(String[] args) throws HongsException {
int port = args.length > 0 ? Integer.parseInt(args[0]) : 8080;
String conf = Core.CORE_PATH + File.separator + "web.xml";
if (!(new File(conf)).exists()) {
conf = Core.CONF_PATH + File.separator + "web.xml";
}
String serd = Core.DATA_PATH + File.separator + "server";
File ppid = new File(serd + File.separator + port + ".pid");
File ppcd = ppid.getParentFile();
// 检查进程
if (ppid.exists() == true) {
CmdletHelper.println("Process already exists!");
return;
}
if (ppcd.exists() == false) {
ppcd.mkdirs();
}
try {
String pid = ManagementFactory.getRuntimeMXBean().getName().split("@", 2)[0];
FileWriter dip = new FileWriter(ppid, true);
dip.write(pid);
dip.close();
} catch (IOException e) {
throw new HongsException.Common(e);
}
// 构建应用
Server server;
WebAppContext webapp;
server = new Server(port);
webapp = new WebAppContext();
webapp.setDescriptor(conf);
webapp.setContextPath(Core.BASE_HREF);
webapp.setResourceBase(Core.BASE_PATH);
webapp.setParentLoaderPriority(true);
server.setHandler(webapp);
// 外部配置
CoreConfig c = CoreConfig.getInstance("defines");
for (Map.Entry t : c.entrySet()) {
String k = (String) t.getKey();
String v = (String) t.getValue();
if (k.startsWith("jetty.attr.")) {
webapp.setAttribute(k.substring(11), v);
} else if (k.startsWith("jetty.para.")) {
webapp.setInitParameter(k.substring(11), v);
}
}
/**
* 初始设置
* 光能外部配置参数还不够方便
* 可能需要替换 JSP 解析器或 Session 容器
* 可以设置 jetty.init 来注入 Initer 对象
*/
String xs = c.getProperty("jetty.init");
if (null != xs) {
String[] xa = xs.split(";");
for (String xn : xa) {
xn = xn.trim();
if ("".equals(xn)) {
continue;
}
try {
((Initer) Class.forName(xn).newInstance()).init(webapp);
} catch (ClassNotFoundException ex) {
throw new HongsError.Common(ex);
} catch (InstantiationException ex) {
throw new HongsError.Common(ex);
} catch (IllegalAccessException ex) {
throw new HongsError.Common(ex);
}
}
}
// 停止机制
Runtime.getRuntime().addShutdownHook(new Stoper(server, ppid));
try {
server.start();
server.join();
} catch (Exception e) {
throw new HongsException.Common(e);
} catch (Error e) {
throw new HongsError.Common(e);
}
}
Aggregations