diff --git a/src/main/java/no/nibio/vips/logic/VIPSLogicApplication.java b/src/main/java/no/nibio/vips/logic/VIPSLogicApplication.java index a7b6950b2bab922a2e4f79d393ff5b4548dee8e8..eda65c3c0169fcdcfaef802149505669f9c672f5 100644 --- a/src/main/java/no/nibio/vips/logic/VIPSLogicApplication.java +++ b/src/main/java/no/nibio/vips/logic/VIPSLogicApplication.java @@ -53,6 +53,7 @@ public class VIPSLogicApplication extends Application resources.add(no.nibio.vips.logic.messaging.sms.SMSHandlingService.class); resources.add(no.nibio.vips.logic.modules.applefruitmoth.AppleFruitMothService.class); resources.add(no.nibio.vips.logic.service.ObservationService.class); + resources.add(no.nibio.vips.logic.service.WeatherProxyService.class); //resources.add(no.nibio.vips.logic.service.JacksonConfig.class); //resources.add(no.nibio.vips.coremanager.service.ManagerResourceImpl.class); } @@ -71,6 +72,7 @@ public class VIPSLogicApplication extends Application resources.add(no.nibio.vips.logic.service.LogicService.class); resources.add(no.nibio.vips.logic.service.ObservationService.class); resources.add(no.nibio.vips.logic.service.VIPSMobileService.class); + resources.add(no.nibio.vips.logic.service.WeatherProxyService.class); resources.add(no.nibio.vips.observationdata.ObservationDataService.class); } } \ No newline at end of file diff --git a/src/main/java/no/nibio/vips/logic/service/LogicService.java b/src/main/java/no/nibio/vips/logic/service/LogicService.java index ec73f6a00d55ca49b2af549c6d91c421111eb881..522d7d18b1b34de0a7e836ed426486712c623a8f 100644 --- a/src/main/java/no/nibio/vips/logic/service/LogicService.java +++ b/src/main/java/no/nibio/vips/logic/service/LogicService.java @@ -555,103 +555,6 @@ public class LogicService { } - @GET - @POST - @Path("weather/proxy/metos/{stationId}") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getMetosWeatherData( - @PathParam("stationId") String stationId, - @FormParam("timeZone") String timeZonePOST, - @QueryParam("timeZone") String timeZoneGET, - @FormParam("startDate") String startDatePOST, - @QueryParam("startDate") String startDateGET - ) - { - List<WeatherObservation> observations; - try - { - String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; - TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - format.setTimeZone(timeZone); - String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; - Date startDate1 = format.parse(startDateParam); - observations = new MetosDataParser().getWeatherObservations(stationId, timeZone, startDate1); - } catch (ParseException | ParseWeatherDataException | NullPointerException ex) { - return Response.serverError().entity(ex).build(); - } - return Response.ok().entity(observations).build(); - } - - @GET - @POST - @Path("weather/proxy/fruitwebdavis/{stationId}") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getFruitWebDavisWeatherData( - @PathParam("stationId") String stationId, - @FormParam("timeZone") String timeZonePOST, - @QueryParam("timeZone") String timeZoneGET, - @FormParam("startDate") String startDatePOST, - @QueryParam("startDate") String startDateGET - ) - { - List<WeatherObservation> observations; - try - { - String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; - TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - format.setTimeZone(timeZone); - String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; - Date startDate1 = format.parse(startDateParam); - observations = new FruitWebDavisDataParser().getWeatherObservations(URLDecoder.decode(stationId, "UTF-8"), timeZone, startDate1); - } catch (ParseException | ParseWeatherDataException | NullPointerException | UnsupportedEncodingException ex) { - return Response.serverError().entity(ex).build(); - } - return Response.ok().entity(observations).build(); - } - - - @GET - @POST - @Path("weather/proxy/alab/{stationId}") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getALabWeatherData( - @PathParam("stationId") String stationId, - @FormParam("timeZone") String timeZonePOST, - @QueryParam("timeZone") String timeZoneGET, - @FormParam("startDate") String startDatePOST, - @QueryParam("startDate") String startDateGET, - @FormParam("userName") String userNamePOST, - @QueryParam("userName") String userNameGET, - @FormParam("password") String passwordPOST, - @QueryParam("password") String passwordGET - ) - { - List<WeatherObservation> observations; - try - { - String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; - TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - format.setTimeZone(timeZone); - String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; - Date startDate1 = format.parse(startDateParam); - String userName = userNamePOST != null ? userNamePOST:userNameGET; - String password = passwordPOST != null ? passwordPOST:passwordGET; - observations = new ALabDataParser().getWeatherObservations(stationId, timeZone, startDate1, userName, password); - } catch (ParseException | ParseWeatherDataException | NullPointerException ex) { - return Response.serverError().entity(ex).build(); - } - return Response.ok().entity(observations).build(); - } - - - - /** * Service available locally for cron jobs. Most useful on test servers * @return diff --git a/src/main/java/no/nibio/vips/logic/service/WeatherProxyService.java b/src/main/java/no/nibio/vips/logic/service/WeatherProxyService.java new file mode 100644 index 0000000000000000000000000000000000000000..0cdd6d80459874c5af87c9f70cea68ff9de41072 --- /dev/null +++ b/src/main/java/no/nibio/vips/logic/service/WeatherProxyService.java @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2016 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSLogic. + * VIPSLogic is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSLogic 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 + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.logic.service; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; +import java.util.TimeZone; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Response; +import no.nibio.vips.entity.WeatherObservation; +import no.nibio.vips.util.weather.ALabDataParser; +import no.nibio.vips.util.weather.FruitWebDavisDataParser; +import no.nibio.vips.util.weather.MetosDataParser; +import no.nibio.vips.util.weather.ParseWeatherDataException; +import org.jboss.resteasy.annotations.GZIP; + +/** + * @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +@Path("rest/weather/proxy") +public class WeatherProxyService { + @GET + @POST + @Path("metos/{stationId}") + @GZIP + @Produces("application/json;charset=UTF-8") + public Response getMetosWeatherData( + @PathParam("stationId") String stationId, + @FormParam("timeZone") String timeZonePOST, + @QueryParam("timeZone") String timeZoneGET, + @FormParam("startDate") String startDatePOST, + @QueryParam("startDate") String startDateGET + ) + { + List<WeatherObservation> observations; + try + { + String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; + TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + format.setTimeZone(timeZone); + String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; + Date startDate1 = format.parse(startDateParam); + observations = new MetosDataParser().getWeatherObservations(stationId, timeZone, startDate1); + } catch (ParseException | ParseWeatherDataException | NullPointerException ex) { + return Response.serverError().entity(ex).build(); + } + return Response.ok().entity(observations).build(); + } + + @GET + @POST + @Path("fruitwebdavis/{stationId}") + @GZIP + @Produces("application/json;charset=UTF-8") + public Response getFruitWebDavisWeatherData( + @PathParam("stationId") String stationId, + @FormParam("timeZone") String timeZonePOST, + @QueryParam("timeZone") String timeZoneGET, + @FormParam("startDate") String startDatePOST, + @QueryParam("startDate") String startDateGET + ) + { + List<WeatherObservation> observations; + try + { + String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; + TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + format.setTimeZone(timeZone); + String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; + Date startDate1 = format.parse(startDateParam); + observations = new FruitWebDavisDataParser().getWeatherObservations(URLDecoder.decode(stationId, "UTF-8"), timeZone, startDate1); + } catch (ParseException | ParseWeatherDataException | NullPointerException | UnsupportedEncodingException ex) { + return Response.serverError().entity(ex).build(); + } + return Response.ok().entity(observations).build(); + } + + + @GET + @POST + @Path("alab/{stationId}") + @GZIP + @Produces("application/json;charset=UTF-8") + public Response getALabWeatherData( + @PathParam("stationId") String stationId, + @FormParam("timeZone") String timeZonePOST, + @QueryParam("timeZone") String timeZoneGET, + @FormParam("startDate") String startDatePOST, + @QueryParam("startDate") String startDateGET, + @FormParam("userName") String userNamePOST, + @QueryParam("userName") String userNameGET, + @FormParam("password") String passwordPOST, + @QueryParam("password") String passwordGET + ) + { + List<WeatherObservation> observations; + try + { + String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; + TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + format.setTimeZone(timeZone); + String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; + Date startDate1 = format.parse(startDateParam); + String userName = userNamePOST != null ? userNamePOST:userNameGET; + String password = passwordPOST != null ? passwordPOST:passwordGET; + observations = new ALabDataParser().getWeatherObservations(stationId, timeZone, startDate1, userName, password); + } catch (ParseException | ParseWeatherDataException | NullPointerException ex) { + return Response.serverError().entity(ex).build(); + } + return Response.ok().entity(observations).build(); + } + +}