001: /*
002: * Created on Jan 6, 2005
003: */
004: package uk.org.ponder.rsf.viewstate.support;
005:
006: import java.util.HashMap;
007: import java.util.List;
008: import java.util.Map;
009:
010: import uk.org.ponder.rsf.viewstate.BaseURLProvider;
011: import uk.org.ponder.rsf.viewstate.ContextURLProvider;
012: import uk.org.ponder.rsf.viewstate.RawURLState;
013: import uk.org.ponder.rsf.viewstate.UltimateURLRenderer;
014: import uk.org.ponder.rsf.viewstate.ViewParamUtil;
015: import uk.org.ponder.rsf.viewstate.ViewParameters;
016: import uk.org.ponder.rsf.viewstate.ViewParamsCodec;
017: import uk.org.ponder.rsf.viewstate.ViewStateHandler;
018: import uk.org.ponder.stringutil.URLUtil;
019: import uk.org.ponder.webapputil.ConsumerInfo;
020:
021: /**
022: * A "simple" ViewStateHandler which in addition to accepting statically
023: * configured Spring information, also is able to look in the
024: * ConsumerRequestInfo threadlocal in order to discover relevant URL rendering
025: * information.
026: *
027: * @author Antranig Basman (antranig@caret.cam.ac.uk)
028: *
029: */
030: public class RSFServletViewStateHandler implements ViewStateHandler {
031:
032: private BaseURLProvider urlprovider;
033: private ConsumerInfo ciproxy;
034:
035: private Map ultimaterenderers = new HashMap();
036: private ViewParamsCodec vpcodec;
037: private ContextURLProvider cup;
038:
039: public void setBaseURLProvider(BaseURLProvider urlprovider) {
040: this .urlprovider = urlprovider;
041: }
042:
043: public void setContextURLProvider(ContextURLProvider cup) {
044: this .cup = cup;
045: }
046:
047: public void setConsumerInfo(ConsumerInfo ciproxy) {
048: this .ciproxy = ciproxy;
049: }
050:
051: public void setViewParamsCodec(ViewParamsCodec vpcodec) {
052: this .vpcodec = vpcodec;
053: }
054:
055: public String getFullURL(ViewParameters viewparams) {
056: // toHTTPRequest provides leading slash, and baseurl includes trailing slash
057: String requestparams = ViewParamUtil.toHTTPRequest(vpcodec,
058: viewparams).substring(1);
059:
060: String usebaseurl = urlprovider.getBaseURL();
061: String extraparams = "";
062: boolean quest = requestparams.indexOf('?') != -1;
063:
064: ConsumerInfo ci = ciproxy.get();
065: if (ci.urlbase != null) {
066: usebaseurl = ci.urlbase;
067: }
068: if (ci.extraparameters != null) {
069: extraparams = ci.extraparameters;
070: // rewrite the first character of extra params to ? if there
071: // a) are any extras and b) are not any base request params.
072: if (!quest && extraparams.length() > 0) {
073: extraparams = "?" + extraparams.substring(1);
074: quest = true;
075: }
076: }
077: String baseurl = usebaseurl + requestparams;
078: int hpos = baseurl.indexOf('#');
079:
080: String path = hpos == -1 ? baseurl + extraparams : baseurl
081: .substring(0, hpos)
082: + extraparams + baseurl.substring(hpos);
083: return path;
084: }
085:
086: public String getActionURL(ViewParameters viewparams) {
087: String fullURL = getFullURL(viewparams);
088: int qpos = fullURL.indexOf('?');
089: return qpos == -1 ? fullURL : fullURL.substring(0, qpos);
090: }
091:
092: public Map getAttrMap(ViewParameters viewparams) {
093: RawURLState rus = vpcodec.renderViewParams(viewparams);
094: Map togo = rus.params;
095: return togo;
096: }
097:
098: public String encodeResourceURL(String resourcepath) {
099: if (URLUtil.isAbsolute(resourcepath)
100: || resourcepath.charAt(0) == '/') {
101: return resourcepath;
102: } else {
103: return cup.getContextBaseURL() + resourcepath;
104: }
105: }
106:
107: // in servlet context, rendered URLs agree with ultimate ones.
108: public String getUltimateURL(ViewParameters viewparams) {
109: ConsumerInfo ci = ciproxy.get();
110: // ConsumerRequestInfo cri = ConsumerRequestInfo.getConsumerRequestInfo();
111: if (ci.externalURL != null) {
112: UltimateURLRenderer uur = (UltimateURLRenderer) ultimaterenderers
113: .get(ci.consumertype);
114: return uur == null ? ci.externalURL : uur.getUltimateURL(
115: ci.externalURL, viewparams, this );
116: }
117: return getFullURL(viewparams);
118: }
119:
120: public void setUltimateRenderers(List ultimates) {
121: for (int i = 0; i < ultimates.size(); ++i) {
122: UltimateURLRenderer ultimate = (UltimateURLRenderer) ultimates
123: .get(i);
124: ultimaterenderers.put(ultimate.getConsumerType(), ultimate);
125: }
126: }
127:
128: }
|