View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.jetspeed.cache.impl;
19  
20  import java.io.Serializable;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.apache.jetspeed.cache.ContentCacheKey;
25  import org.apache.jetspeed.desktop.JetspeedDesktop;
26  import org.apache.jetspeed.request.RequestContext;
27  
28  /***
29   * The content cache key holds an immutable cache key definition.
30   * Cache key definitions are based on the following required properties:
31   * <ul>
32   * <li>username</li>
33   * <li>windowid</li>
34   * <li>pipeline</li>
35   * </ul>
36   * and the following optional properties:
37   * <ul>
38   * <li>sessionid</li>
39   * <li></li>
40   * <li>request.{parameter}</li>
41   * <li>session.{attribute}</li>
42   * </ul>
43   * The string representation of this key is calculated once upon construction.
44   * 
45   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
46   * @version $Id: $
47   */
48  public class JetspeedContentCacheKey implements ContentCacheKey, Serializable
49  {
50      private String username = null;
51      private String pipeline = null;
52      private String windowId = null;
53      private String sessionId = null;
54      private String requestParameter = null;
55      private String sessionAttribute = null;
56      private String key = "";
57      
58      public JetspeedContentCacheKey(List segments, RequestContext context, String windowId)
59      {
60          boolean first = true;
61          Iterator si = segments.iterator();
62          while (si.hasNext())
63          {
64              String segment = (String)si.next();
65              if (segment.equals("username"))
66              {
67                  this.username = context.getUserPrincipal().getName();
68                  key = (first) ? this.username : key + EhPortletContentCacheElementImpl.KEY_SEPARATOR + this.username;                 
69              }
70              else if (segment.equals("windowid"))
71              {
72                  this.windowId = windowId;
73                  key = (first) ? this.windowId : key + EhPortletContentCacheElementImpl.KEY_SEPARATOR + this.windowId;
74              }
75              else if (segment.equals("sessionid"))
76              {
77                  this.sessionId = context.getRequest().getSession().getId();
78                  key = (first) ? this.sessionId : key + EhPortletContentCacheElementImpl.KEY_SEPARATOR + this.sessionId;                
79              }
80              else if (segment.equals("pipeline"))
81              {
82                  String encoder = context.getRequestParameter(JetspeedDesktop.DESKTOP_ENCODER_REQUEST_PARAMETER);
83                  if (encoder != null && encoder.equals(JetspeedDesktop.DESKTOP_ENCODER_REQUEST_PARAMETER_VALUE))
84                  {
85                      this.pipeline = "desktop";
86                  }
87                  else
88                  {
89                      this.pipeline = "portal";
90                  }
91                  key = (first) ? this.pipeline : key + EhPortletContentCacheElementImpl.KEY_SEPARATOR + this.pipeline;                
92              }
93              else if (segment.startsWith("request"))
94              {
95                  int pos = segment.indexOf(".");
96                  if (pos > -1)
97                  {
98                      String parameterName = segment.substring(pos);
99                      this.requestParameter = context.getRequestParameter(parameterName);
100                     if (this.requestParameter != null)
101                     {
102                         key = (first) ? this.requestParameter : key + EhPortletContentCacheElementImpl.KEY_SEPARATOR + this.requestParameter;
103                     }
104                 }
105             }
106             else if (segment.startsWith("session"))
107             {
108                 int pos = segment.indexOf(".");
109                 if (pos > -1)
110                 {
111                     String attributeName = segment.substring(pos);
112                     this.sessionAttribute = (String)context.getSessionAttribute(attributeName);
113                     if (this.sessionAttribute != null)
114                     {
115                         key = (first) ? this.sessionAttribute : key + EhPortletContentCacheElementImpl.KEY_SEPARATOR + this.sessionAttribute;
116                     }
117                 }                
118             }                              
119             first = false;
120         }
121         //System.out.println("*** CACHE KEY IS [" + key + "]");
122     }
123     
124     public JetspeedContentCacheKey()
125     {        
126     }
127     
128     public void createFromUser(String username, String pipeline, String windowId)
129     {
130         this.setUsername(username);
131         this.setPipeline(pipeline);
132         this.setWindowId(windowId);
133         this.key = this.getUsername() + 
134                     EhPortletContentCacheElementImpl.KEY_SEPARATOR + 
135                     this.getPipeline() +
136                     EhPortletContentCacheElementImpl.KEY_SEPARATOR + 
137                     this.getWindowId();
138     }
139 
140     public void createFromSession(String sessionId, String pipeline, String windowId)
141     {
142         this.setSessionId(sessionId);
143         this.setPipeline(pipeline);
144         this.setWindowId(windowId);
145         this.key = this.getSessionId() + 
146         EhPortletContentCacheElementImpl.KEY_SEPARATOR + 
147         this.getPipeline() +
148         EhPortletContentCacheElementImpl.KEY_SEPARATOR + 
149         this.getWindowId();        
150     }
151     
152     public String getKey()
153     {
154         return this.key;
155     }
156 
157     public String getPipeline()
158     {
159         return this.pipeline;
160     }
161 
162     public String getRequestParameter()
163     {
164         return this.requestParameter;
165     }
166 
167     public String getSessionAttribute()
168     {
169         return this.sessionAttribute;
170     }
171 
172     public String getSessionId()
173     {
174         return this.sessionId;
175     }
176 
177     public String getUsername()
178     {
179         return this.username;
180     }
181 
182     public String getWindowId()
183     {
184         return this.windowId;
185     }
186 
187     
188     public void setPipeline(String pipeline)
189     {
190         this.pipeline = pipeline;
191     }
192 
193     
194     public void setRequestParameter(String requestParameter)
195     {
196         this.requestParameter = requestParameter;
197     }
198 
199     
200     public void setSessionAttribute(String sessionAttribute)
201     {
202         this.sessionAttribute = sessionAttribute;
203     }
204 
205     
206     public void setSessionId(String sessionId)
207     {
208         this.sessionId = sessionId;
209     }
210 
211     
212     public void setUsername(String username)
213     {
214         this.username = username;
215     }
216 
217     
218     public void setWindowId(String windowId)
219     {
220         this.windowId = windowId;
221     }
222 }