diff --git a/pom.xml b/pom.xml index d26b4db9..8b3818fe 100644 --- a/pom.xml +++ b/pom.xml @@ -122,6 +122,13 @@ 1.3.2 + + + org.apache.httpcomponents + httpclient + 4.5.2 + + mysql diff --git a/src/main/java/com/smallchill/common/tool/SysCache.java b/src/main/java/com/smallchill/common/tool/SysCache.java index c1d0e7aa..935cb9b4 100644 --- a/src/main/java/com/smallchill/common/tool/SysCache.java +++ b/src/main/java/com/smallchill/common/tool/SysCache.java @@ -89,7 +89,8 @@ public class SysCache implements ConstCache, ConstCacheKey{ return Blade.create(Role.class).findById(roleId); } }); - sb.append(role.getName()).append(","); + if (null != role) + sb.append(role.getName()).append(","); } return StrKit.removeSuffix(sb.toString(), ","); } @@ -113,7 +114,8 @@ public class SysCache implements ConstCache, ConstCacheKey{ return Blade.create(Role.class).findById(roleId); } }); - sb.append(role.getTips()).append(","); + if (null != role) + sb.append(role.getTips()).append(","); } return StrKit.removeSuffix(sb.toString(), ","); } @@ -155,7 +157,8 @@ public class SysCache implements ConstCache, ConstCacheKey{ return Blade.create(Dept.class).findById(deptId); } }); - sb.append(dept.getSimplename()).append(","); + if (null != dept) + sb.append(dept.getSimplename()).append(","); } return StrKit.removeSuffix(sb.toString(), ","); } diff --git a/src/main/java/com/smallchill/core/toolbox/kit/HttpKit.java b/src/main/java/com/smallchill/core/toolbox/kit/HttpKit.java index d13dfbe2..1658afe1 100644 --- a/src/main/java/com/smallchill/core/toolbox/kit/HttpKit.java +++ b/src/main/java/com/smallchill/core/toolbox/kit/HttpKit.java @@ -1,29 +1,24 @@ -/** - * Copyright (c) 2015-2016, Chill Zhuang 庄骞 (smallchill@163.com). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package com.smallchill.core.toolbox.kit; - -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.net.URL; -import java.net.URLConnection; -import java.util.List; +import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; +import java.util.ArrayList; import java.util.Map; +import org.apache.http.HttpEntity; +import org.apache.http.NameValuePair; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; import javax.servlet.http.HttpServletRequest; import org.springframework.web.context.request.RequestContextHolder; @@ -32,7 +27,121 @@ import org.springframework.web.context.request.ServletRequestAttributes; import com.smallchill.core.toolbox.support.WafRequestWrapper; public class HttpKit { + private static PoolingHttpClientConnectionManager cm; + private static String EMPTY_STR = ""; + private static String UTF_8 = "UTF-8"; + private static void init() { + if (cm == null) { + cm = new PoolingHttpClientConnectionManager(); + cm.setMaxTotal(50);// 整个连接池最大连接数 + cm.setDefaultMaxPerRoute(5);// 每路由最大连接数,默认值是2 + } + } + + /** + * 通过连接池获取HttpClient + */ + private static CloseableHttpClient getHttpClient() { + init(); + return HttpClients.custom().setConnectionManager(cm).build(); + } + + public static String get(String url) { + HttpGet httpGet = new HttpGet(url); + return getResult(httpGet); + } + + public static String get(String url, Map params) throws URISyntaxException { + URIBuilder ub = new URIBuilder(); + ub.setPath(url); + + ArrayList pairs = covertParams2NVPS(params); + ub.setParameters(pairs); + + HttpGet httpGet = new HttpGet(ub.build()); + return getResult(httpGet); + } + + public static String get(String url, Map headers, Map params) throws URISyntaxException { + URIBuilder ub = new URIBuilder(); + ub.setPath(url); + + ArrayList pairs = covertParams2NVPS(params); + ub.setParameters(pairs); + + HttpGet httpGet = new HttpGet(ub.build()); + for (Map.Entry param : headers.entrySet()) { + httpGet.addHeader(param.getKey(), String.valueOf(param.getValue())); + } + return getResult(httpGet); + } + + public static String post(String url) { + HttpPost httpPost = new HttpPost(url); + return getResult(httpPost); + } + + public static String post(String url, Map params) throws UnsupportedEncodingException { + HttpPost httpPost = new HttpPost(url); + ArrayList pairs = covertParams2NVPS(params); + httpPost.setEntity(new UrlEncodedFormEntity(pairs, UTF_8)); + return getResult(httpPost); + } + + public static String post(String url, Map headers, Map params) throws UnsupportedEncodingException { + HttpPost httpPost = new HttpPost(url); + + for (Map.Entry param : headers.entrySet()) { + httpPost.addHeader(param.getKey(), String.valueOf(param.getValue())); + } + + ArrayList pairs = covertParams2NVPS(params); + httpPost.setEntity(new UrlEncodedFormEntity(pairs, UTF_8)); + + return getResult(httpPost); + } + + private static ArrayList covertParams2NVPS(Map params) { + ArrayList pairs = new ArrayList(); + for (Map.Entry param : params.entrySet()) { + pairs.add(new BasicNameValuePair(param.getKey(), String + .valueOf(param.getValue()))); + } + + return pairs; + } + + /** + * 处理Http请求 + * + * @param request + * @return + */ + private static String getResult(HttpRequestBase request) { + // CloseableHttpClient httpClient = HttpClients.createDefault(); + CloseableHttpClient httpClient = getHttpClient(); + try { + CloseableHttpResponse response = httpClient.execute(request); + // response.getStatusLine().getStatusCode(); + HttpEntity entity = response.getEntity(); + if (entity != null) { + // long len = entity.getContentLength();// -1 表示长度未知 + String result = EntityUtils.toString(entity); + response.close(); + // httpClient.close(); + return result; + } + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + + } + return EMPTY_STR; + } + /** * 获取 包装防Xss Sql注入的 HttpServletRequest * @return request @@ -41,124 +150,5 @@ public class HttpKit { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); return new WafRequestWrapper(request); } - - /** - * 向指定URL发送GET方法的请求 - * - * @param url 发送请求的URL - * @param param 请求参数 - * @return URL 所代表远程资源的响应结果 - */ - public static String sendGet(String url, Map param) { - String result = ""; - BufferedReader in = null; - try { - String para = ""; - for (String key : param.keySet()) { - para += (key + "=" + param.get(key) + "&"); - } - if (para.lastIndexOf("&") > 0) { - para = para.substring(0, para.length() - 1); - } - String urlNameString = url + "?" + para; - URL realUrl = new URL(urlNameString); - // 打开和URL之间的连接 - URLConnection connection = realUrl.openConnection(); - // 设置通用的请求属性 - connection.setRequestProperty("accept", "*/*"); - connection.setRequestProperty("connection", "Keep-Alive"); - connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); - // 建立实际的连接 - connection.connect(); - // 获取所有响应头字段 - Map> map = connection.getHeaderFields(); - // 遍历所有的响应头字段 - for (String key : map.keySet()) { - System.out.println(key + "--->" + map.get(key)); - } - // 定义 BufferedReader输入流来读取URL的响应 - in = new BufferedReader(new InputStreamReader(connection.getInputStream())); - String line; - while ((line = in.readLine()) != null) { - result += line; - } - } catch (Exception e) { - System.out.println("发送GET请求出现异常!" + e); - e.printStackTrace(); - } - // 使用finally块来关闭输入流 - finally { - try { - if (in != null) { - in.close(); - } - } catch (Exception e2) { - e2.printStackTrace(); - } - } - return result; - } - - /** - * 向指定 URL 发送POST方法的请求 - * - * @param url 发送请求的 URL - * @param param 请求参数 - * @return 所代表远程资源的响应结果 - */ - public static String sendPost(String url, Map param) { - PrintWriter out = null; - BufferedReader in = null; - String result = ""; - try { - String para = ""; - for (String key : param.keySet()) { - para += (key + "=" + param.get(key) + "&"); - } - if (para.lastIndexOf("&") > 0) { - para = para.substring(0, para.length() - 1); - } - String urlNameString = url + "?" + para; - URL realUrl = new URL(urlNameString); - // 打开和URL之间的连接 - URLConnection conn = realUrl.openConnection(); - // 设置通用的请求属性 - conn.setRequestProperty("accept", "*/*"); - conn.setRequestProperty("connection", "Keep-Alive"); - conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); - // 发送POST请求必须设置如下两行 - conn.setDoOutput(true); - conn.setDoInput(true); - // 获取URLConnection对象对应的输出流 - out = new PrintWriter(conn.getOutputStream()); - // 发送请求参数 - out.print(param); - // flush输出流的缓冲 - out.flush(); - // 定义BufferedReader输入流来读取URL的响应 - in = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String line; - while ((line = in.readLine()) != null) { - result += line; - } - } catch (Exception e) { - System.out.println("发送 POST 请求出现异常!" + e); - e.printStackTrace(); - } - // 使用finally块来关闭输出流、输入流 - finally { - try { - if (out != null) { - out.close(); - } - if (in != null) { - in.close(); - } - } catch (IOException ex) { - ex.printStackTrace(); - } - } - return result; - } } diff --git a/src/main/webapp/WEB-INF/view/common/_template/_view/_add.bld b/src/main/webapp/WEB-INF/view/common/_template/_view/_add.bld index 56e8af09..e91b98e9 100644 --- a/src/main/webapp/WEB-INF/view/common/_template/_view/_add.bld +++ b/src/main/webapp/WEB-INF/view/common/_template/_view/_add.bld @@ -10,11 +10,22 @@ ${"@"}layout("/common/_curd/_container.html"){ ${"@"} var _col=[ @trim(){ @ for (attr in attrs) { - @ var _attrName = attr.comment; - @ if (isEmpty(_attrName)) { - @ _attrName = attr.name; - @} - ${"@"} {name:"${_attrName}", index:"${attr.name}", type:"text",newline:true,length:8,required:"required"}, + @ var _attrName = attr.comment; + @ if (isEmpty(_attrName)) { + @ _attrName = attr.name; + @ } + @ if(pkName == attr.name) { + @ continue; + @ } + @ var newline = true; + @ if(attrLP.odd) { + @ newline = false; + @ } + @ var length = 3; + @ if(attrLP.last && newline == true) { + @ length = 8; + @ } + ${"@"} {name:"${_attrName}", index:"${attr.name}", type:"text",newline:${newline},length:${length},required:"required"}, @} @} diff --git a/src/main/webapp/WEB-INF/view/common/_template/_view/_edit.bld b/src/main/webapp/WEB-INF/view/common/_template/_view/_edit.bld index 017a3dc5..e2a50f65 100644 --- a/src/main/webapp/WEB-INF/view/common/_template/_view/_edit.bld +++ b/src/main/webapp/WEB-INF/view/common/_template/_view/_edit.bld @@ -11,11 +11,22 @@ ${"@"}layout("/common/_curd/_container.html"){ ${"@"} var _col=[ @trim(){ @ for (attr in attrs) { - @ var _attrName = attr.comment; - @ if (isEmpty(_attrName)) { - @ _attrName = attr.name; - @} - ${"@"} {name:"${_attrName}", index:"${attr.name}", type:"text",newline:true,length:8,required:"required"}, + @ var _attrName = attr.comment; + @ if (isEmpty(_attrName)) { + @ _attrName = attr.name; + @ } + @ if(pkName == attr.name) { + @ continue; + @ } + @ var newline = true; + @ if(attrLP.odd) { + @ newline = false; + @ } + @ var length = 3; + @ if(attrLP.last && newline == true) { + @ length = 8; + @ } + ${"@"} {name:"${_attrName}", index:"${attr.name}", type:"text",newline:${newline},length:${length},required:"required"}, @} @} diff --git a/src/main/webapp/WEB-INF/view/common/_template/_view/_view.bld b/src/main/webapp/WEB-INF/view/common/_template/_view/_view.bld index 2b7330ee..59ac69fc 100644 --- a/src/main/webapp/WEB-INF/view/common/_template/_view/_view.bld +++ b/src/main/webapp/WEB-INF/view/common/_template/_view/_view.bld @@ -9,11 +9,22 @@ ${"@"}layout("/common/_curd/_container.html"){ ${"@"} var _col=[ @trim(){ @ for (attr in attrs) { - @ var _attrName = attr.comment; - @ if (isEmpty(_attrName)) { - @ _attrName = attr.name; - @} - ${"@"} {name:"${_attrName}", index:"${attr.name}", type:"text",newline:true,length:8}, + @ var _attrName = attr.comment; + @ if (isEmpty(_attrName)) { + @ _attrName = attr.name; + @ } + @ if(pkName == attr.name) { + @ continue; + @ } + @ var newline = true; + @ if(attrLP.odd) { + @ newline = false; + @ } + @ var length = 3; + @ if(attrLP.last && newline == true) { + @ length = 8; + @ } + ${"@"} {name:"${_attrName}", index:"${attr.name}", type:"text",newline:${newline},length:${length}}, @} @} diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 828f0240..b20d46d7 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -1,89 +1,92 @@ - - springBlade - - springmvc - org.springframework.web.servlet.DispatcherServlet - - contextConfigLocation - classpath:/spring/springmvc-servlet.xml - - 1 - true - - - springmvc - / - - - org.springframework.web.util.IntrospectorCleanupListener - - - com.smallchill.core.listener.ConfigListener - - - SpringEncodingFilter - org.springframework.web.filter.CharacterEncodingFilter - - encoding - UTF-8 - - - forceEncoding - true - - - - SpringEncodingFilter - /* - - - DruidWebStatFilter - com.alibaba.druid.support.http.WebStatFilter - - exclusions - *.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/* - - - - DruidWebStatFilter - /* - - - DruidStatView - com.alibaba.druid.support.http.StatViewServlet - - - DruidStatView - /druid/* - - - shiroFilter - org.springframework.web.filter.DelegatingFilterProxy - - targetFilterLifecycle - true - - - - shiroFilter - /* - REQUEST - FORWARD - - - 30 - - - 404 - /error/error404 - - - apk - application/vnd.android.package-archive - - - ipa - application/vnd.iphone - + + springBlade + + springmvc + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + classpath:/spring/springmvc-servlet.xml + + 1 + true + + + springmvc + / + + + org.springframework.web.util.IntrospectorCleanupListener + + + com.smallchill.core.listener.ConfigListener + + + SpringEncodingFilter + org.springframework.web.filter.CharacterEncodingFilter + + encoding + UTF-8 + + + forceEncoding + true + + + + SpringEncodingFilter + /* + + + DruidWebStatFilter + com.alibaba.druid.support.http.WebStatFilter + + exclusions + *.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/* + + + + DruidWebStatFilter + /* + + + DruidStatView + com.alibaba.druid.support.http.StatViewServlet + + + DruidStatView + /druid/* + + + shiroFilter + org.springframework.web.filter.DelegatingFilterProxy + + targetFilterLifecycle + true + + + + shiroFilter + /* + REQUEST + FORWARD + + + 30 + + + 404 + /error/error404 + + + apk + application/vnd.android.package-archive + + + ipa + application/vnd.iphone + \ No newline at end of file