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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 package org.w3c.tidy.servlet.data;
56
57 import javax.servlet.http.HttpServletRequest;
58 import javax.servlet.http.HttpServletResponse;
59 import javax.servlet.http.HttpSession;
60
61 import org.w3c.tidy.servlet.Consts;
62 import org.w3c.tidy.servlet.RepositoryFactory;
63 import org.w3c.tidy.servlet.ResponseRecord;
64 import org.w3c.tidy.servlet.ResponseRecordRepository;
65
66
67 /***
68 * Default Factory implementation, Singleton.
69 * @author Vlad Skarzhevskyy <a href="mailto:skarzhevskyy@gmail.com">skarzhevskyy@gmail.com </a>
70 * @version $Revision: 1.4 $ ($Author: fgiust $)
71 */
72 public class DefaultRepositoryFactory implements RepositoryFactory
73 {
74
75 /***
76 * singleton.
77 */
78 private static ResponseRecordRepository repositoryInstance;
79
80 /***
81 * Global Request key sequence.
82 */
83 private static long staticRequestID;
84
85 private static synchronized ResponseRecordRepository getStaticRepositoryInstance()
86 {
87 if (repositoryInstance == null)
88 {
89 repositoryInstance = new DefaultResponseRecordRepository();
90 }
91 return repositoryInstance;
92 }
93
94 /***
95 * {@inheritDoc}
96 */
97 public ResponseRecordRepository getRepositoryInstance(HttpSession httpSession)
98 {
99 return getStaticRepositoryInstance();
100 }
101
102 /***
103 * Globabl sequence generator
104 * @return Returns the requst new ID when asked.
105 */
106 private static synchronized long getNewRequestID()
107 {
108 staticRequestID++;
109 return staticRequestID;
110 }
111
112 /***
113 * Implementation of sequence generator. Could be overdriven in descendant calss
114 * @return Returns the requst new ID when asked.
115 */
116 public long generateNewRequestID(HttpSession httpSession)
117 {
118 return getNewRequestID();
119 }
120
121 /***
122 * Disable some pages validation or, save numbers for not important pages. Could be overdriven in descendant calss
123 * @return Returns boolean
124 */
125 public boolean allowURI(String uri)
126 {
127 if (uri.endsWith("empty.html"))
128 {
129 return false;
130 }
131 else
132 {
133 return true;
134 }
135 }
136
137 /***
138 * {@inheritDoc}
139 */
140 public Object getResponseID(HttpSession httpSession, HttpServletRequest request, HttpServletResponse response,
141 boolean newResponse)
142 {
143
144 if ((request != null) && (!allowURI(request.getRequestURI())))
145 {
146 return null;
147 }
148
149 Long atribRequestID = null;
150 if ((!newResponse) && (request != null))
151 {
152 atribRequestID = (Long) request.getAttribute(Consts.ATTRIBUTE_REQUEST_ID);
153 }
154
155 if (atribRequestID == null)
156 {
157 atribRequestID = new Long(generateNewRequestID(httpSession));
158 if (request != null)
159 {
160 request.setAttribute(Consts.ATTRIBUTE_REQUEST_ID, atribRequestID);
161
162 }
163 }
164 return atribRequestID;
165 }
166
167 /***
168 * {@inheritDoc}
169 */
170 public ResponseRecord createRecord(Object key, HttpSession httpSession, HttpServletRequest request,
171 HttpServletResponse response)
172 {
173 return new DefaultResponseRecord();
174 }
175
176 }