📄 dispatcherrequesttest.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.pluto.testsuite.test;
import java.io.IOException;
import javax.portlet.PortletContext;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pluto.testsuite.TestResult;
import org.apache.pluto.testsuite.TestUtils;
public class DispatcherRequestTest extends AbstractReflectivePortletTest {
/** Logger. */
private static final Log LOG = LogFactory.getLog(DispatcherRequestTest.class);
/** The path to the companion servlet. */
private static final String SERVLET_PATH = "/test/DispatcherRequestTest_Servlet";
private static final String CHECK_PATH_INFO = "/checkPathInfo";
private static final String CHECK_REQUEST_URI = "/checkRequestURI";
private static final String CHECK_CONTEXT_PATH = "/checkContextPath";
private static final String CHECK_SERVLET_PATH = "/checkServletPath";
private static final String CHECK_QUERY_STRING = "/checkQueryString";
private static final String GET_REQUEST_URI = "/getRequestURI";
private static final String GET_CONTEXT_PATH = "/getContextPath";
private static final String GET_SERVLET_PATH = "/getServletPath";
private static final String GET_QUERY_STRING = "/getQueryString";
/** The query string appended to the dispatch URI. */
private static final String QUERY_STRING = "paramName=paramValue";
/** The request attribute key which associates the test result. */
private static final String RESULT_KEY =
DispatcherRequestTest.class.getName() + ".RESULT_KEY";
/** The key for the expected request URI. */
private static final String EXPECTED_REQUEST_URI =
DispatcherRequestTest.class.getName() + ".REQUEST_URI";
/** The key for the expected context path. */
private static final String EXPECTED_CONTEXT_PATH =
DispatcherRequestTest.class.getName() + ".CONTEXT_PATH";
// Test Methods ------------------------------------------------------------
protected TestResult checkPathInfo(PortletContext context,
PortletRequest request,
PortletResponse response)
throws IOException, PortletException {
return doCheckIncludedAttribute(context, request, response, CHECK_PATH_INFO);
}
protected TestResult checkRequestURI(PortletContext context,
PortletRequest request,
PortletResponse response)
throws IOException, PortletException {
return doCheckIncludedAttribute(context, request, response, CHECK_REQUEST_URI);
}
protected TestResult checkGetRequestURI(PortletContext context,
PortletRequest request,
PortletResponse response)
throws IOException, PortletException {
return doCheckIncludedAttribute(context, request, response, GET_REQUEST_URI);
}
protected TestResult checkContextPath(PortletContext context,
PortletRequest request,
PortletResponse response)
throws IOException, PortletException {
return doCheckIncludedAttribute(context, request, response, CHECK_CONTEXT_PATH);
}
protected TestResult checkGetContextPath(PortletContext context,
PortletRequest request,
PortletResponse response)
throws IOException, PortletException {
return doCheckIncludedAttribute(context, request, response, GET_CONTEXT_PATH);
}
protected TestResult checkServletPath(PortletContext context,
PortletRequest request,
PortletResponse response)
throws IOException, PortletException {
return doCheckIncludedAttribute(context, request, response, CHECK_SERVLET_PATH);
}
protected TestResult checkGetServletPath(PortletContext context,
PortletRequest request,
PortletResponse response)
throws IOException, PortletException {
return doCheckIncludedAttribute(context, request, response, GET_SERVLET_PATH);
}
protected TestResult checkQueryString(PortletContext context,
PortletRequest request,
PortletResponse response)
throws IOException, PortletException {
return doCheckIncludedAttribute(context, request, response, CHECK_QUERY_STRING);
}
protected TestResult checkGetQueryString(PortletContext context,
PortletRequest request,
PortletResponse response)
throws IOException, PortletException {
return doCheckIncludedAttribute(context, request, response, GET_QUERY_STRING);
}
// Private Methods ---------------------------------------------------------
private TestResult doCheckIncludedAttribute(PortletContext context,
PortletRequest request,
PortletResponse response,
String pathInfo)
throws IOException, PortletException {
// Save expected values as request attributes.
String contextPath = request.getContextPath();
String requestUri = contextPath + SERVLET_PATH + pathInfo;
request.setAttribute(EXPECTED_REQUEST_URI, requestUri);
request.setAttribute(EXPECTED_CONTEXT_PATH, contextPath);
// Dispatch to the companion servlet: call checkParameters().
StringBuffer buffer = new StringBuffer();
buffer.append(SERVLET_PATH).append(pathInfo).append("?")
.append(QUERY_STRING);
if (LOG.isDebugEnabled()) {
LOG.debug("Dispatching to: " + buffer.toString());
}
PortletRequestDispatcher dispatcher = context.getRequestDispatcher(
buffer.toString());
dispatcher.include((RenderRequest) request, (RenderResponse) response);
// Retrieve test result returned by the companion servlet.
TestResult result = (TestResult) request.getAttribute(RESULT_KEY);
request.removeAttribute(RESULT_KEY);
request.removeAttribute(EXPECTED_REQUEST_URI);
request.removeAttribute(EXPECTED_CONTEXT_PATH);
return result;
}
// Nested Companion Servlet Class ------------------------------------------
public static class CompanionServlet extends HttpServlet {
private static final long serialVersionUID = -6032809452145653960L;
private static final String KEY_REQUEST_URI = "javax.servlet.include.request_uri";
private static final String KEY_CONTEXT_PATH = "javax.servlet.include.context_path";
private static final String KEY_SERVLET_PATH = "javax.servlet.include.servlet_path";
private static final String KEY_PATH_INFO = "javax.servlet.include.path_info";
private static final String KEY_QUERY_STRING = "javax.servlet.include.query_string";
// GenericServlet Impl -------------------------------------------------
public String getServletInfo() {
return getClass().getName();
}
/**
* This method uses <code>HttpServletRequest.getPathInfo</code> to
* retrieve the requested path info, and forwards to the corresponding
* check method to perform the test.
*/
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
TestResult result = null;
String pathInfo = getPathInfo(request);
if (CHECK_PATH_INFO.equals(pathInfo)) {
result = checkPathInfo(request, pathInfo);
} else if (CHECK_REQUEST_URI.equals(pathInfo)) {
result = checkRequestURI(request);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -