001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013:
014: package com.sun.portal.portlet.taglib;
015:
016: import java.io.IOException;
017:
018: import javax.servlet.jsp.JspException;
019: import javax.servlet.jsp.JspTagException;
020: import javax.servlet.jsp.PageContext;
021: import javax.servlet.jsp.tagext.BodyTagSupport;
022: import javax.servlet.jsp.tagext.Tag;
023: import javax.servlet.http.HttpServletRequest;
024:
025: import javax.portlet.PortletURL;
026: import javax.portlet.PortletMode;
027: import javax.portlet.PortletModeException;
028: import javax.portlet.PortletSecurityException;
029: import javax.portlet.RenderResponse;
030: import javax.portlet.WindowState;
031: import javax.portlet.WindowStateException;
032:
033: abstract public class PortletURLTag extends BodyTagSupport {
034:
035: protected String _mode;
036: protected String _state;
037: protected String _var;
038: protected String _secure;
039: protected PortletURL _portletURL;
040:
041: public int doStartTag() throws JspException {
042:
043: HttpServletRequest req = (HttpServletRequest) pageContext
044: .getRequest();
045:
046: RenderResponse rRes = (RenderResponse) req
047: .getAttribute(PortletTaglibConstants.RENDER_RESPONSE_ATTRIBUTE);
048:
049: _portletURL = createURL(rRes);
050:
051: try {
052: if (_mode != null) {
053: _portletURL.setPortletMode(new PortletMode(_mode
054: .toLowerCase()));
055:
056: }
057: if (_state != null) {
058: _portletURL.setWindowState(new WindowState(_state
059: .toLowerCase()));
060: }
061: if (_secure != null) {
062: _secure = _secure.toLowerCase();
063: if (_secure.equals("true")) {
064: _portletURL.setSecure(true);
065: } else if (_secure.equals("false")) {
066: _portletURL.setSecure(false);
067: } else {
068: throw new JspException(
069: "invalid value for attribute secure");
070: }
071: }
072: } catch (PortletModeException e) {
073: throw new JspException("invalid value for attribute mode",
074: e);
075: } catch (WindowStateException e) {
076: throw new JspException("invalid value for attribute state",
077: e);
078: } catch (PortletSecurityException e) {
079: throw new JspException(
080: "invalid value for attribute secure", e);
081: }
082: return EVAL_BODY_INCLUDE;
083: }
084:
085: public int doEndTag() throws JspException {
086:
087: String urlString = _portletURL.toString();
088: if (_var == null) {
089: try {
090: pageContext.getOut().print(urlString);
091: } catch (IOException e) {
092: throw new JspTagException(
093: "Error: IOException while writing");
094: }
095: } else {
096: pageContext.setAttribute(_var, urlString);
097: }
098: return EVAL_PAGE;
099: }
100:
101: public void setParent(Tag t) {
102: super .setParent(t);
103: _mode = null;
104: _state = null;
105: _var = null;
106: _secure = null;
107: _portletURL = null;
108: }
109:
110: public void setPortletMode(String mode) {
111: _mode = mode;
112: }
113:
114: public void setWindowState(String state) {
115: _state = state;
116: }
117:
118: public void setVar(String var) {
119: _var = var;
120: }
121:
122: public PortletURL getURL() {
123: return _portletURL;
124: }
125:
126: public void setSecure(String secure) {
127: _secure = secure;
128: }
129:
130: abstract public PortletURL createURL(RenderResponse res);
131: }
|