Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/roller/presentation/website/tags/AuthorizeUserTag.java


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  /** 
22    * If user is not authorized, forward to specified global forward. 
23    * @jsp.tag name="AuthorizeUser" 
24    */
25  public class AuthorizeUserTag extends TagSupport 
26  {
27      private static Log mLogger = LogFactory.getFactory().getInstance(
28          AuthorizeUserTag.class);
29  
30      private String name = null;
31    /** @jsp.attribute required="true" */ 
32      public String getFailureForward() { return (this.name); }
33      public void setFailureForward(String name) {this.name = name;}
34  
35     //----------------------------------------------------------- 
36      /**
37       * Defer generation until the end of this tag is encountered.
38       *
39       * @exception JspException if a JSP exception has occurred
40       */
41      public int doStartTag() throws JspException 
42    {
43      return (SKIP_BODY);
44      }
45  
46     //----------------------------------------------------------- 
47      /**
48       * Process start tag.
49       * @return EVAL_SKIP_BODY
50       */
51      public int doEndTag() throws JspException 
52    {
53      HttpServletRequest req = null;
54      UserData user = null; 
55      try
56      {
57        req = (HttpServletRequest)pageContext.getRequest();
58        user = RollerRequest.getRollerRequest(req).getUser();
59      }
60      catch (Exception e)
61      {
62              mLogger.error("ERROR in tag",e);
63        throw new JspException(e);
64      }
65  
66      Principal prince = req.getUserPrincipal(); 
67      if ( prince == null || !prince.getName().equals( user.getUserName() ) )
68      {
69        ServletContext ctx = req.getSession().getServletContext();
70        ModuleConfig mConfig = RequestUtils.getModuleConfig(req,ctx);
71        ForwardConfig fConfig = mConfig.findForwardConfig(name);
72        if (fConfig==null)
73          throw new JspException("Forward "+name+"not found");
74        
75        // Forward or redirect to the corresponding actual path
76        String path = fConfig.getPath();
77        if (fConfig.getRedirect()) 
78        {
79          HttpServletResponse response =
80            (HttpServletResponse) pageContext.getResponse();
81          try 
82          {
83            response.sendRedirect(response.encodeRedirectURL(path));
84          } 
85          catch (Exception e) 
86          {
87                    mLogger.error("ERROR in tag",e);
88            throw new JspException("Error redirecting to forward "+name);
89          }
90        } 
91        else 
92        {
93          try 
94          {
95            pageContext.forward(path);
96          } 
97          catch (Exception e) 
98          {
99                      mLogger.error("ERROR in tag",e);
100           throw new JspException("Error forward to "+name);
101         }
102       }
103 
104       // Skip the remainder of this page
105       return (SKIP_PAGE);
106     }
107     return Tag.SKIP_BODY;
108     }
109 
110   //------------------------------------------------------------------------
111     /**
112      * Release any acquired resources.
113      */
114     public void release() 
115   {
116     super.release();
117     name = null;
118     }
119 }
120