Path: blob/master/src/jdk.httpserver/share/classes/sun/net/httpserver/HttpContextImpl.java
41159 views
/*1* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.httpserver;26import java.util.*;27import java.lang.System.Logger;28import java.util.concurrent.ConcurrentHashMap;29import java.util.concurrent.CopyOnWriteArrayList;30import com.sun.net.httpserver.*;3132/**33* HttpContext represents a mapping between a protocol (http or https) together with a root URI path34* to a {@link HttpHandler} which is invoked to handle requests destined35* for the protocol/path on the associated HttpServer.36* <p>37* HttpContext instances are created by {@link HttpServer#createContext(String,String,HttpHandler,Object)}38* <p>39*/40class HttpContextImpl extends HttpContext {4142private final String path;43private final String protocol;44private final ServerImpl server;45private final AuthFilter authfilter;46private final Map<String,Object> attributes = new ConcurrentHashMap<>();47/* system filters, not visible to applications */48private final List<Filter> sfilters = new CopyOnWriteArrayList<>();49/* user filters, set by applications */50private final List<Filter> ufilters = new CopyOnWriteArrayList<>();51private Authenticator authenticator;52private HttpHandler handler;5354/**55* constructor is package private.56*/57HttpContextImpl (String protocol, String path, HttpHandler cb, ServerImpl server) {58if (path == null || protocol == null || path.length() < 1 || path.charAt(0) != '/') {59throw new IllegalArgumentException ("Illegal value for path or protocol");60}61this.protocol = protocol.toLowerCase();62this.path = path;63if (!this.protocol.equals ("http") && !this.protocol.equals ("https")) {64throw new IllegalArgumentException ("Illegal value for protocol");65}66this.handler = cb;67this.server = server;68authfilter = new AuthFilter(null);69sfilters.add (authfilter);70}7172/**73* returns the handler for this context74* @return the HttpHandler for this context75*/76public HttpHandler getHandler () {77return handler;78}7980public void setHandler (HttpHandler h) {81if (h == null) {82throw new NullPointerException ("Null handler parameter");83}84if (handler != null) {85throw new IllegalArgumentException ("handler already set");86}87handler = h;88}8990/**91* returns the path this context was created with92* @return this context's path93*/94public String getPath() {95return path;96}9798/**99* returns the server this context was created with100* @return this context's server101*/102public HttpServer getServer () {103return server.getWrapper();104}105106ServerImpl getServerImpl () {107return server;108}109110/**111* returns the protocol this context was created with112* @return this context's path113*/114public String getProtocol() {115return protocol;116}117118/**119* returns a mutable Map, which can be used to pass120* configuration and other data to Filter modules121* and to the context's exchange handler.122* <p>123* Every attribute stored in this Map will be visible to124* every HttpExchange processed by this context125*/126public Map<String,Object> getAttributes() {127return attributes;128}129130public List<Filter> getFilters () {131return ufilters;132}133134List<Filter> getSystemFilters () {135return sfilters;136}137138public Authenticator setAuthenticator (Authenticator auth) {139Authenticator old = authenticator;140authenticator = auth;141authfilter.setAuthenticator (auth);142return old;143}144145public Authenticator getAuthenticator () {146return authenticator;147}148Logger getLogger () {149return server.getLogger();150}151}152153154