forked from jvermillard/m3da-server
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Servlet to get the list of clients. Fixes #11
- Loading branch information
Showing
13 changed files
with
539 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,12 +29,12 @@ You can start connecting your M3DA client on the TCP port 44900 (IANA official p | |
REST READ API | ||
-------- | ||
|
||
You can see all the received data for a given client by GETing the URL : http://127.0.0.1:8080/data/{client identifier} | ||
You can see all the received data for a given client by GETing the URL : http://127.0.0.1:8080/clients/{client identifier}/data | ||
|
||
The client identifier is the value of "agent.config.agent.deviceId" in your mihini installation. | ||
|
||
Examples : | ||
> GET http://127.0.0.1:8080/data/01121979 | ||
> GET http://127.0.0.1:8080/clients/01121979/data | ||
RESULT : | ||
|
||
|
@@ -62,12 +62,12 @@ RESULT : | |
REST WRITE API | ||
-------- | ||
|
||
You can push data to a given client by POSTing to the following URL : http://127.0.0.1:8080/data/{client identifier} | ||
You can push data to a given client by POSTing to the following URL : http://127.0.0.1:8080/clients/{client identifier}/data | ||
|
||
The client identifier is the value of "agent.config.agent.deviceId" in your mihini installation. | ||
|
||
Examples : | ||
> POST http://127.0.0.1:8080/data/01121979 | ||
> POST http://127.0.0.1:8080/clients/01121979/data | ||
Content : | ||
|
||
|
@@ -82,3 +82,23 @@ Content : | |
}] | ||
} | ||
``` | ||
|
||
REST CLIENTS API | ||
-------- | ||
|
||
You can get the list of connect client by GETing the URL : http://127.0.0.1:8080/clients . | ||
You'll received the list of "in" clients (those that sent data) and "out" clients (those for which data is waiting to be pushed on the server.) | ||
|
||
Example: | ||
> POST http://127.0.0.1:8080/clients/01121979/data | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
phtrivier
Author
|
||
Content : | ||
```javascript | ||
{ | ||
"in"" : ["12131", "client1", "foobar"], | ||
"out" : ["12131", "other-client"] | ||
} | ||
``` | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package m3da.server.api.json; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* JSON Bean for list of connected clients. | ||
*/ | ||
public class JClients { | ||
|
||
List<String> in; | ||
List<String> out; | ||
|
||
/** | ||
* @return the ids of clients that have recently sent data. | ||
*/ | ||
public List<String> getIn() { | ||
return in; | ||
} | ||
|
||
public void setIn(List<String> in) { | ||
this.in = in; | ||
} | ||
|
||
/** | ||
* @return the ids of client for which there is still some | ||
* data to send. | ||
*/ | ||
public List<String> getOut() { | ||
return out; | ||
} | ||
|
||
public void setOut(List<String> out) { | ||
this.out = out; | ||
} | ||
|
||
|
||
|
||
} |
32 changes: 32 additions & 0 deletions
32
server/src/main/java/m3da/server/services/clients/JClientsService.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,32 @@ | ||
package m3da.server.services.clients; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import m3da.server.api.json.JClients; | ||
import m3da.server.store.StoreService; | ||
|
||
/** | ||
* Service to get JSON bean for the Clients servlet. | ||
*/ | ||
public class JClientsService { | ||
|
||
private StoreService storeService; | ||
|
||
public JClientsService(StoreService storeService) { | ||
this.storeService = storeService; | ||
} | ||
|
||
public JClients getClients() { | ||
JClients res = new JClients(); | ||
|
||
List<String> in = new ArrayList<String>(storeService.incomingClientIds()); | ||
List<String> out = new ArrayList<String>(storeService.outgoingClientIds()); | ||
|
||
res.setIn(in); | ||
res.setOut(out); | ||
|
||
return res; | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
server/src/main/java/m3da/server/services/data/JDataService.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,37 @@ | ||
package m3da.server.services.data; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import m3da.server.api.json.JSystemReadData; | ||
import m3da.server.api.json.JSystemWriteSettings; | ||
import m3da.server.api.mapping.Store2JsonDataMapper; | ||
import m3da.server.store.Envelope; | ||
import m3da.server.store.Message; | ||
import m3da.server.store.StoreService; | ||
|
||
/** | ||
* Service to get JSON Bean for data servlet. | ||
* | ||
*/ | ||
public class JDataService { | ||
|
||
private Store2JsonDataMapper store2JsonMapper; | ||
private StoreService store; | ||
|
||
public JDataService(StoreService store, Store2JsonDataMapper store2JsonMapper) { | ||
this.store = store; | ||
this.store2JsonMapper = store2JsonMapper; | ||
} | ||
|
||
public Map<String, List<JSystemReadData>> lastReceivedData(String system) { | ||
Map<Long, Envelope> data = store.lastReceivedData(system); | ||
return this.store2JsonMapper.mapReceivedData(data); | ||
} | ||
|
||
public void enqueueReceivedData(String system, JSystemWriteSettings settings) { | ||
List<Message> newData = store2JsonMapper.mapDataToSend(settings); | ||
store.enqueueDataToSend(system, newData); | ||
} | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
server/src/main/java/m3da/server/servlet/ClientsServlet.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,101 @@ | ||
package m3da.server.servlet; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Scanner; | ||
import java.util.regex.MatchResult; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import m3da.server.api.json.JClients; | ||
import m3da.server.api.json.JSystemReadData; | ||
import m3da.server.api.json.JSystemWriteSettings; | ||
import m3da.server.services.clients.JClientsService; | ||
import m3da.server.services.data.JDataService; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.codehaus.jackson.JsonParseException; | ||
import org.codehaus.jackson.map.JsonMappingException; | ||
import org.codehaus.jackson.map.ObjectMapper; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Servlet for "clients" API | ||
* | ||
* <ul> | ||
* <li>GET /clients (list of clients)</li> | ||
* <li>GET /clients/1/data (last received data)</li> | ||
* <li>POST /clients/1/data (send data)</li> | ||
* </ul> | ||
* | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class ClientsServlet extends JsonServlet { | ||
|
||
public static final String ERROR_NO_CLIENT = "No client in path"; | ||
|
||
private static final Logger LOG = LoggerFactory | ||
.getLogger(ClientsServlet.class); | ||
private JDataService dataService; | ||
private JClientsService clientsService; | ||
|
||
public ClientsServlet(JDataService dataService, | ||
JClientsService clientsService, ObjectMapper jacksonMapper) { | ||
super(jacksonMapper); | ||
this.dataService = dataService; | ||
this.clientsService = clientsService; | ||
} | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
throws ServletException, IOException { | ||
this.setResponseContentType(resp); | ||
String pathInfo = req.getPathInfo(); | ||
if (StringUtils.isBlank(pathInfo)) { | ||
JClients jClients = this.clientsService.getClients(); | ||
this.jacksonMapper.writeValue(resp.getWriter(), jClients); | ||
} else { | ||
String clientId = getClientId(pathInfo); | ||
if (clientId == null) { | ||
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, ERROR_NO_CLIENT); | ||
} else { | ||
Map<String, List<JSystemReadData>> jData = this.dataService | ||
.lastReceivedData(clientId); | ||
this.jacksonMapper.writeValue(resp.getWriter(), jData); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) | ||
throws ServletException, IOException { | ||
this.setResponseContentType(resp); | ||
|
||
String clientId = getClientId(req.getPathInfo()); | ||
if (clientId == null) { | ||
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, ERROR_NO_CLIENT); | ||
} else { | ||
JSystemWriteSettings settings = this.jacksonMapper.readValue( | ||
req.getInputStream(), JSystemWriteSettings.class); | ||
this.dataService.enqueueReceivedData(clientId, settings); | ||
} | ||
|
||
} | ||
|
||
private String getClientId(String pathInfo) { | ||
String res = null; | ||
Scanner scanner = new Scanner(pathInfo); | ||
String match = scanner.findInLine("/(\\w+)/data"); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
if (match != null) { | ||
MatchResult result = scanner.match(); | ||
res = result.group(1); | ||
} | ||
return res; | ||
} | ||
|
||
} |
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
Oops, something went wrong.
should be:
GET http://127.0.0.1:8080/clients
no?