1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.container.servlet.filters

File SavedRequestManager.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

12
31
10
2
211
91
17
0.55
3.1
5
1.7

Classes

Class Line # Actions
SavedRequestManager 38 22 0% 10 4
0.891891989.2%
SavedRequestManager.SavedRequest 49 9 0% 7 11
0.312531.2%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.container.servlet.filters;
21   
22    import java.io.Serializable;
23    import java.util.HashMap;
24    import java.util.Map;
25   
26    import javax.servlet.http.HttpServletRequest;
27    import javax.servlet.http.HttpSession;
28   
29    import org.apache.commons.lang3.RandomStringUtils;
30    import org.apache.commons.lang3.StringUtils;
31   
32    /**
33    * Allows to save a request and restore it later from the stored request identifier (SRID).
34    *
35    * @version $Id: fe7206aa864c24c03c09e2b1813e6efa04f510b3 $
36    * @since 2.5M1
37    */
 
38    public final class SavedRequestManager
39    {
40    /** The name of the parameter used for identifying a saved request in a new request. */
41    private static final String SAVED_REQUESTS_IDENTIFIER = "srid";
42   
43    /** The key used for storing request data in the HTTP session. */
44    private static final String SAVED_REQUESTS_KEY = SavedRequest.class.getCanonicalName() + "_SavedRequests";
45   
46    /**
47    * Saved request data. Only request parameter are stored, along with the requested URL.
48    */
 
49    public static class SavedRequest implements Serializable
50    {
51    /** Unique serialization identifier. */
52    private static final long serialVersionUID = 8779129900717599986L;
53   
54    /** Saved request data. */
55    private Map<String, String[]> parameters;
56   
57    /**
58    * The request URL; does not include the query string. The data is reused only if the new URL matches this
59    * value.
60    */
61    private String requestUrl;
62   
63    /**
64    * Constructor that copies the needed information from a request.
65    *
66    * @param request the request that needs to be saved
67    */
 
68  6 toggle @SuppressWarnings("unchecked")
69    public SavedRequest(HttpServletRequest request)
70    {
71  6 this.parameters = new HashMap<String, String[]>(request.getParameterMap());
72  6 this.requestUrl = request.getRequestURL().toString();
73    }
74   
75    /**
76    * Gets the value for a parameter, just like {@link javax.servlet.ServletRequest#getParameter(String)}.
77    *
78    * @param name the name of the parameter
79    * @return The first value for this parameter, or <code>null</code> if no value was sent for this parameter.
80    * @see javax.servlet.ServletRequest#getParameter(String)
81    * @see #getParameterValues(String)
82    */
 
83  0 toggle public String getParameter(String name)
84    {
85  0 String[] values = this.parameters.get(name);
86  0 if (values != null && values.length > 0) {
87  0 return values[0];
88    }
89  0 return null;
90    }
91   
92    /**
93    * Gets all the values stored for a parameter, just like
94    * {@link javax.servlet.ServletRequest#getParameterValues(String)}.
95    *
96    * @param name the name of the parameter
97    * @return All the values for this parameter, or <code>null</code> if no value was sent for this parameter.
98    * @see javax.servlet.ServletRequest#getParameterValues(String)
99    * @see #getParameter(String)
100    */
 
101  0 toggle public String[] getParameterValues(String name)
102    {
103  0 return this.parameters.get(name);
104    }
105   
106    /**
107    * Gets all the stored parameters, just like {@link javax.servlet.ServletRequest#getParameterMap()}.
108    *
109    * @return A map with all the stored parameters.
110    * @see javax.servlet.ServletRequest#getParameterMap()
111    */
 
112  0 toggle public Map<String, String[]> getParameterMap()
113    {
114  0 return this.parameters;
115    }
116   
117    /**
118    * Retrieves the original URL used for this request, as a future request will be able to reuse this data only if
119    * it is for the same document. Does not contain the query string.
120    *
121    * @return A <code>String</code> representation of the URL corresponding to this request.
122    */
 
123  1 toggle public String getRequestUrl()
124    {
125  1 return this.requestUrl;
126    }
127    }
128   
129    /**
130    * Forbid instantiation {@link SavedRequestManager}.
131    */
 
132  0 toggle private SavedRequestManager()
133    {
134    }
135   
136    /**
137    * @return the SAVED_REQUESTS_IDENTIFIER
138    */
 
139  37 toggle public static String getSavedRequestIdentifier()
140    {
141  37 return SAVED_REQUESTS_IDENTIFIER;
142    }
143   
144    /**
145    * @return the SAVED_REQUESTS_KEY
146    */
 
147  7 toggle public static String getSavedRequestKey()
148    {
149  7 return SAVED_REQUESTS_KEY;
150    }
151   
152    /**
153    * Saves the data from a request and stores it in the current session. This method is not thread safe, and does not
154    * guarantee that saved requests are not overwritten, but given that this should only happen sparingly, and that
155    * each client uses his own session to save this kind of information, this is not a real issue.
156    *
157    * @param request the request to save
158    * @return the identifier of the saved request
159    */
 
160  3 toggle @SuppressWarnings("unchecked")
161    public static String saveRequest(HttpServletRequest request)
162    {
163    // Saved requests are stored in the request session
164  3 HttpSession session = request.getSession();
165    // Retrieve (and eventually initialize) the list of stored requests
166  3 Map<String, SavedRequest> savedRequests =
167    (Map<String, SavedRequest>) session.getAttribute(getSavedRequestKey());
168  3 if (savedRequests == null) {
169  1 savedRequests = new HashMap<String, SavedRequest>();
170  1 session.setAttribute(getSavedRequestKey(), savedRequests);
171    }
172    // Save the request data
173  3 SavedRequest savedRequest = new SavedRequest(request);
174    // Generate a random key to identify this request
175  3 String key;
176  3 do {
177  3 key = RandomStringUtils.randomAlphanumeric(8);
178  3 } while (savedRequests.containsKey(key));
179    // Store the saved request
180  3 savedRequests.put(key, savedRequest);
181    // Return the generated identifier
182  3 return key;
183    }
184   
185    /**
186    * Retrieves the original URL requested before a detour. This method returns something different from
187    * <code>null</code> only when there's a <em>srid</em> parameter in the current request, indicating that there was
188    * another request which data was saved, related to the current request.
189    *
190    * @param request the current request
191    * @return the original requested URL that triggered a detour, or <code>null</code> if there isn't any original
192    * request information
193    */
 
194  2 toggle @SuppressWarnings("unchecked")
195    public static String getOriginalUrl(HttpServletRequest request)
196    {
197  2 HttpSession session = request.getSession();
198  2 Map<String, SavedRequest> savedRequests =
199    (Map<String, SavedRequest>) session.getAttribute(getSavedRequestKey());
200  2 if (savedRequests != null) {
201  1 String identifier = request.getParameter(getSavedRequestIdentifier());
202  1 if (!StringUtils.isEmpty(identifier)) {
203  1 SavedRequest savedRequest = savedRequests.get(request.getParameter(getSavedRequestIdentifier()));
204  1 if (savedRequest != null) {
205  1 return savedRequest.getRequestUrl() + "?srid=" + identifier;
206    }
207    }
208    }
209  1 return null;
210    }
211    }