001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.jetspeed.cache.impl;
019:
020: import java.io.Serializable;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.jetspeed.cache.ContentCacheKey;
025: import org.apache.jetspeed.desktop.JetspeedDesktop;
026: import org.apache.jetspeed.request.RequestContext;
027:
028: /**
029: * The content cache key holds an immutable cache key definition.
030: * Cache key definitions are based on the following required properties:
031: * <ul>
032: * <li>username</li>
033: * <li>windowid</li>
034: * <li>pipeline</li>
035: * </ul>
036: * and the following optional properties:
037: * <ul>
038: * <li>sessionid</li>
039: * <li></li>
040: * <li>request.{parameter}</li>
041: * <li>session.{attribute}</li>
042: * </ul>
043: * The string representation of this key is calculated once upon construction.
044: *
045: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
046: * @version $Id: $
047: */
048: public class JetspeedContentCacheKey implements ContentCacheKey,
049: Serializable {
050: private String username = null;
051: private String pipeline = null;
052: private String windowId = null;
053: private String sessionId = null;
054: private String requestParameter = null;
055: private String sessionAttribute = null;
056: private String key = "";
057:
058: public JetspeedContentCacheKey(List segments,
059: RequestContext context, String windowId) {
060: boolean first = true;
061: Iterator si = segments.iterator();
062: while (si.hasNext()) {
063: String segment = (String) si.next();
064: if (segment.equals("username")) {
065: this .username = context.getUserPrincipal().getName();
066: key = (first) ? this .username
067: : key
068: + EhPortletContentCacheElementImpl.KEY_SEPARATOR
069: + this .username;
070: } else if (segment.equals("windowid")) {
071: this .windowId = windowId;
072: key = (first) ? this .windowId
073: : key
074: + EhPortletContentCacheElementImpl.KEY_SEPARATOR
075: + this .windowId;
076: } else if (segment.equals("sessionid")) {
077: this .sessionId = context.getRequest().getSession()
078: .getId();
079: key = (first) ? this .sessionId
080: : key
081: + EhPortletContentCacheElementImpl.KEY_SEPARATOR
082: + this .sessionId;
083: } else if (segment.equals("pipeline")) {
084: String encoder = context
085: .getRequestParameter(JetspeedDesktop.DESKTOP_ENCODER_REQUEST_PARAMETER);
086: if (encoder != null
087: && encoder
088: .equals(JetspeedDesktop.DESKTOP_ENCODER_REQUEST_PARAMETER_VALUE)) {
089: this .pipeline = "desktop";
090: } else {
091: this .pipeline = "portal";
092: }
093: key = (first) ? this .pipeline
094: : key
095: + EhPortletContentCacheElementImpl.KEY_SEPARATOR
096: + this .pipeline;
097: } else if (segment.startsWith("request")) {
098: int pos = segment.indexOf(".");
099: if (pos > -1) {
100: String parameterName = segment.substring(pos);
101: this .requestParameter = context
102: .getRequestParameter(parameterName);
103: if (this .requestParameter != null) {
104: key = (first) ? this .requestParameter
105: : key
106: + EhPortletContentCacheElementImpl.KEY_SEPARATOR
107: + this .requestParameter;
108: }
109: }
110: } else if (segment.startsWith("session")) {
111: int pos = segment.indexOf(".");
112: if (pos > -1) {
113: String attributeName = segment.substring(pos);
114: this .sessionAttribute = (String) context
115: .getSessionAttribute(attributeName);
116: if (this .sessionAttribute != null) {
117: key = (first) ? this .sessionAttribute
118: : key
119: + EhPortletContentCacheElementImpl.KEY_SEPARATOR
120: + this .sessionAttribute;
121: }
122: }
123: }
124: first = false;
125: }
126: //System.out.println("*** CACHE KEY IS [" + key + "]");
127: }
128:
129: public JetspeedContentCacheKey() {
130: }
131:
132: public void createFromUser(String username, String pipeline,
133: String windowId) {
134: this .setUsername(username);
135: this .setPipeline(pipeline);
136: this .setWindowId(windowId);
137: this .key = this .getUsername()
138: + EhPortletContentCacheElementImpl.KEY_SEPARATOR
139: + this .getPipeline()
140: + EhPortletContentCacheElementImpl.KEY_SEPARATOR
141: + this .getWindowId();
142: }
143:
144: public void createFromSession(String sessionId, String pipeline,
145: String windowId) {
146: this .setSessionId(sessionId);
147: this .setPipeline(pipeline);
148: this .setWindowId(windowId);
149: this .key = this .getSessionId()
150: + EhPortletContentCacheElementImpl.KEY_SEPARATOR
151: + this .getPipeline()
152: + EhPortletContentCacheElementImpl.KEY_SEPARATOR
153: + this .getWindowId();
154: }
155:
156: public String getKey() {
157: return this .key;
158: }
159:
160: public String getPipeline() {
161: return this .pipeline;
162: }
163:
164: public String getRequestParameter() {
165: return this .requestParameter;
166: }
167:
168: public String getSessionAttribute() {
169: return this .sessionAttribute;
170: }
171:
172: public String getSessionId() {
173: return this .sessionId;
174: }
175:
176: public String getUsername() {
177: return this .username;
178: }
179:
180: public String getWindowId() {
181: return this .windowId;
182: }
183:
184: public void setPipeline(String pipeline) {
185: this .pipeline = pipeline;
186: }
187:
188: public void setRequestParameter(String requestParameter) {
189: this .requestParameter = requestParameter;
190: }
191:
192: public void setSessionAttribute(String sessionAttribute) {
193: this .sessionAttribute = sessionAttribute;
194: }
195:
196: public void setSessionId(String sessionId) {
197: this .sessionId = sessionId;
198: }
199:
200: public void setUsername(String username) {
201: this .username = username;
202: }
203:
204: public void setWindowId(String windowId) {
205: this.windowId = windowId;
206: }
207: }
|