-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
SimpleServlet.java
154 lines (134 loc) · 6.22 KB
/
SimpleServlet.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.test.clustering.single.web;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.function.Function;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @author Paul Ferraro
*/
@WebServlet(urlPatterns = { SimpleServlet.SERVLET_PATH })
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = -592774116315946908L;
private static final String SERVLET_NAME = "simple";
static final String SERVLET_PATH = "/" + SERVLET_NAME;
public static final String REQUEST_DURATION_PARAM = "requestduration";
public static final String HEADER_SERIALIZED = "serialized";
public static final String VALUE_HEADER = "value";
public static final String SESSION_ID_HEADER = "sessionId";
public static final String ATTRIBUTE = "test";
public static final String HEADER_NODE_NAME = "nodename";
public static URI createURI(URL baseURL) throws URISyntaxException {
return createURI(baseURL.toURI());
}
public static URI createURI(URI baseURI) {
return baseURI.resolve(SERVLET_NAME);
}
public static URI createURI(URL baseURL, int requestDuration) throws URISyntaxException {
return createURI(baseURL.toURI(), requestDuration);
}
public static URI createURI(URI baseURI, int requestDuration) {
return baseURI.resolve(SERVLET_NAME + '?' + REQUEST_DURATION_PARAM + '=' + requestDuration);
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.getServletContext().log(String.format("[%s] %s?%s", request.getMethod(), request.getRequestURI(), request.getQueryString()));
super.service(request, response);
}
@Override
protected void doHead(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession(true);
response.addHeader(SESSION_ID_HEADER, session.getId());
}
@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
response.addHeader(SESSION_ID_HEADER, session.getId());
session.removeAttribute(ATTRIBUTE);
}
@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession(false);
if (session != null) {
session.invalidate();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
Function<HttpSession, Mutable> accessor = session -> {
Mutable mutable = (Mutable) session.getAttribute(ATTRIBUTE);
if (mutable == null) {
mutable = new Mutable(0);
session.setAttribute(ATTRIBUTE, mutable);
}
return mutable;
};
this.increment(request, response, accessor);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Function<HttpSession, Mutable> accessor = session -> {
Mutable mutable = (Mutable) session.getAttribute(ATTRIBUTE);
if (mutable == null) {
session.setAttribute(ATTRIBUTE, new Mutable(0));
}
return (mutable == null) ? (Mutable) session.getAttribute(ATTRIBUTE) : mutable;
};
this.increment(request, response, accessor);
}
private void increment(HttpServletRequest request, HttpServletResponse response, Function<HttpSession, Mutable> accessor) throws IOException {
HttpSession session = request.getSession(true);
response.addHeader(SESSION_ID_HEADER, session.getId());
Mutable mutable = accessor.apply(session);
int value = mutable.increment();
response.setIntHeader(VALUE_HEADER, value);
response.setHeader(HEADER_SERIALIZED, Boolean.toString(mutable.wasSerialized()));
Mutable current = (Mutable) session.getAttribute(ATTRIBUTE);
if (!mutable.equals(current)) {
throw new IllegalStateException(String.format("Session attribute value = %s, expected %s", current, mutable));
}
try {
String nodeName = System.getProperty("jboss.node.name");
response.setHeader(HEADER_NODE_NAME, nodeName);
} catch (Exception ignore) {
}
this.getServletContext().log(request.getRequestURI() + ", value = " + value);
// Long running request?
if (request.getParameter(REQUEST_DURATION_PARAM) != null) {
int duration = Integer.parseInt(request.getParameter(REQUEST_DURATION_PARAM));
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
response.getWriter().write("Success");
}
}