| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
package net.sourceforge.pebble.web.filter; |
| 33 | |
|
| 34 | |
import org.apache.commons.logging.Log; |
| 35 | |
import org.apache.commons.logging.LogFactory; |
| 36 | |
|
| 37 | |
import javax.servlet.*; |
| 38 | |
import javax.servlet.jsp.jstl.core.Config; |
| 39 | |
import javax.servlet.http.HttpServletRequest; |
| 40 | |
import java.io.IOException; |
| 41 | |
import java.net.URLDecoder; |
| 42 | |
import java.util.List; |
| 43 | |
import java.util.Collections; |
| 44 | |
import java.util.Locale; |
| 45 | |
|
| 46 | |
import net.sourceforge.pebble.PebbleContext; |
| 47 | |
import net.sourceforge.pebble.Constants; |
| 48 | |
import net.sourceforge.pebble.comparator.BlogEntryComparator; |
| 49 | |
import net.sourceforge.pebble.decorator.ContentDecoratorChain; |
| 50 | |
import net.sourceforge.pebble.api.decorator.ContentDecoratorContext; |
| 51 | |
import net.sourceforge.pebble.domain.*; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | 8 | public class BlogLookupFilter implements Filter { |
| 59 | |
|
| 60 | |
|
| 61 | 4 | private static Log log = LogFactory.getLog(BlogLookupFilter.class); |
| 62 | |
|
| 63 | |
|
| 64 | |
private FilterConfig filterConfig; |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public void init(FilterConfig config) { |
| 72 | 8 | this.filterConfig = config; |
| 73 | 8 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public void destroy() { |
| 79 | 8 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| 85 | |
throws ServletException, IOException { |
| 86 | |
|
| 87 | 28 | HttpServletRequest httpRequest = (HttpServletRequest)request; |
| 88 | 28 | PebbleContext pebbleContext = PebbleContext.getInstance(); |
| 89 | |
AbstractBlog blog; |
| 90 | |
|
| 91 | 28 | String url = pebbleContext.getConfiguration().getUrl(); |
| 92 | 28 | if (pebbleContext != null && (url == null || url.length() == 0)) { |
| 93 | 0 | String scheme = httpRequest.getScheme(); |
| 94 | 0 | url = scheme + "://" + httpRequest.getServerName() + ":" + httpRequest.getServerPort() + httpRequest.getContextPath(); |
| 95 | 0 | log.info("Setting Pebble URL to " + url); |
| 96 | 0 | PebbleContext.getInstance().getConfiguration().setUrl(url); |
| 97 | |
} |
| 98 | |
|
| 99 | |
|
| 100 | 28 | String uri = httpRequest.getRequestURI(); |
| 101 | 28 | uri = uri.substring(httpRequest.getContextPath().length(), uri.length()); |
| 102 | |
|
| 103 | |
|
| 104 | 28 | if (BlogManager.getInstance().isMultiBlog()) { |
| 105 | 16 | if (uri.length() == 0) { |
| 106 | 4 | uri = "/"; |
| 107 | |
} |
| 108 | 16 | int index = uri.indexOf("/", 1); |
| 109 | 16 | if (index == -1) { |
| 110 | 12 | index = uri.length(); |
| 111 | |
} |
| 112 | |
|
| 113 | 16 | String blogName = null; |
| 114 | 16 | if (pebbleContext.getConfiguration().isVirtualHostingEnabled()) { |
| 115 | 0 | String serverName = httpRequest.getServerName(); |
| 116 | 0 | blogName = serverName.substring(0, serverName.indexOf(".")); |
| 117 | 0 | } else { |
| 118 | 16 | blogName = uri.substring(1, index); |
| 119 | |
} |
| 120 | |
|
| 121 | 16 | blogName = URLDecoder.decode(blogName, "UTF-8"); |
| 122 | 16 | if (BlogManager.getInstance().hasBlog(blogName)) { |
| 123 | 8 | blog = BlogManager.getInstance().getBlog(blogName); |
| 124 | 8 | uri = uri.substring(index, uri.length()); |
| 125 | |
} else { |
| 126 | 8 | blog = BlogManager.getInstance().getMultiBlog(); |
| 127 | |
} |
| 128 | 16 | } else { |
| 129 | 12 | blog = BlogManager.getInstance().getBlog(); |
| 130 | |
} |
| 131 | |
|
| 132 | 28 | httpRequest.setAttribute(Constants.BLOG_KEY, blog); |
| 133 | 28 | httpRequest.setAttribute(Constants.BLOG_MANAGER, BlogManager.getInstance()); |
| 134 | 28 | httpRequest.setAttribute(Constants.PEBBLE_CONTEXT, pebbleContext); |
| 135 | |
|
| 136 | 28 | if (blog instanceof Blog) { |
| 137 | 20 | httpRequest.setAttribute(Constants.BLOG_TYPE, "singleblog"); |
| 138 | |
} else { |
| 139 | 8 | httpRequest.setAttribute(Constants.BLOG_TYPE, "multiblog"); |
| 140 | |
} |
| 141 | |
|
| 142 | 28 | chain.doFilter(request, response); |
| 143 | 28 | } |
| 144 | |
} |