001: /*
002: * $Id: ContentUrlTag.java,v 1.4 2004/01/17 22:10:13 ajzeneski Exp $
003: *
004: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.content.webapp.taglib;
026:
027: import java.io.IOException;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.jsp.JspException;
031: import javax.servlet.jsp.tagext.BodyContent;
032: import javax.servlet.jsp.tagext.BodyTagSupport;
033:
034: import org.ofbiz.base.util.Debug;
035: import org.ofbiz.base.util.UtilJ2eeCompat;
036: import org.ofbiz.base.util.UtilProperties;
037: import org.ofbiz.base.util.UtilValidate;
038: import org.ofbiz.content.website.WebSiteWorker;
039: import org.ofbiz.entity.GenericValue;
040:
041: /**
042: * ContentUrlTag - Creates a URL string prepending the content prefix from url.properties
043: *
044: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
045: * @version $Revision: 1.4 $
046: * @since 2.0
047: */
048: public class ContentUrlTag extends BodyTagSupport {
049:
050: public static final String module = UrlTag.class.getName();
051:
052: public static void appendContentPrefix(HttpServletRequest request,
053: StringBuffer urlBuffer) {
054: GenericValue webSite = WebSiteWorker.getWebSite(request);
055: if (request.isSecure()) {
056: if (webSite != null
057: && UtilValidate.isNotEmpty(webSite
058: .getString("secureContentPrefix"))) {
059: urlBuffer.append(webSite.getString(
060: "secureContentPrefix").trim());
061: } else {
062: String prefix = UtilProperties.getPropertyValue("url",
063: "content.url.prefix.secure");
064: if (prefix != null) {
065: urlBuffer.append(prefix.trim());
066: }
067: }
068: } else {
069: if (webSite != null
070: && UtilValidate.isNotEmpty(webSite
071: .getString("standardContentPrefix"))) {
072: urlBuffer.append(webSite.getString(
073: "standardContentPrefix").trim());
074: } else {
075: String prefix = UtilProperties.getPropertyValue("url",
076: "content.url.prefix.standard");
077: if (prefix != null) {
078: urlBuffer.append(prefix.trim());
079: }
080: }
081: }
082: }
083:
084: public static String getContentPrefix(HttpServletRequest request) {
085: StringBuffer buf = new StringBuffer();
086: ContentUrlTag.appendContentPrefix(request, buf);
087: return buf.toString();
088: }
089:
090: public int doEndTag() throws JspException {
091: HttpServletRequest request = (HttpServletRequest) pageContext
092: .getRequest();
093:
094: BodyContent body = getBodyContent();
095: String bodyString = body.getString();
096:
097: StringBuffer newURL = new StringBuffer();
098:
099: appendContentPrefix(request, newURL);
100: newURL.append(bodyString);
101: body.clearBody();
102:
103: try {
104: getPreviousOut().print(newURL.toString());
105: } catch (IOException e) {
106: if (UtilJ2eeCompat.useNestedJspException(pageContext
107: .getServletContext())) {
108: throw new JspException(e.getMessage(), e);
109: } else {
110: Debug
111: .logError(
112: e,
113: "Server does not support nested exceptions, here is the exception",
114: module);
115: throw new JspException(e.toString());
116: }
117: }
118: return SKIP_BODY;
119: }
120: }
|