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.widget;
019:
020: import java.util.Map;
021: import javax.servlet.ServletContext;
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpServletResponse;
024:
025: import org.ofbiz.base.util.UtilHttp;
026: import org.ofbiz.base.util.UtilValidate;
027: import org.ofbiz.webapp.control.RequestHandler;
028: import org.ofbiz.webapp.taglib.ContentUrlTag;
029:
030: public class WidgetWorker {
031:
032: public static final String module = WidgetWorker.class.getName();
033:
034: public WidgetWorker() {
035: }
036:
037: public static void buildHyperlinkUrl(StringBuffer buffer,
038: String requestName, String targetType,
039: HttpServletRequest request, HttpServletResponse response,
040: Map context) {
041: String localRequestName = UtilHttp
042: .encodeAmpersands(requestName);
043:
044: if ("intra-app".equals(targetType)) {
045: appendOfbizUrl(buffer, "/" + localRequestName, request,
046: response);
047: } else if ("inter-app".equals(targetType)) {
048: String fullTarget = localRequestName;
049: buffer.append(fullTarget);
050: String externalLoginKey = (String) request
051: .getAttribute("externalLoginKey");
052: if (UtilValidate.isNotEmpty(externalLoginKey)) {
053: if (fullTarget.indexOf('?') == -1) {
054: buffer.append('?');
055: } else {
056: buffer.append("&");
057: }
058: buffer.append("externalLoginKey=");
059: buffer.append(externalLoginKey);
060: }
061: } else if ("content".equals(targetType)) {
062: appendContentUrl(buffer, localRequestName, request);
063: } else if ("plain".equals(targetType)) {
064: buffer.append(localRequestName);
065: } else {
066: buffer.append(localRequestName);
067: }
068:
069: }
070:
071: public static void appendOfbizUrl(StringBuffer buffer,
072: String location, HttpServletRequest request,
073: HttpServletResponse response) {
074: ServletContext ctx = (ServletContext) request
075: .getAttribute("servletContext");
076: RequestHandler rh = (RequestHandler) ctx
077: .getAttribute("_REQUEST_HANDLER_");
078: // make and append the link
079: buffer.append(rh.makeLink(request, response, location));
080: }
081:
082: public static void appendContentUrl(StringBuffer buffer,
083: String location, HttpServletRequest request) {
084: ContentUrlTag.appendContentPrefix(request, buffer);
085: buffer.append(location);
086: }
087:
088: public static void makeHyperlinkString(StringBuffer buffer,
089: String linkStyle, String targetType, String target,
090: String description, HttpServletRequest request,
091: HttpServletResponse response, Map context,
092: String targetWindow) {
093: if (UtilValidate.isNotEmpty(description)) {
094: buffer.append("<a");
095:
096: if (UtilValidate.isNotEmpty(linkStyle)) {
097: buffer.append(" class=\"");
098: buffer.append(linkStyle);
099: buffer.append("\"");
100: }
101:
102: buffer.append(" href=\"");
103:
104: WidgetWorker.buildHyperlinkUrl(buffer, target, targetType,
105: request, response, context);
106:
107: buffer.append("\"");
108:
109: if (UtilValidate.isNotEmpty(targetWindow)) {
110: buffer.append(" target=\"");
111: buffer.append(targetWindow);
112: buffer.append("\"");
113: }
114:
115: buffer.append('>');
116:
117: buffer.append(description);
118: buffer.append("</a>");
119: }
120: }
121: }
|