KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > RedirectController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
46  * @author Mattias Bogeblad
47  */

48
49 public class RedirectController extends BaseController
50 {
51     private final static Logger logger = Logger.getLogger(RedirectController.class.getName());
52
53     /**
54      * Factory method
55      */

56
57     public static RedirectController getController()
58     {
59         return new RedirectController();
60     }
61
62     public RedirectVO getRedirectVOWithId(Integer JavaDoc redirectId) throws SystemException, Bug
63     {
64         return (RedirectVO) getVOWithId(RedirectImpl.class, redirectId);
65     }
66
67     public Redirect getRedirectWithId(Integer JavaDoc redirectId, Database db) throws SystemException, Bug
68     {
69         return (Redirect) getObjectWithId(RedirectImpl.class, redirectId, db);
70     }
71
72     public List JavaDoc getRedirectVOList() throws SystemException, Bug
73     {
74         List JavaDoc 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     /**
98      * This method checks if there is a redirect that should be used instead.
99      * @param requestURI
100      * @throws Exception
101      */

102     
103     public String JavaDoc getRedirectUrl(HttpServletRequest JavaDoc request) throws Exception JavaDoc
104     {
105         try
106         {
107             String JavaDoc requestURL = request.getRequestURL().toString();
108             int indexOfProtocol = requestURL.indexOf("://");
109             int indexFirstSlash = requestURL.indexOf("/", indexOfProtocol + 3);
110             String JavaDoc base = requestURL.substring(0, indexFirstSlash);
111
112             logger.info("base:" + base);
113
114             //String requestURL = request.getRequestURL().toString();
115
String JavaDoc requestURI = base + getContextURI(request);
116             //System.out.println("requestURL:" + requestURL);
117
logger.info("requestURI:" + requestURI);
118             
119             
120             logger.info("full requestURI:" + requestURI);
121             
122             Collection JavaDoc cachedRedirects = (Collection JavaDoc)CacheController.getCachedObject("redirectCache", "allRedirects");
123             if(cachedRedirects == null)
124             {
125                 cachedRedirects = RedirectController.getController().getRedirectVOList();
126                 CacheController.cacheObject("redirectCache", "allRedirects", cachedRedirects);
127             }
128             
129             Iterator JavaDoc 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(requestURI.startsWith(redirect.getUrl()))
146
if(matches)
147                 {
148                     logger.info("redirectUrl:" + redirect.getRedirectUrl());
149                     String JavaDoc 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 JavaDoc 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 JavaDoc getContextRelativeURI(HttpServletRequest JavaDoc request) {
165         String JavaDoc requestURI = request.getRequestURI();
166         String JavaDoc 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 JavaDoc getContextURI(HttpServletRequest JavaDoc request) {
176         String JavaDoc requestURI = request.getRequestURI();
177         if (requestURI.length() == 0)
178             return "/";
179         return requestURI;
180     }
181
182
183     /**
184      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
185      * is handling.
186      */

187
188     public BaseEntityVO getNewVO()
189     {
190         return new ContentTypeDefinitionVO();
191     }
192 }
193
Popular Tags