0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: package org.apache.jetspeed.headerresource.impl;
0018:
0019: import java.util.HashMap;
0020: import java.util.Iterator;
0021: import java.util.LinkedHashSet;
0022: import java.util.Map;
0023: import java.util.List;
0024: import java.util.ArrayList;
0025: import java.util.Set;
0026:
0027: import org.apache.jetspeed.PortalReservedParameters;
0028: import org.apache.jetspeed.container.url.BasePortalURL;
0029: import org.apache.jetspeed.headerresource.HeaderResource;
0030: import org.apache.jetspeed.headerresource.HeaderResourceLib;
0031: import org.apache.jetspeed.request.RequestContext;
0032:
0033: import org.apache.commons.lang.StringUtils;
0034: import org.apache.commons.logging.Log;
0035: import org.apache.commons.logging.LogFactory;
0036:
0037: /**
0038: * Default implementation for HeaderResource
0039: *
0040: * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a>
0041: * @author <a href="mailto:smilek@apache.org">Steve Milek</a>
0042: * @version $Id: HeaderResourceImpl.java 188569 2005-05-13 13:35:18Z weaver $
0043: */
0044: public class HeaderResourceImpl implements HeaderResource {
0045: protected final static Log log = LogFactory
0046: .getLog(HeaderResourceImpl.class);
0047:
0048: protected final static String EOL = "\r\n"; // html eol
0049: protected final static String UNNAMED_CONTENT_HEADER_NAME = "org.apache.jetspeed.headerresource.unnamed";
0050:
0051: private RequestContext requestContext;
0052:
0053: // base portal url to override default url server info from servlet
0054: private BasePortalURL baseUrlAccess = null;
0055:
0056: private boolean isDesktop;
0057:
0058: private Map headerConfiguration;
0059:
0060: // ... mutable output tracking
0061: // - when depending on this feature, one HeaderResourceImpl instance must process all header inclusion
0062: private HashMap namedResourcesAlreadyOutput;
0063:
0064: // ... as needed, these are obtained from request attributes
0065: private Map headerDynamicConfiguration;
0066: private Map headerNamedResources;
0067: private Map headerNamedResourcesAddedFragments;
0068: private Map headerResourceRegistry;
0069:
0070: // ... save generated portal urls to avoid duplicate effort
0071: private String portalBaseUrl;
0072: private String portalUrl;
0073:
0074: /**
0075: * Default Constructor
0076: *
0077: * @param context
0078: */
0079: public HeaderResourceImpl(RequestContext context) {
0080: this .requestContext = context;
0081: }
0082:
0083: public HeaderResourceImpl(RequestContext context,
0084: BasePortalURL baseUrlAccess, boolean isDesktop,
0085: Map headerConfiguration) {
0086: this .requestContext = context;
0087: this .baseUrlAccess = baseUrlAccess;
0088:
0089: this .isDesktop = isDesktop;
0090:
0091: this .headerConfiguration = headerConfiguration;
0092: }
0093:
0094: /**
0095: * Output all content (that has not already been output)
0096: *
0097: * @return content string for inclusion in html <head>
0098: */
0099: public String getContent() {
0100: StringBuffer header = new StringBuffer();
0101: getNamedResourceContent(null, false, header);
0102: getUnnamedContent(header);
0103: return header.toString();
0104: }
0105:
0106: /**
0107: * Output all content (that has not already been output)
0108: *
0109: * @return content string for inclusion in html <head>
0110: */
0111: public String toString() {
0112: return getContent();
0113: }
0114:
0115: /**
0116: * Output all unnamed (getHeaderInfoSet()) content (that has not already been output)
0117: *
0118: * @return content string for inclusion in html <head>
0119: */
0120: public String getUnnamedContent() {
0121: StringBuffer header = new StringBuffer();
0122: getUnnamedContent(header);
0123: return header.toString();
0124: }
0125:
0126: /**
0127: * Output all getHeaderSections() content (that has not already been output)
0128: *
0129: * @return content string for inclusion in html <head>
0130: */
0131: public String getNamedContent() {
0132: StringBuffer header = new StringBuffer();
0133: getNamedResourceContent(null, false, header);
0134: return header.toString();
0135: }
0136:
0137: /**
0138: * Output the one getHeaderSections() content entry with a key that matches headerName (if it has not already been output)
0139: *
0140: * @return content string for inclusion in html <head>
0141: */
0142: public String getNamedContent(String headerName) {
0143: StringBuffer header = new StringBuffer();
0144: getNamedResourceContent(headerName, false, header);
0145: return header.toString();
0146: }
0147:
0148: /**
0149: * Output getHeaderSections() content entries with key prefixes that match headerNamePrefix (if it has not already been output)
0150: *
0151: * @return content string for inclusion in html <head>
0152: */
0153: public String getNamedContentForPrefix(String headerNamePrefix) {
0154: if (headerNamePrefix == null)
0155: headerNamePrefix = "";
0156: if (!headerNamePrefix.endsWith("."))
0157: headerNamePrefix = headerNamePrefix + ".";
0158: StringBuffer header = new StringBuffer();
0159: getNamedResourceContent(headerNamePrefix, true, header);
0160: return header.toString();
0161: }
0162:
0163: /*
0164: * Output all getHeaderInfoSet() content (that has not already been output)
0165: */
0166: protected void getUnnamedContent(StringBuffer header) {
0167: HashMap namedResourcesInOutput = getNamedResourcesAlreadyOutput();
0168: if (namedResourcesInOutput == null) {
0169: namedResourcesInOutput = new HashMap();
0170: setNamedResourcesAlreadyOutput(namedResourcesInOutput);
0171: }
0172: if (!namedResourcesInOutput
0173: .containsKey(UNNAMED_CONTENT_HEADER_NAME)) {
0174: namedResourcesInOutput.put(UNNAMED_CONTENT_HEADER_NAME,
0175: Boolean.TRUE);
0176: Set headerInfoSet = getHeaderInfoSet();
0177: for (Iterator ite = headerInfoSet.iterator(); ite.hasNext();) {
0178: header.append(((HeaderInfo) ite.next()).toString());
0179: header.append(EOL);
0180: }
0181: }
0182: }
0183:
0184: /*
0185: * Output getHeaderSections() content (that has not already been output) with regard to optional match arguments
0186: */
0187: protected void getNamedResourceContent(String headerNameMatch,
0188: boolean headerNameMatchPrefixOnly, StringBuffer header) {
0189: List headerOrderList = getHeaderSectionOrderList(false);
0190: if (headerOrderList != null && headerOrderList.size() > 0) {
0191: HashMap namedResourcesInOutput = getNamedResourcesAlreadyOutput();
0192: if (namedResourcesInOutput == null) {
0193: namedResourcesInOutput = new HashMap();
0194: setNamedResourcesAlreadyOutput(namedResourcesInOutput);
0195: }
0196: Map namedResources = getHeaderSections();
0197: Map dynamicConfig = getHeaderDynamicConfiguration();
0198: Map headerTypes = getHeaderSectionTypes(false);
0199: Map headerRsrcRegistry = getHeaderResourceRegistry();
0200: HashMap headerReqFlagResults = new HashMap();
0201: boolean inScriptBlock = false;
0202: boolean inStyleBlock = false;
0203: Iterator headerOrderListIter = headerOrderList.iterator();
0204: while (headerOrderListIter.hasNext()) {
0205: String headerName = (String) headerOrderListIter.next();
0206: if (namedResourcesInOutput.containsKey(headerName)) {
0207: continue;
0208: }
0209: if (headerNameMatch != null) {
0210: if (headerNameMatchPrefixOnly) {
0211: if (!headerName.startsWith(headerNameMatch)) {
0212: continue;
0213: }
0214: } else {
0215: if (!headerName.equals(headerNameMatch)) {
0216: continue;
0217: }
0218: }
0219: }
0220: boolean includeHeader = true;
0221: Object[] headerTypePair = ((headerTypes != null) ? (Object[]) headerTypes
0222: .get(headerName)
0223: : (Object[]) null);
0224: String headerReqFlag = ((headerTypePair != null) ? (String) headerTypePair[1]
0225: : (String) null);
0226: if (headerReqFlag != null && headerReqFlag.length() > 0) {
0227: Boolean headerReqFlagResult = (Boolean) headerReqFlagResults
0228: .get(headerReqFlag);
0229: if (headerReqFlagResult == null) {
0230: headerReqFlagResult = Boolean.FALSE;
0231: Object headerReqFlagValObj = dynamicConfig
0232: .get(headerReqFlag);
0233: if (headerReqFlagValObj != null)
0234: headerReqFlagResult = new Boolean(
0235: headerReqFlagValObj.toString());
0236: headerReqFlagResults.put(headerReqFlag,
0237: headerReqFlagResult);
0238: }
0239: includeHeader = headerReqFlagResult.booleanValue();
0240: }
0241: if (includeHeader) {
0242: namedResourcesInOutput
0243: .put(headerName, Boolean.TRUE);
0244: Integer headerTypeIdObj = ((headerTypePair != null) ? (Integer) headerTypePair[0]
0245: : (Integer) null);
0246: int headerTypeId = ((headerTypeIdObj != null) ? headerTypeIdObj
0247: .intValue()
0248: : HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK);
0249:
0250: boolean requiresScriptBlock = false;
0251: boolean requiresStyleBlock = false;
0252: boolean preCloseBlock = false;
0253: boolean postCloseBlock = false;
0254:
0255: switch (headerTypeId) {
0256: case HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK: {
0257: requiresScriptBlock = true;
0258: break;
0259: }
0260: case HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK_START: {
0261: preCloseBlock = true;
0262: requiresScriptBlock = true;
0263: break;
0264: }
0265: case HeaderResource.HEADER_TYPE_ID_SCRIPT_TAG: {
0266: preCloseBlock = true;
0267: break;
0268: }
0269: case HeaderResource.HEADER_TYPE_ID_SCRIPT_BLOCK_END: {
0270: postCloseBlock = true;
0271: requiresScriptBlock = true;
0272: break;
0273: }
0274: case HeaderResource.HEADER_TYPE_ID_STYLE_BLOCK: {
0275: requiresStyleBlock = true;
0276: break;
0277: }
0278: case HeaderResource.HEADER_TYPE_ID_LINK_TAG: {
0279: preCloseBlock = true;
0280: break;
0281: }
0282: case HeaderResource.HEADER_TYPE_ID_BASE_TAG: {
0283: preCloseBlock = true;
0284: break;
0285: }
0286: default: {
0287: log
0288: .error("HeaderResource.getNamedResourceContent() cannot include header section with unknown type; header-section-name="
0289: + headerName
0290: + " header-section-type-id="
0291: + headerTypeId);
0292: includeHeader = false;
0293: break;
0294: }
0295: }
0296: if (includeHeader) {
0297: if (requiresScriptBlock && inStyleBlock) {
0298: preCloseBlock = true;
0299: } else if (requiresStyleBlock && inScriptBlock) {
0300: preCloseBlock = true;
0301: }
0302: if (preCloseBlock) {
0303: if (inScriptBlock) {
0304: header.append("</script>").append(EOL);
0305: inScriptBlock = false;
0306: } else if (inStyleBlock) {
0307: header.append("</style>").append(EOL);
0308: inStyleBlock = false;
0309: }
0310: }
0311:
0312: String headerText = (String) namedResources
0313: .get(headerName);
0314: if (headerText == null) {
0315: headerText = generateHeaderSection(headerName);
0316: if (headerText == null
0317: && headerRsrcRegistry != null) {
0318: headerText = (String) headerRsrcRegistry
0319: .get(headerName);
0320: log
0321: .debug("header resource registry text for header section="
0322: + headerName
0323: + " headerText="
0324: + headerText);
0325: }
0326: }
0327: if (headerText != null
0328: && headerText.length() > 0) {
0329: if (requiresScriptBlock && !inScriptBlock) {
0330: header
0331: .append(
0332: "<script language=\"JavaScript\" type=\"text/javascript\">")
0333: .append(EOL);
0334: inScriptBlock = true;
0335: } else if (requiresStyleBlock
0336: && !inStyleBlock) {
0337: header.append("<style>").append(EOL);
0338: inStyleBlock = true;
0339: }
0340: header.append(headerText).append(EOL);
0341: }
0342: if (postCloseBlock) {
0343: if (inScriptBlock) {
0344: header.append("</script>").append(EOL);
0345: inScriptBlock = false;
0346: } else if (inStyleBlock) {
0347: header.append("</style>").append(EOL);
0348: inStyleBlock = false;
0349: }
0350: }
0351: }
0352: } // if ( includeHeader )
0353: } // while ( headerOrderListIter.hasNext() )
0354: if (inScriptBlock) {
0355: header.append("</script>").append(EOL);
0356: inScriptBlock = false;
0357: } else if (inStyleBlock) {
0358: header.append("</style>").append(EOL);
0359: inStyleBlock = false;
0360: }
0361: } // if ( headerOrderList != null && headerOrderList.size() > 0 )
0362: }
0363:
0364: /*
0365: * Intended as derived class hook into late, auto-generated header resources
0366: */
0367: protected String generateHeaderSection(String headerName) {
0368: if (headerName != null) {
0369: if (headerName.equals(HEADER_SECTION_BASE_TAG)) {
0370: return jetspeedGenerateBasetag();
0371: } else if (headerName
0372: .startsWith(HEADER_SECTION_NAME_PREFIX_DOJO)) {
0373: if (headerName.equals(HEADER_SECTION_DOJO_PREINIT)) {
0374: return dojoGeneratePreinit();
0375: } else if (headerName.equals(HEADER_SECTION_DOJO_INIT)) {
0376: return dojoGenerateInit();
0377: } else if (headerName
0378: .equals(HEADER_SECTION_DOJO_WRITEINCLUDES)) {
0379: return dojoGenerateWriteincludes();
0380: } else if (headerName
0381: .equals(HEADER_SECTION_DOJO_STYLE_BODYEXPAND)) {
0382: return dojoGenerateBodyExpandStyle();
0383: } else if (headerName
0384: .equals(HEADER_SECTION_DOJO_STYLE_BODYEXPAND_NOSCROLL)) {
0385: return dojoGenerateBodyExpandNoScrollStyle();
0386: }
0387: }
0388: }
0389: return null;
0390: }
0391:
0392: /**
0393: * Add text argument to the getHeaderSections() content entry with a key that matches addToHeaderName argument
0394: *
0395: */
0396: public void addHeaderSectionFragment(String addToHeaderName,
0397: String text) {
0398: addHeaderSectionFragment(null, addToHeaderName, text, false);
0399: }
0400:
0401: /**
0402: * If no previous call using value of headerFragmentName argument has been added to any getHeaderSections() content entry,
0403: * add text argument to the getHeaderSections() content entry with a key that matches addToHeaderName argument
0404: *
0405: */
0406: public void addHeaderSectionFragment(String headerFragmentName,
0407: String addToHeaderName, String text) {
0408: addHeaderSectionFragment(headerFragmentName, addToHeaderName,
0409: text, false);
0410: }
0411:
0412: protected void addHeaderSectionFragment(String headerFragmentName,
0413: String addToHeaderName, String text,
0414: boolean alreadyCheckedFragName) {
0415: if (addToHeaderName != null && text != null) {
0416: boolean addText = true;
0417: if (!alreadyCheckedFragName
0418: && headerFragmentName != null
0419: && hasHeaderSectionFragment(headerFragmentName,
0420: true)) {
0421: addText = false;
0422: }
0423: if (addText) {
0424: Map headerRsrcRegistry = getHeaderResourceRegistry();
0425: if (headerRsrcRegistry != null) {
0426: String overrideText = (String) headerRsrcRegistry
0427: .get(headerFragmentName);
0428: if (overrideText != null) {
0429: text = overrideText;
0430: }
0431: }
0432: Map namedResources = getHeaderSections();
0433: String nText = (String) namedResources
0434: .get(addToHeaderName);
0435: if (nText == null) {
0436: nText = text + EOL;
0437: orderHeaderSection(addToHeaderName);
0438: } else {
0439: nText = nText + text + EOL;
0440: }
0441: namedResources.put(addToHeaderName, nText);
0442: }
0443: }
0444: }
0445:
0446: /**
0447: * Indicate whether value of headerFragmentName argument has been used to add to any getHeaderSections() content entry
0448: *
0449: * @return true if headerFragmentName argument has been used to add to any getHeaderSections() content entry
0450: */
0451: public boolean hasHeaderSectionFragment(String headerFragmentName) {
0452: return hasHeaderSectionFragment(headerFragmentName, false);
0453: }
0454:
0455: protected boolean hasHeaderSectionFragment(
0456: String headerFragmentName, boolean setToTrue) {
0457: if (headerFragmentName != null) {
0458: Map namedResourcesAddedFragments = getHeaderSectionsAddedFragments();
0459: if (namedResourcesAddedFragments
0460: .containsKey(headerFragmentName)) {
0461: return true;
0462: } else if (setToTrue) {
0463: namedResourcesAddedFragments.put(headerFragmentName,
0464: Boolean.TRUE);
0465: }
0466: }
0467: return false;
0468: }
0469:
0470: protected void orderHeaderSection(String headerName) {
0471: if (headerName != null) {
0472: Map headerNames = getHeaderSectionNames(true);
0473: if (!headerNames.containsKey(headerName)) {
0474: List headerOrderList = getHeaderSectionOrderList(true);
0475:
0476: headerOrderList.add(headerName);
0477: headerNames.put(headerName, Boolean.TRUE);
0478: }
0479: }
0480: }
0481:
0482: /**
0483: * Indicate whether value of headerName is an included header section
0484: *
0485: * @return true if headerName argument is an included header section
0486: */
0487: public boolean isHeaderSectionIncluded(String headerName) {
0488: if (headerName != null) {
0489: Map headerNames = getHeaderSectionNames(false);
0490: if (headerNames != null
0491: && headerNames.get(headerName) != null) {
0492: return true;
0493: }
0494: }
0495: return false;
0496: }
0497:
0498: /**
0499: * Get the type of the getHeaderSections() content entry with a key that matches headerName argument
0500: *
0501: * @return type of header section
0502: */
0503: public String getHeaderSectionType(String headerName) {
0504: if (headerName != null) {
0505: Map headerTypes = getHeaderSectionTypes(false);
0506: if (headerTypes != null) {
0507: Object[] headerTypePair = (Object[]) headerTypes
0508: .get(headerName);
0509: if (headerTypePair != null) {
0510: Integer headerTypeId = (Integer) headerTypePair[0];
0511: return HeaderResourceLib
0512: .getHeaderType(headerTypeId);
0513: }
0514: }
0515: }
0516: return null;
0517: }
0518:
0519: /**
0520: * Set the type of the getHeaderSections() content entry with a key that matches headerName argument
0521: * to the value of the headerType argument
0522: */
0523: public void setHeaderSectionType(String headerName,
0524: String headerType) {
0525: if (headerName != null) {
0526: int headerTypeId = HeaderResourceLib
0527: .getHeaderTypeId(headerType);
0528: if (headerTypeId < 0) {
0529: log
0530: .error("HeaderResourceImpl.setHeaderSectionType() ignoring specification of unknown header section type; header-section-name="
0531: + headerName
0532: + " header-section-type="
0533: + headerType);
0534: } else {
0535: Map headerTypes = getHeaderSectionTypes(true);
0536: Object[] headerTypePair = (Object[]) headerTypes
0537: .get(headerName);
0538: if (headerTypePair == null) {
0539: if (headerType != null) {
0540: headerTypePair = new Object[] {
0541: new Integer(headerTypeId), null };
0542: headerTypes.put(headerName, headerTypePair);
0543: }
0544: } else {
0545: headerTypePair[0] = new Integer(headerTypeId);
0546: }
0547: }
0548: }
0549: }
0550:
0551: /**
0552: * Get the requiredflag of the getHeaderSections() content entry with a key that matches headerName argument
0553: *
0554: * @return requiredflag for header section
0555: */
0556: public String getHeaderSectionRequiredFlag(String headerName) {
0557: if (headerName != null) {
0558: Map headerTypes = getHeaderSectionTypes(false);
0559: if (headerTypes != null) {
0560: Object[] headerTypePair = (Object[]) headerTypes
0561: .get(headerName);
0562: if (headerTypePair != null) {
0563: return (String) headerTypePair[1];
0564: }
0565: }
0566: }
0567: return null;
0568: }
0569:
0570: /**
0571: * Set the requiredflag of the getHeaderSections() content entry with a key that matches headerName argument
0572: * to the value of the headerReqFlag argument
0573: */
0574: public void setHeaderSectionRequiredFlag(String headerName,
0575: String headerReqFlag) {
0576: if (headerName != null) {
0577: if (headerReqFlag != null && headerReqFlag.length() == 0)
0578: headerReqFlag = null;
0579:
0580: Map headerTypes = getHeaderSectionTypes(true);
0581: Object[] headerTypePair = (Object[]) headerTypes
0582: .get(headerName);
0583: if (headerTypePair == null) {
0584: if (headerReqFlag != null) {
0585: headerTypePair = new Object[] { null, headerReqFlag };
0586: headerTypes.put(headerName, headerTypePair);
0587: }
0588: } else {
0589: headerTypePair[1] = headerReqFlag;
0590: }
0591: }
0592: }
0593:
0594: protected Map getHeaderSectionTypes(boolean create) {
0595: Map dynamicConfig = getHeaderDynamicConfiguration();
0596: Map headerTypes = (Map) dynamicConfig.get(HEADER_CONFIG_TYPES);
0597: if (headerTypes == null && create) {
0598: headerTypes = new HashMap();
0599: dynamicConfig.put(HEADER_CONFIG_TYPES, headerTypes);
0600: }
0601: return headerTypes;
0602: }
0603:
0604: protected Map getHeaderSectionNames(boolean create) {
0605: Map dynamicConfig = getHeaderDynamicConfiguration();
0606: Map headerNames = (Map) dynamicConfig
0607: .get(HEADER_INTERNAL_INCLUDED_NAMES);
0608: if (headerNames == null && create) {
0609: headerNames = new HashMap();
0610: dynamicConfig.put(HEADER_INTERNAL_INCLUDED_NAMES,
0611: headerNames);
0612: }
0613: return headerNames;
0614: }
0615:
0616: protected List getHeaderSectionOrderList(boolean create) {
0617: Map dynamicConfig = getHeaderDynamicConfiguration();
0618: List headerOrderList = (List) dynamicConfig
0619: .get(HEADER_CONFIG_ORDER);
0620: if (headerOrderList == null) {
0621: headerOrderList = new ArrayList();
0622: dynamicConfig.put(HEADER_CONFIG_ORDER, headerOrderList);
0623: }
0624: return headerOrderList;
0625: }
0626:
0627: /**
0628: * Access modifiable header configuration settings
0629: *
0630: * @return Map containing modifiable header configuration settings
0631: */
0632: public Map getHeaderDynamicConfiguration() {
0633: if (this .headerDynamicConfiguration == null) {
0634: this .headerDynamicConfiguration = (Map) requestContext
0635: .getAttribute(PortalReservedParameters.HEADER_CONFIGURATION_ATTRIBUTE);
0636: if (this .headerDynamicConfiguration == null) {
0637: this .headerDynamicConfiguration = new HashMap();
0638: requestContext
0639: .setAttribute(
0640: PortalReservedParameters.HEADER_CONFIGURATION_ATTRIBUTE,
0641: this .headerDynamicConfiguration);
0642: }
0643: }
0644: return this .headerDynamicConfiguration;
0645: }
0646:
0647: protected Map getHeaderSections() {
0648: if (this .headerNamedResources == null) {
0649: this .headerNamedResources = (Map) requestContext
0650: .getAttribute(PortalReservedParameters.HEADER_NAMED_RESOURCE_ATTRIBUTE);
0651: if (this .headerNamedResources == null) {
0652: this .headerNamedResources = new HashMap();
0653: requestContext
0654: .setAttribute(
0655: PortalReservedParameters.HEADER_NAMED_RESOURCE_ATTRIBUTE,
0656: this .headerNamedResources);
0657: }
0658: }
0659: return this .headerNamedResources;
0660: }
0661:
0662: protected Map getHeaderSectionsAddedFragments() {
0663: if (this .headerNamedResourcesAddedFragments == null) {
0664: this .headerNamedResourcesAddedFragments = (Map) requestContext
0665: .getAttribute(PortalReservedParameters.HEADER_NAMED_RESOURCE_ADDED_FRAGMENTS_ATTRIBUTE);
0666: if (this .headerNamedResourcesAddedFragments == null) {
0667: this .headerNamedResourcesAddedFragments = new HashMap();
0668: requestContext
0669: .setAttribute(
0670: PortalReservedParameters.HEADER_NAMED_RESOURCE_ADDED_FRAGMENTS_ATTRIBUTE,
0671: this .headerNamedResourcesAddedFragments);
0672: }
0673: }
0674: return this .headerNamedResourcesAddedFragments;
0675: }
0676:
0677: protected Map getHeaderResourceRegistry() {
0678: if (this .headerResourceRegistry == null) {
0679: this .headerResourceRegistry = (Map) requestContext
0680: .getAttribute(PortalReservedParameters.HEADER_NAMED_RESOURCE_REGISTRY_ATTRIBUTE);
0681: if (this .headerResourceRegistry == null) {
0682: this .headerResourceRegistry = new HashMap();
0683: }
0684: }
0685: return this .headerResourceRegistry;
0686: }
0687:
0688: protected RequestContext getRequestContext() {
0689: return this .requestContext;
0690: }
0691:
0692: protected BasePortalURL getBaseUrlAccess() {
0693: return this .baseUrlAccess;
0694: }
0695:
0696: /**
0697: * Is request for /desktop rather than /portal
0698: *
0699: * @return true if request is for /desktop, false if request is for /portal
0700: */
0701: public boolean isDesktop() {
0702: return this .isDesktop;
0703: }
0704:
0705: /**
0706: * Access complete header configuration settings
0707: *
0708: * @return unmodifiable Map containing complete header configuration settings
0709: */
0710: public Map getHeaderConfiguration() {
0711: return this .headerConfiguration;
0712: }
0713:
0714: protected HashMap getNamedResourcesAlreadyOutput() {
0715: return this .namedResourcesAlreadyOutput;
0716: }
0717:
0718: protected void setNamedResourcesAlreadyOutput(HashMap newOne) {
0719: this .namedResourcesAlreadyOutput = newOne;
0720: }
0721:
0722: // get portal urls - a copy of each of these methods exists in JetspeedDesktopContextImpl.java
0723:
0724: /**
0725: * Portal base url ( e.g. http://localhost:8080/jetspeed )
0726: *
0727: * @return portal base url
0728: */
0729: public String getPortalBaseUrl() {
0730: if (this .portalBaseUrl == null) {
0731: this .portalBaseUrl = HeaderResourceLib.getPortalBaseUrl(
0732: this .requestContext, this .baseUrlAccess);
0733: }
0734: return this .portalBaseUrl;
0735: }
0736:
0737: /**
0738: * Portal base url ( e.g. http://localhost:8080/jetspeed )
0739: *
0740: * @return portal base url
0741: */
0742: public String getPortalBaseUrl(boolean encode) {
0743: String baseurl = getPortalBaseUrl();
0744: if (!encode) {
0745: return baseurl;
0746: } else {
0747: return requestContext.getResponse().encodeURL(baseurl);
0748: }
0749: }
0750:
0751: /**
0752: * Portal base url with relativePath argument appended ( e.g. http://localhost:8080/jetspeed/javascript/dojo/ )
0753: *
0754: * @return portal base url with relativePath argument appended
0755: */
0756: public String getPortalResourceUrl(String relativePath) {
0757: return getPortalResourceUrl(relativePath, false);
0758: }
0759:
0760: /**
0761: * Portal base url with relativePath argument appended ( e.g. http://localhost:8080/jetspeed/javascript/dojo/ )
0762: *
0763: * @return portal base url with relativePath argument appended
0764: */
0765: public String getPortalResourceUrl(String relativePath,
0766: boolean encode) {
0767: return HeaderResourceLib.getPortalResourceUrl(relativePath,
0768: getPortalBaseUrl(), encode, this .requestContext);
0769: }
0770:
0771: /**
0772: * Portal base servlet url ( e.g. http://localhost:8080/jetspeed/desktop/ )
0773: *
0774: * @return portal base servlet url
0775: */
0776: public String getPortalUrl() {
0777: if (this .portalUrl == null) {
0778: this .portalUrl = HeaderResourceLib.getPortalUrl(
0779: getPortalBaseUrl(), this .requestContext);
0780: }
0781: return this .portalUrl;
0782: }
0783:
0784: /**
0785: * Portal base servlet url ( e.g. http://localhost:8080/jetspeed/desktop/ )
0786: *
0787: * @return portal base servlet url
0788: */
0789: public String getPortalUrl(boolean encode) {
0790: return getPortalUrl(null, encode);
0791: }
0792:
0793: /**
0794: * Portal base servlet url with relativePath argument appended ( e.g. http://localhost:8080/jetspeed/desktop/default-page.psml )
0795: *
0796: * @return portal base servlet url with relativePath argument appended
0797: */
0798: public String getPortalUrl(String relativePath) {
0799: return getPortalUrl(relativePath, false);
0800: }
0801:
0802: /**
0803: * Portal base servlet url with relativePath argument appended ( e.g. http://localhost:8080/jetspeed/desktop/default-page.psml )
0804: *
0805: * @return portal base servlet url with relativePath argument appended
0806: */
0807: public String getPortalUrl(String relativePath, boolean encode) {
0808: return HeaderResourceLib.getPortalResourceUrl(relativePath,
0809: getPortalUrl(), encode, this .requestContext);
0810: }
0811:
0812: // jetspeed - special convenience methods
0813:
0814: protected String jetspeedGenerateBasetag() {
0815: StringBuffer basetagOut = new StringBuffer();
0816: // <script type="text/javascript" src='http://localhost:8080/jetspeed/javascript/dojo/dojo.js'></script>
0817: // src='$jetspeedDesktop.getPortalResourceUrl("/javascript/dojo/dojo.js")'
0818: String fullPortalBaseUrl = HeaderResourceLib.getPortalBaseUrl(
0819: this .requestContext, this .baseUrlAccess, true);
0820: String href = HeaderResourceLib.getPortalResourceUrl("/",
0821: fullPortalBaseUrl, false, this .requestContext);
0822: basetagOut.append("<base href=\"").append(href).append("\">");
0823: return basetagOut.toString();
0824: }
0825:
0826: // dojo - special convenience methods
0827:
0828: /**
0829: * If no previous call using value of dojoRequire argument has been added to any getHeaderSections() content entry,
0830: * add text argument to the getHeaderSections() content entry for dojo core require statements
0831: *
0832: */
0833: public void dojoAddCoreLibraryRequire(String dojoRequire) {
0834: dojoAddRequire(dojoRequire, HEADER_SECTION_DOJO_REQUIRES_CORE);
0835: }
0836:
0837: /**
0838: * Split dojoRequires argument using ';' delimiter and for each resulting dojoRequire value, if no previous call
0839: * using dojoRequire value has been added to any getHeaderSections() content entry,
0840: * add text argument to the getHeaderSections() content entry for dojo core require statements
0841: *
0842: */
0843: public void dojoAddCoreLibraryRequires(String dojoRequires) {
0844: dojoAddRequires(dojoRequires, HEADER_SECTION_DOJO_REQUIRES_CORE);
0845: }
0846:
0847: /**
0848: * If no previous call using value of dojoRequire argument has been added to any getHeaderSections() content entry,
0849: * add text argument to the getHeaderSections() content entry for dojo library module require statements
0850: *
0851: */
0852: public void dojoAddModuleLibraryRequire(String dojoRequire) {
0853: dojoAddRequire(dojoRequire,
0854: HEADER_SECTION_DOJO_REQUIRES_MODULES);
0855: }
0856:
0857: /**
0858: * Split dojoRequires argument using ';' delimiter and for each resulting dojoRequire value, if no previous call
0859: * using dojoRequire value has been added to any getHeaderSections() content entry,
0860: * add text argument to the getHeaderSections() content entry for dojo library module require statements
0861: *
0862: */
0863: public void dojoAddModuleLibraryRequires(String dojoRequires) {
0864: dojoAddRequires(dojoRequires,
0865: HEADER_SECTION_DOJO_REQUIRES_MODULES);
0866: }
0867:
0868: /**
0869: * Assure that header section name for dojo body expand style is included
0870: *
0871: */
0872: public void dojoAddBodyExpandStyle(boolean omitWindowScrollbars) {
0873: if (isHeaderSectionIncluded(HEADER_SECTION_DOJO_STYLE_BODYEXPAND)
0874: || isHeaderSectionIncluded(HEADER_SECTION_DOJO_STYLE_BODYEXPAND_NOSCROLL)) {
0875: // already included - first inclusion wins
0876: } else {
0877: if (!omitWindowScrollbars) {
0878: orderHeaderSection(HEADER_SECTION_DOJO_STYLE_BODYEXPAND);
0879: } else {
0880: orderHeaderSection(HEADER_SECTION_DOJO_STYLE_BODYEXPAND_NOSCROLL);
0881: }
0882: }
0883: }
0884:
0885: /**
0886: * Enable dojo by setting appropriate modifiable header configuration setting
0887: *
0888: */
0889: public void dojoEnable() {
0890: getHeaderDynamicConfiguration().put(HEADER_CONFIG_DOJO_ENABLE,
0891: "true");
0892: }
0893:
0894: protected void dojoDisable() {
0895: getHeaderDynamicConfiguration().put(HEADER_CONFIG_DOJO_ENABLE,
0896: "false");
0897: }
0898:
0899: protected String dojoGetPath() {
0900: return (String) getHeaderDynamicConfiguration().get(
0901: HEADER_CONFIG_DOJO_PATH);
0902: }
0903:
0904: protected void dojoAddRequire(String dojoRequire,
0905: String addToHeaderName) {
0906: if (dojoRequire != null && addToHeaderName != null
0907: && !hasHeaderSectionFragment(dojoRequire, true)) {
0908: String requireStatement = " dojo.require(\""
0909: + dojoRequire + "\");";
0910: addHeaderSectionFragment(dojoRequire, addToHeaderName,
0911: requireStatement, true);
0912: }
0913: }
0914:
0915: protected void dojoAddRequires(String dojoRequires,
0916: String addToHeaderName) {
0917: String[] reqStatements = StringUtils.split(dojoRequires, ';');
0918: int reqStatementsLen = (reqStatements == null) ? 0
0919: : reqStatements.length;
0920: if (reqStatementsLen > 0) {
0921: for (int i = 0; i < reqStatementsLen; i++) {
0922: dojoAddRequire(reqStatements[i], addToHeaderName);
0923: }
0924: }
0925: }
0926:
0927: protected String dojoGeneratePreinit() {
0928: StringBuffer preinitOut = new StringBuffer();
0929: //preinitOut.append( " " ).append( "function de_jsessionid_url(url){var tEnds = url.indexOf(';jsessionid=');if (tEnds > 0) url = url.substring(0, tEnds);return url;}" ).append( EOL );
0930: // presence of ;jsessionid in dojo baseScriptUri is bad news
0931: preinitOut.append(" ").append("djConfig.baseScriptUri = \"")
0932: .append(getPortalResourceUrl(dojoGetPath(), false))
0933: .append("\";").append(EOL);
0934: if (this .requestContext.getRequest().getContextPath().length() == 0) {
0935: preinitOut.append(" ").append(
0936: "djConfig.jetspeed.rootContext = \"true\";")
0937: .append(EOL);
0938: }
0939: preinitOut.append(" ").append(
0940: "djConfig.jetspeed.servletPath = \"").append(
0941: this .requestContext.getRequest().getServletPath())
0942: .append("\";");
0943: return preinitOut.toString();
0944: }
0945:
0946: protected String dojoGenerateInit() {
0947: StringBuffer initOut = new StringBuffer();
0948: // <script type="text/javascript" src='http://localhost:8080/jetspeed/javascript/dojo/dojo.js'></script>
0949: // src='$jetspeedDesktop.getPortalResourceUrl("/javascript/dojo/dojo.js")'
0950: initOut.append("<script type=\"text/javascript\" src=\"")
0951: .append(getPortalResourceUrl(dojoGetPath(), false))
0952: .append("dojo.js").append("\"></script>");
0953: return initOut.toString();
0954: }
0955:
0956: protected String dojoGenerateWriteincludes() {
0957: return " dojo.hostenv.writeIncludes();";
0958: }
0959:
0960: protected String dojoGenerateBodyExpandStyle() { // if not defined as getHeaderResourceRegistry(), generate default text
0961: Map headerRsrcRegistry = getHeaderResourceRegistry();
0962: String headerText = (String) headerRsrcRegistry
0963: .get(HEADER_SECTION_DOJO_STYLE_BODYEXPAND);
0964: if (headerText == null) {
0965: headerText = "html, body { width: 100%; height: 100%; padding: 0 0 0 0; margin: 0 0 0 0; }";
0966: }
0967: return headerText;
0968: }
0969:
0970: protected String dojoGenerateBodyExpandNoScrollStyle() { // if not defined as getHeaderResourceRegistry(), generate default text
0971: Map headerRsrcRegistry = getHeaderResourceRegistry();
0972: String headerText = (String) headerRsrcRegistry
0973: .get(HEADER_SECTION_DOJO_STYLE_BODYEXPAND_NOSCROLL);
0974: if (headerText == null) {
0975: headerText = "html, body { width: 100%; height: 100%; overflow: hidden; padding: 0 0 0 0; margin: 0 0 0 0; }";
0976: }
0977: return headerText;
0978: }
0979:
0980: // older content implementation - using HeaderInfo set
0981:
0982: /**
0983: * Gets HeaderInfo set from the request.
0984: *
0985: * @return HeaderInfo set containing content for inclusion in html <head>
0986: */
0987: private Set getHeaderInfoSet() {
0988: Set headerInfoSet = (Set) requestContext
0989: .getAttribute(PortalReservedParameters.HEADER_RESOURCE_ATTRIBUTE);
0990: if (headerInfoSet == null) {
0991: headerInfoSet = new LinkedHashSet();
0992: requestContext.setAttribute(
0993: PortalReservedParameters.HEADER_RESOURCE_ATTRIBUTE,
0994: headerInfoSet);
0995: }
0996: return headerInfoSet;
0997: }
0998:
0999: /*
1000: * (non-Javadoc)
1001: *
1002: * @see org.apache.jetspeed.headerresource.impl.HeaderResource#addHeaderInfo(java.lang.String)
1003: */
1004: public void addHeaderInfo(String text) {
1005: HeaderInfo headerInfo = new HeaderInfo(null, null, text);
1006: if (!containsHeaderInfo(headerInfo)) {
1007: Set headerInfoSet = getHeaderInfoSet();
1008: headerInfoSet.add(headerInfo);
1009: }
1010: }
1011:
1012: /*
1013: * (non-Javadoc)
1014: *
1015: * @see org.apache.jetspeed.headerresource.impl.HeaderResource#addHeaderInfo(java.lang.String,
1016: * java.util.Map,java.lang.String)
1017: */
1018: public void addHeaderInfo(String elementName, Map attributes,
1019: String text) {
1020: HeaderInfo headerInfo = new HeaderInfo(elementName, attributes,
1021: text);
1022: if (!containsHeaderInfo(headerInfo)) {
1023: Set headerInfoSet = getHeaderInfoSet();
1024: headerInfoSet.add(headerInfo);
1025: }
1026: }
1027:
1028: /**
1029: * Returns true if this set contains the specified HeaderInfo.
1030: *
1031: * @param headerInfo
1032: * @return
1033: */
1034: private boolean containsHeaderInfo(HeaderInfo headerInfo) {
1035: Set headerInfoSet = getHeaderInfoSet();
1036: for (Iterator ite = headerInfoSet.iterator(); ite.hasNext();) {
1037: HeaderInfo hInfo = (HeaderInfo) ite.next();
1038: if (headerInfo.equals(hInfo)) {
1039: return true;
1040: }
1041: }
1042: return false;
1043: }
1044:
1045: /*
1046: * (non-Javadoc)
1047: *
1048: * @see org.apache.jetspeed.headerresource.impl.HeaderResource#addJavaScript(java.lang.String,
1049: * boolean)
1050: */
1051: public void addJavaScript(String path, boolean defer) {
1052: HashMap attrs = new HashMap();
1053: attrs.put("src", requestContext.getResponse().encodeURL(path));
1054: attrs.put("type", "text/javascript");
1055: if (defer) {
1056: attrs.put("defer", "true");
1057: }
1058: addHeaderInfo("script", attrs, "");
1059: }
1060:
1061: /*
1062: * (non-Javadoc)
1063: *
1064: * @see org.apache.jetspeed.headerresource.impl.HeaderResource#addJavaScript(java.lang.String)
1065: */
1066: public void addJavaScript(String path) {
1067: addJavaScript(path, false);
1068: }
1069:
1070: /*
1071: * (non-Javadoc)
1072: *
1073: * @see org.apache.jetspeed.headerresource.impl.HeaderResource#addStyleSheet(java.lang.String)
1074: */
1075: public void addStyleSheet(String path) {
1076: HashMap attrs = new HashMap();
1077: attrs.put("rel", "stylesheet");
1078: attrs.put("href", requestContext.getResponse().encodeURL(path));
1079: attrs.put("type", "text/css");
1080: addHeaderInfo("link", attrs, null);
1081: }
1082:
1083: /**
1084: * This class represents tag information for HeaderResouce component
1085: *
1086: * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a>
1087: */
1088: private class HeaderInfo {
1089: /**
1090: * Tag's name
1091: */
1092: private String elementName;
1093:
1094: /**
1095: * Tag's attributes
1096: */
1097: private Map attributes;
1098:
1099: /**
1100: * Tag's content
1101: */
1102: private String text;
1103:
1104: public HeaderInfo(String elementName) {
1105: this (elementName, new HashMap());
1106: }
1107:
1108: public HeaderInfo(String elementName, Map attr) {
1109: this (elementName, attr, null);
1110: }
1111:
1112: public HeaderInfo(String elementName, Map attr, String text) {
1113: setElementName(elementName);
1114: setAttributes(attr);
1115: setText(text);
1116: }
1117:
1118: public void addAttribute(String key, String value) {
1119: attributes.put(key, value);
1120: }
1121:
1122: public String toString() {
1123: StringBuffer buf = new StringBuffer();
1124:
1125: String elmtName = getElementName();
1126: if (elmtName != null && elmtName.length() > 0) {
1127: buf.append("<");
1128: buf.append(getElementName());
1129: buf.append(" ");
1130:
1131: Map attrMap = getAttributes();
1132: if (attrMap != null) {
1133: Set keySet = attrMap.keySet();
1134: for (Iterator ite = keySet.iterator(); ite
1135: .hasNext();) {
1136: String key = (String) ite.next();
1137: buf.append(key);
1138: buf.append("=\"");
1139: buf.append((String) attrMap.get(key));
1140: buf.append("\" ");
1141: }
1142: }
1143: if (getText() != null) {
1144: buf.append(">" + getText() + "</"
1145: + getElementName() + ">");
1146: } else {
1147: buf.append("/>");
1148: }
1149: } else {
1150: if (getText() != null) {
1151: buf.append(getText());
1152: }
1153: }
1154: return buf.toString();
1155: }
1156:
1157: public boolean equals(Object o) {
1158: if (o instanceof HeaderInfo) {
1159: HeaderInfo headerInfo = (HeaderInfo) o;
1160: if (compareString(headerInfo.getElementName(),
1161: getElementName())
1162: && compareString(headerInfo.getText(),
1163: getText())
1164: && compareAttributes(
1165: headerInfo.getAttributes(),
1166: getAttributes())) {
1167: return true;
1168: }
1169: }
1170: return false;
1171: }
1172:
1173: private boolean compareString(String str0, String str1) {
1174: if (str0 == null) {
1175: if (str1 == null) {
1176: return true;
1177: }
1178:
1179: } else if (str1 != null) {
1180: if (str0.equals(str1)) {
1181: return true;
1182: }
1183: }
1184: return false;
1185: }
1186:
1187: private boolean compareAttributes(Map attr0, Map attr1) {
1188: if (attr0 == null) {
1189: if (attr1 == null) {
1190: return true;
1191: }
1192: } else if (attr1 != null) {
1193: if (attr0.equals(attr1)) {
1194: return true;
1195: }
1196: }
1197: return false;
1198: }
1199:
1200: /**
1201: * @return Returns the attributes.
1202: */
1203: public Map getAttributes() {
1204: return attributes;
1205: }
1206:
1207: /**
1208: * @param attributes The attributes to set.
1209: */
1210: public void setAttributes(Map attributes) {
1211: this .attributes = attributes;
1212: }
1213:
1214: /**
1215: * @return Returns the elementName.
1216: */
1217: public String getElementName() {
1218: return elementName;
1219: }
1220:
1221: /**
1222: * @param elementName The elementName to set.
1223: */
1224: public void setElementName(String elementName) {
1225: this .elementName = elementName;
1226: }
1227:
1228: /**
1229: * @return Returns the text.
1230: */
1231: public String getText() {
1232: return text;
1233: }
1234:
1235: /**
1236: * @param text The text to set.
1237: */
1238: public void setText(String text) {
1239: this.text = text;
1240: }
1241: }
1242: }
|