1 24 package org.riotfamily.common.web.view; 25 26 import java.io.IOException ; 27 import java.net.URI ; 28 import java.net.URISyntaxException ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.Random ; 34 35 import javax.servlet.ServletException ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 39 import org.apache.commons.logging.Log; 40 import org.apache.commons.logging.LogFactory; 41 import org.riotfamily.common.beans.PropertyUtils; 42 import org.riotfamily.common.util.FormatUtils; 43 import org.riotfamily.common.web.filter.ResourceStamper; 44 import org.riotfamily.common.web.mapping.ReverseHandlerMapping; 45 import org.riotfamily.common.web.util.ServletUtils; 46 import org.springframework.context.ApplicationContext; 47 import org.springframework.util.StringUtils; 48 import org.springframework.web.servlet.support.RequestContextUtils; 49 50 54 public class CommonMacroHelper { 55 56 private static final Log log = LogFactory.getLog(CommonMacroHelper.class); 57 58 private static Random random = new Random (); 59 60 private ApplicationContext ctx; 61 62 private HttpServletRequest request; 63 64 private HttpServletResponse response; 65 66 private ResourceStamper stamper; 67 68 private List mappings; 69 70 private Locale requestLocale = null; 71 72 public CommonMacroHelper(ApplicationContext ctx, 73 HttpServletRequest request, HttpServletResponse response, 74 ResourceStamper stamper, List mappings) { 75 76 this.ctx = ctx; 77 this.request = request; 78 this.response = response; 79 this.stamper = stamper; 80 this.mappings = mappings; 81 } 82 83 public Random getRandom() { 84 return random; 85 } 86 87 public Locale getLocale() { 88 if (requestLocale == null) { 89 requestLocale = RequestContextUtils.getLocale(request); 90 } 91 return requestLocale; 92 } 93 94 public String getMessage(String code, List args, String defaultMessage) { 95 return ctx.getMessage(code, args.toArray(), defaultMessage, getLocale()); 96 } 97 98 public String resolveAndEncodeUrl(String url) { 99 return ServletUtils.resolveAndEncodeUrl(url, request, response); 100 } 101 102 public String getAbsoluteUrl(String url) { 103 return ServletUtils.getAbsoluteUrlPrefix(request) 104 .append(request.getContextPath()).append(url).toString(); 105 } 106 107 public String getUrlForHandler(String handlerName) { 108 String url = null; 109 Iterator it = mappings.iterator(); 110 while (url == null && it.hasNext()) { 111 ReverseHandlerMapping mapping = (ReverseHandlerMapping) it.next(); 112 url = mapping.getUrlForHandler(handlerName, request); 113 } 114 return url; 115 } 116 117 public String getUrlForHandlerWithAttribute(String handlerName, 118 Object attribute) { 119 120 String url = null; 121 Iterator it = mappings.iterator(); 122 while (url == null && it.hasNext()) { 123 ReverseHandlerMapping mapping = (ReverseHandlerMapping) it.next(); 124 url = mapping.getUrlForHandlerWithAttribute( 125 handlerName, attribute, request); 126 } 127 return url; 128 } 129 130 public String getUrlForHandlerWithAttributes(String handlerName, 131 Object attributes) { 132 133 String url = null; 134 Iterator it = mappings.iterator(); 135 while (url == null && it.hasNext()) { 136 ReverseHandlerMapping mapping = (ReverseHandlerMapping) it.next(); 137 url = mapping.getUrlForHandlerWithAttributes( 138 handlerName, attributes, request); 139 } 140 return url; 141 } 142 143 public String getOriginatingRequestUri() { 144 String uri = ServletUtils.getOriginatingRequestUri(request); 145 if (StringUtils.hasText(request.getQueryString())) { 146 uri = uri + "?" + request.getQueryString(); 147 } 148 return uri; 149 } 150 151 public String getPathWithinApplication() { 152 return ServletUtils.getPathWithinApplication(request); 153 } 154 155 public boolean isExternalUrl(String url) { 156 try { 157 URI uri = new URI (url); 158 if (!uri.isOpaque()) { 159 if (uri.isAbsolute() && !request.getServerName().equals( 160 uri.getHost())) { 161 162 return true; 163 } 164 } 165 } 166 catch (URISyntaxException e) { 167 log.warn(e.getMessage()); 168 } 169 return false; 170 } 171 172 public String include(String url) throws ServletException , IOException { 173 request.getRequestDispatcher(url).include(request, response); 174 return ""; 175 } 176 177 public String addTimestamp(String s) { 178 return stamper.stamp(s); 179 } 180 181 public List partition(Collection c, String titleProperty) { 182 return PropertyUtils.partition(c, titleProperty); 183 } 184 185 public String getFileExtension(String filename, Collection validExtensions, 186 String defaultExtension) { 187 188 String ext = FormatUtils.getExtension(filename); 189 if (validExtensions.isEmpty() || validExtensions.contains(ext)) { 190 return ext; 191 } 192 return defaultExtension; 193 } 194 195 public String baseName(String path) { 196 int begin = path.lastIndexOf('/') + 1; 197 int end = path.indexOf(';'); 198 if (end == -1) { 199 end = path.indexOf('?'); 200 if (end == -1) { 201 end = path.length(); 202 } 203 } 204 return path.substring(begin, end); 205 } 206 207 public String formatByteSize(long bytes) { 208 return FormatUtils.formatByteSize(bytes); 209 } 210 211 public String toTitleCase(String s) { 212 return FormatUtils.fileNameToTitleCase(s); 213 } 214 215 } 216 | Popular Tags |