11package net .b07z .sepia .proxies ;
22
3+ import java .io .BufferedInputStream ;
4+ import java .io .FileInputStream ;
5+ import java .io .IOException ;
6+ import java .util .ArrayList ;
7+ import java .util .List ;
8+ import java .util .Properties ;
9+
310/**
411 * Command-line interface to start a proxy.
512 *
815 */
916public class Start {
1017
18+ //defaults
19+ private static String host = "localhost" ;
20+ private static int port = 20726 ;
21+
22+ //Command-line parameters have priority
23+ private static boolean ignoreSettingsHost = false ;
24+ private static boolean ignoreSettingsPort = false ;
25+
1126 /**
1227 * Run a proxy.
1328 * @param args
@@ -16,22 +31,27 @@ public class Start {
1631 public static void main (String [] args ) throws Exception {
1732 String proxy = "" ;
1833
34+ //Check if arguments are given
35+ if (args == null || args .length == 0 ){
36+ System .out .println ("Missing proxy-name to run, e.g. 'tiny'." );
37+ help ();
38+ return ;
39+ }
40+
1941 //Proxy to run:
2042 if (args [0 ].equals ("tiny" )){
2143 proxy = "tiny" ;
2244
23- //default
24- int port = 20726 ;
25- String host = "localhost" ;
26-
2745 for (String arg : args ){
2846 //Port
2947 if (arg .startsWith ("-port=" )){
3048 port = Integer .parseInt (arg .replaceFirst (".*?=" , "" ).trim ());
49+ ignoreSettingsPort = true ;
3150
3251 //Host
3352 }else if (arg .startsWith ("-host=" )){
3453 host = arg .replaceFirst (".*?=" , "" ).trim ();
54+ ignoreSettingsHost = true ;
3555
3656 //Paths
3757 }else if (arg .startsWith ("-defaultPaths=" )){
@@ -44,19 +64,35 @@ public static void main(String[] args) throws Exception {
4464 }
4565 }
4666
67+ //Read settings
68+ List <ProxyAction > actions = null ;
69+ try {
70+ actions = loadSettings ("settings/proxy.properties" );
71+ }catch (Exception e ){
72+ System .out .println ("Could not read 'settings/proxy.properties' file! Error: " + e .getMessage ());
73+ return ;
74+ }
75+
4776 //Create tiny reverse proxy
4877 TinyReverseProxy reverseProxy = new TinyReverseProxy (host , port );
49-
50- //Add paths - SEPIA defaults for custom-bundle:
78+
79+ //Add actions
80+ for (ProxyAction pa : actions ){
81+ if (pa .actionType .equals ("redirect" )){
82+ reverseProxy .addPrefixPath (pa .redirectPath , pa .redirectTarget );
83+ }
84+ }
85+ /*
5186 reverseProxy.addPrefixPath("/sepia/assist", "http://localhost:20721");
5287 reverseProxy.addPrefixPath("/sepia/teach", "http://localhost:20722");
5388 reverseProxy.addPrefixPath("/sepia/chat", "http://localhost:20723");
89+ */
5490
5591 //Start proxy
5692 reverseProxy .start ();
5793
5894 //Note
59- System .out .println ("SEPIA '" + proxy + "' reverse proxy started at : " + host + ":" + port );
95+ System .out .println ("\n SEPIA '" + proxy + "' reverse proxy started as : " + host + ":" + port );
6096
6197 return ;
6298
@@ -77,5 +113,62 @@ private static void help(){
77113 System .out .println ("tiny - args: -defaultPaths=true, -port=20726, -host=localhost" );
78114 System .out .println ("" );
79115 }
116+
117+ /**
118+ * Class holding proxy actions loaded from settings.
119+ */
120+ private static class ProxyAction {
121+ String redirectPath ;
122+ String redirectTarget ;
123+ boolean targetIsPublic = false ;
124+ String actionType = "" ;
125+
126+ public ProxyAction (){}
127+
128+ public ProxyAction setRedirect (String path , String target , boolean isPublic ){
129+ this .redirectPath = path ;
130+ this .redirectTarget = target ;
131+ this .targetIsPublic = isPublic ;
132+ this .actionType = "redirect" ;
133+ return this ;
134+ }
135+ }
136+
137+ /**
138+ * Load settings from properties file and return actions list.
139+ * @param configFile - path and file
140+ * @throws IOException
141+ */
142+ private static List <ProxyAction > loadSettings (String configFile ) throws IOException {
143+ BufferedInputStream stream =null ;
144+ Properties config = new Properties ();
145+ stream = new BufferedInputStream (new FileInputStream (configFile ));
146+ config .load (stream );
147+ stream .close ();
148+ List <ProxyAction > actions = new ArrayList <>();
149+ for (Object key : config .keySet ()){
150+ String entry = (String ) key ;
151+ //has to be format: action_type_name, e.g. redirect_path_1
152+ //has to have types: path, target, public
153+ if (entry .startsWith ("redirect" )){
154+ String [] info = entry .split ("_" );
155+ if (info .length != 3 ){
156+ throw new RuntimeException ("Settings file has invalid format in entry: " + entry );
157+ }else {
158+ String name = info [2 ];
159+ String path = config .getProperty ("redirect_path_" + name );
160+ String target = config .getProperty ("redirect_target_" + name );
161+ boolean isPublic = Boolean .getBoolean (config .getProperty ("redirect_public_" + name ));
162+ actions .add (new ProxyAction ().setRedirect (path , target , isPublic ));
163+ }
164+
165+ }else if (entry .equals ("host" ) && !ignoreSettingsHost ){
166+ host = config .getProperty (entry );
167+ }else if (entry .equals ("port" ) && !ignoreSettingsPort ){
168+ port = Integer .parseInt (config .getProperty (entry ));
169+ }
170+ }
171+ return actions ;
172+ }
80173
81174}
0 commit comments