aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language-web/src/main/java/tools/refinery/language/web/config/BackendConfigServlet.java
blob: a2f04e343ccdc7f5e7f0852976ac0eb18d9f967d (plain) (blame)
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
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.language.web.config;

import com.google.gson.Gson;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpStatus;

import java.io.IOException;

public class BackendConfigServlet extends HttpServlet {
	public static final String WEBSOCKET_URL_INIT_PARAM = "tools.refinery.language.web.config.BackendConfigServlet" +
			".webSocketUrl";

	private String serializedConfig;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		var webSocketUrl = config.getInitParameter(WEBSOCKET_URL_INIT_PARAM);
		if (webSocketUrl == null) {
			throw new IllegalArgumentException("Init parameter " + WEBSOCKET_URL_INIT_PARAM + " is mandatory");
		}
		var backendConfig = new BackendConfig(webSocketUrl);
		var gson = new Gson();
		serializedConfig = gson.toJson(backendConfig);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setStatus(HttpStatus.OK_200);
		resp.setContentType("application/json");
		var writer = resp.getWriter();
		writer.write(serializedConfig);
		writer.flush();
	}
}