-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #947 from SCADA-LTS/feature_nd/-#921_Improving_the…
…_component_is_Alive #921 correcting the isAlive component now uses the time on the server
- Loading branch information
Showing
2 changed files
with
119 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/org/scada_lts/web/mvc/api/components/is_alive/TimeFromServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.scada_lts.web.mvc.api.components.is_alive; | ||
|
||
import com.serotonin.mango.Common; | ||
import com.serotonin.mango.vo.User; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.time.Instant; | ||
|
||
|
||
/** | ||
* @autor [email protected] on 16.07.19 | ||
*/ | ||
@Controller | ||
public class TimeFromServer { | ||
|
||
private static final Log LOG = LogFactory.getLog(TimeFromServer.class); | ||
|
||
@RequestMapping(value = "/api/is_alive/time", method = RequestMethod.GET) | ||
public ResponseEntity<Long> time(HttpServletRequest request) { | ||
LOG.info("/api/is_alive/time"); | ||
|
||
try { | ||
User user = Common.getUser(request); | ||
|
||
if (user != null) { | ||
long unixTimestamp = Instant.now().getEpochSecond(); | ||
return new ResponseEntity<>(unixTimestamp, HttpStatus.OK); | ||
} | ||
|
||
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); | ||
|
||
} catch (Exception e) { | ||
if (LOG.isTraceEnabled()) { | ||
LOG.trace(e); | ||
} | ||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); | ||
} | ||
} | ||
} |