use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class LuceneRecord method padQry.
/**
* 组织查询条件
* 可覆盖此方法扩展查询条件
*
* @param qr
* @param rd
* @param r 递归层级
* @throws HongsException
*/
protected void padQry(BooleanQuery.Builder qr, Map rd, int r) throws HongsException {
// 条件数量, 否定数量, 计数规避全否定时查不到数据
int i = 0, j = 0;
Map<String, Map> fields = getFields();
Set<String> ks = new LinkedHashSet(fields.keySet());
ks.retainAll(rd.keySet());
for (String k : ks) {
Object v = rd.get(k);
Map m = fields.get(k);
if (m == null || v == null) {
continue;
}
// 自定义条件
if (!padQry(qr, rd, k, v)) {
continue;
}
IQuery qa;
String t = datatype(m);
if (t != null)
switch(t) {
case "int":
qa = new IntQuery();
break;
case "long":
case "date":
qa = new LongQuery();
break;
case "float":
qa = new FloatQuery();
break;
case "double":
case "number":
qa = new DoubleQuery();
break;
case "string":
case "search":
if (!srchable(m)) {
qa = new StringQuery();
} else {
SearchQuery qs;
qa = qs = new SearchQuery();
qs.analyser(getAnalyser(m));
qs.settings(m);
}
break;
default:
continue;
}
else {
continue;
}
// ** 常规查询 **/
Map vd;
if (v instanceof Map) {
vd = (Map) v;
} else if (v instanceof Collection || v instanceof Object[]) {
Set a = Synt.asSet(v);
a.remove("");
if (!a.isEmpty()) {
BooleanQuery.Builder qx = new BooleanQuery.Builder();
for (Object b : a) {
qx.add(qa.whr(k, b), BooleanClause.Occur.SHOULD);
}
qr.add(qx.build(), BooleanClause.Occur.MUST);
i++;
}
continue;
} else {
if (!v.equals("")) {
qr.add(qa.whr(k, v), BooleanClause.Occur.MUST);
i++;
}
continue;
}
// ** 条件关系 **/
v = vd.get(Cnst.OR_REL);
if (Cnst.OR_KEY.equals(v) || Cnst.NR_KEY.equals(v)) {
List r2 = new ArrayList(vd.size() - 1);
for (Object ot : vd.entrySet()) {
Map.Entry et = (Map.Entry) ot;
Object k2 = et.getKey();
if (!Cnst.OR_KEY.equals(k2)) {
Object v2 = et.getValue();
r2.add(Synt.mapOf(k, Synt.mapOf(k2, v2)));
}
}
if (!r2.isEmpty()) {
padQry(qr, Synt.mapOf(v, r2), r - 1);
}
continue;
}
// ** 空值查询 **/
v = vd.get(Cnst.IS_REL);
if (v != null) {
String b = qa instanceof SearchQuery ? "$" : "@";
String a = Synt.asString(v).toUpperCase();
Query p;
try {
p = new QueryParser(b + k, new StandardAnalyzer()).parse("[* TO *]");
} catch (ParseException e) {
throw new HongsExemption(e);
}
switch(a) {
case "WELL":
case "NOT-NULL":
qr.add(p, BooleanClause.Occur.MUST);
i++;
break;
case "NULL":
case "NOT-WELL":
qr.add(p, BooleanClause.Occur.MUST_NOT);
i++;
j++;
break;
default:
throw new UnsupportedOperationException("Unsupported `is`: " + v);
}
}
// ** 精确匹配 **/
v = vd.get(Cnst.EQ_REL);
if (v != null) {
qr.add(qa.whr(k, v), BooleanClause.Occur.MUST);
i++;
}
v = vd.get(Cnst.NE_REL);
if (v != null) {
qr.add(qa.whr(k, v), BooleanClause.Occur.MUST_NOT);
i++;
j++;
}
// ** 模糊匹配 **/
v = vd.get(Cnst.CQ_REL);
if (v != null && !"".equals(v)) {
qr.add(qa.wdr(k, v), BooleanClause.Occur.MUST);
i++;
}
v = vd.get(Cnst.NC_REL);
if (v != null && !"".equals(v)) {
qr.add(qa.wdr(k, v), BooleanClause.Occur.MUST_NOT);
i++;
j++;
}
// ** 集合查询 **/
v = vd.get(Cnst.AI_REL);
if (v != null) {
Set vs = Synt.asSet(v);
if (!vs.isEmpty()) {
for (Object vv : vs) {
qr.add(qa.whr(k, vv), BooleanClause.Occur.MUST);
i++;
}
}
}
v = vd.get(Cnst.NI_REL);
if (v != null) {
Set vs = Synt.asSet(v);
if (!vs.isEmpty()) {
for (Object vv : vs) {
qr.add(qa.whr(k, vv), BooleanClause.Occur.MUST_NOT);
i++;
j++;
}
}
}
v = vd.get(Cnst.IN_REL);
if (v != null) {
Set vs = Synt.asSet(v);
if (!vs.isEmpty()) {
BooleanQuery.Builder qx = new BooleanQuery.Builder();
for (Object vv : vs) {
qx.add(qa.whr(k, vv), BooleanClause.Occur.SHOULD);
}
qr.add(qx.build(), BooleanClause.Occur.MUST);
i++;
}
}
v = vd.get(Cnst.ON_REL);
if (v != null) {
if (v instanceof Collection || v instanceof Object[]) {
Set a = Synt.asSet(v);
// 与 in 不同
a.remove("");
if (!a.isEmpty()) {
BooleanQuery.Builder qx = new BooleanQuery.Builder();
for (Object b : a) {
qx.add(qa.whr(k, b), BooleanClause.Occur.SHOULD);
}
qr.add(qx.build(), BooleanClause.Occur.MUST);
i++;
}
} else {
if (!v.equals("")) {
qr.add(qa.whr(k, v), BooleanClause.Occur.MUST);
i++;
}
}
}
// ** 区间查询 **/
Object n, x;
boolean l, g;
n = vd.get(Cnst.GT_REL);
if (n != null) {
l = false;
} else {
n = vd.get(Cnst.GE_REL);
if (n != null) {
l = true;
} else {
n = null;
l = true;
}
}
x = vd.get(Cnst.LT_REL);
if (x != null) {
g = false;
} else {
x = vd.get(Cnst.LE_REL);
if (x != null) {
g = true;
} else {
x = null;
g = true;
}
}
if ((n != null && !"".equals(n)) || (x != null && !"".equals(x))) {
Query qu = qa.whr(k, n, x, l, g);
qr.add(qu, BooleanClause.Occur.MUST);
i++;
}
Set s = null;
v = vd.get(Cnst.RG_REL);
if (v != null) {
s = Synt.asSet(v);
}
if (s != null && !s.isEmpty()) {
BooleanQuery.Builder qx = new BooleanQuery.Builder();
for (Object o : s) {
Object[] a = Synt.toRange(o);
if (a == null) {
continue;
}
n = a[0];
l = (boolean) a[2];
x = a[1];
g = (boolean) a[3];
if (n == null && x == null) {
continue;
}
Query qu = qa.whr(k, n, x, l, g);
qx.add(qu, BooleanClause.Occur.SHOULD);
}
BooleanQuery qz = qx.build();
if (qz.clauses().size() > 0) {
qr.add(qz, BooleanClause.Occur.MUST);
i++;
}
}
}
Object v;
// ** 全局搜索 **/
v = rd.get(Cnst.WD_KEY);
if (v != null && !"".equals(v)) {
Set<String> fs = getRschable();
if (fs.size() > 0) {
BooleanQuery.Builder qx = new BooleanQuery.Builder();
SearchQuery qs = new SearchQuery();
for (String fn : fs) {
Map fc = fields.get(fn);
if (fc == null)
continue;
qs.settings(/*parser*/
fc);
qs.analyser(getAnalyser(fc));
qx.add(qs.wdr(fn, v), BooleanClause.Occur.SHOULD);
}
BooleanQuery qa = qx.build();
if (!qa.clauses().isEmpty()) {
qr.add(qa, BooleanClause.Occur.MUST);
i++;
}
}
}
// ** 递归条件 **/
v = rd.get(Cnst.AR_KEY);
if (v != null) {
if (r > 2) {
throw new HongsException(400, "Key '" + Cnst.AR_KEY + "' can not exceed 2 layers");
}
Set<Map> set = Synt.asSet(v);
if (set != null && !set.isEmpty()) {
for (Map map : set) {
// 规避 NullPointerException
if (map == null)
continue;
BooleanQuery.Builder qx = new BooleanQuery.Builder();
padQry(qx, map, r + 1);
BooleanQuery qa = qx.build();
if (!qa.clauses().isEmpty()) {
// 权重
Query qb = qa;
v = map.get(Cnst.WT_REL);
if (v != null && !"".equals(v)) {
qb = new BoostQuery(qa, Synt.declare(v, 1f));
}
qr.add(qb, BooleanClause.Occur.MUST);
i++;
}
}
}
}
v = rd.get(Cnst.NR_KEY);
if (v != null) {
if (r > 2) {
throw new HongsException(400, "Key '" + Cnst.NR_KEY + "' can not exceed 2 layers");
}
Set<Map> set = Synt.asSet(v);
if (set != null && !set.isEmpty()) {
for (Map map : set) {
// 规避 NullPointerException
if (map == null)
continue;
BooleanQuery.Builder qx = new BooleanQuery.Builder();
padQry(qx, map, r + 1);
BooleanQuery qa = qx.build();
if (!qa.clauses().isEmpty()) {
// 权重
Query qb = qa;
v = map.get(Cnst.WT_REL);
if (v != null && !"".equals(v)) {
qb = new BoostQuery(qa, Synt.declare(v, 1f));
}
qr.add(qb, BooleanClause.Occur.MUST_NOT);
i++;
j++;
}
}
}
}
v = rd.get(Cnst.OR_KEY);
if (v != null) {
if (r > 2) {
throw new HongsException(400, "Key '" + Cnst.OR_KEY + "' can not exceed 2 layers");
}
Set<Map> set = Synt.asSet(v);
if (set != null && !set.isEmpty()) {
BooleanQuery.Builder qz = new BooleanQuery.Builder();
for (Map map : set) {
// 规避 NullPointerException
if (map == null)
continue;
BooleanQuery.Builder qx = new BooleanQuery.Builder();
padQry(qx, map, r + 1);
BooleanQuery qa = qx.build();
if (!qa.clauses().isEmpty()) {
// 权重
Query qb = qa;
v = map.get(Cnst.WT_REL);
if (v != null && !"".equals(v)) {
qb = new BoostQuery(qa, Synt.declare(v, 1f));
}
qz.add(qb, BooleanClause.Occur.SHOULD);
}
}
BooleanQuery qa = qz.build();
if (!qa.clauses().isEmpty()) {
qr.add(qa, BooleanClause.Occur.MUST);
i++;
}
}
}
/**
* 如果全部条件都是 MUST_NOT 会导致取不到数据
* 故有必要增加一个 MUST all 从而规避这个问题
*/
if (i > 0 & i == j) {
qr.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
}
}
use of io.github.ihongs.HongsExemption 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 = new File(serd);
// 检查进程
if (ppid.exists() != false) {
System.err.println("ERROR: The server has not exit, or did not exit normally.");
System.exit(126);
return;
}
if (ppcd.exists() == false) {
ppcd.mkdirs();
}
try (FileWriter fw = new FileWriter(ppid, true)) {
fw.write(ManagementFactory.getRuntimeMXBean().getName().split("@", 2)[0]);
fw.close();
} catch (IOException e) {
throw new HongsException(e);
}
CoreConfig cc = CoreConfig.getInstance("defines");
/**
* 取消名称
* 日志中将记录各独立的线程名
* 以便于区分不同的非动作任务
*/
Core.ACTION_NAME.remove();
Server server;
WebAppContext webapp;
ServerConnector conner;
Connector[] connes;
server = new Server(new QueuedThreadPool(cc.getProperty("jetty.pool.max.threads", 254), cc.getProperty("jetty.pool.min.threads", 010), cc.getProperty("jetty.pool.idle.timeout", 30000)));
webapp = new WebAppContext();
conner = new ServerConnector(server);
connes = new Connector[] { conner };
conner.setPort(port);
conner.setHost(null);
conner.setIdleTimeout(cc.getProperty("jetty.conn.idle.timeout", 30000L));
conner.setAcceptQueueSize(cc.getProperty("jetty.conn.accept.queue.size", 254));
conner.setAcceptedSendBufferSize(cc.getProperty("jetty.conn.accept.sndbuf.size", -1));
conner.setAcceptedReceiveBufferSize(cc.getProperty("jetty.conn.accept.rcvbuf.size", -1));
server.setConnectors(connes);
webapp.setDescriptor(conf);
webapp.setContextPath(Core.SERV_PATH);
webapp.setResourceBase(Core.BASE_PATH);
webapp.setTempDirectory(new File(Core.DATA_PATH + "/server/temp"));
webapp.setPersistTempDirectory(true);
webapp.setParentLoaderPriority(true);
webapp.setThrowUnavailableOnStartupException(true);
// webapp.setMaxFormKeys(cc.getProperty("jetty.serv.max.form.keys", 10000));
// webapp.setMaxFormContentSize(cc.getProperty("jetty.serv.max.form.size", 200000));
server.setHandler(webapp);
String x;
// 默认微调
x = org.eclipse.jetty.servlet.DefaultServlet.CONTEXT_INIT;
webapp.setInitParameter(x + "useFileMappedBuffer", "false");
webapp.setInitParameter(x + "dirAllowed", "false");
/**
* 初始设置
* 光能外部配置参数还不够方便
* 可能需要替换 JSP 解析器或 Session 容器
* 可以设置 jetty.init 来注入 Initer 对象
*/
x = cc.getProperty("jetty.init");
if (null != x) {
String[] a = x.split(";");
for (String n : a) {
n = n.trim();
if (n.isEmpty()) {
continue;
}
try {
((Initer) Class.forName(n).getDeclaredConstructor().newInstance()).init(webapp);
} catch (ClassNotFoundException e) {
throw new HongsExemption(e);
} catch (NoSuchMethodException e) {
throw new HongsExemption(e);
} catch (InstantiationException e) {
throw new HongsExemption(e);
} catch (IllegalAccessException e) {
throw new HongsExemption(e);
} catch (InvocationTargetException e) {
throw new HongsExemption(e);
}
}
}
// 中止机制
Runtime.getRuntime().addShutdownHook(new Stoper(server, ppid));
// 启动服务
try {
server.start();
if (Core.DEBUG == 0) {
System.err.println("HTTP server is started.");
}
/**
* URL 会话参数等同于对应的 Cookie, 总是小写
* server.start_ 执行之前 web.xml 还没解析
*/
x = webapp.getInitParameter(SessionHandler.__SessionIdPathParameterNameProperty);
if (x == null || x.isEmpty()) {
x = webapp.getServletContext().getSessionCookieConfig().getName();
x = x.toLowerCase();
webapp.getSessionHandler().setSessionIdPathParameterName(x);
}
server.join();
} catch (Exception e) {
throw new HongsException(e);
} catch (Error e) {
throw new HongsExemption(e);
}
}
use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class Capts method newCapt.
public void newCapt() {
buff = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// 单位宽度
int fw = width / (1 + codeCount);
// 干扰线框
int ls = (int) ((float) height * maskRatio);
// 最大字号
int fs = (int) ((float) height * fontRatio);
// 最小字号
int fz = (int) ((float) fs * 0.80f);
Random rd = new Random();
StringBuilder sb = new StringBuilder();
Graphics2D gd = buff.createGraphics();
gd.setColor(backColor);
gd.fillRect(0, 0, width, height);
gd.setColor(fontColor);
gd.setStroke(new BasicStroke(ls, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font;
try {
InputStream is;
if (fontFile.startsWith("!")) {
is = Capts.class.getResourceAsStream(fontFile.substring(1));
} else {
is = new FileInputStream(new File(fontFile));
}
font = Font.createFont(Font.TRUETYPE_FONT, is);
} catch (FontFormatException ex) {
throw new HongsExemption(ex);
} catch (IOException ex) {
throw new HongsExemption(ex);
}
for (int i = 0; i < codeCount; i++) {
/**
* medRatio 是字号误差系数
* 实际可能偏大, 需要缩减尺寸,
* 修正高度误差, 避免超出画布.
*/
int fn = rd.nextInt(fs - fz) + fz;
int fe = (int) (fn * mendRatio);
int j = rd.nextInt(fontDict.length);
String ss = String.valueOf(fontDict[j]);
sb.append(ss);
gd.setFont(font.deriveFont(Font.CENTER_BASELINE, fn));
int fx = (fw - gd.getFontMetrics().stringWidth(ss)) / 2;
gd.drawString(ss, fw / 2 + fw * i + fx, (height - fn) / 2 + fn - fe);
}
for (int i = 0; i < maskCount; i++) {
int xs = rd.nextInt(width);
int ys = rd.nextInt(height);
int xe = xs + rd.nextInt(fw * 2);
int ye = ys + rd.nextInt(fs / 2);
if (i % 3 == 0) {
gd.setColor(fontColor);
} else {
gd.setColor(backColor);
}
gd.drawLine(xs, ys, xe, ye);
}
// 扭曲画面
shearX(width, height, gd, rd, backColor);
shearY(width, height, gd, rd, backColor);
code = sb.toString();
}
use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class SearchEntity method commit.
@Override
public void commit() {
super.commit();
if (WRITES.isEmpty()) {
return;
}
try {
IndexWriter iw = getWriter();
synchronized (iw) {
// 此处才会是真的更新文档
for (Map.Entry<String, Document> et : WRITES.entrySet()) {
String id = et.getKey();
Document dc = et.getValue();
if (dc != null) {
iw.updateDocument(new Term("@" + Cnst.ID_KEY, id), dc);
} else {
iw.deleteDocuments(new Term("@" + Cnst.ID_KEY, id));
}
}
iw.commit();
}
} catch (HongsException ex) {
throw ex.toExemption();
} catch (IOException ex) {
throw new HongsExemption(1055, ex);
} finally {
WRITES.clear();
DOCK = null;
}
}
use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class ActionHelper method flush.
// ** 输出内容 **/
/**
* 输出数据
* 按情况以 JSON/JSONP 格式输出
*/
public void flush() {
if (null == this.responseData) {
return;
}
// 动作调用了 forward 之后
// 这时再调用 getWriter 会抛 STREAM 异常
// 因容器调用 getOutputStream 来输出内容
// 所以必须将 OutputStream 包装成 Writer
Writer out;
try {
out = getOutputWriter();
} catch (IllegalStateException e) {
out = new OutputStreamWriter(getOutputStream());
}
// 检查是否有 JSONP 的回调函数
String fun = null;
if (request != null) {
fun = request.getParameter(Cnst.CB_KEY);
if (fun == null) {
fun = request.getParameter(CoreConfig.getInstance().getProperty("core.callback", "callback"));
}
}
// 特殊前缀则返回嵌 JS 的 XHTML
try {
if (fun != null && !fun.isEmpty() && !fun.equals("~")) {
if (fun.startsWith("top.") || fun.startsWith("parent.") || fun.startsWith("opener.") || fun.startsWith("frames.")) {
if (this.response != null && !this.response.isCommitted()) {
this.response.setCharacterEncoding("UTF-8");
this.response.setContentType("text/html");
}
out.append("<script type=\"text/javascript\">");
out.append(fun);
out.append("(");
Dawn.append(out, this.responseData);
out.append(");");
out.append("</script>");
} else {
if (this.response != null && !this.response.isCommitted()) {
this.response.setCharacterEncoding("UTF-8");
this.response.setContentType("text/javascript");
}
out.append(fun);
out.append("(");
Dawn.append(out, this.responseData);
out.append(");");
}
} else {
if (this.response != null && !this.response.isCommitted()) {
this.response.setCharacterEncoding("UTF-8");
this.response.setContentType("application/json");
}
Dawn.append(out, this.responseData);
}
out.flush();
} catch (IOException e) {
throw new HongsExemption(1110, "Can not send to client.", e);
}
this.responseData = null;
}
Aggregations