eperusteet-opintopolku-app/src/main/java/fi/vm/sade/eperusteet/opintopolku/ConfigServlet.java
package fi.vm.sade.eperusteet.opintopolku;
/*
* Copyright (c) 2013 The Finnish Board of Education - Opetushallitus
*
* This program is free software: Licensed under the EUPL, Version 1.1 or - as soon as they will be approved by the European Commission - subsequent versions of
* the EUPL (the "Licence");
*
* You may not use this work except in compliance with the Licence. You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl
*
* 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 European Union Public Licence for more details.
*/
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author jhyoty
*/
@WebServlet(urlPatterns = {"/config.json"}, loadOnStartup = 1)
public class ConfigServlet extends HttpServlet {
private Map<String, String> config;
private final ObjectMapper mapper = new ObjectMapper();
@Override
public void init() throws ServletException {
final String home = System.getProperty("user.home");
final Path path = FileSystems.getDefault().getPath(home,"oph-configuration","eperusteet-opintopolku.properties");
try (Reader props = Files.newBufferedReader(path, StandardCharsets.ISO_8859_1)) {
Properties p = new Properties();
p.load(props);
HashMap<String, String> tmp = new HashMap<>();
for (Map.Entry<Object, Object> e : p.entrySet()) {
tmp.put(e.getKey().toString(), e.getValue().toString());
}
this.config = Collections.unmodifiableMap(tmp);
} catch (IOException ex) {
throw new ServletException(ex);
}
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
try (PrintWriter out = response.getWriter()) {
mapper.writeValue(out, config);
}
}
/**
* Handles the HTTP <code>GET</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 doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}