~ryoji-kodakari/gt/jettytiddlywiki

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package jettyTiddlywiki.response;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jettyTiddlywiki.factory.URCF;
import jettyTiddlywiki.response.AbnormalPrinter.ResCode;
import jettyTiddlywiki.security.Authority;

public enum AdminRedirecter {
	Singleton;
	/**
	 * 
	 * @param req
	 * @param res
	 * @param redirectURL
	 * @return ture redirected, false not redirected
	 */
	public boolean checkAndRedirect(final HttpServletRequest req, final HttpServletResponse res,final String requesturi){
    	HttpSession session = req.getSession(true);
        String id = (String)session.getAttribute("id");
    	Authority auth = (id!=null&&!id.isEmpty()) ? Authority.authorized:Authority.notauthorized;
    	if(!auth.isAuthorized()){
        	try {
        		res.sendRedirect("/login?redirect="+URCF.encode(requesturi));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return true;
    	}
    	return false;
	}
	
	/**
	 * 
	 * @param req
	 * @param res
	 * @return ture printed, false not printed
	 */
	public boolean checkAndPrint(final HttpServletRequest req, final HttpServletResponse res){
    	HttpSession session = req.getSession(true);
        String id = (String)session.getAttribute("id");
    	Authority auth = (id!=null&&!id.isEmpty()) ? Authority.authorized:Authority.notauthorized;
    	if(!auth.isAuthorized()){
    		AbnormalPrinter.Singleton.print(res, req, ResCode.Unauthorized);
    		return true;
    	}
    	return false;
	}
}