Search in sources :

Example 16 with RequestDispatcher

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));
}
Also used : HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) JspContext(javax.servlet.jsp.JspContext) ServletConfig(javax.servlet.ServletConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestDispatcher(javax.servlet.RequestDispatcher) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) JspException(javax.servlet.jsp.JspException) ServletContext(javax.servlet.ServletContext) PrintWriter(java.io.PrintWriter)

Example 17 with RequestDispatcher

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 18 with RequestDispatcher

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);
}
Also used : RequestDispatcher(javax.servlet.RequestDispatcher)

Example 19 with RequestDispatcher

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);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 20 with RequestDispatcher

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);
    }
}
Also used : Binding(com.google.inject.Binding) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestDispatcher(javax.servlet.RequestDispatcher) HttpServletRequest(javax.servlet.http.HttpServletRequest) Injector(com.google.inject.Injector) EasyMock.anyObject(org.easymock.EasyMock.anyObject)

Aggregations

RequestDispatcher (javax.servlet.RequestDispatcher)203 ServletException (javax.servlet.ServletException)63 HttpSession (javax.servlet.http.HttpSession)58 IOException (java.io.IOException)41 SQLException (java.sql.SQLException)31 HttpServletRequest (javax.servlet.http.HttpServletRequest)30 HttpServletResponse (javax.servlet.http.HttpServletResponse)26 Properties (java.util.Properties)14 ServletContext (javax.servlet.ServletContext)12 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)9 WebUser (org.compiere.util.WebUser)9 ClienteDAO (br.senac.tads3.pi03b.gruposete.dao.ClienteDAO)8 PrintWriter (java.io.PrintWriter)8 ServletResponse (javax.servlet.ServletResponse)8 Cliente (br.senac.tads3.pi03b.gruposete.models.Cliente)7 ServletRequest (javax.servlet.ServletRequest)7 User (org.opennms.netmgt.config.users.User)7 HotelDAO (br.senac.tads3.pi03b.gruposete.dao.HotelDAO)6 VooDAO (br.senac.tads3.pi03b.gruposete.dao.VooDAO)6