View Javadoc

1   /**
2    * This file Copyright (c) 2010-2014 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.context.MgnlContext;
45  import info.magnolia.repository.RepositoryConstants;
46  
47  import javax.jcr.Node;
48  import javax.jcr.RepositoryException;
49  
50  /**
51   * Adds support for redirects based on uuids. By default supports the formats "website:uuid" and "dms:uuid" for sending
52   * redirects for the website and dms workspaces. The prefixes and the workspaces they map to are configurable and
53   * can be changed to support more workspaces.
54   *
55   * @see UuidRedirectView
56   * @since 1.2
57   */
58  public class UuidRedirectViewResolver implements ViewResolver, Ordered, InitializingBean {
59  
60      public static final String REDIRECT_MAIN_CONTENT_PLACEHOLDER = "magnolia-redirect:main-content";
61      public static final String REDIRECT_CURRENT_CONTENT_PLACEHOLDER = "magnolia-redirect:current-content";
62  
63      private String[] workspaces = new String[]{RepositoryConstants.WEBSITE, "dms"};
64      private String[] prefixes;
65      private boolean http10Compatible = true;
66      private int order = Integer.MAX_VALUE;
67      private boolean exposeModelAttributes = true;
68  
69      @Override
70      public int getOrder() {
71          return order;
72      }
73  
74      public void setOrder(int order) {
75          this.order = order;
76      }
77  
78      public String[] getWorkspaces() {
79          return workspaces;
80      }
81  
82      public void setWorkspaces(String[] workspaces) {
83          this.workspaces = workspaces;
84      }
85  
86      public void setWorkspace(String workspace) {
87          this.workspaces = new String[]{workspace};
88      }
89  
90      @Deprecated
91      public String[] getRepositories() {
92          return workspaces;
93      }
94  
95      @Deprecated
96      public void setRepositories(String[] repositories) {
97          this.workspaces = repositories;
98      }
99  
100     @Deprecated
101     public void setRepository(String repository) {
102         this.workspaces = new String[]{repository};
103     }
104 
105     public String[] getPrefixes() {
106         return prefixes;
107     }
108 
109     public void setPrefixes(String[] prefixes) {
110         this.prefixes = prefixes;
111     }
112 
113     public void setPrefix(String prefix) {
114         this.prefixes = new String[]{prefix};
115     }
116 
117     public boolean isHttp10Compatible() {
118         return http10Compatible;
119     }
120 
121     public void setHttp10Compatible(boolean http10Compatible) {
122         this.http10Compatible = http10Compatible;
123     }
124 
125     public boolean isExposeModelAttributes() {
126         return exposeModelAttributes;
127     }
128 
129     public void setExposeModelAttributes(boolean exposeModelAttributes) {
130         this.exposeModelAttributes = exposeModelAttributes;
131     }
132 
133     @Override
134     public View resolveViewName(String viewName, Locale locale) throws Exception {
135         if (viewName.equals(REDIRECT_MAIN_CONTENT_PLACEHOLDER)) {
136             return createView(MgnlContext.getAggregationState().getMainContentNode());
137         }
138         if (viewName.equals(REDIRECT_CURRENT_CONTENT_PLACEHOLDER)) {
139             return createView(MgnlContext.getAggregationState().getCurrentContentNode());
140         }
141         for (int i = 0; i < prefixes.length; i++) {
142             String prefix = prefixes[i];
143             if (viewName.startsWith(prefix)) {
144                 return createView(workspaces[i], viewName.substring(prefix.length()));
145             }
146         }
147         return null;
148     }
149 
150     protected View createView(Node node) throws RepositoryException {
151         return createView(node.getSession().getWorkspace().getName(), node.getIdentifier());
152     }
153 
154     protected View createView(String workspace, String uuid) {
155         UuidRedirectView view = new UuidRedirectView(workspace, uuid);
156         view.setHttp10Compatible(http10Compatible);
157         view.setExposeModelAttributes(exposeModelAttributes);
158         return view;
159     }
160 
161     @Override
162     public void afterPropertiesSet() throws Exception {
163         if (prefixes == null) {
164             prefixes = new String[workspaces.length];
165             for (int i = 0; i < workspaces.length; i++) {
166                 prefixes[i] = workspaces[i] + ":";
167             }
168         }
169         if (prefixes.length != workspaces.length) {
170             throw new BeanInitializationException("The number of prefixes configured must match the number of workspaces.");
171         }
172     }
173 }