use of javax.servlet.RequestDispatcher in project jersey by jersey.
the class Include method doTag.
public void doTag() throws JspException, IOException {
final JspContext jspContext = getJspContext();
final Class<?> resolvingClass = (Class<?>) jspContext.getAttribute(RequestDispatcherWrapper.RESOLVING_CLASS_ATTRIBUTE_NAME, PageContext.REQUEST_SCOPE);
final String basePath = (String) jspContext.getAttribute(RequestDispatcherWrapper.BASE_PATH_ATTRIBUTE_NAME, PageContext.REQUEST_SCOPE);
final ServletConfig servletConfig = (ServletConfig) getPageObject(PageContext.CONFIG);
final ServletContext servletContext = servletConfig.getServletContext();
for (Class<?> clazz = resolvingClass; clazz != Object.class; clazz = clazz.getSuperclass()) {
final String template = basePath + TemplateHelper.getAbsolutePath(clazz, page, '/');
if (servletContext.getResource(template) != null) {
// Tomcat returns a RequestDispatcher even if the JSP file doesn't exist so check if the resource exists first.
final RequestDispatcher dispatcher = servletContext.getRequestDispatcher(template);
if (dispatcher != null) {
try {
final HttpServletRequest request = (HttpServletRequest) getPageObject(PageContext.REQUEST);
final HttpServletResponse response = (HttpServletResponse) getPageObject(PageContext.RESPONSE);
dispatcher.include(request, new Wrapper(response, new PrintWriter(jspContext.getOut())));
} catch (ServletException e) {
throw new JspException(e);
}
return;
}
}
}
throw new JspException(LocalizationMessages.UNABLE_TO_FIND_PAGE_FOR_RESOLVING_CLASS(page, resolvingClass));
}
use of javax.servlet.RequestDispatcher in project hudson-2.x by hudson.
the class BasicAuthenticationFilter method doFilter.
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse rsp = (HttpServletResponse) response;
String authorization = req.getHeader("Authorization");
String path = req.getServletPath();
if (authorization == null || req.getUserPrincipal() != null || path.startsWith("/secured/") || !Hudson.getInstance().isUseSecurity()) {
// normal requests, or security not enabled
if (req.getUserPrincipal() != null) {
// before we route this request, integrate the container authentication
// to Acegi. For anonymous users that doesn't have user principal,
// AnonymousProcessingFilter that follows this should create
// an Authentication object.
SecurityContextHolder.getContext().setAuthentication(new ContainerAuthentication(req));
}
try {
chain.doFilter(request, response);
} finally {
SecurityContextHolder.clearContext();
}
return;
}
// authenticate the user
String username = null;
String password = null;
String uidpassword = Scrambler.descramble(authorization.substring(6));
int idx = uidpassword.indexOf(':');
if (idx >= 0) {
username = uidpassword.substring(0, idx);
password = uidpassword.substring(idx + 1);
}
if (username == null) {
rsp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
rsp.setHeader("WWW-Authenticate", "Basic realm=\"Hudson administrator\"");
return;
}
path = req.getContextPath() + "/secured" + path;
String q = req.getQueryString();
if (q != null)
path += '?' + q;
// prepare a redirect
rsp.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
rsp.setHeader("Location", path);
// ... but first let the container authenticate this request
RequestDispatcher d = servletContext.getRequestDispatcher("/j_security_check?j_username=" + URLEncoder.encode(username, "UTF-8") + "&j_password=" + URLEncoder.encode(password, "UTF-8"));
d.include(req, rsp);
}
use of javax.servlet.RequestDispatcher in project nutz by nutzam.
the class ForwardView method render.
public void render(HttpServletRequest req, HttpServletResponse resp, Object obj) throws Exception {
String path = evalPath(req, obj);
String args = "";
if (path != null && path.contains("?")) {
//将参数部分分解出来
args = path.substring(path.indexOf('?'));
path = path.substring(0, path.indexOf('?'));
}
String ext = getExt();
// 空路径,采用默认规则
if (Strings.isBlank(path)) {
path = Mvcs.getRequestPath(req);
path = "/WEB-INF" + (path.startsWith("/") ? "" : "/") + Files.renameSuffix(path, ext);
} else // 绝对路径 : 以 '/' 开头的路径不增加 '/WEB-INF'
if (path.charAt(0) == '/') {
if (!path.toLowerCase().endsWith(ext))
path += ext;
} else // 包名形式的路径
{
path = "/WEB-INF/" + path.replace('.', '/') + ext;
}
// 执行 Forward
path = path + args;
RequestDispatcher rd = req.getRequestDispatcher(path);
if (rd == null)
throw Lang.makeThrow("Fail to find Forward '%s'", path);
// Do rendering
rd.forward(req, resp);
}
use of javax.servlet.RequestDispatcher in project jodd by oblac.
the class ServletDispatcherResult method renderView.
/**
* Renders the view by dispatching to the target JSP.
*/
protected void renderView(ActionRequest actionRequest, String target) throws Exception {
target = processTarget(actionRequest, target);
HttpServletRequest request = actionRequest.getHttpServletRequest();
HttpServletResponse response = actionRequest.getHttpServletResponse();
RequestDispatcher dispatcher = request.getRequestDispatcher(target);
if (dispatcher == null) {
// should never happened
response.sendError(SC_NOT_FOUND, "Result not found: " + target);
return;
}
if (DispatcherUtil.isPageIncluded(request, response)) {
dispatcher.include(request, response);
} else {
dispatcher.forward(request, response);
}
}
use of javax.servlet.RequestDispatcher in project roboguice by roboguice.
the class ServletPipelineRequestDispatcherTest method forwardToManagedServletFailureOnCommittedBuffer.
public final void forwardToManagedServletFailureOnCommittedBuffer() throws IOException, ServletException {
String pattern = "blah.html";
final ServletDefinition servletDefinition = new ServletDefinition(pattern, Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
final Injector injector = createMock(Injector.class);
final Binding binding = createMock(Binding.class);
final HttpServletRequest mockRequest = createMock(HttpServletRequest.class);
final HttpServletResponse mockResponse = createMock(HttpServletResponse.class);
expect(mockResponse.isCommitted()).andReturn(true);
final HttpServlet mockServlet = new HttpServlet() {
protected void service(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException {
final Object o = request.getAttribute(A_KEY);
assertEquals("Wrong attrib returned - " + o, A_VALUE, o);
}
};
expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
expect(injector.getBinding(Key.get(HttpServlet.class))).andReturn(binding);
expect(injector.getInstance(Key.get(HttpServlet.class))).andReturn(mockServlet);
final Key<ServletDefinition> servetDefsKey = Key.get(TypeLiteral.get(ServletDefinition.class));
Binding<ServletDefinition> mockBinding = createMock(Binding.class);
expect(injector.findBindingsByType(eq(servetDefsKey.getTypeLiteral()))).andReturn(ImmutableList.<Binding<ServletDefinition>>of(mockBinding));
Provider<ServletDefinition> bindingProvider = Providers.of(servletDefinition);
expect(mockBinding.getProvider()).andReturn(bindingProvider);
replay(injector, binding, mockRequest, mockResponse, mockBinding);
// Have to init the Servlet before we can dispatch to it.
servletDefinition.init(null, injector, Sets.<HttpServlet>newIdentityHashSet());
final RequestDispatcher dispatcher = new ManagedServletPipeline(injector).getRequestDispatcher(pattern);
assertNotNull(dispatcher);
try {
dispatcher.forward(mockRequest, mockResponse);
} finally {
verify(injector, mockRequest, mockResponse, mockBinding);
}
}
Aggregations