use of com.nabalive.framework.web.Route in project NabAlive by jcheype.
the class AppTest method testRouteMatcher.
@Test
public void testRouteMatcher() {
final Route route = new Route("/api/:name/:id");
final Map<String, String> rez = route.parse("/api/julien/123", null);
System.out.println(rez);
assertEquals("should contain julien", rez.get("name"), "julien");
assertEquals("should contain 123", rez.get("id"), "123");
}
use of com.nabalive.framework.web.Route in project NabAlive by jcheype.
the class AppTest method testRouteMatcherFalse.
@Test
public void testRouteMatcherFalse() {
final Route route = new Route("/api/:name/:id");
final Map<String, String> rez = route.parse("/api/julien/toto/123", null);
assertNull("should contain julien", rez);
}
use of com.nabalive.framework.web.Route in project NabAlive by jcheype.
the class ChorController method init.
@PostConstruct
void init() {
restHandler.get(new Route("/api/chor") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
String chor = checkNotNull(request.getParam("data"));
int loop = Integer.parseInt(firstNonNull(request.getParam("loop"), "1"));
ChorBuilder chorBuilder = new ChorBuilder(chor, loop);
response.write(chorBuilder.build());
}
}).get(new Route("/api/chor/ears") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
ChorBuilder chorBuilder = new ChorBuilder();
String left = request.getParam("left");
String right = request.getParam("right");
if (left != null) {
int val = Integer.parseInt(left);
chorBuilder.setEar((byte) 0, (byte) (val > 0 ? 0 : 1), (byte) Math.abs(val));
}
if (right != null) {
int val = Integer.parseInt(right);
chorBuilder.setEar((byte) 1, (byte) (val > 0 ? 0 : 1), (byte) Math.abs(val));
}
response.write(chorBuilder.build());
}
}).get(new Route("/api/chor/led/:led/:color") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
ChorBuilder chorBuilder = new ChorBuilder();
String color = checkNotNull(map.get("color"));
int led = Integer.parseInt(checkNotNull(map.get("led")));
chorBuilder.setLed((byte) led, color);
response.write(chorBuilder.build());
}
}).get(new Route("/api/chor/rand/:time") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
int time = Integer.parseInt(map.get("time"));
Random rand = new Random();
ChorBuilder chorBuilder = new ChorBuilder();
for (int t = 0; t < time; t++) {
for (int led = 0; led < 5; led++) if (rand.nextBoolean()) {
chorBuilder.setLed((byte) led, (byte) rand.nextInt(), (byte) rand.nextInt(), (byte) rand.nextInt());
}
if (t % 10 == 0) {
if (rand.nextBoolean()) {
chorBuilder.setEar((byte) 0, (byte) (rand.nextBoolean() ? 0 : 1), (byte) rand.nextInt(0x4));
}
if (rand.nextBoolean()) {
chorBuilder.setEar((byte) 1, (byte) (rand.nextBoolean() ? 0 : 1), (byte) rand.nextInt(0x4));
}
}
chorBuilder.waitChor(1);
}
chorBuilder.setEar((byte) 1, (byte) 0, (byte) 0);
chorBuilder.setEar((byte) 0, (byte) 0, (byte) 0);
response.write(chorBuilder.build());
}
});
}
use of com.nabalive.framework.web.Route in project NabAlive by jcheype.
the class LocateController method init.
//private final Pattern domainPattern = Pattern.compile("([^:]+)");
@PostConstruct
void init() {
restHandler.get(new Route(".*/locate.jsp") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
StringBuilder sb = new StringBuilder();
sb.append("ping " + HOST + "\n");
sb.append("broad " + HOST + "\n");
if (XMPP_PORT != null)
sb.append("xmpp_domain " + HOST + ":" + XMPP_PORT + "\n");
else
sb.append("xmpp_domain " + HOST + "\n");
response.write(sb.toString());
}
}).get(new Route(".*/bc.jsp") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
InputStream resourceAsStream = getClass().getResourceAsStream("/bc.jsp");
response.write(ByteStreams.toByteArray(resourceAsStream));
}
});
}
use of com.nabalive.framework.web.Route in project NabAlive by jcheype.
the class RecordController method init.
@PostConstruct
void init() {
restHandler.post(new Route("/vl/record.jsp") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
String mac = checkNotNull(request.getParam("sn")).toLowerCase();
if (!connectionManager.containsKey(mac))
throw new HttpException(HttpResponseStatus.NOT_FOUND, "sn is not connected");
Nabaztag nabaztag = checkNotNull(nabaztagDAO.findOne("macAddress", mac));
ChannelBuffer content = request.request.getContent();
logger.debug("record orig size: {}", content.readableBytes());
ChannelBufferInputStream inputStream = new ChannelBufferInputStream(content);
TmpData sound = new TmpData();
sound.setData(ByteStreams.toByteArray(inputStream));
tmpDataDAO.save(sound, WriteConcern.SAFE);
String host = request.request.getHeader("Host");
String url = "http://" + host + "/record/" + sound.getId().toString();
logger.debug("sound url: {}", url);
final String command = "ST " + url + "\nMW\n";
Query<Nabaztag> query = nabaztagDAO.createQuery();
query.filter("subscribe.objectId", nabaztag.getId().toString());
List<Nabaztag> nabaztags = nabaztagDAO.find(query).asList();
logger.debug("sending to {} subscribers", nabaztags.size());
for (Nabaztag nab : nabaztags) {
if (connectionManager.containsKey(nab.getMacAddress())) {
final Nabaztag nabTmp = nab;
Runnable runnable = new Runnable() {
@Override
public void run() {
logger.debug("sending to {}", nabTmp.getMacAddress());
logger.debug("command {}", command);
messageService.sendMessage(nabTmp.getMacAddress(), command);
}
};
ses.schedule(runnable, 500, TimeUnit.MILLISECONDS);
}
}
response.write("ok");
}
}).get(new Route("/record/:recordId") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
ObjectId recordId = new ObjectId(checkNotNull(map.get("recordId")));
TmpData sound = checkNotNull(tmpDataDAO.get(recordId));
response.write(sound.getData());
}
});
}
Aggregations