Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pcace/profiles endpoint #1

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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