001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.webapp.taglib;
019:
020: import java.io.IOException;
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.jsp.JspException;
023: import javax.servlet.jsp.tagext.BodyContent;
024: import javax.servlet.jsp.tagext.BodyTagSupport;
025:
026: import org.ofbiz.base.util.Debug;
027: import org.ofbiz.base.util.UtilJ2eeCompat;
028: import org.ofbiz.base.util.UtilProperties;
029: import org.ofbiz.base.util.UtilValidate;
030: import org.ofbiz.webapp.website.WebSiteWorker;
031: import org.ofbiz.entity.GenericValue;
032:
033: /**
034: * ContentUrlTag - Creates a URL string prepending the content prefix from url.properties
035: */
036: public class ContentUrlTag extends BodyTagSupport {
037:
038: public static final String module = UrlTag.class.getName();
039:
040: public static void appendContentPrefix(HttpServletRequest request,
041: StringBuffer urlBuffer) {
042: if (request == null) {
043: Debug
044: .logWarning(
045: "WARNING: request was null in appendContentPrefix; this probably means this was used where it shouldn't be, like using ofbizContentUrl in a screen rendered through a service; using best-bet behavior: standard prefix from url.properties (no WebSite or security setting known)",
046: module);
047: String prefix = UtilProperties.getPropertyValue("url",
048: "content.url.prefix.standard");
049: if (prefix != null) {
050: urlBuffer.append(prefix.trim());
051: }
052: return;
053: }
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: }
|