1/*
2 * Copyright (c) 2009, 2016, 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.www.protocol.http;
27
28import java.net.Authenticator;
29import java.net.Authenticator.RequestorType;
30import java.net.InetAddress;
31import java.net.URL;
32
33/**
34 * Used in HTTP/Negotiate, to feed HTTP request info into JGSS as a HttpCaller,
35 * so that special actions can be taken, including special callback handler,
36 * special useSubjectCredsOnly value.
37 *
38 * This is an immutable class. It can be instantiated in two styles;
39 *
40 * 1. Un-schemed: Create at the beginning before the preferred scheme is
41 * determined. This object can be fed into AuthenticationHeader to check
42 * for the preference.
43 *
44 * 2. Schemed: With the scheme field filled, can be used in JGSS-API calls.
45 */
46public final class HttpCallerInfo {
47    // All info that an Authenticator needs.
48    public final URL url;
49    public final String host, protocol, prompt, scheme;
50    public final int port;
51    public final InetAddress addr;
52    public final RequestorType authType;
53    public final Authenticator authenticator;
54
55    /**
56     * Create a schemed object based on an un-schemed one.
57     */
58    public HttpCallerInfo(HttpCallerInfo old, String scheme) {
59        this.url = old.url;
60        this.host = old.host;
61        this.protocol = old.protocol;
62        this.prompt = old.prompt;
63        this.port = old.port;
64        this.addr = old.addr;
65        this.authType = old.authType;
66        this.scheme = scheme;
67        this.authenticator =  old.authenticator;
68    }
69
70    /**
71     * Constructor an un-schemed object for site access.
72     */
73    public HttpCallerInfo(URL url, Authenticator a) {
74        this.url= url;
75        prompt = "";
76        host = url.getHost();
77
78        int p = url.getPort();
79        if (p == -1) {
80            port = url.getDefaultPort();
81        } else {
82            port = p;
83        }
84
85        InetAddress ia;
86        try {
87            ia = InetAddress.getByName(url.getHost());
88        } catch (Exception e) {
89            ia = null;
90        }
91        addr = ia;
92
93        protocol = url.getProtocol();
94        authType = RequestorType.SERVER;
95        scheme = "";
96        authenticator = a;
97    }
98
99    /**
100     * Constructor an un-schemed object for proxy access.
101     */
102    public HttpCallerInfo(URL url, String host, int port, Authenticator a) {
103        this.url= url;
104        this.host = host;
105        this.port = port;
106        prompt = "";
107        addr = null;
108        protocol = url.getProtocol();
109        authType = RequestorType.PROXY;
110        scheme = "";
111        authenticator = a;
112    }
113}
114