1 24 package org.riotfamily.common.web.controller; 25 26 import java.io.IOException ; 27 import java.io.UnsupportedEncodingException ; 28 import java.net.URLEncoder ; 29 import java.util.Enumeration ; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import org.riotfamily.common.web.util.ServletUtils; 35 import org.springframework.web.servlet.ModelAndView; 36 import org.springframework.web.servlet.mvc.Controller; 37 38 41 public class RedirectController implements Controller { 42 43 private boolean http10Compatible = true; 44 45 private boolean addContextPath = false; 46 47 private boolean addServletMapping = false; 48 49 private String encodingScheme = "UTF-8"; 50 51 private String url; 52 53 public RedirectController(String url) { 54 this.url = url; 55 } 56 57 public RedirectController(String url, boolean addContextPath, 58 boolean addServletMapping) { 59 60 this.url = url; 61 this.addContextPath = addContextPath; 62 this.addServletMapping = addServletMapping; 63 } 64 65 protected RedirectController() { 66 } 67 68 79 public void setHttp10Compatible(boolean http10Compatible) { 80 this.http10Compatible = http10Compatible; 81 } 82 83 84 public void setAddContextPath(boolean contextRelative) { 85 this.addContextPath = contextRelative; 86 } 87 88 public void setAddServletMapping(boolean addServletMapping) { 89 this.addServletMapping = addServletMapping; 90 } 91 92 95 public void setEncodingScheme(String encodingScheme) { 96 this.encodingScheme = encodingScheme; 97 } 98 99 public ModelAndView handleRequest(HttpServletRequest request, 100 HttpServletResponse response) throws Exception { 101 102 String destination = getDestination(request); 103 if (destination == null) { 104 response.sendError(HttpServletResponse.SC_NOT_FOUND); 105 return null; 106 } 107 108 StringBuffer url = new StringBuffer (); 109 110 if (addContextPath && destination.startsWith("/")) { 111 url.append(request.getContextPath()); 112 } 113 if (addServletMapping) { 114 url.append(ServletUtils.getServletPrefix(request)); 115 } 116 117 url.append(destination); 118 119 if (addServletMapping) { 120 url.append(ServletUtils.getServletSuffix(request)); 121 } 122 appendParameters(url, request); 123 sendRedirect(request, response, url.toString()); 124 return null; 125 } 126 127 protected String getDestination(HttpServletRequest request) { 128 return url; 129 } 130 131 protected void appendParameters(StringBuffer targetUrl, 132 HttpServletRequest request) throws UnsupportedEncodingException { 133 134 boolean first = (targetUrl.indexOf("?") == -1); 135 Enumeration paramNames = request.getParameterNames(); 136 while (paramNames.hasMoreElements()) { 137 String name = (String ) paramNames.nextElement(); 138 String [] values = request.getParameterValues(name); 139 for (int i = 0; i < values.length; i++) { 140 if (first) { 141 targetUrl.append('?'); 142 first = false; 143 } 144 else { 145 targetUrl.append('&'); 146 } 147 targetUrl.append(name).append('='); 148 if (values[i] != null) { 149 targetUrl.append(URLEncoder.encode(values[i], encodingScheme)); 150 } 151 } 152 } 153 } 154 155 162 protected void sendRedirect(HttpServletRequest request, 163 HttpServletResponse response, String targetUrl) throws IOException { 164 165 if (http10Compatible) { 166 response.sendRedirect(response.encodeRedirectURL(targetUrl)); 168 } 169 else { 170 response.setStatus(303); 172 response.setHeader("Location", response.encodeRedirectURL(targetUrl)); 173 } 174 } 175 176 } 177 | Popular Tags |