1 package org.roller.presentation.website.tags; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 import org.apache.struts.config.ForwardConfig; 6 import org.apache.struts.config.ModuleConfig; 7 import org.apache.struts.util.RequestUtils; 8 import org.roller.pojos.UserData; 9 import org.roller.presentation.RollerRequest; 10 11 import java.security.Principal ; 12 13 import javax.servlet.ServletContext ; 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.servlet.http.HttpServletResponse ; 16 import javax.servlet.jsp.JspException ; 17 import javax.servlet.jsp.tagext.Tag ; 18 import javax.servlet.jsp.tagext.TagSupport ; 19 20 21 25 public class AuthorizeUserTag extends TagSupport 26 { 27 static final long serialVersionUID = 1684786313445066867L; 28 29 private static Log mLogger = LogFactory.getFactory().getInstance( 30 AuthorizeUserTag.class); 31 32 private String name = null; 33 34 public String getFailureForward() { return (this.name); } 35 public void setFailureForward(String name) {this.name = name;} 36 37 43 public int doStartTag() throws JspException 44 { 45 return (SKIP_BODY); 46 } 47 48 53 public int doEndTag() throws JspException 54 { 55 HttpServletRequest req = null; 56 UserData user = null; 57 try 58 { 59 req = (HttpServletRequest )pageContext.getRequest(); 60 user = RollerRequest.getRollerRequest(req).getUser(); 61 } 62 catch (Exception e) 63 { 64 mLogger.error("ERROR in tag",e); 65 throw new JspException (e); 66 } 67 68 Principal prince = req.getUserPrincipal(); 69 if ( prince == null || !prince.getName().equals( user.getUserName() ) ) 70 { 71 ServletContext ctx = req.getSession().getServletContext(); 72 ModuleConfig mConfig = RequestUtils.getModuleConfig(req,ctx); 73 ForwardConfig fConfig = mConfig.findForwardConfig(name); 74 if (fConfig==null) 75 throw new JspException ("Forward "+name+"not found"); 76 77 String path = fConfig.getPath(); 79 if (fConfig.getRedirect()) 80 { 81 HttpServletResponse response = 82 (HttpServletResponse ) pageContext.getResponse(); 83 try 84 { 85 response.sendRedirect(response.encodeRedirectURL(path)); 86 } 87 catch (Exception e) 88 { 89 mLogger.error("ERROR in tag",e); 90 throw new JspException ("Error redirecting to forward "+name); 91 } 92 } 93 else 94 { 95 try 96 { 97 pageContext.forward(path); 98 } 99 catch (Exception e) 100 { 101 mLogger.error("ERROR in tag",e); 102 throw new JspException ("Error forward to "+name); 103 } 104 } 105 106 return (SKIP_PAGE); 108 } 109 return Tag.SKIP_BODY; 110 } 111 112 116 public void release() 117 { 118 super.release(); 119 name = null; 120 } 121 } 122 123 | Popular Tags |