1 16 17 package org.springframework.web.portlet.handler; 18 19 import javax.portlet.PortletException; 20 import javax.portlet.PortletRequest; 21 import javax.portlet.PortletResponse; 22 import javax.portlet.RenderRequest; 23 import javax.portlet.RenderResponse; 24 25 import org.springframework.web.portlet.context.PortletApplicationObjectSupport; 26 27 39 public abstract class PortletContentGenerator extends PortletApplicationObjectSupport { 40 41 private boolean requireSession = false; 42 43 private int cacheSeconds = -1; 44 45 46 49 public final void setRequireSession(boolean requireSession) { 50 this.requireSession = requireSession; 51 } 52 53 56 public final boolean isRequireSession() { 57 return requireSession; 58 } 59 60 67 public final void setCacheSeconds(int seconds) { 68 this.cacheSeconds = seconds; 69 } 70 71 74 public final int getCacheSeconds() { 75 return cacheSeconds; 76 } 77 78 79 87 protected final void check(PortletRequest request, PortletResponse response) throws PortletException { 88 if (this.requireSession) { 89 if (request.getPortletSession(false) == null) { 90 throw new PortletSessionRequiredException("Pre-existing session required but none found"); 91 } 92 } 93 } 94 95 103 protected final void checkAndPrepare(RenderRequest request, RenderResponse response) 104 throws PortletException { 105 106 checkAndPrepare(request, response, this.cacheSeconds); 107 } 108 109 119 protected final void checkAndPrepare( 120 RenderRequest request, RenderResponse response, int cacheSeconds) 121 throws PortletException { 122 123 check(request, response); 124 applyCacheSeconds(response, cacheSeconds); 125 } 126 127 130 protected final void preventCaching(RenderResponse response) { 131 cacheForSeconds(response, 0); 132 } 133 134 140 protected final void cacheForSeconds(RenderResponse response, int seconds) { 141 response.setProperty(RenderResponse.EXPIRATION_CACHE, Integer.toString(seconds)); 142 } 143 144 150 protected final void applyCacheSeconds(RenderResponse response, int seconds) { 151 if (seconds > 0) { 152 cacheForSeconds(response, seconds); 153 } 154 else if (seconds == 0) { 155 preventCaching(response); 156 } 157 } 159 160 } 161 | Popular Tags |