diff --git a/src/main/java/com/hunantv/fw/Dispatcher.java b/src/main/java/com/hunantv/fw/Dispatcher.java index 7b5d9bc..e7e8255 100644 --- a/src/main/java/com/hunantv/fw/Dispatcher.java +++ b/src/main/java/com/hunantv/fw/Dispatcher.java @@ -21,7 +21,7 @@ public class Dispatcher extends AbstractHandler { public final static FwLogger logger = new FwLogger(Dispatcher.class); Application app = Application.getInstance(); - protected Routes routes = app.getRoutes();; + protected Routes routes = app.getRoutes(); protected boolean debug = app.isDebug(); protected static final MultipartConfigElement MULTI_PART_CONFIG = new MultipartConfigElement(System.getProperty("java.io.tmpdir")); @@ -32,7 +32,9 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques Long btime = System.currentTimeMillis(); logger.initSeqid(); logger.debug(target); - response.setContentType("text/html;charset=UTF-8"); + if (WebUtil.isTextHtml(request)) { + response.setContentType("text/html;charset=UTF-8"); + } if (WebUtil.isMultipart(request)) { request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, MULTI_PART_CONFIG); } @@ -47,7 +49,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques this.err500(response, ex); } finally { Long etime = System.currentTimeMillis(); - logger.delayInfo("cost", new Long(etime - btime).toString()); + logger.delayInfo("cost", Long.toString(etime - btime)); logger.clearSeqid(); } } diff --git a/src/main/java/com/hunantv/fw/db/C3P0.java b/src/main/java/com/hunantv/fw/db/C3P0.java index b525b25..ee06293 100644 --- a/src/main/java/com/hunantv/fw/db/C3P0.java +++ b/src/main/java/com/hunantv/fw/db/C3P0.java @@ -15,92 +15,89 @@ import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3P0 { - public static final FwLogger logger = new FwLogger(C3P0.class); - private static C3P0 c3p0 = null; - private Map dataSources = new HashMap(); - public static final String DEFUAT_DB_NAME = "write"; - - private C3P0() { - try { - Map> specialPros = initPros(); - for (Iterator iter = specialPros.keySet().iterator(); iter.hasNext();) { - String name = iter.next(); - printPros(name, specialPros.get(name)); - DataSource ds = new ComboPooledDataSource(); - BeanUtils.populate(ds, specialPros.get(name)); - this.dataSources.put(name, ds); - } - } catch (Exception ex) { - throw new RuntimeException(ex); - } - } - - private void printPros(String name, Map pros) { - logger.debug("datasource name:" + name); - for (Iterator iter = pros.keySet().iterator(); iter.hasNext();) { - String key = iter.next(); - logger.debug(key + "=" + (String) pros.get(key)); - } - } - - private Map> initPros() { - try { - Properties pros = new SysConf().read("c3p0.properties"); - - Map sharePros = new HashMap(); - String[] names = StringUtil.split(((String) pros.get("c3p0.names")).trim(), ","); - - Map> specialPros = new HashMap>(); - for (String name : names) { - specialPros.put(name, new HashMap()); - } - - for (Iterator iter = pros.keySet().iterator(); iter.hasNext();) { - String key = (String) iter.next(); - if (key.startsWith("c3p0.") && !key.startsWith("c3p0.names")) { - for (String specialName : specialPros.keySet()) { - if (!key.startsWith("c3p0." + specialName)) { - sharePros.put(key.substring(5), pros.get(key)); - } else { - // c3p0. 这个长度是5,最后一个 1是最后还有一个 . 的长度 - int len = 5 + specialName.length() + 1; - specialPros.get(specialName).put(key.substring(len), pros.get(key)); - } - } - } - } - - /** - * 把常规属性放到特殊属性里面去 - */ - for (Map ps : specialPros.values()) { - for (Iterator iter = sharePros.keySet().iterator(); iter.hasNext();) { - String key = iter.next(); - ps.put(key, sharePros.get(key)); - } - } - return specialPros; - } catch (Exception ex) { - throw new RuntimeException(ex); - } - } - - public static C3P0 instance() { - if (c3p0 == null) - c3p0 = new C3P0(); - return c3p0; - } - - public DataSource getDataSource() { - return this.getDataSource(this.DEFUAT_DB_NAME); - } - - public DataSource getDataSource(String name) { - return this.dataSources.get(name); - } - - public static void main(String[] args) throws Exception { - ComboPooledDataSource ds = (ComboPooledDataSource) new C3P0().getDataSource(); - System.out.println(ds.getMaxPoolSize()); - } + public static final FwLogger logger = new FwLogger(C3P0.class); + private static C3P0 c3p0 = null; + private Map dataSources = new HashMap<>(); + public static final String DEFUAT_DB_NAME = "write"; + + private C3P0() { + try { + Map> specialPros = initPros(); + for (Iterator iter = specialPros.keySet().iterator(); iter.hasNext(); ) { + String name = iter.next(); + printPros(name, specialPros.get(name)); + DataSource ds = new ComboPooledDataSource(); + BeanUtils.populate(ds, specialPros.get(name)); + this.dataSources.put(name, ds); + } + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + private void printPros(String name, Map pros) { + logger.debug("datasource name:" + name); + pros.forEach((k, v) -> logger.debug(k + "=" + v)); + } + + private Map> initPros() { + try { + Properties pros = new SysConf().read("c3p0.properties"); + + Map sharePros = new HashMap<>(); + String[] names = StringUtil.split(((String) pros.get("c3p0.names")).trim(), ","); + + Map> specialPros = new HashMap<>(); + for (String name : names) { + specialPros.put(name, new HashMap<>()); + } + + for (Iterator iter = pros.keySet().iterator(); iter.hasNext(); ) { + String key = (String) iter.next(); + if (key.startsWith("c3p0.") && !key.startsWith("c3p0.names")) { + for (String specialName : specialPros.keySet()) { + if (!key.startsWith("c3p0." + specialName)) { + sharePros.put(key.substring(5), pros.get(key)); + } else { + // c3p0. 这个长度是5,最后一个 1是最后还有一个 . 的长度 + int len = 5 + specialName.length() + 1; + specialPros.get(specialName).put(key.substring(len), pros.get(key)); + } + } + } + } + + /** + * 把常规属性放到特殊属性里面去 + */ + for (Map ps : specialPros.values()) { + for (Iterator iter = sharePros.keySet().iterator(); iter.hasNext(); ) { + String key = iter.next(); + ps.put(key, sharePros.get(key)); + } + } + return specialPros; + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + public static C3P0 instance() { + if (c3p0 == null) + c3p0 = new C3P0(); + return c3p0; + } + + public DataSource getDataSource() { + return this.getDataSource(this.DEFUAT_DB_NAME); + } + + public DataSource getDataSource(String name) { + return this.dataSources.get(name); + } + + public static void main(String[] args) throws Exception { + ComboPooledDataSource ds = (ComboPooledDataSource) new C3P0().getDataSource(); + System.out.println(ds.getMaxPoolSize()); + } } diff --git a/src/main/java/com/hunantv/fw/route/Route.java b/src/main/java/com/hunantv/fw/route/Route.java index 891e303..0795258 100644 --- a/src/main/java/com/hunantv/fw/route/Route.java +++ b/src/main/java/com/hunantv/fw/route/Route.java @@ -16,8 +16,8 @@ import com.hunantv.fw.utils.StringUtil; public class Route { - public static enum HttpMethod { - GET, POST, PUT, DELETE, OPTIONS, HEAD, TRACE; + public enum HttpMethod { + GET, POST, PUT, DELETE, OPTIONS, HEAD, TRACE } Map classAndRegMapping = new HashMap() { @@ -260,9 +260,8 @@ public ControllerAndAction buildControllerAndAction() throws HttpException { public ControllerAndAction buildControllerAndAction(Object[] args) throws HttpException { Class controllerClass = this.getController(); - Controller controller = null; try { - controller = controllerClass.newInstance(); + Controller controller = controllerClass.newInstance(); if (args == null) return new ControllerAndAction(controller, this.action); return new ControllerAndAction(controller, this.action, args); diff --git a/src/main/java/com/hunantv/fw/route/Routes.java b/src/main/java/com/hunantv/fw/route/Routes.java index 152aa56..260d511 100644 --- a/src/main/java/com/hunantv/fw/route/Routes.java +++ b/src/main/java/com/hunantv/fw/route/Routes.java @@ -14,12 +14,12 @@ public class Routes { /** * { uri: { httpMethod: Route } } */ - private Map> staticRoutes = new HashMap>(); + private Map> staticRoutes = new HashMap<>(); /** * { Route: { httpMethod } } */ - private Map> dyRoutes = new HashMap>(); + private Map> dyRoutes = new HashMap<>(); public Routes() { } @@ -38,7 +38,7 @@ public Routes addStatic(Route route) { String routeUri = route.getUriReg(); Map tmpMap = staticRoutes.get(routeUri); if (null == tmpMap) { - tmpMap = new HashMap(); + tmpMap = new HashMap<>(); staticRoutes.put(routeUri, tmpMap); } tmpMap.put(route.getHttpMethod().toString(), route); diff --git a/src/main/java/com/hunantv/fw/utils/SysConf.java b/src/main/java/com/hunantv/fw/utils/SysConf.java index 9638b6e..861ec04 100644 --- a/src/main/java/com/hunantv/fw/utils/SysConf.java +++ b/src/main/java/com/hunantv/fw/utils/SysConf.java @@ -77,10 +77,9 @@ private String sysPath() throws Exception { String path = url.getFile(); int fileStrPosition = path.indexOf("file:/"); int begin = 0; - int end = path.length(); if (fileStrPosition >= 0) begin = fileStrPosition + 5; - end = path.indexOf("WEB-INF/"); + int end = path.indexOf("WEB-INF/"); if (end > 0) { String rf = path.substring(begin, end); webPath = rf; diff --git a/src/main/java/com/hunantv/fw/utils/WebUtil.java b/src/main/java/com/hunantv/fw/utils/WebUtil.java index c35fa4b..e690beb 100644 --- a/src/main/java/com/hunantv/fw/utils/WebUtil.java +++ b/src/main/java/com/hunantv/fw/utils/WebUtil.java @@ -8,7 +8,10 @@ public static boolean isMultipart(HttpServletRequest request) { String contentType = request.getContentType(); return contentType != null && contentType.startsWith("multipart/form-data"); } - + public static boolean isTextHtml(HttpServletRequest request) { + String HeaderValue = request.getHeader("Accept"); + return HeaderValue != null && HeaderValue.startsWith("text/html"); + } private static String[] IP_HEADS = new String[] { "X-Forwarded-For", "Proxy-Client-IP", diff --git a/src/main/java/com/hunantv/fw/view/View.java b/src/main/java/com/hunantv/fw/view/View.java index 49a5823..6e98aad 100644 --- a/src/main/java/com/hunantv/fw/view/View.java +++ b/src/main/java/com/hunantv/fw/view/View.java @@ -12,11 +12,11 @@ public interface View { FwLogger logger = FwLogger.getLogger(View.class); - public abstract String render(); + String render(); - public abstract void renderTo(Writer out) throws IOException; + void renderTo(Writer out) throws IOException; - public abstract Object getV(); + Object getV(); /** * Determine if this view should rendered as byte's stream,
@@ -24,7 +24,7 @@ public interface View { * * @return */ - public default boolean isStreamView() { + default boolean isStreamView() { return false; } @@ -35,7 +35,7 @@ public default boolean isStreamView() { * @param response: HttpServletResponse * @throws IOException */ - public default void renderTo(HttpServletResponse response) throws IOException { + default void renderTo(HttpServletResponse response) throws IOException { if (isStreamView()) { renderTo(response.getOutputStream()); } else { @@ -48,7 +48,7 @@ public default void renderTo(HttpServletResponse response) throws IOException { * * @return View's bytes */ - public default byte[] getBytes() { + default byte[] getBytes() { return null; } @@ -61,7 +61,7 @@ public default byte[] getBytes() { * @param out: OutputStream * @throws IOException */ - public default void renderTo(OutputStream out) throws IOException { + default void renderTo(OutputStream out) throws IOException { byte[] bytes = getBytes(); if (bytes == null) { diff --git a/src/test/java/fw/test/unit/DummyServletOutputStream.java b/src/test/java/fw/test/unit/DummyServletOutputStream.java index 99c35f9..3bae8b5 100644 --- a/src/test/java/fw/test/unit/DummyServletOutputStream.java +++ b/src/test/java/fw/test/unit/DummyServletOutputStream.java @@ -33,8 +33,8 @@ public void write(byte[] bytes) throws IOException { } @Override - public void write(byte[] b, int len, int off) throws IOException { - outputStream.write(b, len, off); + public void write(byte[] b, int off, int len) throws IOException { + outputStream.write(b, off, len); } @Override