Skip to content

Commit

Permalink
* Bugfix: 当requestContext初始化失败时,错误处理程序报NPE的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Zhou committed Dec 29, 2012
1 parent 2f0b601 commit e415677
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES_SINCE_3.1.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@
* Bugfix: <renderResultAsJson> valve,当redirect发生时,仍然执行渲染的问题。

* 改进Navigator接口,添加了forwardTo(target, action, actionEvent)方法,使之可内部重定向到指定action。
* Bugfix: 当requestContext初始化失败时,错误处理程序报NPE的问题。
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public final void service(HttpServletRequest request, HttpServletResponse respon
giveUpControl(requestContext, chain);
}
} catch (Throwable e) {
handleException(requestContext, e);
handleException(requestContext, request, response, e);
} finally {
commitRequest(requestContext);
}
Expand Down Expand Up @@ -204,9 +204,14 @@ private boolean handleInternalRequest(HttpServletRequest request, HttpServletRes
* 3. 假如不幸errorHandler本身遇到异常,则servlet engine就会接管这个异常。通常是显示web.xml中指定的错误页面。
* 这种情况下,新老异常都会被记录到日志中。
*/
private void handleException(RequestContext requestContext, Throwable e) throws ServletException, IOException {
HttpServletRequest request = requestContext.getRequest();
HttpServletResponse response = requestContext.getResponse();
private void handleException(RequestContext requestContext, HttpServletRequest request, HttpServletResponse response, Throwable e)
throws ServletException, IOException {
// 当requestContext初始化失败时,requestContext可能为null。
// 此时直接使用原始的request/response对象。
if (requestContext != null) {
request = requestContext.getRequest();
response = requestContext.getResponse();
}

try {
try {
Expand Down
1 change: 1 addition & 0 deletions webx/framework/src/test/config/WEB-INF/webx.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

<services:request-contexts xmlns="http://www.alibaba.com/schema/services/request-contexts">
<rundata />
<request-context class="com.alibaba.citrus.webx.AbstractWebxTests.RequestContextTesterFactory" />
</services:request-contexts>

<beans:bean id="componentsAware"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
import javax.servlet.http.HttpServletResponse;

import com.alibaba.citrus.service.pipeline.PipelineContext;
import com.alibaba.citrus.service.requestcontext.RequestContext;
import com.alibaba.citrus.service.requestcontext.RequestContextFactory;
import com.alibaba.citrus.service.requestcontext.rundata.RunData;
import com.alibaba.citrus.service.requestcontext.support.AbstractRequestContextFactory;
import com.alibaba.citrus.util.ServletUtil;
import com.alibaba.citrus.util.internal.Servlet3Util;
import com.alibaba.citrus.util.io.StreamUtil;
Expand All @@ -66,6 +69,8 @@ public abstract class AbstractWebxTests {
protected int clientResponseCode;
protected String clientResponseContent;

protected static final ThreadLocal<RequestContextFactory> requestContextFactoryHolder = new ThreadLocal<RequestContextFactory>();

static {
Servlet3Util.setDisableServlet3Features(true); // 禁用servlet3,因为httpunit还不支持
}
Expand Down Expand Up @@ -116,6 +121,7 @@ public final void initPipeline() {
public void dispose() {
TestValve.runnerHolder.remove();
TestExceptionValve.runnerHolder.remove();
requestContextFactoryHolder.remove();
}

/** 设置<code>WebxDispatcherServlet.internalHandlerMapping.errorHandler</code>。 */
Expand Down Expand Up @@ -216,4 +222,24 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
public void destroy() {
}
}

public static class RequestContextTesterFactory extends AbstractRequestContextFactory<RequestContext> {
public RequestContext getRequestContextWrapper(RequestContext wrappedContext) {
RequestContextFactory<RequestContext> threadLocalFactory = requestContextFactoryHolder.get();

if (threadLocalFactory == null) {
return wrappedContext;
} else {
return threadLocalFactory.getRequestContextWrapper(wrappedContext);
}
}

public String[] getFeatures() {
return null;
}

public FeatureOrder[] featureOrders() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import javax.servlet.ServletException;

import com.alibaba.citrus.service.pipeline.PipelineContext;
import com.alibaba.citrus.service.requestcontext.RequestContext;
import com.alibaba.citrus.service.requestcontext.rundata.RunData;
import com.alibaba.citrus.service.requestcontext.support.AbstractRequestContextFactory;
import com.alibaba.citrus.webx.AbstractWebxTests;
import com.alibaba.citrus.webx.BadRequestException;
import com.alibaba.citrus.webx.ResourceNotFoundException;
Expand Down Expand Up @@ -76,6 +78,31 @@ public void passthru() throws Exception {
assertEquals("hello!", clientResponseContent.trim()); // text from valve
}

@Test
public void requestContextFailed() throws Exception {
requestContextFactoryHolder.set(new AbstractRequestContextFactory() {
@Override
public RequestContext getRequestContextWrapper(RequestContext wrappedContext) {
throw new IllegalArgumentException("ouch!");
}

public String[] getFeatures() {
return null;
}

public FeatureOrder[] featureOrders() {
return null;
}
});

invokeServlet("/myapps/app1/test.htm");

assertEquals(500, clientResponseCode);
assertEquals("text/html", clientResponse.getContentType());
assertThat(clientResponseContent,
containsAll("<pre>", "</pre>", IllegalArgumentException.class.getName(), "ouch!"));
}

@Test
public void internalRequest_illegal() throws Exception {
// internal
Expand Down

0 comments on commit e415677

Please sign in to comment.