jdk/src/share/classes/com/sun/net/httpserver/Filter.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6906 d57020b6118f
child 20742 4ae78e8060d6
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6906
diff changeset
     2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
6906
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    28
import java.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A filter used to pre- and post-process incoming requests. Pre-processing occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * before the application's exchange handler is invoked, and post-processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * occurs after the exchange handler returns.  Filters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * are organised in chains, and are associated with HttpContext instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Each Filter in the chain, invokes the next filter within its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * doFilter() implementation. The final Filter in the chain invokes the applications
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * exchange handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public abstract class Filter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    protected Filter () {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * a chain of filters associated with a HttpServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Each filter in the chain is given one of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * so it can invoke the next filter in the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static class Chain {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        /* the last element in the chain must invoke the users
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
         * handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        private ListIterator<Filter> iter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        private HttpHandler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public Chain (List<Filter> filters, HttpHandler handler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            iter = filters.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            this.handler = handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         * calls the next filter in the chain, or else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
         * the users exchange handler, if this is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         * final filter in the chain. The Filter may decide
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         * to terminate the chain, by not calling this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         * In this case, the filter <b>must</b> send the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
         * response to the request, because the application's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
         * exchange handler will not be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         * @param exchange the HttpExchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         * @throws IOException let exceptions pass up the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         * @throws NullPointerException if exchange is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public void doFilter (HttpExchange exchange) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (!iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                handler.handle (exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                Filter f = iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                f.doFilter (exchange, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Asks this filter to pre/post-process the given exchange. The filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * can :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <ul><li>examine or modify the request headers</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <li>filter the request body or the response body, by creating suitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * filter streams and calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * {@link HttpExchange#setStreams(InputStream,OutputStream)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * <li>set attribute Objects in the exchange, which other filters or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * exchange handler can access.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <li>decide to either :-<ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <li>invoke the next filter in the chain, by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * {@link Filter.Chain#doFilter(HttpExchange)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <li>terminate the chain of invocation, by <b>not</b> calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * {@link Filter.Chain#doFilter(HttpExchange)}</li></ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <li>if option 1. above taken, then when doFilter() returns all subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * filters in the Chain have been called, and the response headers can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * examined or modified.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <li>if option 2. above taken, then this Filter must use the HttpExchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * to send back an appropriate response</li></ul><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param exchange the <code>HttpExchange</code> to be filtered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param chain the Chain which allows the next filter to be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @throws IOException may be thrown by any filter module, and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *          caught, must be rethrown again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @throws NullPointerException if either exchange or chain are <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public abstract void doFilter (HttpExchange exchange, Chain chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * returns a short description of this Filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return a string describing the Filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public abstract String description ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
}