001: package org.apache.turbine.util.uri;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.turbine.util.RunData;
023: import org.apache.turbine.util.ServerData;
024:
025: /**
026: * This class can convert a simple link into a turbine relative
027: * URL. It should be used to convert references for images, style
028: * sheets and similar references.
029: *
030: * The resulting links have no query data or path info. If you need
031: * this, use TurbineURI or TemplateURI.
032: *
033: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
034: * @version $Id: DataURI.java 534527 2007-05-02 16:10:59Z tv $
035: *
036: */
037:
038: public class DataURI extends BaseURI implements URIConstants {
039: /**
040: * Empty C'tor. Uses Turbine.getDefaultServerData().
041: *
042: */
043: public DataURI() {
044: super ();
045: }
046:
047: /**
048: * Constructor with a RunData object
049: *
050: * @param runData A RunData object
051: */
052: public DataURI(RunData runData) {
053: super (runData);
054: }
055:
056: /**
057: * Constructor, set explicit redirection
058: *
059: * @param runData A RunData object
060: * @param redirect True if redirection allowed.
061: */
062: public DataURI(RunData runData, boolean redirect) {
063: super (runData, redirect);
064: }
065:
066: /**
067: * Constructor with a ServerData object
068: *
069: * @param serverData A ServerData object
070: */
071: public DataURI(ServerData serverData) {
072: super (serverData);
073: }
074:
075: /**
076: * Constructor, set explicit redirection
077: *
078: * @param serverData A ServerData object
079: * @param redirect True if redirection allowed.
080: */
081: public DataURI(ServerData serverData, boolean redirect) {
082: super (serverData, redirect);
083: }
084:
085: /**
086: * Content Tool wants to be able to turn the encoding
087: * of the servlet container off. After calling this method,
088: * the encoding will not happen any longer.
089: */
090: public void clearResponse() {
091: setResponse(null);
092: }
093:
094: /**
095: * Builds the URL with all of the data URL-encoded as well as
096: * encoded using HttpServletResponse.encodeUrl(). The resulting
097: * URL is absolute; it starts with http/https...
098: *
099: * <p>
100: * <code><pre>
101: * TurbineURI tui = new TurbineURI (data, "UserScreen");
102: * tui.addPathInfo("user","jon");
103: * tui.getAbsoluteLink();
104: * </pre></code>
105: *
106: * The above call to absoluteLink() would return the String:
107: *
108: * <p>
109: * http://www.server.com/servlets/Turbine/screen/UserScreen/user/jon
110: *
111: * @return A String with the built URL.
112: */
113: public String getAbsoluteLink() {
114: StringBuffer output = new StringBuffer();
115:
116: getSchemeAndPort(output);
117: getContextAndScript(output);
118:
119: if (hasReference()) {
120: output.append('#');
121: output.append(getReference());
122: }
123:
124: //
125: // Encode Response does all the fixup for the Servlet Container
126: //
127: return encodeResponse(output.toString());
128: }
129:
130: /**
131: * Builds the URL with all of the data URL-encoded as well as
132: * encoded using HttpServletResponse.encodeUrl(). The resulting
133: * URL is relative to the webserver root.
134: *
135: * <p>
136: * <code><pre>
137: * TurbineURI tui = new TurbineURI (data, "UserScreen");
138: * tui.addPathInfo("user","jon");
139: * tui.getRelativeLink();
140: * </pre></code>
141: *
142: * The above call to absoluteLink() would return the String:
143: *
144: * <p>
145: * /servlets/Turbine/screen/UserScreen/user/jon
146: *
147: * @return A String with the built URL.
148: */
149: public String getRelativeLink() {
150: StringBuffer output = new StringBuffer();
151:
152: getContextAndScript(output);
153:
154: if (hasReference()) {
155: output.append('#');
156: output.append(getReference());
157: }
158:
159: //
160: // Encode Response does all the fixup for the Servlet Container
161: //
162: return encodeResponse(output.toString());
163: }
164:
165: /**
166: * toString() simply calls getAbsoluteLink. You should not use this in your
167: * code unless you have to. Use getAbsoluteLink.
168: *
169: * @return This URI as a String
170: *
171: */
172: public String toString() {
173: return getAbsoluteLink();
174: }
175: }
|