First pass implementation of an OAI-PMH Controller for identifiable objects
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / view / PatternViewResolver.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.cdm.remote.view;
12
13 import java.util.Arrays;
14 import java.util.HashSet;
15 import java.util.Locale;
16 import java.util.Set;
17
18 import org.springframework.beans.BeansException;
19 import org.springframework.beans.factory.BeanFactory;
20 import org.springframework.context.support.AbstractApplicationContext;
21 import org.springframework.stereotype.Component;
22 import org.springframework.util.AntPathMatcher;
23 import org.springframework.util.Assert;
24 import org.springframework.util.PathMatcher;
25 import org.springframework.web.servlet.View;
26 import org.springframework.web.servlet.view.XmlViewResolver;
27
28 import com.sun.xml.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
29
30 /**
31 * Resolves views by using a {@link PathMatcher} to match the view name to the bean names.
32 *
33 * Dy default the {@link AntPathMatcher} is used which supports Ant-style path patterns.
34 * The mapping matches views using the following rules:
35 * <ul>
36 * <li>? matches one character
37 * <li>* matches zero or more characters
38 * <li>** matches zero or more 'directories' in a path
39 * </ul>
40 * @author ben.clark
41 */
42 @Component
43 public class PatternViewResolver extends XmlViewResolver {
44 private final Set<String> viewSet = new HashSet<String>();
45
46 private PathMatcher pathMatcher = new AntPathMatcher();
47
48 public void setPathMatcher(PathMatcher pathMatcher) {
49 Assert.notNull(pathMatcher, "PathMatcher must not be null");
50 this.pathMatcher = pathMatcher;
51 }
52
53 public PathMatcher getPathMatcher() {
54 return this.pathMatcher;
55 }
56
57 @Override
58 public View resolveViewName(String viewName, Locale locale) throws Exception {
59 // Direct match?
60 if (this.viewSet.contains(viewName)) {
61 return super.resolveViewName(viewName, locale);
62 }
63 // Pattern match?
64 String bestPathMatch = null;
65 for (String registeredPath : this.viewSet) {
66 if (getPathMatcher().match(registeredPath, viewName) &&
67 (bestPathMatch == null || bestPathMatch.length() < registeredPath.length())) {
68 bestPathMatch = registeredPath;
69 }
70 }
71 if (bestPathMatch != null) {
72 return super.resolveViewName(bestPathMatch, locale);
73 }
74 // No view found...
75 return null;
76 }
77
78 @Override
79 protected synchronized BeanFactory initFactory() throws BeansException {
80 AbstractApplicationContext beanFactory = (AbstractApplicationContext)super.initFactory();
81 String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
82 for(String beanDefinitionName : beanDefinitionNames) {
83 viewSet.add(beanDefinitionName);
84 viewSet.addAll(Arrays.asList(beanFactory.getAliases(beanDefinitionName)));
85 }
86 return beanFactory;
87 }
88
89 }