Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

Commit f05a06ba authored by Haejoong Lee's avatar Haejoong Lee
Browse files

finished the http server

parent a378a77a
...@@ -19,36 +19,33 @@ ...@@ -19,36 +19,33 @@
<span>File <b class="caret"></b></span> <span>File <b class="caret"></b></span>
</button> </button>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li><a href="#folder-dialog" data-toggle="modal">
Load Folder
</a></li>
<li><a id="open-from-aikuma" data-toggle="modal">
Load from Aikuma App
</a></li>
<li><a id="open-remote-trans" data-toggle="modal">
Load Sample from Web
</a></li>
<li class="divider"></li>
<li><a href="#" id="new-transcript-menu"> <li><a href="#" id="new-transcript-menu">
New Transcript New Transcript
</a></li> </a></li>
<li class="divider"></li>
<li><a href="#file-dialog" data-toggle="modal"> <li><a href="#file-dialog" data-toggle="modal">
Open Transcript Open Transcript
</a></li> </a></li>
<li><a href="#dialog-open-elan" data-toggle="modal"> <li><a href="#dialog-open-elan" data-toggle="modal">
Open ELAN Transcript Open ELAN Transcript
</a></li> </a></li>
<li><a href="#folder-dialog" data-toggle="modal"> <li class="divider"></li>
Open Folder
</a></li>
<li><a href="#save-file-dialog" data-toggle="modal"> <li><a href="#save-file-dialog" data-toggle="modal">
Save Transcript Save Transcript
</a></li> </a></li>
<li><a href="#save-elan-dialog" data-toggle="modal"> <li><a href="#save-elan-dialog" data-toggle="modal">
Save as ELAN Format Save as ELAN Format
</a></li> </a></li>
<li><a id="open-remote-trans" data-toggle="modal">
Open Remote Index File (sample)
</a></li>
<li class="disabled"><a href="#" data-toggle="modal">
Open Original Recording
</a></li>
<li class="disabled"><a href="#" data-toggle="modal">
Open Respeaking
</a></li>
<li class="disabled"><a href="#" data-toggle="modal">
Open Verbal Translation
</a></li>
</ul> </ul>
</div> </div>
<div class="col-md-11 btn-group"> <div class="col-md-11 btn-group">
......
This source diff could not be displayed because it is too large. You can view the blob instead.
package org.lp20.aikuma.http; package org.lp20.aikuma.http;
import org.apache.http.conn.util.InetAddressUtils; import org.apache.http.conn.util.InetAddressUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.lp20.aikuma.http.NanoHTTPD; import org.lp20.aikuma.http.NanoHTTPD;
import org.lp20.aikuma.http.NanoHTTPD.Response.Status; import org.lp20.aikuma.http.NanoHTTPD.Response.Status;
import org.lp20.aikuma.model.Recording; import org.lp20.aikuma.model.Recording;
import org.lp20.aikuma.model.Speaker; import org.lp20.aikuma.model.Speaker;
import org.lp20.aikuma.util.ImageUtils;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
...@@ -40,15 +41,38 @@ public class Server extends NanoHTTPD { ...@@ -40,15 +41,38 @@ public class Server extends NanoHTTPD {
// Sets up a request processing chain. // Sets up a request processing chain.
proc_ = (new Proc() { proc_ = (new Proc() {
@Override
public Response run(IHTTPSession session) {
return serveIndex(session.getUri());
}
}).add(new Proc() {
// serve recordings by uuid // serve recordings by uuid
@Override @Override
public Response run(IHTTPSession session) { public Response run(IHTTPSession session) {
return serveRecording(session.getUri()); return serveRecording(session.getUri());
} }
}).add(new Proc() { }).add(new Proc() {
// serve recordings by uuid
@Override @Override
public Response run(IHTTPSession session) { public Response run(IHTTPSession session) {
return serveIndex(session.getUri()); return serveMapFile(session.getUri());
}
}).add(new Proc() {
@Override
public Response run(IHTTPSession session) {
return serveShapeFile(session.getUri());
}
}).add(new Proc() {
// serve recordings by uuid
@Override
public Response run(IHTTPSession session) {
return serveSpeakerImage(session.getUri());
}
}).add(new Proc() {
// serve recordings by uuid
@Override
public Response run(IHTTPSession session) {
return serveSpeakerSmallImage(session.getUri());
} }
}).add(new Proc() { }).add(new Proc() {
// serve static assets // serve static assets
...@@ -154,55 +178,89 @@ public class Server extends NanoHTTPD { ...@@ -154,55 +178,89 @@ public class Server extends NanoHTTPD {
} }
private Response serveIndex(String path) { private Response serveIndex(String path) {
if (path.startsWith("/index/")) { String a[] = path.split("/");
List<Recording> rs = Recording.readAll(); if (a[1].equals("index.json")) {
Iterator<Recording> it = rs.iterator(); JSONObject originals = new JSONObject();
JSONObject commentaries = new JSONObject();
JSONObject speakers = new JSONObject();
for (Recording r: Recording.readAll()) {
if (r.isOriginal())
originals.put(r.getUUID().toString(), r.encode());
else
commentaries.put(r.getUUID().toString(), r.encode());
}
for (Speaker r: Speaker.readAll()) {
speakers.put(r.getUUID().toString(), r.encode());
}
JSONObject index = new JSONObject(); JSONObject index = new JSONObject();
while (it.hasNext()) { index.put("originals", originals);
Recording r = it.next(); index.put("commentaries", commentaries);
if (r.isOriginal()) { index.put("speakers", speakers);
JSONObject obj = new JSONObject(); return new Response(Status.OK, "application/json", index.toString());
}
else {
return null;
}
}
private Response serveRecording(String path) {
String[] a = path.split("/");
if (!a[1].equals("recording") || a.length != 3)
return null;
// add speaker information
JSONObject speakers = new JSONObject();
for (UUID spkr_uuid : r.getSpeakersUUIDs()) {
try { try {
speakers.put(spkr_uuid.toString(), Speaker.read(spkr_uuid).encode()); 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) { catch (IOException e) {
continue; return mkNotFoundResponse(path);
} }
catch (IllegalArgumentException e) {
return mkNotFoundResponse(path);
} }
obj.put("speakers", speakers);
// add respeakings
JSONObject respeakings = new JSONObject();
for (Recording rspk : r.getRespeakings()) {
respeakings.put(rspk.getUUID().toString(), rspk.encode());
} }
obj.put("respeakings", respeakings);
obj.put("original", r.encode());
index.put(r.getUUID().toString(), obj); private Response serveMapFile(String path) {
String[] a = path.split("/");
if (!a[1].equals("recording") || a.length != 4 || !a[3].equals("mapfile"))
return null;
try {
File mapfile = new File(Recording.getRecordingsPath(), a[2] + ".map");
android.util.Log.e("abc", Recording.getRecordingsPath().toString());
InputStream is = new FileInputStream(mapfile);
return new Response(Status.OK, "text/plain", is);
} }
catch (FileNotFoundException e) {
return mkNotFoundResponse(path);
} }
return new Response(Status.OK, "application/json", index.toString());
} }
else {
private Response serveShapeFile(String path) {
String[] a = path.split("/");
if (!a[1].equals("recording") || a.length != 4 || !a[3].equals("shapefile"))
return null; return null;
try {
File mapfile = new File(Recording.getRecordingsPath(), a[2] + ".shape");
InputStream is = new FileInputStream(mapfile);
return new Response(Status.OK, "application/octet-stream", is);
}
catch (FileNotFoundException e) {
return mkNotFoundResponse(path);
} }
} }
private Response serveRecording(String path) { private Response serveSpeakerImage(String path) {
if (path.startsWith("/recording/")) {
String[] a = path.split("/"); String[] a = path.split("/");
if (a.length < 3) { if (!a[1].equals("speaker") || a.length != 4 || !a[3].equals("image"))
return mkNotFoundResponse(path); return null;
}
try { try {
UUID uuid = UUID.fromString(a[2]); UUID uuid = UUID.fromString(a[2]);
InputStream is = new FileInputStream(Recording.read(uuid).getFile()); InputStream is = new FileInputStream(ImageUtils.getImageFile(uuid));
return new Response(Status.OK, "audio/wave", is); return new Response(Status.OK, "image/jpeg", is);
} }
catch (IOException e) { catch (IOException e) {
return mkNotFoundResponse(path); return mkNotFoundResponse(path);
...@@ -211,8 +269,22 @@ public class Server extends NanoHTTPD { ...@@ -211,8 +269,22 @@ public class Server extends NanoHTTPD {
return mkNotFoundResponse(path); return mkNotFoundResponse(path);
} }
} }
else {
private Response serveSpeakerSmallImage(String path) {
String[] a = path.split("/");
if (!a[1].equals("speaker") || a.length != 4 || !a[3].equals("smallimage"))
return null; return null;
try {
UUID uuid = UUID.fromString(a[2]);
InputStream is = new FileInputStream(ImageUtils.getSmallImageFile(uuid));
return new Response(Status.OK, "image/jpeg", is);
}
catch (IOException e) {
return mkNotFoundResponse(path);
}
catch (IllegalArgumentException e) {
return mkNotFoundResponse(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