1*3eeae035SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*3eeae035SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*3eeae035SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*3eeae035SAndrew Rist * distributed with this work for additional information 6*3eeae035SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*3eeae035SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*3eeae035SAndrew Rist * "License"); you may not use this file except in compliance 9*3eeae035SAndrew Rist * with the License. You may obtain a copy of the License at 10*3eeae035SAndrew Rist * 11*3eeae035SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*3eeae035SAndrew Rist * 13*3eeae035SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*3eeae035SAndrew Rist * software distributed under the License is distributed on an 15*3eeae035SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*3eeae035SAndrew Rist * KIND, either express or implied. See the License for the 17*3eeae035SAndrew Rist * specific language governing permissions and limitations 18*3eeae035SAndrew Rist * under the License. 19*3eeae035SAndrew Rist * 20*3eeae035SAndrew Rist *************************************************************/ 21cdf0e10cSrcweir 22cdf0e10cSrcweir package httpserv; 23cdf0e10cSrcweir 24cdf0e10cSrcweir import com.sun.net.httpserver.Headers; 25cdf0e10cSrcweir import com.sun.net.httpserver.HttpExchange; 26cdf0e10cSrcweir import com.sun.net.httpserver.HttpHandler; 27cdf0e10cSrcweir import com.sun.net.httpserver.HttpServer; 28cdf0e10cSrcweir import java.io.File; 29cdf0e10cSrcweir import java.io.FileInputStream; 30cdf0e10cSrcweir import java.io.IOException; 31cdf0e10cSrcweir import java.io.OutputStream; 32cdf0e10cSrcweir import java.net.InetSocketAddress; 33cdf0e10cSrcweir 34cdf0e10cSrcweir /** 35cdf0e10cSrcweir * 36cdf0e10cSrcweir * @author jochen 37cdf0e10cSrcweir */ 38cdf0e10cSrcweir public class Main { 39cdf0e10cSrcweir 40cdf0e10cSrcweir /** 41cdf0e10cSrcweir * @param args the command line arguments 42cdf0e10cSrcweir */ main(String[] args)43cdf0e10cSrcweir public static void main(String[] args) { 44cdf0e10cSrcweir try { 45cdf0e10cSrcweir 46cdf0e10cSrcweir Option[] opts = new Option[2]; 47cdf0e10cSrcweir opts[0] = new Option("--help", "-h", false); 48cdf0e10cSrcweir opts[1] = new Option("--accept", "-a", true); 49cdf0e10cSrcweir if (!parseOptions(args, opts)) { 50cdf0e10cSrcweir return; 51cdf0e10cSrcweir } 52cdf0e10cSrcweir HttpServer server = HttpServer.create( 53cdf0e10cSrcweir new InetSocketAddress((Integer) opts[1].value), 0); 54cdf0e10cSrcweir server.createContext("/", new MyHandler()); 55cdf0e10cSrcweir server.setExecutor(null); 56cdf0e10cSrcweir server.start(); 57cdf0e10cSrcweir } catch (Exception e) { 58cdf0e10cSrcweir e.printStackTrace(); 59cdf0e10cSrcweir } 60cdf0e10cSrcweir } // TODO code application logic here 61cdf0e10cSrcweir parseOptions(String[] args, Option[] inout_options)62cdf0e10cSrcweir static boolean parseOptions(String[] args, Option[] inout_options) { 63cdf0e10cSrcweir if (args.length == 0) { 64cdf0e10cSrcweir printUsage(); 65cdf0e10cSrcweir return false; 66cdf0e10cSrcweir } 67cdf0e10cSrcweir 68cdf0e10cSrcweir boolean bWrongArgs = true; 69cdf0e10cSrcweir Option currentOpt = null; 70cdf0e10cSrcweir 71cdf0e10cSrcweir for (String s : args) { 72cdf0e10cSrcweir // get the value for an option 73cdf0e10cSrcweir if (currentOpt != null && currentOpt.bHasValue) { 74cdf0e10cSrcweir //now we expect the value for the option 75cdf0e10cSrcweir //check the type 76cdf0e10cSrcweir try { 77cdf0e10cSrcweir if (currentOpt.sLong.equals("--accept")) { 78cdf0e10cSrcweir currentOpt.value = Integer.decode(s); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir } catch (Exception e ) { 81cdf0e10cSrcweir printUsage(); 82cdf0e10cSrcweir return false; 83cdf0e10cSrcweir } 84cdf0e10cSrcweir currentOpt = null; 85cdf0e10cSrcweir continue; 86cdf0e10cSrcweir } else { 87cdf0e10cSrcweir currentOpt = null; 88cdf0e10cSrcweir } 89cdf0e10cSrcweir 90cdf0e10cSrcweir 91cdf0e10cSrcweir // get the option 92cdf0e10cSrcweir for (Option o : inout_options) { 93cdf0e10cSrcweir if (s.equals(o.sLong) || s.equals(o.sShort)) { 94cdf0e10cSrcweir bWrongArgs = false; 95cdf0e10cSrcweir //special handling for --help 96cdf0e10cSrcweir if (o.sLong.equals("--help")) { 97cdf0e10cSrcweir printUsage(); 98cdf0e10cSrcweir return false; 99cdf0e10cSrcweir } 100cdf0e10cSrcweir else 101cdf0e10cSrcweir { 102cdf0e10cSrcweir currentOpt = o; 103cdf0e10cSrcweir if (!o.bHasValue) { 104cdf0e10cSrcweir o.bSet = true; 105cdf0e10cSrcweir } 106cdf0e10cSrcweir break; 107cdf0e10cSrcweir } 108cdf0e10cSrcweir } 109cdf0e10cSrcweir } 110cdf0e10cSrcweir } 111cdf0e10cSrcweir 112cdf0e10cSrcweir if (bWrongArgs) { 113cdf0e10cSrcweir printUsage(); 114cdf0e10cSrcweir return false; 115cdf0e10cSrcweir } 116cdf0e10cSrcweir return true; 117cdf0e10cSrcweir } 118cdf0e10cSrcweir printUsage()119cdf0e10cSrcweir static void printUsage() { 120cdf0e10cSrcweir String usage = new String( 121cdf0e10cSrcweir "Usage: \n" + 122cdf0e10cSrcweir "java -jar httpserv [options] \n" + 123cdf0e10cSrcweir "\n" + 124cdf0e10cSrcweir "Options are: \n" + 125cdf0e10cSrcweir "-h --help \t this help \n" + 126cdf0e10cSrcweir "-a --accept port \t the port number to which this server listens \n"); 127cdf0e10cSrcweir System.out.println(usage); 128cdf0e10cSrcweir } 129cdf0e10cSrcweir } 130cdf0e10cSrcweir 131cdf0e10cSrcweir class MyHandler implements HttpHandler { 132cdf0e10cSrcweir handle(HttpExchange xchange)133cdf0e10cSrcweir public void handle(HttpExchange xchange) throws IOException { 134cdf0e10cSrcweir try { 135cdf0e10cSrcweir //First get the path to the file 136cdf0e10cSrcweir File fileCurrent = new File("."); 137cdf0e10cSrcweir String sRequestPath = xchange.getRequestURI().getPath(); 138cdf0e10cSrcweir System.out.println("requested: " + sRequestPath); 139cdf0e10cSrcweir File fileRequest = new File(new File(".").getCanonicalPath(), sRequestPath); 140cdf0e10cSrcweir if (!fileRequest.exists()) { 141cdf0e10cSrcweir throw new Exception("The file " + fileRequest.toString() + " does not exist!\n"); 142cdf0e10cSrcweir } 143cdf0e10cSrcweir else if (fileRequest.isDirectory()) { 144cdf0e10cSrcweir throw new Exception(fileRequest.toString() + " is a directory!\n"); 145cdf0e10cSrcweir } 146cdf0e10cSrcweir 147cdf0e10cSrcweir 148cdf0e10cSrcweir //Read the file into a byte array 149cdf0e10cSrcweir byte[] data = new byte[(int) fileRequest.length()]; 150cdf0e10cSrcweir FileInputStream fr = new FileInputStream(fileRequest); 151cdf0e10cSrcweir int count = fr.read(data); 152cdf0e10cSrcweir 153cdf0e10cSrcweir //set the Content-type header 154cdf0e10cSrcweir Headers h = xchange.getResponseHeaders(); 155cdf0e10cSrcweir String canonicalPath = fileRequest.getCanonicalPath(); 156cdf0e10cSrcweir int lastIndex = canonicalPath.lastIndexOf("."); 157cdf0e10cSrcweir String fileExtension = canonicalPath.substring(lastIndex + 1); 158cdf0e10cSrcweir 159cdf0e10cSrcweir if (fileExtension.equalsIgnoreCase("crl")) 160cdf0e10cSrcweir { 161cdf0e10cSrcweir //h.set("Content-Type","application/x-pkcs7-crl"); 162cdf0e10cSrcweir h.set("Content-Type","application/pkix-crl"); 163cdf0e10cSrcweir } 164cdf0e10cSrcweir else if (fileExtension.equalsIgnoreCase("crt") 165cdf0e10cSrcweir || fileExtension.equalsIgnoreCase("cer") 166cdf0e10cSrcweir || fileExtension.equalsIgnoreCase("der")) 167cdf0e10cSrcweir { 168cdf0e10cSrcweir h.set("Content-Type", "application/x-x509-ca-cert"); 169cdf0e10cSrcweir } 170cdf0e10cSrcweir 171cdf0e10cSrcweir //write out the requested file 172cdf0e10cSrcweir xchange.sendResponseHeaders(200, data.length); 173cdf0e10cSrcweir OutputStream os = xchange.getResponseBody(); 174cdf0e10cSrcweir os.write(data); 175cdf0e10cSrcweir os.close(); 176cdf0e10cSrcweir System.out.println("delivered: " + fileRequest.toString()); 177cdf0e10cSrcweir 178cdf0e10cSrcweir } catch (Exception e) { 179cdf0e10cSrcweir xchange.sendResponseHeaders(404, e.getMessage().length()); 180cdf0e10cSrcweir OutputStream os = xchange.getResponseBody(); 181cdf0e10cSrcweir os.write(e.getMessage().getBytes()); 182cdf0e10cSrcweir os.close(); 183cdf0e10cSrcweir System.out.println("Error: " + e.getMessage()); 184cdf0e10cSrcweir } 185cdf0e10cSrcweir } 186cdf0e10cSrcweir } 187cdf0e10cSrcweir 188cdf0e10cSrcweir class Option { 189cdf0e10cSrcweir Option(String _sLong, String _sShort, boolean _bHasValue)190cdf0e10cSrcweir Option(String _sLong, String _sShort, boolean _bHasValue) { 191cdf0e10cSrcweir sLong = _sLong; 192cdf0e10cSrcweir sShort = _sShort; 193cdf0e10cSrcweir bHasValue = _bHasValue; 194cdf0e10cSrcweir } 195cdf0e10cSrcweir String sLong; 196cdf0e10cSrcweir String sShort; 197cdf0e10cSrcweir boolean bHasValue; 198cdf0e10cSrcweir Object value; 199cdf0e10cSrcweir //indicates if this option was set if it does not need a value. Otherwise value 200cdf0e10cSrcweir //is set. 201cdf0e10cSrcweir boolean bSet; 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204cdf0e10cSrcweir 205