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.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   * Utility class for storing FormState in session and getting the form state token from a request.
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          // This can fail if the response is already committed
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          // This can fail if the response is already committed
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          // This can fail if the response is already committed
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         // be sure that workspace is not null
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         // be sure that workspace is not null
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 }