1 24 package org.riotfamily.website.txt2img; 25 26 import java.io.IOException ; 27 import java.io.InputStreamReader ; 28 import java.io.PrintWriter ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Locale ; 34 import java.util.Map ; 35 import java.util.regex.Pattern ; 36 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 40 import org.riotfamily.cachius.spring.AbstractCacheableController; 41 import org.riotfamily.common.io.IOUtils; 42 import org.riotfamily.common.util.FormatUtils; 43 import org.riotfamily.common.web.util.ServletUtils; 44 import org.springframework.core.io.ClassPathResource; 45 import org.springframework.core.io.Resource; 46 import org.springframework.util.Assert; 47 import org.springframework.util.StringUtils; 48 import org.springframework.web.bind.ServletRequestUtils; 49 import org.springframework.web.servlet.ModelAndView; 50 import org.springframework.web.servlet.mvc.LastModified; 51 import org.springframework.web.servlet.support.RequestContextUtils; 52 import org.springframework.web.util.HtmlUtils; 53 54 58 public class Txt2ImgController extends AbstractCacheableController 59 implements LastModified { 60 61 private static final Resource SCRIPT_RESOURCE = new ClassPathResource( 62 "txt2img.js", Txt2ImgController.class); 63 64 private static final Resource PIXEL_RESOURCE = new ClassPathResource( 65 "pixel.gif", Txt2ImgController.class); 66 67 private long lastModified = System.currentTimeMillis(); 68 69 70 private Map generators = new HashMap (); 71 72 private List selectors = new ArrayList (); 73 74 private ImageGenerator defaultGenerator = new ImageGenerator(); 75 76 private Pattern refererPattern; 77 78 81 public void setRules(List rules) { 82 Iterator it = rules.iterator(); 83 while (it.hasNext()) { 84 ReplacementRule rule = (ReplacementRule) it.next(); 85 addRule(rule); 86 } 87 } 88 89 public void addRule(ReplacementRule rule) { 90 String [] sel = StringUtils.commaDelimitedListToStringArray(rule.getSelector()); 91 for (int i = 0; i < sel.length; i++) { 92 selectors.add(sel[i]); 93 generators.put(sel[i], rule); 94 } 95 } 96 97 101 public void setDefaultGenerator(ImageGenerator defaultGenerator) { 102 this.defaultGenerator = defaultGenerator; 103 } 104 105 protected void appendCacheKey(StringBuffer key, HttpServletRequest request) { 106 key.append(ServletUtils.getRequestUrlWithQueryString(request)); 107 } 108 109 public long getLastModified(HttpServletRequest request) { 110 return lastModified; 111 } 112 113 118 public void setRefererPattern(Pattern refererPattern) { 119 this.refererPattern = refererPattern; 120 } 121 122 public ModelAndView handleRequest(HttpServletRequest request, 123 HttpServletResponse response) throws Exception { 124 125 if (refererPattern != null) { 126 String referer = request.getHeader("Referer"); 127 if (referer != null && !refererPattern.matcher(referer).matches()) { 128 response.sendError(HttpServletResponse.SC_FORBIDDEN); 129 } 130 } 131 132 String extension = FormatUtils.getExtension(request.getRequestURI()); 133 String text = getText(request); 134 if (text != null) { 135 serveImage(text, request, response); 136 } 137 else if (extension.equals("gif")) { 138 servePixelGif(response); 139 } 140 else if (request.getParameter("empty") == null) { 141 serveScript(request, response); 142 } 143 return null; 144 } 145 146 151 protected Locale getLocale(HttpServletRequest request) { 152 String localeString = request.getParameter("locale"); 153 if (StringUtils.hasText(localeString)) { 154 return StringUtils.parseLocaleString(localeString); 155 } 156 return RequestContextUtils.getLocale(request); 157 } 158 159 protected String getEncodedParam(HttpServletRequest request, String name) { 160 String value = request.getParameter(name); 161 return value != null ? FormatUtils.uriUnescape(value.replace('@', '%')) : null; 162 } 163 164 169 protected String getText(HttpServletRequest request) { 170 String text = getEncodedParam(request, "text"); 171 if (text != null) { 172 text = HtmlUtils.htmlUnescape(text); 173 String transform = request.getParameter("transform"); 174 if (StringUtils.hasText(transform)) { 175 Locale locale = getLocale(request); 176 if (transform.equalsIgnoreCase("uppercase")) { 177 text = text.toUpperCase(locale); 178 } 179 if (transform.equalsIgnoreCase("lowercase")) { 180 text = text.toLowerCase(locale); 181 } 182 } 183 } 184 return text; 185 } 186 187 190 protected void serveImage(String text, HttpServletRequest request, 191 HttpServletResponse response) throws IOException { 192 193 ImageGenerator generator = defaultGenerator; 194 String selector = getEncodedParam(request, "selector"); 195 if (selector != null) { 196 generator = (ImageGenerator) generators.get(selector); 197 } 198 Assert.notNull(generator, "No ImageGenerator found for selector '" 199 + selector + "' and no default generator is set."); 200 201 text = FormatUtils.stripWhitespaces(text, true); 202 int maxWidth = ServletRequestUtils.getIntParameter(request, "width", 0); 203 if (maxWidth <= 0) { 204 maxWidth = Integer.MAX_VALUE; 205 } 206 String color = getEncodedParam(request, "color"); 207 response.setContentType("image/png"); 208 ServletUtils.setCacheHeaders(response, "1M"); 209 generator.generate(text, maxWidth, color, response.getOutputStream()); 210 } 211 212 216 protected void serveScript(HttpServletRequest request, 217 HttpServletResponse response) throws IOException { 218 219 response.setContentType("text/javascript"); 220 ServletUtils.setCacheHeaders(response, "1M"); 221 PrintWriter out = response.getWriter(); 222 out.println("var IEDOMReadyScript = '" + request.getRequestURI() + "?empty=true';"); 223 224 IOUtils.copy(new InputStreamReader ( 225 SCRIPT_RESOURCE.getInputStream(), "UTF-8"), out); 226 227 out.print("var txt2img = new RiotImageReplacement('"); 228 out.print(getGeneratorUrl(request)); 229 out.print("?locale="); 230 out.print(RequestContextUtils.getLocale(request)); 231 out.print("', '"); 232 out.print(getPixelUrl(request)); 233 out.print("', ["); 234 Iterator it = selectors.iterator(); 235 while (it.hasNext()) { 236 out.print("'"); 237 out.print(it.next()); 238 out.print("'"); 239 if (it.hasNext()) { 240 out.print(", "); 241 } 242 } 243 out.print("]);"); 244 } 245 246 private String getGeneratorUrl(HttpServletRequest request) { 247 return FormatUtils.stripExtension(request.getRequestURI()) + ".png"; 248 } 249 250 private String getPixelUrl(HttpServletRequest request) { 251 return FormatUtils.stripExtension(request.getRequestURI()) + ".gif"; 252 } 253 254 258 protected void servePixelGif(HttpServletResponse response) throws IOException { 259 response.setContentType("image/gif"); 260 ServletUtils.setCacheHeaders(response, "1M"); 261 IOUtils.copy(PIXEL_RESOURCE.getInputStream(), response.getOutputStream()); 262 } 263 } 264 | Popular Tags |