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.form.engine;
35
36 import info.magnolia.context.MgnlContext;
37 import info.magnolia.context.WebContext;
38 import info.magnolia.link.LinkUtil;
39 import info.magnolia.repository.RepositoryConstants;
40
41 import java.io.IOException;
42 import java.util.Map;
43 import java.util.Map.Entry;
44
45 import javax.jcr.RepositoryException;
46 import javax.servlet.http.HttpSession;
47
48 import org.apache.commons.lang.RandomStringUtils;
49 import org.apache.commons.lang.StringUtils;
50
51
52
53
54 public class FormStateUtil {
55
56 public static final String FORM_TOKEN_PARAMETER_NAME = "mgnlFormToken";
57 private static final String FORM_STATE_ATTRIBUTE_PREFIX = FormEngine.class.getName() + "-formState-";
58
59 public static FormState createAndSetFormState() {
60
61
62 HttpSession session = MgnlContext.getWebContext().getRequest().getSession();
63
64 FormState formState = newFormState();
65 session.setAttribute(FORM_STATE_ATTRIBUTE_PREFIX + formState.getToken(), formState);
66 return formState;
67 }
68
69 public static FormState newFormState() {
70 FormState formState = new FormState();
71 formState.setToken(RandomStringUtils.randomAlphanumeric(32));
72 return formState;
73 }
74
75 public static String getFormStateToken() throws FormStateTokenMissingException {
76 String formStateToken = MgnlContext.getParameter(FORM_TOKEN_PARAMETER_NAME);
77 if (formStateToken == null) {
78 throw new FormStateTokenMissingException();
79 }
80 return formStateToken;
81 }
82
83 public static FormState getFormState(String formStateToken) throws NoSuchFormStateException {
84
85
86 HttpSession session = MgnlContext.getWebContext().getRequest().getSession();
87
88 FormState formState = (FormState) session.getAttribute(FORM_STATE_ATTRIBUTE_PREFIX + formStateToken);
89 if (formState == null) {
90 throw new NoSuchFormStateException(formStateToken);
91 }
92
93 return formState;
94 }
95
96 public static void destroyFormState(FormState formState) {
97
98
99 HttpSession session = MgnlContext.getWebContext().getRequest().getSession();
100
101 session.removeAttribute(FORM_STATE_ATTRIBUTE_PREFIX + formState.getToken());
102 }
103
104 public static void sendRedirect(String uuid) throws RepositoryException, IOException {
105 sendRedirect(uuid, RepositoryConstants.WEBSITE);
106 }
107
108 public static void sendRedirect(String uuid, String workspace) throws RepositoryException, IOException {
109
110 workspace = StringUtils.isBlank(workspace) ? RepositoryConstants.WEBSITE : workspace;
111 String link = LinkUtil.createAbsoluteLink(workspace, uuid);
112 ((WebContext) MgnlContext.getInstance()).getResponse().sendRedirect(link);
113 }
114
115
116 public static void sendRedirectWithToken(String uuid, String formExecutionToken) throws RepositoryException, IOException {
117 sendRedirectWithTokenAndParameters(uuid, formExecutionToken, null);
118 }
119
120 public static void sendRedirectWithToken(String uuid, String formExecutionToken, String workspace) throws RepositoryException, IOException {
121 sendRedirectWithTokenAndParameters(uuid, formExecutionToken, null, workspace);
122 }
123
124 public static void sendRedirectWithTokenAndParameters(String uuid, String formExecutionToken, Map<String, String> parameters) throws RepositoryException, IOException {
125 sendRedirectWithTokenAndParameters(uuid, formExecutionToken, parameters, RepositoryConstants.WEBSITE);
126 }
127
128 public static void sendRedirectWithTokenAndParameters(String uuid, String formExecutionToken, Map<String, String> parameters, String workspace) throws RepositoryException, IOException {
129
130 workspace = StringUtils.isBlank(workspace) ? RepositoryConstants.WEBSITE : workspace;
131 String link = LinkUtil.createAbsoluteLink(workspace, uuid);
132 link += "?" + FORM_TOKEN_PARAMETER_NAME + "=" + formExecutionToken;
133 if (parameters != null) {
134 for (Entry<String, String> param : parameters.entrySet()) {
135 link = link + "&" + param.getKey() + "=" + param.getValue();
136 }
137 }
138 ((WebContext) MgnlContext.getInstance()).getResponse().sendRedirect(link);
139 }
140 }