1/*
2 * Copyright (c) 2005, 2006, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.net.httpserver;
27import java.io.*;
28import java.util.*;
29import java.lang.System.Logger;
30import com.sun.net.httpserver.*;
31import com.sun.net.httpserver.spi.*;
32
33/**
34 * HttpContext represents a mapping between a protocol (http or https) together with a root URI path
35 * to a {@link HttpHandler} which is invoked to handle requests destined
36 * for the protocol/path on the associated HttpServer.
37 * <p>
38 * HttpContext instances are created by {@link HttpServer#createContext(String,String,HttpHandler,Object)}
39 * <p>
40 */
41class HttpContextImpl extends HttpContext {
42
43    private String path;
44    private String protocol;
45    private HttpHandler handler;
46    private Map<String,Object> attributes = new HashMap<String,Object>();
47    private ServerImpl server;
48    /* system filters, not visible to applications */
49    private LinkedList<Filter> sfilters = new LinkedList<Filter>();
50    /* user filters, set by applications */
51    private LinkedList<Filter> ufilters = new LinkedList<Filter>();
52    private Authenticator authenticator;
53    private AuthFilter authfilter;
54
55    /**
56     * constructor is package private.
57     */
58    HttpContextImpl (String protocol, String path, HttpHandler cb, ServerImpl server) {
59        if (path == null || protocol == null || path.length() < 1 || path.charAt(0) != '/') {
60            throw new IllegalArgumentException ("Illegal value for path or protocol");
61        }
62        this.protocol = protocol.toLowerCase();
63        this.path = path;
64        if (!this.protocol.equals ("http") && !this.protocol.equals ("https")) {
65            throw new IllegalArgumentException ("Illegal value for protocol");
66        }
67        this.handler = cb;
68        this.server = server;
69        authfilter = new AuthFilter(null);
70        sfilters.add (authfilter);
71    }
72
73    /**
74     * returns the handler for this context
75     * @return the HttpHandler for this context
76     */
77    public HttpHandler getHandler () {
78        return handler;
79    }
80
81    public void setHandler (HttpHandler h) {
82        if (h == null) {
83            throw new NullPointerException ("Null handler parameter");
84        }
85        if (handler != null) {
86            throw new IllegalArgumentException ("handler already set");
87        }
88        handler = h;
89    }
90
91    /**
92     * returns the path this context was created with
93     * @return this context's path
94     */
95    public String getPath() {
96        return path;
97    }
98
99    /**
100     * returns the server this context was created with
101     * @return this context's server
102     */
103    public HttpServer getServer () {
104        return server.getWrapper();
105    }
106
107    ServerImpl getServerImpl () {
108        return server;
109    }
110
111    /**
112     * returns the protocol this context was created with
113     * @return this context's path
114     */
115    public String getProtocol() {
116        return protocol;
117    }
118
119    /**
120     * returns a mutable Map, which can be used to pass
121     * configuration and other data to Filter modules
122     * and to the context's exchange handler.
123     * <p>
124     * Every attribute stored in this Map will be visible to
125     * every HttpExchange processed by this context
126     */
127    public Map<String,Object> getAttributes() {
128        return attributes;
129    }
130
131    public List<Filter> getFilters () {
132        return ufilters;
133    }
134
135    List<Filter> getSystemFilters () {
136        return sfilters;
137    }
138
139    public Authenticator setAuthenticator (Authenticator auth) {
140        Authenticator old = authenticator;
141        authenticator = auth;
142        authfilter.setAuthenticator (auth);
143        return old;
144    }
145
146    public Authenticator getAuthenticator () {
147        return authenticator;
148    }
149    Logger getLogger () {
150        return server.getLogger();
151    }
152}
153