Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package airport.web.controller; import airport.database.services.chat.Message; import airport.database.services.users.User; import airport.database.dispatcher.airplane.FlyingMachineType; import airport.database.dispatcher.airplane.Flight; import airport.database.dispatcher.airplane.Runaway; import airport.dispatcher.weather.GetWeather; import airport.services.chat.ServiceChat; import airport.services.dispatcher.ServiceDispatcher; import airport.services.dispatcher.ServiceFlyingType; import airport.services.git.NodeDisctributor; import airport.services.git.ServiceDistributor; import airport.services.setting.ServiceSetting; import airport.database.services.setting.SettingFrontEnd; import airport.database.services.statistics.Statistics; import airport.database.services.users.Dispatcher; import airport.services.statistics.ServiceStatistics; import airport.services.users.ServiceDispatcherName; import airport.services.users.ServiceUsers; import airport.services.weather.ServiceWeather; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.log4j.Logger; import org.codehaus.jackson.annotate.JsonIgnore; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * * @author mcdoker */ @RestController public class ServicesController { private final static Logger LOG = Logger.getLogger(ServicesController.class); @Autowired private ServiceFlyingType serviceFlyingType; @Autowired private ServiceChat serviceChat; @Autowired private ServiceUsers serviceUsers; @Autowired private ServiceDistributor serviceDistributor; @Autowired private ServiceDispatcher serviceDispatcher; @Autowired private ServiceDispatcherName serviceDispatcherName; @Autowired private ServiceSetting serviceSetting; @Autowired private ServiceWeather serviceWeather; @Autowired private ServiceStatistics serviceStatistics; @JsonIgnore @RequestMapping("/service/setting") public void serviceSetting(HttpServletRequest request, HttpServletResponse response) { User user = new User(); HttpSession httpSession = request.getSession(); user.setId(httpSession.getId()); if (!serviceUsers.checkUserOnline(user)) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); if (LOG.isInfoEnabled()) { LOG.info("the user isn't authorized. Session id : " + httpSession.getId() + ". URL : service/setting"); } return; } SettingFrontEnd settingFrontEnd = new SettingFrontEnd(); settingFrontEnd.setCheckSettingMusicAirplane(Integer.valueOf(request.getParameter("musicAirplane"))); settingFrontEnd.setCheckSettingMusicChat(Integer.valueOf(request.getParameter("musicChat"))); settingFrontEnd.setCheckSettingMusicService(Integer.valueOf(request.getParameter("musicService"))); settingFrontEnd.setCheckSettingShowClock(Integer.valueOf(request.getParameter("showClock"))); settingFrontEnd .setCheckSettingShowDispatcherOnline(Integer.valueOf(request.getParameter("showDispatcher"))); settingFrontEnd.setCheckSettingShowNewMessage(Integer.valueOf(request.getParameter("showNewMessage"))); settingFrontEnd.setCheckSettingShowProfile(Integer.valueOf(request.getParameter("showProfile"))); settingFrontEnd.setCheckSettingShowWeather(Integer.valueOf(request.getParameter("showWeather"))); serviceSetting.setSettingFrontEnd(user, settingFrontEnd); if (LOG.isInfoEnabled()) { LOG.info("user set setting. Session id : " + httpSession.getId() + ". User : " + user + ". URL : service/setting"); } } @RequestMapping(value = "/service/dispatcher/typeMachine", produces = "application/json") public FlyingMachineType serviceDispatcherTypeMachine(@RequestParam(name = "typeMachine") String typeMachine, HttpServletRequest request, HttpServletResponse response) { HttpSession httpSession = request.getSession(); User user = (User) httpSession.getAttribute("user"); if (!serviceUsers.checkUserOnline(user)) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); if (LOG.isInfoEnabled()) { LOG.info("the user isn't authorized. Session id : " + httpSession.getId() + ". URL : /service/dispatcher/typeMachine"); } return null; } if (LOG.isInfoEnabled()) { LOG.info("user get dispatcher type machine. Session id : " + httpSession.getId() + ". User : " + user + ". URL : /service/dispatcher/typeMachine"); } return serviceFlyingType.getFlyingMachineType(typeMachine); } @RequestMapping(value = "/service/weather", produces = "application/json") public GetWeather serviceWeather(HttpServletRequest request, HttpServletResponse response) { HttpSession httpSession = request.getSession(); User user = (User) httpSession.getAttribute("user"); if (!serviceUsers.checkUserOnline(user)) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); if (LOG.isInfoEnabled()) { LOG.info("the user isn't authorized. Session id : " + httpSession.getId() + ". URL : /service/weather"); } return null; } if (LOG.isInfoEnabled()) { LOG.info("user get weather. Session id : " + httpSession.getId() + ". User : " + user + ". URL : /service/weather"); } return serviceWeather.getWeather(); } @RequestMapping(value = "/service/chat/get", produces = "application/json") public List<Message> serviceChatGet(HttpServletRequest request, HttpServletResponse response) { HttpSession httpSession = request.getSession(); User user = (User) httpSession.getAttribute("user"); if (serviceUsers.checkUserOnline(user)) { int numberMessage = Integer.parseInt(request.getParameter("numberMessage")); if (LOG.isInfoEnabled()) { LOG.info("user get messages. Session id : " + httpSession.getId() + ". User : " + user + ". URL : /service/chat/get"); } return serviceChat.getMessageMiss(numberMessage); } else { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); if (LOG.isInfoEnabled()) { LOG.info("the user isn't authorized. Session id : " + httpSession.getId() + ". URL : /service/chat/get"); } } return new ArrayList<>(); } @RequestMapping(value = "/service/chat/add", produces = "application/json") public void serviceChatAdd(HttpServletRequest request, HttpServletResponse response) { HttpSession httpSession = request.getSession(); User user = (User) httpSession.getAttribute("user"); if (serviceUsers.checkUserOnline(user)) { String text = request.getParameter("charText"); serviceChat.addMessage(user, text); if (LOG.isInfoEnabled()) { LOG.info("user add message. Session id : " + httpSession.getId() + ". User : " + user + ". URL : /service/chat/add"); } } else { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); if (LOG.isInfoEnabled()) { LOG.info("the user isn't authorized. Session id : " + httpSession.getId() + ". URL : /service/chat/add"); } } } @RequestMapping(value = "/service/dispatcher/getflights", produces = "application/json") public List<NodeDisctributor> serviceDispatcherGetFlights(HttpServletRequest request, HttpServletResponse response) { HttpSession httpSession = request.getSession(); User user = (User) httpSession.getAttribute("user"); if (serviceUsers.checkUserOnline(user)) { List<NodeDisctributor> list = serviceDistributor.getActualInformation(user); if (LOG.isInfoEnabled()) { LOG.info("user get flight. Session id : " + httpSession.getId() + ". User : " + user + ". URL : /service/dispatcher/getflights"); } return list; } else { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); if (LOG.isInfoEnabled()) { LOG.info("the user isn't authorized. Session id : " + httpSession.getId() + ". URL : /service/dispatcher/getflights"); } } return new ArrayList<>(); } @JsonIgnore @RequestMapping(value = "/service/dispatcher/delflight") public boolean serviceDispatcherDelFlights(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = "runawayId") int runawayId) { HttpSession httpSession = request.getSession(); User user = (User) httpSession.getAttribute("user"); if (serviceUsers.checkUserOnline(user)) { String numberFlight = request.getParameter("numberFlight"); Flight flight = new Flight(); flight.setFlightNumber(numberFlight); if (serviceDispatcher.getFlightState(numberFlight).equals("")) { serviceStatistics.incAmountTakenOffPlane(user); } else { serviceStatistics.incAmoubtLendedPlane(user); } boolean result = serviceDispatcher.delFlight(flight, runawayId); if (LOG.isInfoEnabled()) { LOG.info("user del flight. Session id : " + httpSession.getId() + ". User : " + user + ". URL : /service/dispatcher/delflight"); } return result; } else { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); if (LOG.isInfoEnabled()) { LOG.info("the user isn't authorized. Session id : " + httpSession.getId() + ". URL : /service/dispatcher/delflight"); } return false; } } @RequestMapping(value = "/service/dispatcher") public Dispatcher serviceDispatcher(@RequestParam(name = "login") String login, HttpServletRequest request, HttpServletResponse response) { HttpSession httpSession = request.getSession(); User user = (User) httpSession.getAttribute("user"); if (!serviceUsers.checkUserOnline(user)) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); if (LOG.isInfoEnabled()) { LOG.info("the user isn't authorized. Session id : " + httpSession.getId() + ". URL : /service/dispatcher/typeMachine"); } return null; } if (LOG.isInfoEnabled()) { LOG.info("user get dispacther. Session id : " + httpSession.getId() + ". User : " + user + ". URL : /service/dispatcher/typeMachine"); } return serviceDispatcherName.getDispatcher(user); } @RequestMapping(value = "/service/statistics/common", produces = "application/json") public Statistics serviceStatisticsCommon(HttpServletRequest request, HttpServletResponse response) { User user = new User(); HttpSession httpSession = request.getSession(); user.setId(httpSession.getId()); if (!serviceUsers.checkUserOnline(user)) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); if (LOG.isInfoEnabled()) { LOG.info("the user isn't authorized. Session id : " + httpSession.getId() + ". URL : /service/statistics/common"); } return null; } if (LOG.isInfoEnabled()) { LOG.info("user get common statistics. Session id : " + httpSession.getId() + ". User : " + user + ". URL : /service/statistics/common"); } return serviceStatistics.getStatisticsAll(); } @RequestMapping(value = "/service/statistics/private", produces = "application/json") public Statistics serviceStatisticsPrivate(HttpServletRequest request, HttpServletResponse response) { User user = new User(); HttpSession httpSession = request.getSession(); user.setId(httpSession.getId()); if (!serviceUsers.checkUserOnline(user)) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); if (LOG.isInfoEnabled()) { LOG.info("the user isn't authorized. Session id : " + httpSession.getId() + ". URL : /service/statistics/private"); } return null; } if (LOG.isInfoEnabled()) { LOG.info("user get private statistics. Session id : " + httpSession.getId() + ". User : " + user + ". URL : /service/statistics/private"); } return serviceStatistics.getStatisticsUser(user); } @RequestMapping(value = "/service/runaways", produces = "application/json") public List<Runaway> getRuaways(HttpServletRequest request, HttpServletResponse response) { User user = new User(); HttpSession httpSession = request.getSession(); user.setId(httpSession.getId()); if (!serviceUsers.checkUserOnline(user)) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); if (LOG.isInfoEnabled()) { LOG.info("the user isn't authorized. Session id : " + httpSession.getId() + ". URL : /service/statistics/private"); } return null; } if (LOG.isInfoEnabled()) { LOG.info("user get private statistics. Session id : " + httpSession.getId() + ". User : " + user + ". URL : /service/statistics/private"); } return serviceDispatcher.getAllRunaways(); } }