001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: Redirect.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.engine.elements;
009:
010: import com.uwyn.rife.engine.Element;
011: import com.uwyn.rife.engine.ElementInfo;
012: import com.uwyn.rife.engine.annotations.Elem;
013: import com.uwyn.rife.engine.exceptions.PropertyRequiredException;
014: import com.uwyn.rife.tools.Localization;
015: import com.uwyn.rife.tools.StringUtils;
016:
017: @Elem
018: public class Redirect extends Element {
019: public void processElement() {
020: String url = null;
021:
022: // obtain the mandatory to property
023: String to = null;
024: if (hasProperty("to")) {
025: to = getPropertyString("to");
026: }
027:
028: to = Localization.extractLocalizedUrl(to);
029:
030: if (null == to) {
031: throw new PropertyRequiredException(getDeclarationName(),
032: "to");
033: }
034:
035: // obtain the type property
036: String type = "url";
037: if (hasProperty("type")) {
038: type = getPropertyString("type");
039: }
040:
041: // interprete to according to the redirect type
042: if (type.equals("url")) {
043: url = to;
044: } else if (type.equals("element")) {
045: ElementInfo element_info = getSite().resolveId(to,
046: getElementInfo());
047: if (null == element_info) {
048: throw new IllegalArgumentException("The element '"
049: + getDeclarationName()
050: + "' specified an unknown element id '" + to
051: + "' for redirection.");
052: }
053: url = element_info.getUrl();
054: }
055:
056: // verify if the url was successfully obtained
057: if (null == url) {
058: throw new IllegalArgumentException("The element '"
059: + getDeclarationName()
060: + "' declared an unsupported redirect type '"
061: + type + "'.");
062: }
063:
064: String redirect = null;
065:
066: // handle full-blown urls
067: if (url.startsWith("http:")) {
068: redirect = url;
069: }
070: // handle absolute urls
071: else if (url.startsWith("/")) {
072: String root = getWebappRootUrl();
073: root = root.substring(0, root.length() - 1);
074:
075: StringBuilder buffer = new StringBuilder(root);
076: buffer.append(url);
077: redirect = buffer.toString();
078: }
079: // handle relative urls
080: else {
081: String root = getWebappRootUrl();
082:
083: StringBuilder buffer = new StringBuilder(root);
084:
085: String element_url = getElementInfo().getUrl();
086: int last_slash = element_url.lastIndexOf("/");
087: if (last_slash != -1) {
088: buffer.append(element_url.substring(0, last_slash));
089: }
090:
091: buffer.append(url);
092:
093: redirect = buffer.toString();
094: }
095:
096: // obtain the redirectInputs property
097: boolean redirect_inputs = false;
098: if (hasProperty("redirectInputs")) {
099: Object value = getProperty("redirectInputs");
100: if (value instanceof Boolean) {
101: redirect_inputs = ((Boolean) value).booleanValue();
102: } else {
103: redirect_inputs = StringUtils.convertToBoolean(String
104: .valueOf(value));
105: }
106: }
107:
108: if (redirect_inputs) {
109: String anchor = null;
110: int index_anchor = redirect.indexOf('#');
111: if (index_anchor != -1) {
112: redirect = redirect.substring(0, index_anchor);
113: anchor = redirect.substring(index_anchor);
114: }
115:
116: String query = null;
117: int index_query = redirect.indexOf('?');
118: if (index_query != -1) {
119: redirect = redirect.substring(0, index_query);
120: query = redirect.substring(index_query + 1);
121: }
122:
123: boolean has_querystring_seperator = false;
124:
125: StringBuilder buffer = new StringBuilder(redirect);
126: for (String input : getElementInfo().getInputNames()) {
127: // add query string parameters
128: String[] values = getInputValues(input);
129: if (null == values) {
130: continue;
131: }
132:
133: for (String value : values) {
134: if (!has_querystring_seperator) {
135: buffer.append("?");
136: has_querystring_seperator = true;
137: } else {
138: buffer.append("&");
139: }
140:
141: buffer.append(StringUtils.encodeUrl(input));
142: buffer.append("=");
143: buffer.append(StringUtils.encodeUrl(value));
144: }
145: }
146:
147: // add the previous query string
148: if (query != null) {
149: if (!has_querystring_seperator) {
150: buffer.append("?");
151: }
152: buffer.append(query);
153: }
154:
155: // add the previous anchor
156: if (anchor != null) {
157: buffer.append(anchor);
158: }
159:
160: redirect = buffer.toString();
161: }
162:
163: sendRedirect(redirect);
164: }
165: }
|