forked from i-m-purecloud/API-Watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alakh Raghuvanshi
committed
Jul 18, 2019
1 parent
a98d7d9
commit 04238c9
Showing
8 changed files
with
307 additions
and
2 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
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
53 changes: 53 additions & 0 deletions
53
src/main/java/com/api/monitor/API/Monitor/UrlRunnable.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,53 @@ | ||
package com.api.monitor.API.Monitor; | ||
|
||
import com.api.monitor.API.Monitor.models.Url; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.net.*; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
public class UrlRunnable implements Runnable { | ||
|
||
private Url url; | ||
private Logger logger = LoggerFactory.getLogger(UrlRunnable.class); | ||
|
||
public UrlRunnable(Url url) { | ||
this.url = url; | ||
} | ||
|
||
private void setHeaders(HttpURLConnection con) throws IOException { | ||
con.setRequestProperty("Content-Type", "application/json"); | ||
con.setRequestProperty("Accept", "*/*"); | ||
con.setRequestProperty("Cache-Control", "no-cache"); | ||
con.setRequestProperty("Connection", "keep-alive"); | ||
con.setDoOutput(true); | ||
DataOutputStream out = new DataOutputStream(con.getOutputStream()); | ||
out.flush(); | ||
out.close(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
URL url = new URL(this.url.getUrlAddress()); | ||
HttpURLConnection con = (HttpURLConnection) url.openConnection(); | ||
con.setRequestMethod(this.url.getRequestMethod()); | ||
setHeaders(con); | ||
this.url.setStatus(con.getResponseCode()); | ||
} catch (ConnectException ex) { | ||
this.url.setStatus(503); | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException(e); | ||
} catch (ProtocolException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
this.url.setTimestamp(new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date())); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/api/monitor/API/Monitor/controllers/ApiController.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,39 @@ | ||
package com.api.monitor.API.Monitor.controllers; | ||
|
||
import com.api.monitor.API.Monitor.models.RequestUrl; | ||
import com.api.monitor.API.Monitor.models.ResponseUrl; | ||
import com.api.monitor.API.Monitor.services.UrlMonitoringService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.servlet.ServletException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
@RestController | ||
public class ApiController { | ||
private Logger logger = LoggerFactory.getLogger(ApiController.class); | ||
private UrlMonitoringService urlMonitoringService; | ||
|
||
@Autowired | ||
public ApiController(UrlMonitoringService urlMonitoringService) { | ||
this.urlMonitoringService = urlMonitoringService; | ||
} | ||
|
||
@RequestMapping(value = "/addUrl", method = RequestMethod.GET) | ||
public ResponseUrl addUrlToWatch(@RequestBody RequestUrl requestUrl) throws ServletException { | ||
|
||
if(requestUrl.getUrl() == null || requestUrl.getRequestMethod() == null || requestUrl.getName() == null || requestUrl.getTag() == null) { | ||
throw new ServletException("Need all the parameters"); | ||
} | ||
logger.info("New Api added {}, name {}",requestUrl.getUrl(), requestUrl.getName()); | ||
|
||
return new ResponseUrl("Url "+requestUrl.getUrl()+" is being monitored",new ResponseUrl.Details(new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()),"200")); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/api/monitor/API/Monitor/models/RequestUrl.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,59 @@ | ||
package com.api.monitor.API.Monitor.models; | ||
|
||
public class RequestUrl { | ||
private String url; | ||
private String description; | ||
private String requestMethod; | ||
private Integer portNumber; | ||
private String name; | ||
private String tag; | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getTag() { | ||
return tag; | ||
} | ||
|
||
public void setTag(String tag) { | ||
this.tag = tag; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
|
||
public String getRequestMethod() { | ||
return requestMethod; | ||
} | ||
|
||
public void setRequestMethod(String requestMethod) { | ||
this.requestMethod = requestMethod; | ||
} | ||
|
||
public Integer getPortNumber() { | ||
return portNumber; | ||
} | ||
|
||
public void setPortNumber(Integer portNumber) { | ||
this.portNumber = portNumber; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/com/api/monitor/API/Monitor/models/ResponseUrl.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,63 @@ | ||
package com.api.monitor.API.Monitor.models; | ||
|
||
public class ResponseUrl { | ||
private String message; | ||
private Details details; | ||
|
||
public ResponseUrl(String message, Details details) { | ||
this.message = message; | ||
this.details = details; | ||
} | ||
|
||
public ResponseUrl() { | ||
} | ||
|
||
public Details getDetails() { | ||
return details; | ||
} | ||
|
||
public void setDetails(Details details) { | ||
this.details = details; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("Test [message=%s]", message); | ||
} | ||
|
||
public static class Details { | ||
private String date; | ||
private String statusCode; | ||
|
||
public String getStatusCode() { | ||
return statusCode; | ||
} | ||
|
||
public void setStatusCode(String statusCode) { | ||
this.statusCode = statusCode; | ||
} | ||
|
||
public Details(String date, String statusCode){ | ||
this.date = date; | ||
this.statusCode = statusCode; | ||
} | ||
|
||
public String getDate() { | ||
return date; | ||
} | ||
|
||
public void setDate(String date) { | ||
this.date = date; | ||
} | ||
} | ||
|
||
|
||
} |
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 com.api.monitor.API.Monitor.models; | ||
|
||
public class Url { | ||
|
||
private String urlAddress; | ||
private Integer status; | ||
private String requestMethod; | ||
private String timestamp; | ||
|
||
public Url(String urlAddress, String requestMethod) { | ||
this.urlAddress = urlAddress; | ||
this.requestMethod = requestMethod; | ||
} | ||
|
||
public String getUrlAddress() { | ||
return urlAddress; | ||
} | ||
|
||
public void setUrlAddress(String urlAddress) { | ||
this.urlAddress = urlAddress; | ||
} | ||
|
||
public Integer getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(Integer status) { | ||
this.status = status; | ||
} | ||
|
||
public String getRequestMethod() { | ||
return requestMethod; | ||
} | ||
|
||
public void setRequestMethod(String requestMethod) { | ||
this.requestMethod = requestMethod; | ||
} | ||
|
||
public String getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public void setTimestamp(String timestamp) { | ||
this.timestamp = timestamp; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/api/monitor/API/Monitor/services/UrlMonitoringService.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,39 @@ | ||
package com.api.monitor.API.Monitor.services; | ||
|
||
import com.api.monitor.API.Monitor.UrlRunnable; | ||
import com.api.monitor.API.Monitor.models.Url; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Service | ||
public class UrlMonitoringService { | ||
|
||
private List<Url> currentMonitoringUrl = new ArrayList<>(); | ||
private List<Url> readyToMonitorUrl = new ArrayList<>(); | ||
private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10); | ||
private Logger logger = LoggerFactory.getLogger(UrlMonitoringService.class); | ||
|
||
//runs threads for every 2 seconds | ||
public void runReadyToMonitorUrlQueue() { | ||
|
||
for (int i = 0; i < readyToMonitorUrl.size(); i++) { | ||
scheduledExecutorService.scheduleAtFixedRate | ||
(new UrlRunnable(readyToMonitorUrl.get(i)), 0, 2, TimeUnit.SECONDS); | ||
logger.info("run {}",readyToMonitorUrl.get(i).getStatus()); | ||
} | ||
currentMonitoringUrl.addAll(readyToMonitorUrl); | ||
readyToMonitorUrl.clear(); | ||
} | ||
|
||
public void addUrlToMonitor(Url url) { | ||
readyToMonitorUrl.add(url); | ||
} | ||
|
||
} |