Example usage for javax.servlet UnavailableException getMessage

List of usage examples for javax.servlet UnavailableException getMessage

Introduction

In this page you can find the example usage for javax.servlet UnavailableException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:fi.hoski.web.auth.LoginServlet.java

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setCharacterEncoding("UTF-8");

    String email = request.getParameter("email");
    String activationKey = request.getParameter("activationKey");
    try {//w  w w.  j  a v  a  2s .c  o  m
        if (email != null && activationKey != null) {
            Map<String, Object> user = userDirectory.useActivationKey(email, activationKey);

            if (user != null) {
                HttpSession session = request.getSession(true);
                session.setAttribute(USER, user);
            }

            // redirect always, if user is not logged in,
            // there will be a login screen
            response.sendRedirect("/member"); //TODO target make configurable
        } else {
            HttpSession session = request.getSession(false);
            String etag = request.getHeader("If-None-Match");
            @SuppressWarnings("unchecked")
            Map<String, Object> user = (session != null) ? (Map<String, Object>) session.getAttribute(USER)
                    : null;
            String userEtag = getEtag(user);

            if (etag != null && etag.equals(userEtag)) {
                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            } else {
                response.setHeader("ETag", userEtag);
                response.setHeader("Cache-Control", "private");
                response.setHeader("Vary", "Cookie");

                writeUserJSON(user, response);
            }
        }
    } catch (UnavailableException ex) {
        log(ex.getMessage(), ex);
        response.sendError(HttpServletResponse.SC_FORBIDDEN, ex.getMessage());
    } catch (EmailNotUniqueException ex) {
        log(ex.getMessage(), ex);
        response.sendError(HttpServletResponse.SC_FORBIDDEN, ex.getMessage());
    }
}

From source file:fi.hoski.web.auth.LoginServlet.java

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setCharacterEncoding("UTF-8");

    response.setHeader("Cache-Control", "private, max-age=0, no-cache");
    String action = request.getParameter("action");
    try {/*  www . j a v a 2  s  . c  o m*/
        if (action == null || action.equals("login")) {
            // login

            String email = request.getParameter("email");
            String password = request.getParameter("password");
            email = (email != null) ? email.trim() : null;

            // 1. check params
            if (email == null || email.isEmpty() || password == null || password.isEmpty()) {
                log("email or password not ok");
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
            } else {
                // 2. check user exists
                Map<String, Object> user = userDirectory.authenticateUser(email, password);
                if (user == null) {
                    log("user not found");
                    response.sendError(HttpServletResponse.SC_FORBIDDEN);
                } else {
                    // 3. create session
                    HttpSession session = request.getSession(true);
                    session.setAttribute(USER, user);

                    response.getWriter().println("Logged in");
                }
            }
        } else {
            // logout

            HttpSession session = request.getSession(false);
            if (session != null) {
                session.setAttribute(USER, null);
                session.invalidate();
            }

            // change Cookie so that Vary: Cookie works
            Cookie c = new Cookie("JSESSIONID", null);
            c.setMaxAge(0);
            response.addCookie(c);

            response.getWriter().println("Logged out");
        }
    } catch (UnavailableException ex) {
        log(ex.getMessage(), ex);
        response.sendError(HttpServletResponse.SC_FORBIDDEN, ex.getMessage());
    } catch (EmailNotUniqueException ex) {
        log(ex.getMessage(), ex);
        response.sendError(HttpServletResponse.SC_FORBIDDEN, ex.getMessage());
    }
}

From source file:org.apache.jasper.servlet.JspServletWrapper.java

public void service(HttpServletRequest request, HttpServletResponse response, boolean precompile)
        throws ServletException, IOException, FileNotFoundException {
    try {/*from w w  w. jav  a2s .com*/

        if (ctxt.isRemoved()) {
            throw new FileNotFoundException(jspUri);
        }

        if ((available > 0L) && (available < Long.MAX_VALUE)) {
            response.setDateHeader("Retry-After", available);
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,
                    Localizer.getMessage("jsp.error.unavailable"));
        }

        if (options.getDevelopment() || firstTime) {
            synchronized (this) {
                ctxt.compile();
            }
        }

        if (reload) {
            getServlet();
        }

        // If a page is to only to be precompiled return.
        if (precompile) {
            return;
        }

        if (theServlet instanceof SingleThreadModel) {
            // sync on the wrapper so that the freshness
            // of the page is determined right before servicing
            synchronized (this) {
                theServlet.service(request, response);
            }
        } else {
            theServlet.service(request, response);
        }

    } catch (UnavailableException ex) {
        String includeRequestUri = (String) request.getAttribute("javax.servlet.include.request_uri");
        if (includeRequestUri != null) {
            // This file was included. Throw an exception as
            // a response.sendError() will be ignored by the
            // servlet engine.
            throw ex;
        } else {
            int unavailableSeconds = ex.getUnavailableSeconds();
            if (unavailableSeconds <= 0) {
                unavailableSeconds = 60; // Arbitrary default
            }
            available = System.currentTimeMillis() + (unavailableSeconds * 1000L);
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, ex.getMessage());
        }
    } catch (FileNotFoundException ex) {
        String includeRequestUri = (String) request.getAttribute("javax.servlet.include.request_uri");
        if (includeRequestUri != null) {
            // This file was included. Throw an exception as
            // a response.sendError() will be ignored by the
            // servlet engine.
            throw new ServletException(ex);
        } else {
            try {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, ex.getMessage());
            } catch (IllegalStateException ise) {
                log.error(Localizer.getMessage("jsp.error.file.not.found", ex.getMessage()), ex);
            }
        }
    } catch (ServletException ex) {
        throw ex;
    } catch (IOException ex) {
        throw ex;
    } catch (IllegalStateException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new JasperException(ex);
    }
}

From source file:org.tinygroup.jspengine.servlet.JspServletWrapper.java

public void service(HttpServletRequest request, HttpServletResponse response, boolean precompile)
        throws ServletException, IOException {

    try {/*from  w w w.jav a 2 s. c  o m*/

        if (ctxt.isRemoved()) {
            jspFileNotFound(request, response);
            return;
        }

        if ((available > 0L) && (available < Long.MAX_VALUE)) {
            response.setDateHeader("Retry-After", available);
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,
                    Localizer.getMessage("jsp.error.unavailable"));
        }

        /*
         * (1) Compile
         */
        // BEGIN S1AS 6181923
        // if (options.getDevelopment() || firstTime) {
        // END S1AS 6181923
        // BEGIN S1AS 6181923
        if (!options.getUsePrecompiled() && (options.getDevelopment() || firstTime)) {
            // END S1AS 6181923
            synchronized (this) {
                firstTime = false;

                // The following sets reload to true, if necessary
                ctxt.compile();
            }
        } else {
            if (compileException != null) {
                // Throw cached compilation exception
                throw compileException;
            }
        }

        /*
         * (2) (Re)load servlet class file
         */
        getServlet();

        // If a page is to be precompiled only, return.
        if (precompile) {
            return;
        }

        /*
         * (3) Service request
         */
        if (theServlet instanceof SingleThreadModel) {
            // sync on the wrapper so that the freshness
            // of the page is determined right before servicing
            synchronized (this) {
                theServlet.service(request, response);
            }
        } else {
            theServlet.service(request, response);
        }

    } catch (UnavailableException ex) {
        String includeRequestUri = (String) request.getAttribute("javax.servlet.include.request_uri");
        if (includeRequestUri != null) {
            // This file was included. Throw an exception as
            // a response.sendError() will be ignored by the
            // servlet engine.
            throw ex;
        } else {
            int unavailableSeconds = ex.getUnavailableSeconds();
            if (unavailableSeconds <= 0) {
                unavailableSeconds = 60; // Arbitrary default
            }
            available = System.currentTimeMillis() + (unavailableSeconds * 1000L);
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, ex.getMessage());
        }
    } catch (ServletException ex) {
        throw ex;
    } catch (IOException ex) {
        throw ex;
    } catch (IllegalStateException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new JasperException(ex);
    }
}