1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import javax.servlet.http.HttpServletRequest ; 31 32 import org.apache.log4j.Logger; 33 import org.exolab.castor.jdo.Database; 34 import org.infoglue.cms.entities.kernel.BaseEntityVO; 35 import org.infoglue.cms.entities.management.ContentTypeDefinitionVO; 36 import org.infoglue.cms.entities.management.Redirect; 37 import org.infoglue.cms.entities.management.RedirectVO; 38 import org.infoglue.cms.entities.management.impl.simple.RedirectImpl; 39 import org.infoglue.cms.exception.Bug; 40 import org.infoglue.cms.exception.ConstraintException; 41 import org.infoglue.cms.exception.SystemException; 42 import org.infoglue.deliver.util.CacheController; 43 44 45 48 49 public class RedirectController extends BaseController 50 { 51 private final static Logger logger = Logger.getLogger(RedirectController.class.getName()); 52 53 56 57 public static RedirectController getController() 58 { 59 return new RedirectController(); 60 } 61 62 public RedirectVO getRedirectVOWithId(Integer redirectId) throws SystemException, Bug 63 { 64 return (RedirectVO) getVOWithId(RedirectImpl.class, redirectId); 65 } 66 67 public Redirect getRedirectWithId(Integer redirectId, Database db) throws SystemException, Bug 68 { 69 return (Redirect) getObjectWithId(RedirectImpl.class, redirectId, db); 70 } 71 72 public List getRedirectVOList() throws SystemException, Bug 73 { 74 List redirectVOList = getAllVOObjects(RedirectImpl.class, "redirectId"); 75 76 return redirectVOList; 77 } 78 79 public RedirectVO create(RedirectVO redirectVO) throws ConstraintException, SystemException 80 { 81 Redirect redirect = new RedirectImpl(); 82 redirect.setValueObject(redirectVO); 83 redirect = (Redirect) createEntity(redirect); 84 return redirect.getValueObject(); 85 } 86 87 public void delete(RedirectVO redirectVO) throws ConstraintException, SystemException 88 { 89 deleteEntity(RedirectImpl.class, redirectVO.getRedirectId()); 90 } 91 92 public RedirectVO update(RedirectVO redirectVO) throws ConstraintException, SystemException 93 { 94 return (RedirectVO) updateEntity(RedirectImpl.class, redirectVO); 95 } 96 97 102 103 public String getRedirectUrl(HttpServletRequest request) throws Exception 104 { 105 try 106 { 107 String requestURL = request.getRequestURL().toString(); 108 int indexOfProtocol = requestURL.indexOf("://"); 109 int indexFirstSlash = requestURL.indexOf("/", indexOfProtocol + 3); 110 String base = requestURL.substring(0, indexFirstSlash); 111 112 logger.info("base:" + base); 113 114 String requestURI = base + getContextURI(request); 116 logger.info("requestURI:" + requestURI); 118 119 120 logger.info("full requestURI:" + requestURI); 121 122 Collection cachedRedirects = (Collection )CacheController.getCachedObject("redirectCache", "allRedirects"); 123 if(cachedRedirects == null) 124 { 125 cachedRedirects = RedirectController.getController().getRedirectVOList(); 126 CacheController.cacheObject("redirectCache", "allRedirects", cachedRedirects); 127 } 128 129 Iterator redirectsIterator = cachedRedirects.iterator(); 130 while(redirectsIterator.hasNext()) 131 { 132 RedirectVO redirect = (RedirectVO)redirectsIterator.next(); 133 logger.info("url:" + redirect.getUrl()); 134 boolean matches = false; 135 if(redirect.getUrl().startsWith(".*")) 136 { 137 if(requestURI.indexOf(redirect.getUrl().substring(2)) > -1) 138 matches = true; 139 } 140 else if(requestURI.startsWith(redirect.getUrl())) 141 { 142 matches = true; 143 } 144 145 if(matches) 147 { 148 logger.info("redirectUrl:" + redirect.getRedirectUrl()); 149 String remainingURI = requestURI.replaceAll(redirect.getUrl(), ""); 150 logger.info("remainingURI:" + remainingURI); 151 return redirect.getRedirectUrl() + remainingURI + (request.getQueryString() != null && request.getQueryString().length() > 0 ? "?" + request.getQueryString() : ""); 152 } 153 } 154 } 155 catch(Exception e) 156 { 157 e.printStackTrace(); 158 throw new SystemException("An error occurred when looking for page:" + e.getMessage()); 159 } 160 161 return null; 162 } 163 164 private String getContextRelativeURI(HttpServletRequest request) { 165 String requestURI = request.getRequestURI(); 166 String contextPath = request.getContextPath(); 167 if (contextPath != null && requestURI.length() > 0) { 168 requestURI = requestURI.substring(contextPath.length(), requestURI.length()); 169 } 170 if (requestURI.length() == 0) 171 return "/"; 172 return requestURI; 173 } 174 175 private String getContextURI(HttpServletRequest request) { 176 String requestURI = request.getRequestURI(); 177 if (requestURI.length() == 0) 178 return "/"; 179 return requestURI; 180 } 181 182 183 187 188 public BaseEntityVO getNewVO() 189 { 190 return new ContentTypeDefinitionVO(); 191 } 192 } 193 | Popular Tags |