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;
027
028import net.sf.json.JSONArray;
029import net.sf.json.JSONObject;
030import net.sf.json.JSONSerializer;
031import pt.ua.dicoogle.sdk.datastructs.MoveDestination;
032import pt.ua.dicoogle.core.ServerSettings;
033import pt.ua.dicoogle.core.XMLSupport;
034import pt.ua.dicoogle.server.web.utils.ResponseUtil;
035
036/**
037 * 
038 * @author Frederico Silva <fredericosilva@ua.pt>
039 */
040public class ServerStorageServlet extends HttpServlet{
041
042        @Override
043        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
044                        throws ServletException, IOException {
045        resp.setContentType("application/json");
046                resp.getWriter().write(JSONSerializer.toJSON(ServerSettings.getInstance().getMoves()).toString());
047        }
048
049        @Override
050        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
051                        throws ServletException, IOException {
052                
053                String type = req.getParameter("type");
054
055                if (type == null) {
056                        sendError(resp, 400, "Missing argument \"type\": must be either \"add\" or \"remove\"");
057                        return;
058                }
059
060                String ip = req.getParameter("ip");
061                String aetitle = req.getParameter("aetitle");
062                String port = req.getParameter("port");
063                String description = req.getParameter("description");
064                String paramPublic = req.getParameter("public");
065                boolean isPublic = paramPublic.isEmpty() || Boolean.parseBoolean(paramPublic);
066
067                // validate
068                if (aetitle == null) {
069                        sendError(resp, 400, "Missing argument \"aetitle\"");
070                        return;
071                }
072
073                switch(type){
074                case "add": {
075
076                        if (ip == null) {
077                                sendError(resp, 400, "Missing argument \"ip\"");
078                                return;
079                        }
080
081                        if (port == null) {
082                                sendError(resp, 400, "Missing argument \"port\"");
083                                return;
084                        }
085                        int _port = Integer.parseInt(port);
086                        if (_port < 1 || _port > 65535) {
087                                sendError(resp, 400, "Illegal argument \"port\": must be in network socket port range");
088                                return;
089                        }
090
091                        ServerSettings.getInstance().add(new MoveDestination(aetitle, ip, _port, isPublic, description));
092                        ResponseUtil.simpleResponse(resp, "added", true);
093                        break;
094                }
095                case "remove": {
096                        boolean removed = ServerSettings.getInstance().removeMoveDestination(aetitle);
097                        ResponseUtil.simpleResponse(resp, "removed", removed);
098                        break;
099                }
100                }
101                
102                  new XMLSupport().printXML();
103        }
104
105        private static void sendError(HttpServletResponse resp, int code, String message) throws IOException {
106                resp.setStatus(code);
107                resp.setContentType("application/json");
108                JSONObject obj = new JSONObject();
109                obj.put("error", message);
110                resp.getWriter().append(obj.toString());
111        }
112}