001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.internal;
020:
021: import javax.servlet.ServletContext;
022: import javax.servlet.ServletRequest;
023:
024: import org.apache.beehive.netui.core.urls.MutableURI;
025: import org.apache.beehive.netui.core.urls.TemplatedURLFormatter;
026: import org.apache.beehive.netui.core.urls.URIContext;
027: import org.apache.beehive.netui.core.urltemplates.URLTemplate;
028: import org.apache.beehive.netui.core.urltemplates.URLTemplatesFactory;
029:
030: /**
031: * Default implementation of TemplatedURLFormatter for formatting URLs
032: * based on templates from a URL template config file.
033: *
034: * <p>
035: * Used by URLRewriterService to apply any relevant templates to a URL,
036: * after all other rewriting has been done on the URL.
037: * </p>
038: */
039: public class DefaultTemplatedURLFormatter extends TemplatedURLFormatter {
040: /**
041: * Format the given URL using a URL template, if defined in a URL
042: * template config file. The {@link org.apache.beehive.netui.core.urls.URIContext}
043: * encapsulates some additional data needed to write out the string form.
044: * E.g. It defines if the "&amp;" entity or the
045: * '&' character should be used to separate quary parameters.
046: *
047: * @param servletContext the current ServletContext.
048: * @param request the current ServletRequest.
049: * @param uri the MutableURI to be formatted into a String.
050: * @param key key for the URL template type to use for formatting the URI
051: * @param uriContext data required to write out the string form.
052: * @return the URL as a <code>String</code>
053: */
054: public String getTemplatedURL(ServletContext servletContext,
055: ServletRequest request, MutableURI uri, String key,
056: URIContext uriContext) {
057: // Look for the template config and get the right template.
058: // If it is found, apply the value to the template.
059: String result = null;
060: URLTemplate template = null;
061: URLTemplatesFactory factory = URLTemplatesFactory
062: .getURLTemplatesFactory(request);
063:
064: if (factory != null) {
065: String templateName = factory.getTemplateNameByRef(
066: DEFAULT_TEMPLATE_REF, key);
067:
068: if (templateName != null) {
069: template = factory.getURLTemplate(templateName);
070: }
071: }
072:
073: if (template != null) {
074: result = formatURIWithTemplate(request, uri, uriContext,
075: template);
076: } else {
077: // no template found, just return the uri as a String...
078: result = uri.getURIString(uriContext);
079: }
080:
081: return result;
082: }
083:
084: private String formatURIWithTemplate(ServletRequest request,
085: MutableURI uri, URIContext uriContext, URLTemplate template) {
086: String scheme = uri.getScheme();
087: String host = uri.getHost();
088: int port = uri.getPort();
089:
090: if (scheme == null || scheme.length() == 0) {
091: scheme = request.getScheme();
092: }
093:
094: if (host == null || host.length() == 0) {
095: host = request.getServerName();
096: }
097:
098: if (port < 0) {
099: port = request.getServerPort();
100: }
101:
102: template.substitute(TemplatedURLFormatter.SCHEME_TOKEN, scheme);
103: template.substitute(TemplatedURLFormatter.DOMAIN_TOKEN, host);
104: template.substitute(TemplatedURLFormatter.PORT_TOKEN, port);
105: template.substitute(TemplatedURLFormatter.PATH_TOKEN, uri
106: .getPath());
107:
108: String query = null;
109: query = uri.getQuery(uriContext);
110:
111: if (query == null) {
112: query = "";
113: }
114:
115: template.substitute(TemplatedURLFormatter.QUERY_STRING_TOKEN,
116: query);
117:
118: String fragment = uri.getFragment();
119:
120: if (fragment == null) {
121: fragment = "";
122: }
123:
124: template.substitute(TemplatedURLFormatter.FRAGMENT_TOKEN,
125: fragment);
126:
127: return template.format();
128: }
129: }
|