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;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.Iterator;
024import java.util.Map;
025import java.util.HashMap;
026import java.util.Enumeration;
027
028import javax.servlet.http.HttpServlet;
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpServletResponse;
031
032import org.apache.commons.lang3.StringUtils;
033
034import net.sf.json.JSONArray;
035import net.sf.json.JSONException;
036import net.sf.json.JSONObject;
037import pt.ua.dicoogle.sdk.datastructs.MoveDestination;
038import pt.ua.dicoogle.core.ServerSettings;
039import pt.ua.dicoogle.core.XMLSupport;
040import pt.ua.dicoogle.server.web.management.Dicoogle;
041import pt.ua.dicoogle.server.web.management.Services;
042import pt.ua.dicoogle.server.web.auth.Session;
043import static pt.ua.dicoogle.sdk.settings.Utils.processAdvancedSettings;
044
045/**
046 * Handles requests for applying the general/advanced Dicoogle Settings.
047 *
048 * @author António Novo <antonio.novo@ua.pt>
049 */
050@Deprecated
051public class SettingsServlet extends HttpServlet
052{
053        /**
054         * 
055         */
056        private static final long serialVersionUID = 1L;
057
058        private static Dicoogle dic;
059
060        public static final String PARAM_ACTION = "action";
061
062        public static final String ACTION_SET_ADVANCED_SETTINGS = "setadvancedsettings";
063        public static final String ACTION_SET_ACCESS_LIST_SETTINGS = "setaccesslistsettings";
064        public static final String ACTION_SET_SOP_CLASS_GLOBAL_TRANSFER_STORAGE_SETTINGS = "settransferstorageglobalsettings";
065        public static final String ACTION_SET_STORAGE_SERVICES = "setstorageservices";
066
067        /*
068         * Action codes for internal use.
069         */
070        private static final int ACTION_CODE_INVALID = 0;
071        private static final int ACTION_CODE_SET_ADVANCED_SETTINGS = 1;
072        private static final int ACTION_CODE_SET_ACCESS_LIST_SETTINGS = 2;
073        private static final int ACTION_CODE_SET_SOP_CLASS_GLOBAL_TRANSFER_STORAGE_SETTINGS = 3;
074        private static final int ACTION_CODE_SET_STORAGE_SERVICES = 4;
075
076        public SettingsServlet()
077        {
078                dic = Dicoogle.getInstance();
079        }
080
081        @Override
082        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
083        {
084                // TODO validate user session credentials
085
086                // get which action to take
087                String actionStr = request.getParameter(PARAM_ACTION);
088
089                // translate each action string to a action identification
090                int action = ACTION_CODE_INVALID;
091                if (actionStr != null)
092                {
093                        if (actionStr.equalsIgnoreCase(ACTION_SET_ADVANCED_SETTINGS))
094                                action = ACTION_CODE_SET_ADVANCED_SETTINGS;
095                        else if (actionStr.equalsIgnoreCase(ACTION_SET_ACCESS_LIST_SETTINGS))
096                                action = ACTION_CODE_SET_ACCESS_LIST_SETTINGS;
097                        else if (actionStr.equalsIgnoreCase(ACTION_SET_SOP_CLASS_GLOBAL_TRANSFER_STORAGE_SETTINGS))
098                                action = ACTION_CODE_SET_SOP_CLASS_GLOBAL_TRANSFER_STORAGE_SETTINGS;
099                        else if (actionStr.equalsIgnoreCase(ACTION_SET_STORAGE_SERVICES))
100                                action = ACTION_CODE_SET_STORAGE_SERVICES;
101
102                }
103
104                // response to each action accordingly
105                switch (action)
106                {
107                        case ACTION_CODE_SET_ADVANCED_SETTINGS:
108                                HashMap<String, String[]> advSettings = new HashMap<String, String[]>();
109
110                                // get all the settings and their values
111                                Enumeration<String> params = request.getParameterNames();
112                                while (params.hasMoreElements())
113                                {
114                                        String name = params.nextElement();
115
116                                        // ignore the main params (the ones that go us here)
117                                        if (name.equalsIgnoreCase(PARAM_ACTION))
118                                                continue;
119
120                                        String[] value = request.getParameterValues(name);
121                                        advSettings.put(name, value);
122                                }
123
124                                HashMap<String, Object> settings = dic.getIndexingSettings();
125
126                                processAdvancedSettings(settings, advSettings);
127
128                                // try to apply the settings
129                                if (dic.tryIndexingSettings(settings))
130                                {
131                                        dic.setIndexingSettings(settings);
132
133                                        Services svcs = Services.getInstance();
134                                        svcs.saveSettings();
135
136                                        // send the client back the to previous page
137                                        response.sendRedirect(Session.getLastVisitedURL(request));
138                                }
139                                else
140                                        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid parameters!");
141                        break;
142
143                        case ACTION_CODE_SET_ACCESS_LIST_SETTINGS:
144                                advSettings = new HashMap<String, String[]>();
145
146                                // get all the settings and their values
147                                params = request.getParameterNames();
148                                while (params.hasMoreElements())
149                                {
150                                        String name = params.nextElement();
151
152                                        // ignore the main params (the ones that go us here)
153                                        if (name.equalsIgnoreCase(PARAM_ACTION))
154                                                continue;
155
156                                        String[] value = request.getParameterValues(name);
157
158                                        advSettings.put(name, value);
159                                }
160
161                                settings = dic.getAccessListSettings();
162
163                                processAdvancedSettings(settings, advSettings);
164
165                                // try to apply the settings
166                                if (dic.tryAccessListSettings(settings))
167                                {
168                                        dic.setAccessListSettings(settings);
169
170                                        Services svcs = Services.getInstance();
171                                        svcs.saveSettings();
172                                        // TODO force the reload of the IAccessList implementation for the RMI interface
173
174                                        // send the client back the to previous page
175                                        response.sendRedirect(Session.getLastVisitedURL(request));
176                                }
177                                else
178                                        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid parameters!");
179                        break;
180
181                        case ACTION_CODE_SET_SOP_CLASS_GLOBAL_TRANSFER_STORAGE_SETTINGS:
182                                advSettings = new HashMap<String, String[]>();
183
184                                // get all the settings and their values
185                                params = request.getParameterNames();
186                                while (params.hasMoreElements())
187                                {
188                                        String name = params.nextElement();
189
190                                        // ignore the main params (the ones that go us here)
191                                        if (name.equalsIgnoreCase(PARAM_ACTION))
192                                                continue;
193
194                                        String[] value = request.getParameterValues(name);
195
196                                        advSettings.put(name, value);
197                                }
198
199                                settings = dic.getSOPClassGlobalSettings();
200
201                                processAdvancedSettings(settings, advSettings);
202
203                                // try to apply the settings
204                                if (dic.trySOPClassGlobalSettings(settings))
205                                {
206                                        dic.setSOPClassGlobalSettings(settings);
207
208                                        Services svcs = Services.getInstance();
209                                        svcs.saveSettings();
210
211                                        // send the client back the to previous page
212                                        response.sendRedirect(Session.getLastVisitedURL(request));
213                                }
214                                else
215                                        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid parameters!");
216                        break;
217                        
218                        case ACTION_CODE_SET_STORAGE_SERVICES:
219
220                                String requestParam = request.getParameter("MOVES");
221
222                                if(requestParam == null){
223                                        response.sendError(401,"No Moves Defined");
224                                        return;
225                                }
226                                
227                                ServerSettings set = ServerSettings.getInstance();
228                                ArrayList<MoveDestination> nmoves = new ArrayList<MoveDestination>();
229                                
230                                JSONArray moves = JSONArray.fromObject(requestParam);
231                                Iterator it = moves.iterator();
232                                while(it.hasNext()){
233                                        JSONObject obj = (JSONObject) it.next();
234                                        
235                                        try{
236                                                String aet = obj.getString("AETitle");
237                                                String ip = obj.getString("ipAddrs");
238                                                int port = obj.getInt("port");
239                                                
240                                                if( StringUtils.isNotBlank(aet) && StringUtils.isNotBlank(ip) && port > 0){
241                                                        nmoves.add(new MoveDestination(aet, ip, port)); 
242                                                }       
243                                        }catch(JSONException ex){
244                                                
245                                        }                       
246                                }
247                                
248                                set.setMoves(nmoves);
249                                
250                                XMLSupport _xml = new XMLSupport();
251                                _xml.printXML();
252                                _xml = null;
253                                
254                                break;
255
256                        default:
257                                response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid action!");
258                                return;
259                }
260        }
261
262        @Override
263        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
264        {
265                doGet(request, response);
266        }
267
268        /**
269         * Based on the request, returns a String containing a form with all the settings inputs boxes regarding the indexing options.
270         * These boxes are rendered/specified in accordance with the each setting value type reported by the plugin/service.
271         *
272         * @param request the servlet request object.
273         * @param brokerURL the URL of the broker that will apply the settings after receiving this forms post.
274         * @param elementID the ID of this HTML form, can be null.
275         * @return a String containing the HTML structure for the form with all the settings.
276         */
277        public static String getHTMLIndexingAdvancedSettingsForm(HttpServletRequest request, String brokerURL, String elementID) throws IOException
278        {
279                String result = "";
280
281                if ((elementID == null) || elementID.trim().isEmpty())
282                        result += "<form ";
283                else
284                        result += "<form id=\"" + elementID + "\" ";
285                result += "action=\"" + brokerURL + "\" method=\"get\">";
286
287                result += "<input type=\"hidden\" name=\"" + PARAM_ACTION + "\" value=\"" + ACTION_SET_ADVANCED_SETTINGS + "\" />";
288
289                result += "<table class=\"table table-hover\"><tbody>";
290
291                HashMap<String, Object> settings = dic.getIndexingSettings();
292                HashMap<String, String> settingsHelp = dic.getIndexingSettingsHelp();
293
294                Services svcs = Services.getInstance();
295
296                // just to avoid any unwanted exceptions, in case a plugin misbehaves
297                if (settings == null)
298                        settings = new HashMap<String, Object>();
299                if (settingsHelp == null)
300                        settingsHelp = new HashMap<String, String>();
301
302                // create a table row for each setting (includes name, value/type and help, if available)
303                for (Map.Entry<String, Object> setting : settings.entrySet())
304                {
305                        String key = setting.getKey();
306                        Object value = setting.getValue();
307                        String help = settingsHelp.get(key);
308
309                        result += svcs.getHTMLAdvancedSettingsFormRow(key, value, help);
310                }
311
312                result += "</tbody></table><br />";
313
314                result += "<input type=\"submit\" value=\"Apply Settings\" class=\"btn btn-primary\"/>";
315                result += "</form>";
316
317                return result;
318        }
319
320        /**
321         * Based on the request, returns a String containing a form with all the settings inputs boxes regarding the Dicoogle Access List settings.
322         * These boxes are rendered/specified in accordance with the each setting value type reported by the plugin/service.
323         *
324         * @param request the servlet request object.
325         * @param brokerURL the URL of the broker that will apply the settings after receiving this forms post.
326         * @param elementID the ID of this HTML form, can be null.
327         * @return a String containing the HTML structure for the form with all the settings.
328         */
329        public static String getHTMLAccessListSettingsForm(HttpServletRequest request, String brokerURL, String elementID) throws IOException
330        {
331                String result = "";
332
333                if ((elementID == null) || elementID.trim().isEmpty())
334                        result += "<form ";
335                else
336                        result += "<form id=\"" + elementID + "\" ";
337                result += "action=\"" + brokerURL + "\" method=\"get\">";
338
339                result += "<input type=\"hidden\" name=\"" + PARAM_ACTION + "\" value=\"" + ACTION_SET_ACCESS_LIST_SETTINGS + "\" />";
340
341                result += "<table class=\"table table-hover\"><tbody>";
342
343                HashMap<String, Object> settings = dic.getAccessListSettings();
344                HashMap<String, String> settingsHelp = dic.getAccessListSettingsHelp();
345
346                Services svcs = Services.getInstance();
347
348                // just to avoid any unwanted exceptions, in case a plugin misbehaves
349                if (settings == null)
350                        settings = new HashMap<String, Object>();
351                if (settingsHelp == null)
352                        settingsHelp = new HashMap<String, String>();
353
354                // create a table row for each setting (includes name, value/type and help, if available)
355                for (Map.Entry<String, Object> setting : settings.entrySet())
356                {
357                        String key = setting.getKey();
358                        Object value = setting.getValue();
359                        String help = settingsHelp.get(key);
360
361                        result += svcs.getHTMLAdvancedSettingsFormRow(key, value, help);
362                }
363
364                result += "</tbody></table><br />";
365
366                result += "<input type=\"submit\" value=\"Apply Settings\" class=\"btn btn-primary\"/>";
367                result += "</form>";
368
369                return result;
370        }
371
372        /**
373         * Based on the request, returns a String containing a form with all the settings inputs boxes regarding the Dicoogle Global SOP Classes Transfer Storage settings.
374         * These boxes are rendered/specified in accordance with the each setting value type reported by the plugin/service.
375         *
376         * @param request the servlet request object.
377         * @param brokerURL the URL of the broker that will apply the settings after receiving this forms post.
378         * @param elementID the ID of this HTML form, can be null.
379         * @return a String containing the HTML structure for the form with all the settings.
380         */
381        public static String getHTMLGlobalTransferStorageSettingsForm(HttpServletRequest request, String brokerURL, String elementID) throws IOException
382        {
383                String result = "";
384
385                if ((elementID == null) || elementID.trim().isEmpty())
386                        result += "<form ";
387                else
388                        result += "<form id=\"" + elementID + "\" ";
389                result += "action=\"" + brokerURL + "\" method=\"get\">";
390
391                result += "<input type=\"hidden\" name=\"" + PARAM_ACTION + "\" value=\"" + ACTION_SET_SOP_CLASS_GLOBAL_TRANSFER_STORAGE_SETTINGS + "\" />";
392
393                result += "<table class=\"table table-condensed\"><tbody>";
394                
395                HashMap<String, Object> settings = dic.getSOPClassGlobalSettings();
396                HashMap<String, String> settingsHelp = dic.getSOPClassGlobalSettingsHelp();
397
398                Services svcs = Services.getInstance();
399
400                // just to avoid any unwanted exceptions, in case a plugin misbehaves
401                if (settings == null)
402                        settings = new HashMap<String, Object>();
403                if (settingsHelp == null)
404                        settingsHelp = new HashMap<String, String>();
405
406                // create a table row for each setting (includes name, value/type and help, if available)
407                for (Map.Entry<String, Object> setting : settings.entrySet())
408                {
409                        String key = setting.getKey();
410                        Object value = setting.getValue();
411                        String help = settingsHelp.get(key);
412                        
413                        result += svcs.getHTMLAdvancedSettingsFormRow(key, value, help);
414                }
415                
416                result += "</tbody></table><br />";
417
418                result += "<input type=\"submit\" value=\"Apply Settings\" class=\"btn btn-primary\"/>";
419                result += "</form>";
420
421                return result;
422        }
423}