profile upload (a first implementation), similar to CgiUpload,
adds a new customprofiledir argument, a new file is created on each upload
This commit is contained in:
parent
ba867b4779
commit
6f9cef9b3f
5 changed files with 126 additions and 14 deletions
|
@ -20,6 +20,7 @@ import btools.router.OsmNodeNamed;
|
|||
import btools.router.OsmTrack;
|
||||
import btools.router.RoutingContext;
|
||||
import btools.router.RoutingEngine;
|
||||
import btools.server.request.ProfileUploadHandler;
|
||||
import btools.server.request.RequestHandler;
|
||||
import btools.server.request.ServerHandler;
|
||||
|
||||
|
@ -65,6 +66,16 @@ public class RouteServer extends Thread
|
|||
{
|
||||
handler = new ServerHandler( serviceContext, params );
|
||||
}
|
||||
else if ("/brouter/profile".equals(url))
|
||||
{
|
||||
writeHttpHeader(bw);
|
||||
|
||||
ProfileUploadHandler uploadHandler = new ProfileUploadHandler( serviceContext );
|
||||
uploadHandler.handlePostRequest(br, bw);
|
||||
|
||||
bw.flush();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException( "unknown request syntax: " + getline );
|
||||
|
@ -76,12 +87,7 @@ public class RouteServer extends Thread
|
|||
cr.quite = true;
|
||||
cr.doRun( maxRunningTime );
|
||||
|
||||
// http-header
|
||||
bw.write( "HTTP/1.1 200 OK\n" );
|
||||
bw.write( "Connection: close\n" );
|
||||
bw.write( "Content-Type: text/xml; charset=utf-8\n" );
|
||||
bw.write( "Access-Control-Allow-Origin: *\n" );
|
||||
bw.write( "\n" );
|
||||
writeHttpHeader(bw);
|
||||
|
||||
if ( cr.getErrorMessage() != null )
|
||||
{
|
||||
|
@ -115,22 +121,23 @@ public class RouteServer extends Thread
|
|||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
System.out.println("BRouter 0.9.9 / 18042014 / abrensch");
|
||||
if ( args.length != 4 )
|
||||
if ( args.length != 5 )
|
||||
{
|
||||
System.out.println("serve BRouter protocol");
|
||||
System.out.println("usage: java RouteServer <segmentdir> <profiledir> <port> <maxthreads>");
|
||||
System.out.println("usage: java RouteServer <segmentdir> <profiledir> <customprofiledir> <port> <maxthreads>");
|
||||
return;
|
||||
}
|
||||
|
||||
ServiceContext serviceContext = new ServiceContext();
|
||||
serviceContext.segmentDir = args[0];
|
||||
System.setProperty( "profileBaseDir", args[1] );
|
||||
serviceContext.customProfileDir = args[2];
|
||||
|
||||
int maxthreads = Integer.parseInt( args[3] );
|
||||
int maxthreads = Integer.parseInt( args[4] );
|
||||
|
||||
TreeMap<Long,RouteServer> threadMap = new TreeMap<Long,RouteServer>();
|
||||
|
||||
ServerSocket serverSocket = new ServerSocket(Integer.parseInt(args[2]));
|
||||
ServerSocket serverSocket = new ServerSocket(Integer.parseInt(args[3]));
|
||||
long last_ts = 0;
|
||||
for (;;)
|
||||
{
|
||||
|
@ -187,4 +194,13 @@ public class RouteServer extends Thread
|
|||
}
|
||||
return maxRunningTime;
|
||||
}
|
||||
|
||||
private static void writeHttpHeader(BufferedWriter bw) throws IOException {
|
||||
// http-header
|
||||
bw.write( "HTTP/1.1 200 OK\n" );
|
||||
bw.write( "Connection: close\n" );
|
||||
bw.write( "Content-Type: text/xml; charset=utf-8\n" );
|
||||
bw.write( "Access-Control-Allow-Origin: *\n" );
|
||||
bw.write( "\n" );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import btools.router.OsmNodeNamed;
|
|||
public class ServiceContext
|
||||
{
|
||||
public String segmentDir;
|
||||
public String customProfileDir;
|
||||
public Map<String,String> profileMap = null;
|
||||
public List<OsmNodeNamed> nogoList;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
package btools.server.request;
|
||||
|
||||
import btools.server.ServiceContext;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
/**
|
||||
* Custom profile uploads
|
||||
*/
|
||||
public class ProfileUploadHandler
|
||||
{
|
||||
// maximum number of characters (file size limit for custom profiles)
|
||||
private static final int MAX_LENGTH = 100000;
|
||||
|
||||
private ServiceContext serviceContext;
|
||||
|
||||
public ProfileUploadHandler( ServiceContext serviceContext)
|
||||
{
|
||||
this.serviceContext = serviceContext;
|
||||
}
|
||||
|
||||
public void handlePostRequest(BufferedReader br, BufferedWriter response) throws IOException
|
||||
{
|
||||
BufferedWriter fileWriter = null;
|
||||
|
||||
try
|
||||
{
|
||||
String id = getOrCreateCustomProfileDir() + "/" + System.currentTimeMillis();
|
||||
File file = new File ( id + ".brf" );
|
||||
fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( file ) ) );
|
||||
//StringWriter sw = new StringWriter(); bw = new BufferedWriter(sw);
|
||||
|
||||
// only profile text as content
|
||||
readPostData(br, fileWriter, id);
|
||||
|
||||
fileWriter.flush();
|
||||
//System.out.println("data: |" + sw.toString() + "|");
|
||||
|
||||
response.write("profileid=" + id);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if ( fileWriter != null ) try { fileWriter.close(); } catch( Exception e ) {}
|
||||
}
|
||||
}
|
||||
|
||||
private File getOrCreateCustomProfileDir()
|
||||
{
|
||||
File customProfileDir = new File(serviceContext.customProfileDir);
|
||||
if (!customProfileDir.exists())
|
||||
{
|
||||
customProfileDir.mkdir();
|
||||
}
|
||||
return customProfileDir;
|
||||
}
|
||||
|
||||
// reads HTTP POST content from input into output stream/writer
|
||||
private static void readPostData( BufferedReader ir, BufferedWriter bw, String id ) throws IOException
|
||||
{
|
||||
// Content-Type: text/plain;charset=UTF-8
|
||||
|
||||
for(;;)
|
||||
{
|
||||
// headers
|
||||
String line = ir.readLine();
|
||||
if ( line == null ) break;
|
||||
|
||||
// blank line before content after headers
|
||||
if ( line.length() == 0 )
|
||||
{
|
||||
int numChars = 0;
|
||||
|
||||
// Content-Length header is in bytes (!= characters for UTF8),
|
||||
// but Reader reads characters, so don't know number of characters to read
|
||||
for(;;)
|
||||
{
|
||||
// read will block when false, occurs at end of stream rather than -1
|
||||
if (!ir.ready()) break;
|
||||
int c = ir.read();
|
||||
if ( c == -1) break;
|
||||
bw.write( c );
|
||||
|
||||
numChars++;
|
||||
if (numChars > MAX_LENGTH)
|
||||
throw new IOException("Maximum number of characters exceeded (" + MAX_LENGTH + ", " + id + ")");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
@echo off
|
||||
|
||||
REM BRouter standalone server
|
||||
REM java -cp brouter.jar btools.brouter.RouteServer <segmentdir> <profile-map> <port>
|
||||
REM java -cp brouter.jar btools.brouter.RouteServer <segmentdir> <profile-map> <customprofiledir> <port> <maxthreads>
|
||||
|
||||
set JAVA_OPTS=-Xmx128M -Xms128M -Xmn8M
|
||||
set CLASSPATH=../brouter.jar
|
||||
|
||||
java %JAVA_OPTS% -cp %CLASSPATH% btools.server.RouteServer ..\segments2 ..\profiles2 17777 1
|
||||
java %JAVA_OPTS% -cp %CLASSPATH% btools.server.RouteServer ..\segments2 ..\profiles2 ..\customprofiles 17777 1
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#!/bin/sh
|
||||
|
||||
# BRouter standalone server
|
||||
# java -cp brouter.jar btools.brouter.RouteServer <segmentdir> <profile-map> <port>
|
||||
# java -cp brouter.jar btools.brouter.RouteServer <segmentdir> <profile-map> <customprofiledir> <port> <maxthreads>
|
||||
|
||||
JAVA_OPTS="-Xmx128M -Xms128M -Xmn8M"
|
||||
CLASSPATH=../brouter.jar
|
||||
|
||||
java $JAVA_OPTS -cp $CLASSPATH btools.server.RouteServer ../segments2 ../profiles2 17777 1
|
||||
java $JAVA_OPTS -cp $CLASSPATH btools.server.RouteServer ../segments2 ../profiles2 ../customprofiles 17777 1
|
||||
|
|
Loading…
Reference in a new issue