Skip to content

Commit

Permalink
JFinal 1.4 Release :)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfinal committed May 1, 2013
1 parent 30f069f commit e94f606
Show file tree
Hide file tree
Showing 28 changed files with 401 additions and 280 deletions.
45 changes: 33 additions & 12 deletions src/com/jfinal/config/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.jfinal.kit.StringKit;
import com.jfinal.log.ILoggerFactory;
import com.jfinal.log.Logger;
import com.jfinal.render.IErrorRenderFactory;
import com.jfinal.render.IMainRenderFactory;
import com.jfinal.render.RenderFactory;
import com.jfinal.render.ViewType;
Expand All @@ -33,8 +34,6 @@
*/
final public class Constants {

private String error404View;
private String error500View;
private String fileRenderPath;
private String uploadedFileSaveDirectory;

Expand Down Expand Up @@ -153,28 +152,44 @@ public void setVelocityViewExtension(String velocityViewExtension) {
this.velocityViewExtension = velocityViewExtension.startsWith(".") ? velocityViewExtension : "." + velocityViewExtension;
}

public String getError404View() {
return error404View;
}

/**
* Set error 404 view.
* @param error404View the error 404 view
*/
public void setError404View(String error404View) {
this.error404View = error404View;
}

public String getError500View() {
return error500View;
errorViewMapping.put(404, error404View);
}

/**
* Set error 500 view.
* @param error500View the error 500 view
*/
public void setError500View(String error500View) {
this.error500View = error500View;
errorViewMapping.put(500, error500View);
}

/**
* Set error 401 view.
* @param error401View the error 401 view
*/
public void setError401View(String error401View) {
errorViewMapping.put(401, error401View);
}

/**
* Set error 403 view.
* @param error403View the error 403 view
*/
public void setError403View(String error403View) {
errorViewMapping.put(403, error403View);
}

private java.util.Map<Integer, String> errorViewMapping = new java.util.HashMap<Integer, String>();
public void setErrorView(int errorCode, String errorView) {
errorViewMapping.put(errorCode, errorView);
}
public String getErrorView(int errorCode) {
return errorViewMapping.get(errorCode);
}

public String getFileRenderPath() {
Expand Down Expand Up @@ -294,6 +309,12 @@ public void setLoggerFactory(ILoggerFactory loggerFactory) {
throw new IllegalArgumentException("loggerFactory can not be null.");
Logger.setLoggerFactory(loggerFactory);
}

public void setErrorRenderFactory(IErrorRenderFactory errorRenderFactory) {
if (errorRenderFactory == null)
throw new IllegalArgumentException("errorRenderFactory can not be null.");
RenderFactory.setErrorRenderFactory(errorRenderFactory);
}
}


Expand Down
57 changes: 57 additions & 0 deletions src/com/jfinal/core/ActionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) 2011-2012, James Zhan 詹波 ([email protected]).
*
* 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.jfinal.core;

import com.jfinal.kit.StringKit;
import com.jfinal.render.Render;
import com.jfinal.render.RenderFactory;

/**
* ActionException.
*/
public class ActionException extends RuntimeException {

private static final long serialVersionUID = 1998063243843477017L;
private int errorCode;
private Render errorRender;

public ActionException(int errorCode, Render errorRender) {
if (errorRender == null)
throw new IllegalArgumentException("The parameter errorRender can not be null.");

this.errorCode = errorCode;
this.errorRender = errorRender;
}

public ActionException(int errorCode, String errorView) {
if (StringKit.isBlank(errorView))
throw new IllegalArgumentException("The parameter errorView can not be blank.");

this.errorCode = errorCode;
this.errorRender = RenderFactory.me().getRender(errorView);
}

public int getErrorCode() {
return errorCode;
}

public Render getErrorRender() {
return errorRender;
}
}


45 changes: 30 additions & 15 deletions src/com/jfinal/core/ActionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import com.jfinal.config.Constants;
import com.jfinal.handler.Handler;
import com.jfinal.log.Logger;
import com.jfinal.render.Error404Exception;
import com.jfinal.render.Error500Exception;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;
import com.jfinal.render.RenderFactory;
Expand Down Expand Up @@ -62,7 +60,7 @@ public final void handle(String target, HttpServletRequest request, HttpServletR
String qs = request.getQueryString();
log.warn("Action not found: " + (qs == null ? target : target + "?" + qs));
}
renderFactory.getError404Render().setContext(request, response).render();
renderFactory.getErrorRender(404).setContext(request, response).render();
return ;
}

Expand Down Expand Up @@ -99,26 +97,43 @@ public final void handle(String target, HttpServletRequest request, HttpServletR
log.error(qs == null ? target : target + "?" + qs, e);
}
}
catch (Error404Exception e) {
if (log.isWarnEnabled()) {
String qs = request.getQueryString();
log.warn("Resource not found: " + (qs == null ? target : target + "?" + qs));
catch (ActionException e) {
int errorCode = e.getErrorCode();
if (errorCode == 404) {
if (log.isWarnEnabled()) {
String qs = request.getQueryString();
log.warn("404 Not Found: " + (qs == null ? target : target + "?" + qs));
}
e.getErrorRender().setContext(request, response).render();
}
e.getError404Render().setContext(request, response).render();
}
catch (Error500Exception e) {
if (log.isErrorEnabled()) {
String qs = request.getQueryString();
log.error(qs == null ? target : target + "?" + qs, e);
else if (errorCode == 401) {
if (log.isWarnEnabled()) {
String qs = request.getQueryString();
log.warn("401 Unauthorized: " + (qs == null ? target : target + "?" + qs));
}
e.getErrorRender().setContext(request, response).render();
}
else if (errorCode == 403) {
if (log.isWarnEnabled()) {
String qs = request.getQueryString();
log.warn("403 Forbidden: " + (qs == null ? target : target + "?" + qs));
}
e.getErrorRender().setContext(request, response).render();
}
else {
if (log.isErrorEnabled()) {
String qs = request.getQueryString();
log.error(qs == null ? target : target + "?" + qs, e);
}
e.getErrorRender().setContext(request, response).render();
}
e.getError500Render().setContext(request, response).render();
}
catch (Exception e) {
if (log.isErrorEnabled()) {
String qs = request.getQueryString();
log.error(qs == null ? target : target + "?" + qs, e);
}
renderFactory.getError500Render().setContext(request, response).render();
renderFactory.getErrorRender(500).setContext(request, response).render();
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/com/jfinal/core/ActionMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private Set<String> buildExcludedMethodName() {
}

void buildActionMapping() {
mapping.clear();
Set<String> excludedMethodName = buildExcludedMethodName();
InterceptorBuilder interceptorBuilder = new InterceptorBuilder();
Interceptor[] defaultInters = interceptors.getInterceptorArray();
Expand Down
2 changes: 1 addition & 1 deletion src/com/jfinal/core/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public interface Const {

String JFINAL_VERSION = "1.3";
String JFINAL_VERSION = "1.4";

ViewType DEFAULT_VIEW_TYPE = ViewType.FREE_MARKER;

Expand Down
65 changes: 29 additions & 36 deletions src/com/jfinal/core/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ public Integer getAttrForInt(String name) {
return (Integer)request.getAttribute(name);
}

private Integer getParaToInt_(String result, Integer defaultValue) {
if (result == null)
private Integer toInt(String value, Integer defaultValue) {
if (value == null)
return defaultValue;
if (result.startsWith("N") || result.startsWith("n"))
return -Integer.parseInt(result.substring(1));
return Integer.parseInt(result);
if (value.startsWith("N") || value.startsWith("n"))
return -Integer.parseInt(value.substring(1));
return Integer.parseInt(value);
}

/**
Expand All @@ -217,7 +217,7 @@ private Integer getParaToInt_(String result, Integer defaultValue) {
* @return a Integer representing the single value of the parameter
*/
public Integer getParaToInt(String name) {
return getParaToInt_(request.getParameter(name), null);
return toInt(request.getParameter(name), null);
}

/**
Expand All @@ -226,15 +226,15 @@ public Integer getParaToInt(String name) {
* @return a Integer representing the single value of the parameter
*/
public Integer getParaToInt(String name, Integer defaultValue) {
return getParaToInt_(request.getParameter(name), defaultValue);
return toInt(request.getParameter(name), defaultValue);
}

private Long getParaToLong_(String result, Long defaultValue) {
if (result == null)
private Long toLong(String value, Long defaultValue) {
if (value == null)
return defaultValue;
if (result.startsWith("N") || result.startsWith("n"))
return -Long.parseLong(result.substring(1));
return Long.parseLong(result);
if (value.startsWith("N") || value.startsWith("n"))
return -Long.parseLong(value.substring(1));
return Long.parseLong(value);
}

/**
Expand All @@ -243,7 +243,7 @@ private Long getParaToLong_(String result, Long defaultValue) {
* @return a Integer representing the single value of the parameter
*/
public Long getParaToLong(String name) {
return getParaToLong_(request.getParameter(name), null);
return toLong(request.getParameter(name), null);
}

/**
Expand All @@ -252,7 +252,7 @@ public Long getParaToLong(String name) {
* @return a Integer representing the single value of the parameter
*/
public Long getParaToLong(String name, Long defaultValue) {
return getParaToLong_(request.getParameter(name), defaultValue);
return toLong(request.getParameter(name), defaultValue);
}

/**
Expand Down Expand Up @@ -522,42 +522,42 @@ public String getPara(int index, String defaultValue) {
* Get para from url and conver to Integer. The first index is 0
*/
public Integer getParaToInt(int index) {
return getParaToInt_(getPara(index), null);
return toInt(getPara(index), null);
}

/**
* Get para from url and conver to Integer with default value if it is null.
*/
public Integer getParaToInt(int index, Integer defaultValue) {
return getParaToInt_(getPara(index), defaultValue);
return toInt(getPara(index), defaultValue);
}

/**
* Get para from url and conver to Long.
*/
public Long getParaToLong(int index) {
return getParaToLong_(getPara(index), null);
return toLong(getPara(index), null);
}

/**
* Get para from url and conver to Long with default value if it is null.
*/
public Long getParaToLong(int index, Long defaultValue) {
return getParaToLong_(getPara(index), defaultValue);
return toLong(getPara(index), defaultValue);
}

/**
* Get all para from url and convert to Integer
*/
public Integer getParaToInt() {
return getParaToInt_(getPara(), null);
return toInt(getPara(), null);
}

/**
* Get all para from url and convert to Long
*/
public Long getParaToLong() {
return getParaToLong_(getPara(), null);
return toLong(getPara(), null);
}

/**
Expand Down Expand Up @@ -975,31 +975,24 @@ public void redirect301(String url, boolean withQueryString) {
}

/**
* Render with view and 404 status
* Render with view and errorCode status
*/
public void renderError404(String view) {
throw new com.jfinal.render.Error404Exception(renderFactory.getError404Render(view));
public void renderError(int errorCode, String view) {
throw new ActionException(errorCode, renderFactory.getErrorRender(errorCode, view));
}

/**
* Render with view and 404 status configured in JFinalConfig
* Render with render and errorCode status
*/
public void renderError404() {
throw new com.jfinal.render.Error404Exception(renderFactory.getError404Render());
public void renderError(int errorCode, Render render) {
throw new ActionException(errorCode, render);
}

/**
* Render with view and 500 status
* Render with view and errorCode status configured in JFinalConfig
*/
public void renderError500(String view) {
throw new com.jfinal.render.Error500Exception(renderFactory.getError500Render(view));
}

/**
* Render with view and 500 status configured in JFinalConfig
*/
public void renderError500() {
throw new com.jfinal.render.Error500Exception(renderFactory.getError500Render());
public void renderError(int errorCode) {
throw new ActionException(errorCode, renderFactory.getErrorRender(errorCode));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/com/jfinal/ext/interceptor/GET.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public void intercept(ActionInvocation ai) {
if ("GET".equalsIgnoreCase(controller.getRequest().getMethod()))
ai.invoke();
else
controller.renderError404();
controller.renderError(404);
}
}
Loading

0 comments on commit e94f606

Please sign in to comment.