22
33import java .io .File ;
44import java .io .PrintStream ;
5+ import java .util .List ;
56import joptsimple .OptionParser ;
67import joptsimple .OptionSet ;
78import org .eclipse .jetty .server .Server ;
89import org .eclipse .jetty .servlet .ServletContextHandler ;
910import org .eclipse .jetty .servlet .ServletHolder ;
11+ import org .wiztools .commons .MultiValueMap ;
12+ import org .wiztools .commons .MultiValueMapArrayList ;
13+ import org .wiztools .commons .MultiValueMapLinkedHashSet ;
1014
1115/**
1216 *
@@ -20,10 +24,12 @@ private static void printHelp(PrintStream out) {
2024 out .println ("\t -f\t : File to serve. When not given, prints <p>Hello World!</p>" );
2125 out .println ("\t -c\t : Response Content-Type. Default is text/html." );
2226 out .println ("\t -r\t : Response character encoding. Default is utf-8." );
27+ out .println ("\t -H\t : Header in the format: `header:value'." );
28+ out .println ("\t -h\t : Print this help." );
2329 }
2430
2531 public static void main (String [] args ) throws Exception {
26- OptionParser parser = new OptionParser ("p:f:c:r:h" );
32+ OptionParser parser = new OptionParser ("p:f:c:r:H: h" );
2733 OptionSet options = parser .parse (args );
2834 if (options .has ("h" )) {
2935 printHelp (System .out );
@@ -47,29 +53,35 @@ public static void main(String[] args) throws Exception {
4753 System .exit (1 );
4854 }
4955
50- final File file ;
56+ AnyUrlServlet servlet = new AnyUrlServlet ();
57+
5158 if (options .has ("f" )) {
5259 String fileStr = options .valueOf ("f" ).toString ();
53- file = new File (fileStr );
54- }
55- else {
56- file = null ;
60+ servlet .setFile (new File (fileStr ));
5761 }
5862
59- String contentType = null ;
6063 if (options .has ("c" )) {
61- contentType = options .valueOf ("c" ).toString ();
64+ servlet . setContentType ( options .valueOf ("c" ).toString () );
6265 }
6366
64- String charset = null ;
6567 if (options .has ("r" )) {
66- charset = options .valueOf ("r" ).toString ();
68+ servlet . setCharset ( options .valueOf ("r" ).toString () );
6769 }
6870
69- AnyUrlServlet servlet = new AnyUrlServlet ();
70- servlet .setFile (file );
71- servlet .setContentType (contentType );
72- servlet .setCharset (charset );
71+ if (options .has ("H" )) {
72+ MultiValueMap headers = new MultiValueMapLinkedHashSet ();
73+ for (Object t : options .valuesOf ("H" )) {
74+ final String headerLine = t .toString ().trim ();
75+ final int sepIdx = headerLine .indexOf (':' );
76+ if ((sepIdx > 0 ) && (sepIdx < headerLine .length ())) {
77+ final String header = headerLine .substring (0 , sepIdx );
78+ final String value = headerLine .substring (sepIdx + 1 , headerLine .length ());
79+ System .out .printf ("\n Header: Value => %s:%s\n " , header , value );
80+ headers .put (header , value );
81+ }
82+ }
83+ servlet .setHeaders (headers );
84+ }
7385
7486 Server server = new Server (port );
7587 server .setStopAtShutdown (true );
0 commit comments