src/jdk.httpserver/share/classes/sun/net/httpserver/HttpContextImpl.java
author prappo
Fri, 17 Nov 2017 13:55:41 +0300
branchhttp-client-branch
changeset 55824 b922df193260
parent 47216 71c04702a3d5
permissions -rw-r--r--
http-client-branch: (WebSocket) immutable builder copy; illegal headers;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 2006, 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 sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
41592
855537e5ad9c 8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents: 25859
diff changeset
    29
import java.lang.System.Logger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.net.httpserver.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * HttpContext represents a mapping between a protocol (http or https) together with a root URI path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * to a {@link HttpHandler} which is invoked to handle requests destined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * for the protocol/path on the associated HttpServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * HttpContext instances are created by {@link HttpServer#createContext(String,String,HttpHandler,Object)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class HttpContextImpl extends HttpContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private String protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private HttpHandler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private Map<String,Object> attributes = new HashMap<String,Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private ServerImpl server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /* system filters, not visible to applications */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private LinkedList<Filter> sfilters = new LinkedList<Filter>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /* user filters, set by applications */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private LinkedList<Filter> ufilters = new LinkedList<Filter>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private Authenticator authenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private AuthFilter authfilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * constructor is package private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    HttpContextImpl (String protocol, String path, HttpHandler cb, ServerImpl server) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (path == null || protocol == null || path.length() < 1 || path.charAt(0) != '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new IllegalArgumentException ("Illegal value for path or protocol");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.protocol = protocol.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.path = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (!this.protocol.equals ("http") && !this.protocol.equals ("https")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            throw new IllegalArgumentException ("Illegal value for protocol");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.handler = cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.server = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        authfilter = new AuthFilter(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        sfilters.add (authfilter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * returns the handler for this context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @return the HttpHandler for this context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public HttpHandler getHandler () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public void setHandler (HttpHandler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (h == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new NullPointerException ("Null handler parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (handler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new IllegalArgumentException ("handler already set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        handler = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * returns the path this context was created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @return this context's path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public String getPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * returns the server this context was created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @return this context's server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public HttpServer getServer () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return server.getWrapper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    ServerImpl getServerImpl () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * returns the protocol this context was created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @return this context's path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public String getProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * returns a mutable Map, which can be used to pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * configuration and other data to Filter modules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * and to the context's exchange handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Every attribute stored in this Map will be visible to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * every HttpExchange processed by this context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public Map<String,Object> getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public List<Filter> getFilters () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return ufilters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    List<Filter> getSystemFilters () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return sfilters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public Authenticator setAuthenticator (Authenticator auth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        Authenticator old = authenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        authenticator = auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        authfilter.setAuthenticator (auth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public Authenticator getAuthenticator () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return authenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    Logger getLogger () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return server.getLogger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}