Skip to content

Commit

Permalink
Merge pull request #1 from pcace/pcace/profilesEndpoint
Browse files Browse the repository at this point in the history
Pcace/profiles endpoint
  • Loading branch information
pcace authored Dec 21, 2024
2 parents 625ff0c + 0ef4628 commit 75e86c1
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions brouter-server/src/main/java/btools/server/RouteServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.io.FileInputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
Expand Down Expand Up @@ -38,6 +39,7 @@

public class RouteServer extends Thread implements Comparable<RouteServer> {
public static final String PROFILE_UPLOAD_URL = "/brouter/profile";
public static final String PROFILES_URL = "/brouter/getprofiles";
static final String HTTP_STATUS_OK = "200 OK";
static final String HTTP_STATUS_BAD_REQUEST = "400 Bad Request";
static final String HTTP_STATUS_FORBIDDEN = "403 Forbidden";
Expand Down Expand Up @@ -180,6 +182,9 @@ public void run() {
bw.flush();
return;
}
} else if (url.startsWith(PROFILES_URL)) {
handleProfilesRequest(url, bw);
return;
} else if (url.startsWith("/brouter/suspects")) {
writeHttpHeader(bw, url.endsWith(".json") ? "application/json" : "text/html", HTTP_STATUS_OK);
SuspectManager.process(url, bw);
Expand Down Expand Up @@ -278,6 +283,45 @@ public void run() {
}
}

private void handleProfilesRequest(String url, BufferedWriter bw) throws IOException {
File profileDir = new File(serviceContext.profileDir);
File[] profiles = profileDir.listFiles((dir, name) -> name.endsWith(".brf"));
if (profiles == null) {
writeHttpHeader(bw, HTTP_STATUS_INTERNAL_SERVER_ERROR);
bw.write("Could not list profiles");
bw.flush();
return;
}

if (url.equals(PROFILES_URL)) {
writeHttpHeader(bw, "application/json", HTTP_STATUS_OK);
bw.write("[");
for (int i = 0; i < profiles.length; i++) {
if (i > 0) {
bw.write(",");
}
bw.write("\"" + profiles[i].getName() + "\"");
}
bw.write("]");
} else {
String profileName = url.substring(PROFILES_URL.length() + 1);
File profileFile = new File(profileDir, profileName);
if (!profileFile.exists() || !profileFile.isFile()) {
writeHttpHeader(bw, HTTP_STATUS_NOT_FOUND);
bw.write("Profile not found: " + profileName);
} else {
writeHttpHeader(bw, "text/plain", HTTP_STATUS_OK);
BufferedReader profileReader = new BufferedReader(new InputStreamReader(new FileInputStream(profileFile), "UTF-8"));
String line;
while ((line = profileReader.readLine()) != null) {
bw.write(line);
bw.write("\n");
}
profileReader.close();
}
}
bw.flush();
}

public static void main(String[] args) throws Exception {
System.out.println("BRouter " + OsmTrack.version + " / " + OsmTrack.versionDate);
Expand Down

0 comments on commit 75e86c1

Please sign in to comment.