KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > pageflow > HasPageTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib.pageflow;
11
12 import org.mmbase.bridge.jsp.taglib.util.Attribute;
13 import org.mmbase.bridge.jsp.taglib.*;
14 import java.net.*;
15 import java.io.*;
16 import javax.servlet.jsp.JspTagException JavaDoc;
17 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
18 import javax.servlet.http.*;
19 import javax.servlet.*;
20
21 import java.util.*;
22
23 import org.mmbase.util.*;
24 import org.mmbase.util.logging.Logger;
25 import org.mmbase.util.logging.Logging;
26
27 /**
28  *
29  * @author Michiel Meeuwissen
30  * @version $Id: HasPageTag.java,v 1.1 2005/10/18 16:42:34 michiel Exp $
31  * @since MMBase-1.8
32  */

33
34 public class HasPageTag extends ContextReferrerTag implements Condition {
35
36     private static final Logger log = Logging.getLoggerInstance(HasPageTag.class);
37
38     protected Attribute page = Attribute.NULL;
39     public void setPage(String JavaDoc p) throws JspTagException JavaDoc {
40         page = getAttribute(p);
41     }
42     protected Attribute inverse = Attribute.NULL;
43
44     public void setInverse(String JavaDoc b) throws JspTagException JavaDoc {
45         inverse = getAttribute(b);
46     }
47     protected boolean getInverse() throws JspTagException JavaDoc {
48         return inverse.getBoolean(this, false);
49     }
50
51
52
53     public int doStartTag() throws JspTagException JavaDoc {
54         if (page == Attribute.NULL) {
55             throw new JspTagException JavaDoc("Attribute 'page' was not specified");
56         }
57         String JavaDoc resource = page.getString(this);
58         if (! resource.startsWith("/")) {
59             HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
60             // Fetch the current servlet from request attribute.
61
// This is needed when we are resolving relatively.
62
String JavaDoc includingServlet = (String JavaDoc) request.getAttribute(IncludeTag.INCLUDE_PATH_KEY);
63             if (includingServlet == null) {
64                 includingServlet = request.getServletPath();
65             }
66             try {
67                 log.debug("URL was relative");
68                 // Using url-objects only because they know how to resolve relativity
69
URL u = new URL("http", "localhost", includingServlet);
70                 URL dir = new URL(u, "."); // directory
71
File currentDir = new File(includingServlet + "includetagpostfix"); // to make sure that it is not a directory (tomcat 5 does not redirect then)
72
resource = new URL(dir, resource).getFile();
73             } catch (MalformedURLException mfue) {
74                 log.error(mfue);
75             }
76             
77         }
78         try {
79             if (ResourceLoader.getWebRoot().getResource(resource).openConnection().getDoInput() != getInverse()) {
80                 return EVAL_BODY;
81             } else {
82                 return SKIP_BODY;
83             }
84         } catch (IOException ioe) {
85             log.error(ioe);
86             return SKIP_BODY;
87         }
88     }
89     
90     public int doAfterBody() throws JspTagException JavaDoc {
91         if (EVAL_BODY == EVAL_BODY_BUFFERED) { // not needed if EVAL_BODY_INCLUDE
92
if (bodyContent != null) {
93                 try{
94                     if(bodyContent != null) {
95                         bodyContent.writeOut(bodyContent.getEnclosingWriter());
96                     }
97                 } catch(java.io.IOException JavaDoc e){
98                     throw new TaglibException(e);
99                 }
100             }
101         }
102         return SKIP_BODY;
103     }
104 }
105
Popular Tags