Commit a0f9c4bb authored by Haejoong Lee's avatar Haejoong Lee

added http request chain; started building recordings index page

parent 42f08c13
package org.lp20.aikuma.http;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.lp20.aikuma.http.NanoHTTPD.IHTTPSession;
import org.lp20.aikuma.http.NanoHTTPD.Response;
/**
* A chain of HTTP request processors.
*
* @author haejoong
*/
abstract public class Proc {
private List<Proc> procs;
/**
* Add a new processor to the processor chain and returns itself.
* @param p
* @return this object.
*/
public Proc add(Proc p) {
if (procs == null) {
procs = new ArrayList<Proc>();
procs.add(this);
}
procs.add(p);
return this;
}
/**
* The actual behavior of the object should be implemented here.
* @param session
*/
abstract public Response run(IHTTPSession session);
public Response exec(IHTTPSession session) {
Iterator<Proc> it = procs.iterator();
while (it.hasNext()) {
Response r = it.next().run(session);
if (r != null) {
return r;
}
}
return null;
}
}
package org.lp20.aikuma.http;
import org.apache.http.conn.util.InetAddressUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.lp20.aikuma.http.NanoHTTPD;
import org.lp20.aikuma.http.NanoHTTPD.Response.Status;
import org.lp20.aikuma.model.Recording;
import org.lp20.aikuma.model.Speaker;
import android.content.res.AssetManager;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.net.NetworkInterface;
import java.net.InetAddress;
import java.net.URI;
public class Server extends NanoHTTPD {
private static String host_; // hostname
private static int port_ = -1; // port number
private static Server server_; // singleton server object
private static AssetManager am_;
private Proc proc_;
/**
* Protected constructor used by a factory function.
......@@ -31,6 +38,26 @@ public class Server extends NanoHTTPD {
*/
protected Server(String host, int port) {
super(host, port);
// Sets up a request processing chain.
proc_ = (new Proc() {
// serve recordings by uuid
@Override
public Response run(IHTTPSession session) {
return serveRecording(session.getUri());
}
}).add(new Proc() {
@Override
public Response run(IHTTPSession session) {
return serveIndex(session.getUri());
}
}).add(new Proc() {
// serve static assets
@Override
public Response run(IHTTPSession session) {
return serveAsset(session.getUri());
}
});
}
/**
......@@ -109,9 +136,6 @@ public class Server extends NanoHTTPD {
if (!addr.isLoopbackAddress()) {
String ip = addr.getHostAddress();
if (InetAddressUtils.isIPv4Address(ip)) {
android.util.Log.e("http--", ff.getName());
android.util.Log.e("http--", addr.getHostName());
android.util.Log.e("http--", ip);
ips.put(ff.getName(), ip);
}
}
......@@ -127,32 +151,78 @@ public class Server extends NanoHTTPD {
@Override
public Response serve(IHTTPSession session) {
String path = session.getUri();
return proc_.exec(session);
}
private Response serveIndex(String path) {
if (path.startsWith("/index/")) {
List<Recording> rs = Recording.readAll();
Iterator<Recording> it = rs.iterator();
JSONObject index = new JSONObject();
while (it.hasNext()) {
Recording r = it.next();
if (r.isOriginal()) {
JSONObject obj = new JSONObject();
JSONArray speakers = new JSONArray();
Iterator<UUID> uuid_it = r.getSpeakersUUIDs().iterator();
while (uuid_it.hasNext()) {
Speaker spkr;
try {
spkr = Speaker.read(uuid_it.next());
}
catch (IOException e) {
continue;
}
speakers.put(spkr.encode());
}
try {
obj.put("speakers", speakers);
index.put(r.getUUID().toString(), obj);
}
catch (JSONException e) {
continue;
}
}
}
return new Response(Status.OK, "text/plain", index.toString());
}
else {
return null;
}
}
private Response serveRecording(String path) {
if (path.startsWith("/recording/")) {
String uuid_str = path.split("/")[2];
String[] a = path.split("/");
if (a.length < 3) {
return mkNotFoundResponse(path);
}
try {
UUID uuid = UUID.fromString(uuid_str);
UUID uuid = UUID.fromString(a[2]);
InputStream is = new FileInputStream(Recording.read(uuid).getFile());
return new Response(Status.OK, "audio/wave", is);
}
catch (IOException e) {
return mkNotFoundResponse(uuid_str);
return mkNotFoundResponse(path);
}
catch (IllegalArgumentException e) {
return mkNotFoundResponse(uuid_str);
}
return mkNotFoundResponse(path);
}
}
else {
try {
InputStream is = am_.open(path.substring(1));
String mimeType = guessMimeType(path);
return new Response(Status.OK, mimeType, is);
}
catch (IOException e) {
return mkNotFoundResponse(path);
}
return null;
}
}
private Response serveAsset(String path) {
try {
InputStream is = am_.open(path.substring(1));
String mimeType = guessMimeType(path);
return new Response(Status.OK, mimeType, is);
}
catch (IOException e) {
return mkNotFoundResponse(path);
}
}
private String guessMimeType(String path) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment