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
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
52
53
54
55
56
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 }