001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.server.web.servlets.management;
020
021import java.io.IOException;
022
023import javax.servlet.ServletException;
024import javax.servlet.http.HttpServlet;
025import javax.servlet.http.HttpServletRequest;
026import javax.servlet.http.HttpServletResponse;
027import net.sf.json.JSONObject;
028
029import pt.ua.dicoogle.core.ServerSettings;
030import pt.ua.dicoogle.server.ControlServices;
031import pt.ua.dicoogle.server.web.management.Services;
032
033/** Servlet for reading and writing DICOM service configurations.
034 * Modifying the "running" setting will trigger a start or a stop on the actual service.
035 *
036 * At the moment, applying settings to PLUGIN-type services is not implemented, resulting in a no-op.
037 * 
038 * @author Frederico Silva <fredericosilva@ua.pt>
039 */
040public class ServicesServlet extends HttpServlet {
041        
042        public final static int STORAGE = 0;
043        public final static int PLUGIN = 1;
044        public final static int QUERY = 2;
045        
046        private final int mType;
047        public ServicesServlet(int type) {
048                if (type < 0 || type > 2) {
049                        throw new IllegalArgumentException("Bad service type, must be 0, 1 or 2");
050                }
051                mType = type;
052        }
053
054        @Override
055        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
056                        throws ServletException, IOException {
057
058                final ControlServices controlServices = ControlServices.getInstance();
059                final ServerSettings serverSettings = ServerSettings.getInstance();
060
061                boolean isRunning = false; 
062                int port = -1;
063        boolean autostart = false;
064                switch (mType) {
065                case STORAGE:
066                        isRunning = controlServices.storageIsRunning();
067                        port = serverSettings.getStoragePort();
068                        autostart = serverSettings.isStorage();
069                        break;
070                case QUERY:
071                        isRunning = controlServices.queryRetrieveIsRunning();
072                        port = serverSettings.getWlsPort();
073                        autostart = serverSettings.isQueryRetrive();
074                        break;
075
076                default:
077                        break;
078                }
079                
080        JSONObject obj = new JSONObject();
081        obj.element("isRunning", isRunning);
082        obj.element("port", port);
083        obj.element("autostart", autostart);
084        
085        resp.setContentType("application/json");
086        resp.getWriter().print(obj.toString());
087        }
088
089        @Override
090        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
091                        throws ServletException, IOException {
092
093        JSONObject obj = new JSONObject();
094
095                // gather settings to update
096                boolean updateAutostart = false, updatePort = false, updateRunning = false;
097                boolean autostart = false, running = false;
098                int port = 0;
099
100                String paramPort = req.getParameter("port");
101                if (paramPort != null) {
102                        try {
103                                port = Integer.parseInt(paramPort);
104                                if (port <= 0 || port > 65535) {
105                                        throw new NumberFormatException();
106                                }
107                                updatePort = true;
108                        } catch (NumberFormatException ex) {
109                                obj.element("success", false);
110                                obj.element("error", "Bad service port: must be a valid port number");
111                                reply(resp, 400, obj);
112                                return;
113                        }
114                }
115
116        String paramAutostart = req.getParameter("autostart");
117        if (paramAutostart != null) {
118            autostart = Boolean.parseBoolean(paramAutostart);
119                        updateAutostart = true;
120        }
121        
122        String paramRunning = req.getParameter("running");
123        if (paramRunning != null) {
124                        running = Boolean.parseBoolean(paramRunning);
125                        updateRunning = true;
126        }
127
128                final ControlServices controlServices = ControlServices.getInstance();
129                final ServerSettings serverSettings = ServerSettings.getInstance();
130
131                // update auto-start
132                if (updateAutostart) {
133                        switch (mType) {
134                                case STORAGE:
135                                        serverSettings.setStorage(autostart);
136                                        obj.element("autostart", autostart);
137                                        break;
138                                case QUERY:
139                                        serverSettings.setQueryRetrive(autostart);
140                                        obj.element("autostart", autostart);
141                                        break;
142                        }
143                }
144
145                // update port
146                if (updatePort) {
147                        switch (mType) {
148                                case STORAGE:
149                                        serverSettings.setStoragePort(port);
150                                        obj.element("port", port);
151                                        break;
152                                case QUERY:
153                                        serverSettings.setWlsPort(port);
154                                        obj.element("port", port);
155                                        break;
156                        }
157                }
158
159                // update running
160                if (updateRunning) {
161                        switch (mType) {
162                                case STORAGE:
163                                        if (running) {
164                                                controlServices.startStorage();
165                                                obj.element("running", true);
166                                        } else {
167                                                controlServices.stopStorage();
168                                                obj.element("running", false);
169                                        }
170                                        break;
171
172                                case QUERY:
173                                        if (running) {
174                                                controlServices.startQueryRetrieve();
175                                                obj.element("running", true);
176
177                                        } else {
178                                                controlServices.stopQueryRetrieve();
179                                                obj.element("running", false);
180                                        }
181                                        break;
182
183                                default:
184                                        break;
185                        }
186                }
187                Services.getInstance().saveSettings();
188                obj.element("success", true);
189                reply(resp, 200, obj);
190        }
191
192        private static void reply(HttpServletResponse resp, int code, JSONObject object) throws IOException {
193                resp.setContentType("application/json");
194                resp.setStatus(code);
195                resp.getWriter().print(object.toString());
196        }
197
198}