-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopaServlet.java
179 lines (167 loc) · 5.95 KB
/
CopaServlet.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (C) 2013 UniCoPA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package unicopa.copa.server.servlet;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import unicopa.copa.base.com.exception.APIException;
import unicopa.copa.base.com.exception.InternalErrorException;
import unicopa.copa.base.com.serialization.ServerSerializer;
import unicopa.copa.server.CopaSystem;
import unicopa.copa.server.GeneralUserPermission;
/**
* This servlet receives all requests (AbstractRequest) from the clients, passes
* them to the CopaSystem and sends the reponses back.
*
* @author Felix Wiemuth
*/
public class CopaServlet extends HttpServlet implements Filter {
private CopaSystem system;
private List<String> permissions;
private static final Logger LOG = Logger.getLogger(CopaServlet.class
.getName());
private static final String PARAM_REQUEST = "req"; // the parameter name of
// the HTTP POST method
// which includes the
// client request
private static final String CONTENT_TYPE = "text/plain;charset=UTF-8";
@Override
public void init() throws ServletException {
system = CopaSystem.getInstance();
try {
LOG.addHandler(new FileHandler(system.getContext()
.getLogDirectory().getCanonicalPath()
+ "/copa-servlet.log", 10000000, 1, true));
} catch (IOException | SecurityException ex) {
Logger.getLogger(CopaServlet.class.getName()).log(Level.SEVERE,
null, ex);
}
// determine permissions config
File settingsDirectory = new File(this.system.getContext()
.getSettingsDirectory(), "permissions");
settingsDirectory.mkdirs();
File permissionsFile = new File(settingsDirectory, "permissions.txt");
try {
this.permissions = new ArrayList<>();
if (!permissionsFile.exists()) {
File src = new File(this.getClass()
.getResource("externalAddresses.txt").toURI());
unicopa.copa.server.util.IOutils.copyFile(src, permissionsFile);
}
FileInputStream extAddrs = new FileInputStream(permissionsFile);
Scanner scn = new Scanner(new BufferedInputStream(extAddrs));
if (scn.hasNextLine())
scn.nextLine();
if (scn.hasNextLine())
scn.nextLine();
if (scn.hasNextLine())
scn.nextLine(); // ignore first three lines
while (scn.hasNextLine()) {
String nextRole = scn.nextLine();
this.permissions.add(nextRole);
}
} catch (FileNotFoundException ex) {
LOG.log(Level.SEVERE, null, ex);
} catch (IOException | URISyntaxException ex) {
LOG.log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request
* servlet request
* @param response
* servlet response
* @throws ServletException
* if a servlet-specific error occurs
* @throws IOException
* if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String resp;
response.setContentType(CONTENT_TYPE);
String userName = request.getUserPrincipal().getName();
String req = request.getParameter(PARAM_REQUEST);
if (req == null) {
resp = ServerSerializer.serialize(new APIException(
"The HTTP POST request does not contain"
+ " the required parameter \"req\"."
+ " The request to the system must "
+ " be the value of this parameter."));
} else {
GeneralUserPermission userPermission = determineGeneralPermission(request);
resp = system.processClientMessage(req, userName, userPermission);
}
response.getWriter().print(resp);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
try {
chain.doFilter(request, response);
} catch (IOException | ServletException e) {
LOG.log(Level.SEVERE, "Internal Error.", e);
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.getWriter().print(
ServerSerializer.serialize(new InternalErrorException(
"The server failed to handle the request: "
+ e.getMessage())));
}
}
public GeneralUserPermission determineGeneralPermission(
HttpServletRequest request) {
for (String role : this.permissions) {
if (request.isUserInRole(role)) {
return GeneralUserPermission.POSSIBLE_OWNER;
}
}
return GeneralUserPermission.NONE;
}
}