1 /*
2  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.net.*;
25 import java.util.*;
26 import java.text.*;
27 import java.io.*;
28 import java.nio.*;
29 import java.nio.channels.*;
30 import java.util.*;
31 import com.sun.net.httpserver.*;
32 import javax.security.auth.*;
33 import javax.security.auth.callback.*;
34 import javax.security.auth.login.*;
35 
36 class LogFilter extends Filter {
37 
38     PrintStream ps;
39     DateFormat df;
40 
LogFilter(File file)41     LogFilter (File file) throws IOException {
42         ps = new PrintStream (new FileOutputStream (file));
43         df = DateFormat.getDateTimeInstance();
44     }
45 
46     /**
47      * The filter's implementation, which is invoked by the serve r
48      */
doFilter(HttpExchange t, Filter.Chain chain)49     public void doFilter (HttpExchange t, Filter.Chain chain) throws IOException
50     {
51         chain.doFilter (t);
52         HttpContext context = t.getHttpContext();
53         Headers rmap = t.getRequestHeaders();
54         String s = df.format (new Date());
55         s = s +" " + t.getRequestMethod() + " " + t.getRequestURI() + " ";
56         s = s +" " + t.getResponseCode () +" " + t.getRemoteAddress();
57         ps.println (s);
58     }
59 
init(HttpContext ctx)60     public void init (HttpContext ctx) {}
61 
description()62     public String description () {
63         return "Request logger";
64     }
65 
destroy(HttpContext c)66     public void destroy (HttpContext c){}
67 }
68