void init() {
restHandler
.get(new Route("/nabaztags") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
Token token = TokenUtil.decode(checkNotNull(request.getParamOrHeader("token")), Token.class);
List<Nabaztag> nabaztagList = nabaztagDAO.find(nabaztagDAO.createQuery().filter("owner", token.getUserId())).asList();
for (Nabaztag nabaztag : nabaztagList) {
if (connectionManager.containsKey(nabaztag.getMacAddress())) {
nabaztag.setConnected(true);
}
}
response.writeJSON(nabaztagList);
}
})
.post(new Route("/nabaztags", ".*json.*") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
Token token = TokenUtil.decode(checkNotNull(request.getParamOrHeader("token")), Token.class);
logger.debug("received json: {}", request.content);
Map<String, String> nabMap = mapper.readValue(request.content, Map.class);
String mac = CharMatcher.JAVA_LETTER_OR_DIGIT.retainFrom(checkNotNull(nabMap.get("mac")).toLowerCase());
if (!connectionManager.containsKey(mac)) {
throw new HttpException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Nabaztag not Connected");
}
Nabaztag nabaztag = new Nabaztag();
nabaztag.setMacAddress(mac);
nabaztag.setName(nabMap.get("name"));
nabaztag.setApikey(UUID.randomUUID().toString());
nabaztag.setOwner(token.getUserId());
try {
nabaztagDAO.save(nabaztag);
} catch (MongoException.DuplicateKey e) {
ImmutableMap<String, String> error = (new ImmutableMap.Builder<String, String>()).put("error", "Adresse mac déjà enregistrée").build();
response.writeJSON(error);
return;
}
messageService.sendMessage(mac, "ST " + OPERATIONNEL_URL + "\nMW\n");
response.writeJSON(nabaztag);
}
})
.post(new Route("/nabaztags") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
Token token = TokenUtil.decode(checkNotNull(request.getParamOrHeader("token")), Token.class);
String mac = CharMatcher.JAVA_LETTER_OR_DIGIT.retainFrom(checkNotNull(request.getParam("mac")).toLowerCase());
String name = request.getParam("name");
if (!connectionManager.containsKey(mac)) {
throw new HttpException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Nabaztag not Connected");
}
Nabaztag nabaztag = new Nabaztag();
nabaztag.setMacAddress(mac);
nabaztag.setName(name);
nabaztag.setApikey(UUID.randomUUID().toString());
nabaztag.setOwner(token.getUserId());
try {
nabaztagDAO.save(nabaztag);
} catch (MongoException.DuplicateKey e) {
ImmutableMap<String, String> error = (new ImmutableMap.Builder<String, String>()).put("error", "Adresse mac déjà enregistrée").build();
response.writeJSON(error);
return;
}
messageService.sendMessage(mac, "ST " + OPERATIONNEL_URL + "\nMW\n");
response.writeJSON(nabaztag);
}
})
.post(new Route("/nabaztags/:mac/addconfig") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
Token token = TokenUtil.decode(checkNotNull(request.getParamOrHeader("token")), Token.class);
List<String> tags = request.parameters.get("rfid");
String appApikey = checkNotNull(request.getParam("apikey"));
String name = checkNotNull(request.getParam("name"));
String appName = checkNotNull(request.getParam("appName"));
String uuid = request.getParam("uuid");
String mac = checkNotNull(map.get("mac"));
Nabaztag nabaztag = checkNotNull(nabaztagDAO.findOne("macAddress", mac));
if (!nabaztag.getOwner().equals(token.getUserId()))
throw new IllegalArgumentException();
Iterator<ApplicationConfig> iterator = nabaztag.getApplicationConfigList().iterator();
while (iterator.hasNext()) {
ApplicationConfig next = iterator.next();
if (tags != null)
next.getTags().removeAll(tags);
if (next.getUuid().equals(uuid))
iterator.remove();
}
ApplicationConfig config = new ApplicationConfig();
config.setApplicationStoreApikey(appApikey);
config.getTags().clear();
if (tags != null)
config.getTags().addAll(tags);
for (Map.Entry<String, List<String>> entry : request.parameters.entrySet()) {
if (entry.getKey().startsWith("parameter.")) {
String key = entry.getKey().substring("parameter.".length());
config.getParameters().put(key, entry.getValue());
}
}
config.setName(name);
config.setAppName(appName);
nabaztag.addApplicationConfig(config);
nabaztagDAO.save(nabaztag);
tts(nabaztag.getMacAddress(), request.request.getHeader("Host"), "fr", Format.get("app.install.success", appName));
response.writeJSON(nabaztag);
}
})
.delete(new Route("/config/:uuid") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
Token token = TokenUtil.decode(checkNotNull(request.getParamOrHeader("token")), Token.class);
String uuid = checkNotNull(map.get("uuid"));
Nabaztag nabaztag = checkNotNull(nabaztagDAO.findOne("applicationConfigList.uuid", uuid));
if (!nabaztag.getOwner().equals(token.getUserId()))
throw new IllegalArgumentException();
Iterator<ApplicationConfig> iterator = nabaztag.getApplicationConfigList().iterator();
while (iterator.hasNext()) {
ApplicationConfig next = iterator.next();
if (next.getUuid().equals(uuid))
iterator.remove();
}
nabaztagDAO.save(nabaztag);
response.writeJSON(nabaztag);
}
})
.get(new Route("/nabaztags/:mac") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
Token token = TokenUtil.decode(checkNotNull(request.getParamOrHeader("token")), Token.class);
String mac = checkNotNull(map.get("mac"));
Nabaztag nabaztag = checkNotNull(nabaztagDAO.findOne("macAddress", mac));
if (token.getUserId().equals(nabaztag.getOwner())) {
response.writeJSON(nabaztag);
} else {
response.write(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED));
}
}
})
.delete(new Route("/nabaztags/:mac") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
Token token = TokenUtil.decode(checkNotNull(request.getParamOrHeader("token")), Token.class);
String mac = checkNotNull(map.get("mac"));
Nabaztag nabaztag = checkNotNull(nabaztagDAO.findOne("macAddress", mac));
if (token.getUserId().equals(nabaztag.getOwner())) {
nabaztagDAO.delete(nabaztag);
response.writeJSON("ok");
} else {
response.write(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED));
}