forked from abrensch/brouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerHandler.java
136 lines (117 loc) · 4.91 KB
/
ServerHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package btools.server.request;
import java.io.File;
import java.util.Map;
import btools.router.FormatCsv;
import btools.router.FormatGpx;
import btools.router.FormatJson;
import btools.router.FormatKml;
import btools.router.OsmTrack;
import btools.router.RoutingContext;
import btools.server.ServiceContext;
/**
* URL query parameter handler for web and standalone server. Supports all
* BRouter features without restrictions.
* <p>
* Parameters:
* <p>
* lonlats = lon,lat|... (unlimited list of lon,lat waypoints separated by |)
* nogos = lon,lat,radius,weight|... (optional, list of lon, lat, radius in meters, weight (optional) separated by |)
* polylines = lon,lat,lon,lat,...,weight|... (unlimited list of lon,lat and weight (optional), lists separated by |)
* polygons = lon,lat,lon,lat,...,weight|... (unlimited list of lon,lat and weight (optional), lists separated by |)
* profile = profile file name without .brf
* alternativeidx = [0|1|2|3] (optional, default 0)
* format = [kml|gpx|geojson] (optional, default gpx)
* trackname = name used for filename and format specific trackname (optional, default brouter)
* exportWaypoints = 1 to export them (optional, default is no export)
* pois = lon,lat,name|... (optional)
* timode = turnInstructionMode [0=none, 1=auto-choose, 2=locus-style, 3=osmand-style, 4=comment-style, 5=gpsies-style, 6=orux-style, 7=locus-old-style] default 0
* heading = angle (optional to give a route a start direction)
* profile:xxx = parameter in profile (optional)
* straight = idx1,idx2,.. (optional, minimum one value, index of a direct routing point in the waypoint list)
* <p>
* Example URLs:
* {@code http://localhost:17777/brouter?lonlats=8.799297,49.565883|8.811764,49.563606&nogos=&profile=trekking&alternativeidx=0&format=gpx}
* {@code http://localhost:17777/brouter?lonlats=1.1,1.2|2.1,2.2|3.1,3.2|4.1,4.2&nogos=-1.1,-1.2,1|-2.1,-2.2,2&profile=shortest&alternativeidx=1&format=kml&trackname=Ride&pois=1.1,2.1,Barner Bar}
*/
public class ServerHandler extends RequestHandler {
private RoutingContext rc;
public ServerHandler(ServiceContext serviceContext, Map<String, String> params) {
super(serviceContext, params);
}
@Override
public RoutingContext readRoutingContext() {
rc = new RoutingContext();
rc.memoryclass = 128;
String profile = params.get("profile");
// when custom profile replace prefix with directory path
if (profile.startsWith(ProfileUploadHandler.CUSTOM_PREFIX)) {
String customProfile = profile.substring(ProfileUploadHandler.CUSTOM_PREFIX.length());
profile = new File(serviceContext.customProfileDir, customProfile).getPath();
} else if (profile.startsWith(ProfileUploadHandler.SHARED_PREFIX)) {
String customProfile = profile.substring(ProfileUploadHandler.SHARED_PREFIX.length());
profile = new File(serviceContext.sharedProfileDir, customProfile).getPath();
}
rc.localFunction = profile;
return rc;
}
@Override
public String formatTrack(OsmTrack track) {
String result;
// optional, may be null
String format = params.get("format");
String trackName = getTrackName();
if (trackName != null) {
track.name = trackName;
}
String exportWaypointsStr = params.get("exportWaypoints");
if (exportWaypointsStr != null && Integer.parseInt(exportWaypointsStr) != 0) {
track.exportWaypoints = true;
}
if (format == null || "gpx".equals(format)) {
result = new FormatGpx(rc).format(track);
} else if ("kml".equals(format)) {
result = new FormatKml(rc).format(track);
} else if ("geojson".equals(format)) {
result = new FormatJson(rc).format(track);
} else if ("csv".equals(format)) {
result = new FormatCsv(rc).format(track);
} else {
System.out.println("unknown track format '" + format + "', using default");
//result = track.formatAsGpx();
result = new FormatGpx(rc).format(track);
}
return result;
}
@Override
public String getMimeType() {
// default
String result = "text/plain";
// optional, may be null
String format = params.get("format");
if (format != null) {
if ("gpx".equals(format)) {
result = "application/gpx+xml";
} else if ("kml".equals(format)) {
result = "application/vnd.google-earth.kml+xml";
} else if ("geojson".equals(format)) {
result = "application/geo+json";
} else if ("csv".equals(format)) {
result = "text/tab-separated-values";
}
}
return result;
}
@Override
public String getFileName() {
String fileName = null;
String format = params.get("format");
String trackName = getTrackName();
if (format != null) {
fileName = (trackName == null ? "brouter" : trackName) + "." + format;
}
return fileName;
}
private String getTrackName() {
return params.get("trackname") == null ? null : params.get("trackname").replaceAll("[^a-zA-Z0-9 \\._\\-]+", "");
}
}