Skip to content

Commit

Permalink
pr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fiqare-emergya-dev committed Jan 16, 2020
1 parent 4d08aff commit 05982ba
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 64 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ jdk:
- openjdk11
branches:
only:
- master
- /.*/

install:
- mvn test -B

before_install:
- mvn clean
- mvn clean
2 changes: 1 addition & 1 deletion perseo-main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<groupId>org.owasp.encoder</groupId>
<artifactId>encoder</artifactId>
<version>1.2.2</version>
</dependency>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
logger.debug(String.format("event was sent: %s", eventMap));
} catch (Exception je) {
try {
logger.error(String.format("error: %s" ,je));
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getOutputStream().print(String.format("{\"error\":\"%s\"}%n", Encode.forHtmlContent(je.getMessage())));
}catch(IOException exception) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
logger.error(String.format("error: %s" ,je));
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getOutputStream().print(String.format("{\"error\":\"%s\"}%n", Encode.forHtmlContent(je.getMessage())));
} catch (IOException exception) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class LogLevelServlet extends HttpServlet {
protected void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String levelName = request.getParameter("level");
String levelName = request.getParameter("level");
logger.info(String.format("changing log level to %s",levelName));
Level level = levels.get(levelName);
if (level == null) {
Expand All @@ -82,7 +82,7 @@ protected void doPut(HttpServletRequest request, HttpServletResponse response)
LogManager.getRootLogger().setLevel(level);
}
response.setStatus(HttpServletResponse.SC_OK);
}catch(IOException e) {
} catch (IOException e) {
logger.error("IOException in log level");
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
Expand All @@ -109,10 +109,10 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_OK);
response.getOutputStream().print(String.format("{\"level\":\"%s\"}",Encode.forHtmlContent(currentLevel)));
}catch(IOException e) {
logger.error("IOException in log level");
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
} catch (IOException e) {
logger.error("IOException in log level");
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
ruleName = ruleName == null ? "/" : ruleName;
Result r = RulesManager.get(epService, ruleName.substring(1));
response.setStatus(r.getStatusCode());
response.getOutputStream().print(Encode.forHtmlContent(r.getMessage()));

}catch(Exception je) {
logger.error(String.format("error: %s" ,je));
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getOutputStream().print(Encode.forHtmlContent(r.getMessage()));
} catch (Exception je) {
logger.error(String.format("error: %s" ,je));
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}

}
Expand All @@ -115,18 +114,17 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
response.setContentType("application/json;charset=UTF-8");

try {

String body = Utils.getBodyAsString(request);
// Save temporary rules, not triggered by events external to the core
TimeRulesStore.getInstance().saveTimeRules(body);
Result r = RulesManager.make(epService, body);
response.setStatus(r.getStatusCode());
response.getOutputStream().print(Encode.forHtmlContent(r.getMessage()));
}catch(Exception je) {
logger.error(String.format("error: %s" ,je));
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);

// Save temporary rules, not triggered by events external to the core
TimeRulesStore.getInstance().saveTimeRules(body);

Result r = RulesManager.make(epService, body);
response.setStatus(r.getStatusCode());
response.getOutputStream().print(Encode.forHtmlContent(r.getMessage()));
} catch (Exception je) {
logger.error(String.format("error: %s" ,je));
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}

}
Expand Down Expand Up @@ -155,7 +153,7 @@ protected void doPut(HttpServletRequest request, HttpServletResponse response)
Result r = RulesManager.updateAll(epService, body);
response.setStatus(r.getStatusCode());
response.getOutputStream().print(Encode.forHtmlContent(r.getMessage()));
}catch(Exception je) {
} catch (Exception je) {
logger.error(String.format("error: %s" ,je));
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
Expand Down Expand Up @@ -197,7 +195,7 @@ protected void doDelete(HttpServletRequest request, HttpServletResponse response

response.setStatus(r.getStatusCode());
response.getOutputStream().print(r.getMessage());
}catch(Exception je) {
} catch (Exception je) {
logger.error(String.format("error: %s" ,je));
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
*
* This file is part of perseo-core project.
*
* perseo-core is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License version 2 as published by the Free Software Foundation.
* perseo-core is free software: you can redistribute it and/or
* modify it under the terms of the GNU
* General Public License version 2 as published by the Free Software
* Foundation.
*
* perseo-core 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 GNU General Public License
* perseo-core 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 GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with perseo-core. If not, see
* You should have received a copy of the GNU General Public License
* along with perseo-core. If not, see
* http://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU General Public License please contact with
* For those usages not covered by the GNU General Public License
* please contact with
* iot_support at tid dot es
*/

Expand All @@ -22,12 +28,9 @@
import ca.rmen.sunrisesunset.SunriseSunset;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -40,19 +43,23 @@ public class DateTimeUtils {
* Gets next sunrise.
* Uses car.men.sunrisesunset library
* It calculates next sunrise considering provided date and time
* If date and time provided is older than sunrise it adds +1 day to date and calculates again
* If date and time provided is older than sunrise it adds +1 day
* to date and calculates again
*
* @param day the day Calendar object on ISO format
* @param latitude the latitude
* @param longitude the longitude
* @return the next sunrise
*/
public static Calendar getNextSunrise(Calendar day, double latitude, double longitude) {
Calendar[] sunriseSunset = SunriseSunset.getSunriseSunset(day, latitude, longitude);
public static Calendar getNextSunrise(Calendar day,
double latitude, double longitude) {
Calendar[] sunriseSunset = SunriseSunset.getSunriseSunset(day,
latitude, longitude);
Calendar day2 = (Calendar) day.clone();
if (sunriseSunset[0].compareTo(day) < 0) {
day2.add(Calendar.DATE, 1);
sunriseSunset = SunriseSunset.getSunriseSunset(day2, latitude, longitude);
sunriseSunset = SunriseSunset.getSunriseSunset(day2,
latitude, longitude);
}
return sunriseSunset[0];
}
Expand All @@ -61,89 +68,103 @@ public static Calendar getNextSunrise(Calendar day, double latitude, double long
* Gets next sunset.
* Uses car.men.sunrisesunset library
* It calculates next sunset considering provided date and time
* If date and time provided is older than sunset it adds +1 day to date and calculates again
* If date and time provided is older than sunset it adds +1 day
* to date and calculates again
*
* @param day the day Calendar object on ISO format
* @param latitude the latitude
* @param longitude the longitude
* @return the next sunset
*/
public static Calendar getNextSunset(Calendar day, double latitude, double longitude) {
Calendar[] sunriseSunset = SunriseSunset.getSunriseSunset(day, latitude, longitude);
public static Calendar getNextSunset(Calendar day,
double latitude, double longitude) {
Calendar[] sunriseSunset = SunriseSunset.getSunriseSunset(day,
latitude, longitude);
Calendar day2 = (Calendar) day.clone();
if (sunriseSunset[1].compareTo(day) < 0) {
day2.add(Calendar.DATE, 1);
sunriseSunset = SunriseSunset.getSunriseSunset(day2, latitude, longitude);
sunriseSunset = SunriseSunset.getSunriseSunset(day2,
latitude, longitude);
}
return sunriseSunset[1];
}

/**
* Gets milis to next sunrise.
* It calculates next sunrise considering provided date and time
* If date and time provided is older than sunrise it adds +1 day to date and calculates again
* If date and time provided is older than sunrise it adds +1 day
* to date and calculates again
* Finally, tunrns result into miliseconds
*
* @param day the day Calendar object on ISO format
* @param latitude the latitude
* @param longitude the longitude
* @return the milis to next sunrise
*/
public static long getMilisToNextSunrise(Calendar day, double latitude, double longitude) {
public static long getMilisToNextSunrise(Calendar day,
double latitude, double longitude) {
Calendar sunriseSunrise = getNextSunrise(day, latitude, longitude);
return (sunriseSunrise.getTimeInMillis() - day.getTimeInMillis());
}

/**
* Gets seconds to next sunset.
* It calculates next sunset considering provided date and time
* If date and time provided is older than sunset it adds +1 day to date and calculates again
* If date and time provided is older than sunset it adds +1 day
* to date and calculates again
* Finally, tunrns result into seconds
*
* @param day the day Calendar object on ISO format
* @param latitude the latitude
* @param longitude the longitude
* @return the seconds to next sunset
*/
public static long getSecondsToNextSunset(Calendar day, double latitude, double longitude) {
public static long getSecondsToNextSunset(Calendar day,
double latitude, double longitude) {
long sunriseSunset = getMilisToNextSunset(day, latitude, longitude);
long seconds = TimeUnit.MILLISECONDS.toSeconds(sunriseSunset);
if ((seconds*1000) < sunriseSunset)
if ((seconds * 1000) < sunriseSunset) {
seconds += 1;
}
return seconds;
}

/**
* Gets seconds to next sunrise.
* It calculates next sunrise considering provided date and time
* If date and time provided is older than sunrise it adds +1 day to date and calculates again
* If date and time provided is older than sunrise it adds +1 day
* to date and calculates again
* Finally, tunrns result into seconds
*
* @param day the day Calendar object on ISO format
* @param latitude the latitude
* @param longitude the longitude
* @return the seconds to next sunrise
*/
public static long getSecondsToNextSunrise(Calendar day, double latitude, double longitude) {
public static long getSecondsToNextSunrise(Calendar day,
double latitude, double longitude) {
long sunriseSunset = getMilisToNextSunrise(day, latitude, longitude);
long seconds = TimeUnit.MILLISECONDS.toSeconds(sunriseSunset);
if ((seconds*1000) < sunriseSunset)
if ((seconds * 1000) < sunriseSunset) {
seconds += 1;
}
return seconds;
}

/**
* Gets milis to next sunset.
* It calculates next sunset considering provided date and time
* If date and time provided is older than sunset it adds +1 day to date and calculates again
* If date and time provided is older than sunset it adds +1 day
* to date and calculates again
* Finally, tunrns result into miliseconds
*
* @param day the day Calendar object on ISO format
* @param latitude the latitude
* @param longitude the longitude
* @return the milis to next sunset
*/
public static long getMilisToNextSunset(Calendar day, double latitude, double longitude) {
public static long getMilisToNextSunset(Calendar day,
double latitude, double longitude) {
Calendar sunriseSunset = getNextSunset(day, latitude, longitude);
return (sunriseSunset.getTimeInMillis() - day.getTimeInMillis());
}
Expand All @@ -159,7 +180,8 @@ public static long getMilisToNextSunset(Calendar day, double latitude, double lo

public static Calendar dateToUTC(String day) {
ZonedDateTime zonedDateTime = ZonedDateTime.parse(day);
ZonedDateTime zonedDateTimeUTC = zonedDateTime.withZoneSameInstant(ZoneId.of("UTC"));
ZonedDateTime zonedDateTimeUTC = zonedDateTime.withZoneSameInstant(
ZoneId.of("UTC"));
return (Calendar) GregorianCalendar.from(zonedDateTimeUTC);
}

Expand All @@ -173,10 +195,11 @@ public static Calendar dateToUTC(String day) {
* @return hours on UTC format
*/

public static int timeToUTC(int time, String timeZone) {
LocalDateTime localDateTime = LocalDateTime.of(LocalDateTime.now().getYear(),
public static int timeToUTC(int time, final String timeZone) {
LocalDateTime localDateTime = LocalDateTime.of(LocalDateTime.now()
.getYear(),
LocalDateTime.now().getMonth(),
LocalDateTime.now().getDayOfMonth()+1,
LocalDateTime.now().getDayOfMonth() + 1,
time,
LocalDateTime.now().getMinute());
ZonedDateTime isoDateZLDT = localDateTime.atZone(ZoneId.of(timeZone));
Expand Down

0 comments on commit 05982ba

Please sign in to comment.