View Javadoc

1   /**
2    * This file Copyright (c) 2010-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.blossom.view;
35  
36  import java.util.Locale;
37  
38  import org.springframework.beans.factory.BeanInitializationException;
39  import org.springframework.beans.factory.InitializingBean;
40  import org.springframework.core.Ordered;
41  import org.springframework.web.servlet.View;
42  import org.springframework.web.servlet.ViewResolver;
43  
44  import info.magnolia.repository.RepositoryConstants;
45  
46  /**
47   * Adds support for redirects based on uuids. By default supports the formats "website:uuid" and "dms:uuid" for sending
48   * redirects for the website and dms workspaces. The prefixes and the workspaces they map to are configurable and
49   * can be changed to support more workspaces.
50   *
51   * @see UuidRedirectView
52   * @since 1.2
53   */
54  public class UuidRedirectViewResolver implements ViewResolver, Ordered, InitializingBean {
55  
56      private String[] workspaces = new String[]{RepositoryConstants.WEBSITE, "dms"};
57      private String[] prefixes;
58      private boolean http10Compatible = true;
59      private int order = Integer.MAX_VALUE;
60  
61      @Override
62      public int getOrder() {
63          return order;
64      }
65  
66      public void setOrder(int order) {
67          this.order = order;
68      }
69  
70      public String[] getWorkspaces() {
71          return workspaces;
72      }
73  
74      public void setWorkspaces(String[] workspaces) {
75          this.workspaces = workspaces;
76      }
77  
78      public void setWorkspace(String workspace) {
79          this.workspaces = new String[]{workspace};
80      }
81  
82      @Deprecated
83      public String[] getRepositories() {
84          return workspaces;
85      }
86  
87      @Deprecated
88      public void setRepositories(String[] repositories) {
89          this.workspaces = repositories;
90      }
91  
92      @Deprecated
93      public void setRepository(String repository) {
94          this.workspaces = new String[]{repository};
95      }
96  
97      public String[] getPrefixes() {
98          return prefixes;
99      }
100 
101     public void setPrefixes(String[] prefixes) {
102         this.prefixes = prefixes;
103     }
104 
105     public void setPrefix(String prefix) {
106         this.prefixes = new String[]{prefix};
107     }
108 
109     public boolean isHttp10Compatible() {
110         return http10Compatible;
111     }
112 
113     public void setHttp10Compatible(boolean http10Compatible) {
114         this.http10Compatible = http10Compatible;
115     }
116 
117     @Override
118     public View resolveViewName(String viewName, Locale locale) throws Exception {
119         for (int i = 0; i < prefixes.length; i++) {
120             String prefix = prefixes[i];
121             if (viewName.startsWith(prefix)) {
122                 UuidRedirectView view = new UuidRedirectView(workspaces[i], viewName.substring(prefix.length()));
123                 view.setHttp10Compatible(http10Compatible);
124                 return view;
125             }
126         }
127         return null;
128     }
129 
130     @Override
131     public void afterPropertiesSet() throws Exception {
132         if (prefixes == null) {
133             prefixes = new String[workspaces.length];
134             for (int i = 0; i < workspaces.length; i++) {
135                 prefixes[i] = workspaces[i] + ":";
136             }
137         }
138         if (prefixes.length != workspaces.length) {
139             throw new BeanInitializationException("The number of prefixes configured must match the number of workspaces.");
140         }
141     }
142 }