Example usage for javax.servlet.http HttpServletResponse sendRedirect

List of usage examples for javax.servlet.http HttpServletResponse sendRedirect

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse sendRedirect.

Prototype

public void sendRedirect(String location) throws IOException;

Source Link

Document

Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer.

Usage

From source file:com.hp.autonomy.frontend.configuration.authentication.XhrAccessDeniedHandler.java

@Override
public void handle(final HttpServletRequest request, final HttpServletResponse response,
        final AccessDeniedException e) throws IOException, ServletException {
    // if AJAX, add 403 to the response, otherwise redirect to the given page
    if ("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN, "Blocked by " + this.getClass().getName());
    } else {// w  w  w  .  j  ava2 s. c o  m
        response.sendRedirect(request.getContextPath() + '/' + loginPage);
    }
}

From source file:com.dyuproject.demos.openidservlet.HomeServlet.java

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    if (request.getSession().getAttribute(WebConstants.SessionConstants.RC_USER) != null) {
        response.sendRedirect("/login/logged.html");
        return;//ww  w  . j  a v a2  s.  c o  m
    }
    String loginWith = request.getParameter("loginWith");
    if (loginWith != null) {
        // If the ui supplies a LoginWithGoogle or LoginWithYahoo
        // link/button,
        // this will speed up the openid process by skipping discovery.
        // The override is done by adding the OpenIdUser to the request
        // attribute.
        if (loginWith.equals("google")) {
            OpenIdUser user = OpenIdUser.populate("https://www.google.com/accounts/o8/id",
                    YadisDiscovery.IDENTIFIER_SELECT, "https://www.google.com/accounts/o8/ud");
            request.setAttribute(OpenIdUser.ATTR_NAME, user);

        } else if (loginWith.equals("yahoo")) {
            OpenIdUser user = OpenIdUser.populate("http://https://me.yahoo.com/user",
                    YadisDiscovery.IDENTIFIER_SELECT, "https://open.login.yahooapis.com/openid/op/auth");
            request.setAttribute(OpenIdUser.ATTR_NAME, user);
        }
    }

    String errorMsg = OpenIdServletFilter.DEFAULT_ERROR_MSG;
    try {
        OpenIdUser user = _relyingParty.discover(request);
        if (user == null) {
            if (RelyingParty.isAuthResponse(request)) {
                // authentication timeout
                response.sendRedirect(request.getRequestURI());
            } else {
                // set error msg if the openid_identifier is not resolved.
                if (request.getParameter(_relyingParty.getIdentifierParameter()) != null) {
                    request.setAttribute(OpenIdServletFilter.ERROR_MSG_ATTR, errorMsg);
                }

                // new user
                request.getRequestDispatcher("/login/login.jsp").forward(request, response);
            }
            return;
        }

        if (user.isAuthenticated()) {
            // user already authenticated
            request.getRequestDispatcher("/preAuth").forward(request, response);
            return;
        }

        if (user.isAssociated() && RelyingParty.isAuthResponse(request)) {
            // verify authentication
            if (_relyingParty.verifyAuth(user, request, response)) {
                // authenticated
                // redirect to home to remove the query params instead of
                // doing:
                // request.getRequestDispatcher("/home.jsp").forward(request,
                // response);
                response.sendRedirect(request.getContextPath() + "/home/");
            } else {
                // failed verification
                request.getRequestDispatcher("/login/login.jsp").forward(request, response);
            }
            return;
        }

        // associate and authenticate user
        StringBuffer url = request.getRequestURL();
        String trustRoot = url.substring(0, url.indexOf("/", 9));
        String realm = url.substring(0, url.lastIndexOf("/"));
        String returnTo = url.toString();
        if (_relyingParty.associateAndAuthenticate(user, request, response, trustRoot, realm, returnTo)) {
            // successful association
            return;
        }

    } catch (UnknownHostException uhe) {
        System.err.println("not found");
        errorMsg = OpenIdServletFilter.ID_NOT_FOUND_MSG;
    } catch (FileNotFoundException fnfe) {
        System.err.println("could not be resolved");
        errorMsg = OpenIdServletFilter.DEFAULT_ERROR_MSG;
    } catch (Exception e) {
        errorMsg = OpenIdServletFilter.DEFAULT_ERROR_MSG;
    }
    request.setAttribute(OpenIdServletFilter.ERROR_MSG_ATTR, errorMsg);

    request.getRequestDispatcher("/login/login.jsp").forward(request, response);
}

From source file:pivotal.au.se.gemfirexdweb.controller.JMXController.java

@RequestMapping(value = "/jmxmbeans", method = RequestMethod.POST)
public String performJMXAction(Model model, HttpServletResponse response, HttpServletRequest request,
        HttpSession session) throws Exception {
    if (session.getAttribute("user_key") == null) {
        logger.debug("user_key is null new Login required");
        response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
        return null;
    } else {//from w ww.jav a 2s .  c o  m
        Connection conn = AdminUtil.getConnection((String) session.getAttribute("user_key"));
        if (conn == null) {
            response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
            return null;
        } else {
            if (conn.isClosed()) {
                response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
                return null;
            }
        }

    }

    logger.debug("Received request to search for Mbeans");
    UserPref userPref = (UserPref) session.getAttribute("prefs");
    String jolokiaURL = userPref.getJolokiaURL();

    logger.debug("jolokiaURL = " + jolokiaURL);
    String searchStr = request.getParameter("search");
    logger.debug("searchStr = " + searchStr);

    List<String> mbeanNames = searchMbeans("", jolokiaURL);

    List<String> returnMbeanList = new ArrayList<String>();

    for (String mbean : mbeanNames) {
        if (mbean.toUpperCase().contains(searchStr.toUpperCase())) {
            returnMbeanList.add(mbean);
        }
    }

    model.addAttribute("mbeanNames", returnMbeanList);
    model.addAttribute("records", returnMbeanList.size());
    model.addAttribute("search", searchStr);

    // This will resolve to /WEB-INF/jsp/showmbeans.jsp
    return "showmbeans";

}

From source file:com.edm.app.interceptor.AuthInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    String link = request.getServletPath();

    if (StringUtils.startsWith(link, "/403") || StringUtils.startsWith(link, "/404")) {
        return true;
    }/*from  ww  w  .j a v  a  2 s  .  co m*/

    if (!Auth.isSetup()) {
        logger.info("(Auth:handle) error: auth is off");
        response.sendRedirect("/403");
        return false;
    }

    String robot = Auth.ROBOT;
    //      String code = Auth.MAP.get(UrlMap.CODE.getAction());
    //      String code = Auth.MAP.get("LICENSE_PASSWD");

    if (!Auth.size()) {
        logger.info("(Auth:handle) error: LICENSE is error");
        response.sendRedirect("/403");
        return false;
    }
    if (StringUtils.isBlank(robot)) {
        logger.info("(Auth:handle) error: ROBOT is error");
        response.sendRedirect("/403");
        return false;
    }
    if (!Auth.key(robot)) {
        logger.info("Auth is fail ");
        logger.info("(Auth:handle) error: KEY is error");
        response.sendRedirect("/403");
        return false;
    }
    //      if (!Auth.link(link, robot, code)) {
    //         logger.info("(Auth:handle) error: " + link + " is forbid");
    //         response.sendRedirect("/404");
    //         return false;
    //      }

    return true;
}

From source file:pivotal.au.se.gemfirexdweb.controller.MemberController.java

@RequestMapping(value = "/members", method = RequestMethod.GET)
public String showDiskstores(Model model, HttpServletResponse response, HttpServletRequest request,
        HttpSession session) throws Exception {
    javax.servlet.jsp.jstl.sql.Result allMemberInfoResult = null;

    if (session.getAttribute("user_key") == null) {
        logger.debug("user_key is null new Login required");
        response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
        return null;
    } else {//from  w  ww  . j ava  2 s  .c  om
        Connection conn = AdminUtil.getConnection((String) session.getAttribute("user_key"));
        if (conn == null) {
            response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
            return null;
        } else {
            if (conn.isClosed()) {
                response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
                return null;
            }
        }

    }

    logger.debug("Received request to show members");

    MemberDAO mbrDAO = GemFireXDWebDAOFactory.getMemberDAO();

    String memberAction = request.getParameter("memberAction");
    String propertyAction = request.getParameter("propertyAction");

    if (memberAction != null) {
        logger.debug("memberAction = " + memberAction);
        logger.debug("propertyAction = " + propertyAction);

        if (memberAction.equals("ALLMEMBEREVENTINFO")) {
            allMemberInfoResult = mbrDAO.getMemberInfo((String) request.getParameter("memberId"),
                    (String) session.getAttribute("user_key"));
            model.addAttribute("allMemberInfoResult", allMemberInfoResult);
            model.addAttribute("memberid", (String) request.getParameter("memberId"));
        } else if (memberAction.equals("PROPERTIES")) {
            String props = mbrDAO.getMemberProperties((String) request.getParameter("memberId"), propertyAction,
                    (String) session.getAttribute("user_key"));

            model.addAttribute("props", props);
            model.addAttribute("propertyAction", propertyAction);
            model.addAttribute("memberid", (String) request.getParameter("memberId"));
        }
    }

    List<Member> members = mbrDAO.retrieveMembers((String) session.getAttribute("user_key"));

    model.addAttribute("members", members);
    model.addAttribute("records", members.size());
    model.addAttribute("estimatedrecords", members.size());

    // This will resolve to /WEB-INF/jsp/members.jsp
    return "members";
}

From source file:eionet.eea.template.RefreshTemplateServlet.java

public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    updateTemplate("http://www.eea.europa.eu/templates/v2/getHeader", "header");
    updateTemplate("http://www.eea.europa.eu/templates/v2/getRequiredHead", "required_head");
    updateTemplate("http://www.eea.europa.eu/templates/v2/getFooter", "footer");

    res.sendRedirect("");
}

From source file:util.LoginInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    HttpSession session = request.getSession();
    String token = (String) session.getAttribute("token");
    if (request.getRequestURI().endsWith("efetuarLogin.htm") || (token != null && token.equals("aprovado"))
            || (request.getRequestURI().endsWith("login.htm"))) {
        return true;
    } else {/*from   ww w .j  a  va 2 s. co m*/
        response.sendRedirect("login.htm");
        return false;
    }
}

From source file:pivotal.au.se.gemfirexdweb.controller.ReportsController.java

@RequestMapping(value = "/executequeryreport", method = RequestMethod.GET)
public String executeQuery(Model model, HttpServletResponse response, HttpServletRequest request,
        HttpSession session) throws Exception {
    if (session.getAttribute("user_key") == null) {
        logger.debug("user_key is null new Login required");
        response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
        return null;
    } else {//from   w w  w.  j a  v  a  2  s . c  o m
        Connection conn = AdminUtil.getConnection((String) session.getAttribute("user_key"));
        if (conn == null) {
            response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
            return null;
        } else {
            if (conn.isClosed()) {
                response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
                return null;
            }
        }

    }

    logger.debug("Received request to execute a query");

    javax.servlet.jsp.jstl.sql.Result queryResult = null;
    QueryBeanReader reader = QueryBeanReader.getInstance();
    ConnectionManager cm = ConnectionManager.getInstance();

    String beanId = request.getParameter("beanId");

    if (beanId != null) {

        Query qb = reader.getQueryBean(beanId);

        if (qb instanceof GenericQuery) {
            logger.debug("Generic Query will be run");
            GenericQuery genericQuery = (GenericQuery) qb;

            queryResult = genericQuery.invoke(cm.getConnection((String) session.getAttribute("user_key")));
            model.addAttribute("queryResults", queryResult);
            model.addAttribute("queryDescription", genericQuery.getQueryDescription());
            model.addAttribute("querySQL", genericQuery.getQuery().trim());
            model.addAttribute("beanId", beanId);
        } else if (qb instanceof ParameterQuery) {
            ParameterQuery paramQuery = (ParameterQuery) qb;
            model.addAttribute("queryDescription", paramQuery.getQueryDescription());
            model.addAttribute("querySQL", paramQuery.getQuery().trim());
            model.addAttribute("beanId", beanId);
            model.addAttribute("paramMap", paramQuery.getParamMap());

            if (request.getParameter("pSubmit") != null) {
                // execute pressed, get param values
                Map paramValues = new HashMap();
                Set<String> keys = paramQuery.getParamMap().keySet();
                for (String param : keys) {
                    paramValues.put(param, (String) request.getParameter(param));
                }

                paramQuery.setParamValues(paramValues);
                queryResult = paramQuery.invoke(cm.getConnection((String) session.getAttribute("user_key")));
                request.setAttribute("queryResults", queryResult);
            }
        } else {
            // do nothing here
        }
    }

    // This will resolve to /WEB-INF/jsp/displayquery.jsp
    return "displayquery";
}

From source file:org.sakaiproject.metaobj.shared.control.CheckForTimeout.java

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    if (getIgnoreList().contains(request.getServletPath())) {
        return true;
    }/*from w  w  w  .j av  a 2 s.com*/
    Agent agent = getAuthenticationManager().getAgent();
    boolean timeOut = (agent == null || agent.getId() == null || agent.getId().getValue().length() == 0);
    if (timeOut) {
        response.sendRedirect(getTimeoutUrl());
    }
    return !timeOut;
}

From source file:pivotal.au.se.gemfirexdweb.controller.AddWriterController.java

@RequestMapping(value = "/addwriter", method = RequestMethod.POST)
public String createWriterAction(@ModelAttribute("writerAttribute") AddWriter writerAttribute, Model model,
        HttpServletResponse response, HttpServletRequest request, HttpSession session) throws Exception {
    if (session.getAttribute("user_key") == null) {
        logger.debug("user_key is null new Login required");
        response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
        return null;
    } else {// www  .  j a v a2s. co m
        Connection conn = AdminUtil.getConnection((String) session.getAttribute("user_key"));
        if (conn == null) {
            response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
            return null;
        } else {
            if (conn.isClosed()) {
                response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
                return null;
            }
        }

    }

    logger.debug("Received request to action an event for Table Writer");

    // perform some action here with what we have
    String submit = request.getParameter("pSubmit");

    if (submit != null) {
        // build create HDFS Store SQL
        StringBuffer addWriter = new StringBuffer();

        addWriter.append("CALL SYS.ATTACH_WRITER ('" + writerAttribute.getSchemaName() + "', \n");
        addWriter.append("'" + writerAttribute.getTableName() + "', \n");
        addWriter.append("'" + writerAttribute.getFunctionString() + "', \n");

        if (!checkIfParameterEmpty(request, "initInfoString")) {
            addWriter.append("'" + writerAttribute.getInitInfoString() + "', \n");
        } else {
            addWriter.append("NULL, \n");
        }

        if (!checkIfParameterEmpty(request, "serverGroups")) {
            addWriter.append("'" + writerAttribute.getServerGroups() + "')");
        } else {
            addWriter.append("NULL), \n");
        }

        if (submit.equalsIgnoreCase("create")) {
            Result result = new Result();

            logger.debug("Creating Table Writer as -> " + addWriter.toString());

            result = GemFireXDWebDAOUtil.runStoredCommand(addWriter.toString(),
                    (String) session.getAttribute("user_key"));

            model.addAttribute("result", result);

        } else if (submit.equalsIgnoreCase("Show SQL")) {
            logger.debug("Create Table Writer SQL as follows as -> " + addWriter.toString());
            model.addAttribute("sql", addWriter.toString());
        } else if (submit.equalsIgnoreCase("Save to File")) {
            response.setContentType(SAVE_CONTENT_TYPE);
            response.setHeader("Content-Disposition", "attachment; filename="
                    + String.format(FILENAME, writerAttribute.getTableName() + "-Writer"));

            ServletOutputStream out = response.getOutputStream();
            out.println(addWriter.toString());
            out.close();
            return null;
        }

    }

    // This will resolve to /WEB-INF/jsp/addwriter.jsp
    return "addwriter";
}