/*COPYRIGHT_START*********************************************************** * * IBM Confidential OCO Source Material * 5639-D57 (C) COPYRIGHT International Business Machines Corp. * 1996,1997,1998,1999,2000,2001 * * The source code for this program is not published or otherwise divested * of its trade secrets, irrespective of what has been deposited with the * U.S. Copyright Office. * * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE * OR PERFORMANCE OF THIS SOFTWARE. * * @(#) 1.1 ncf/buildtools/copyright.txt, WEBSJAVA.BUILD, ASV, o0119.20 3/3/01 13:13:43 [5/19/01 17:35:35] * *COPYRIGHT_END*************************************************************/ import javax.servlet.*; import javax.servlet.http.*; import java.util.*; import com.ibm.websphere.servlet.cache.*; public class SampleIdGeneratorImpl implements IdGenerator { public String getId(ServletCacheRequest request) { try { StringBuffer sb = new StringBuffer(); //use this attribute, since request.getRequestURI() only returns the URI for the original external //request. E.g. if A includes B, then getRequestURI() from inside B will return A's URI. This //attribute will return B's URI. sb.append((String) request.getAttribute("javax.servlet.include.request_uri")).append(":"); //let's say you want to cache only when the 'action' request parameter is equal to "get_score" or //"get_team_info", but you don't want to cache any other actions. String action = request.getParameter("action"); if (action.equals("get_score") || action.equals("get_team_info")) { //the parameter is correct, include it and it's value in the id sb.append("action=").append(action); } else { //the parameter is wrong, so don't cache this request. return null; } /* let's also say that this app supports different levels of service, e.g. Platinum, Gold, Silver, Bronze, and that Platinum level members always get real time scores and info. This means that you want to cache all requests unless the level of service is Platinum. In addition let's say that service level is stored on the session and is set during login. If the session variable is not present, the servlet will forward to the login page. Service Level Action Platinum don't cache anything except Platinum same output in these cases ... cache not present different output (forward) ... cache */ //check to make sure there is a session, don't want to create one by accident. HttpSession session = request.getSession(false); if (session != null) { String level = (String) session.getAttribute("ServiceLevel"); if (level == null) { sb.append("ServiceLevel=NotPresent"); } else if (level.equals("Platinum")) { //don't cache...give them a real time update. return null; } else { sb.append("ServiceLevel=NonPlatinum"); } } else { sb.append("ServiceLevel=NotPresent"); } //only need this if your servlet handles different languages. String encoding = request.getCharacterEncoding(); if (encoding != null) { sb.append(encoding); } String out = sb.toString(); if (out.equals("")) {return null;} return out; } catch (Exception e) { e.printStackTrace(); return null; } } public int getSharingPolicy(ServletCacheRequest request){ /************************** Included for future compatibility with distributed caching ***************************/ return EntryInfo.NOT_SHARED; } public void initialize(CacheConfig cc){ /************************** The initialize() method is called during startup. Normally, the cache processes a servlet's configuration and builds a CacheConfig object that is made available to the IdGenerator here. initialize() would then build a list of request and ses- sion variables to be included in the cache ids for this servlet. Since plugged in IdGenerators are created with a specific servlet's behavior in mind, working with the CacheConfig is unnecessary; just hard code the configuration into the getId() method. ***************************/ } } |