1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.internal.net.www.protocol.http;
19
20import java.io.IOException;
21import java.net.Proxy;
22import java.net.URL;
23import java.net.URLConnection;
24import java.net.URLStreamHandler;
25
26import org.apache.harmony.luni.util.Msg;
27
28/**
29 * This is the handler that manages all transactions between the client and a
30 * HTTP remote server.
31 */
32public class Handler extends URLStreamHandler {
33
34    /**
35     * Returns a connection to the HTTP server specified by this
36     * <code>URL</code>.
37     *
38     * @param u
39     *            the URL to which the connection is pointing to
40     * @return a connection to the resource pointed by this url.
41     *
42     * @throws IOException
43     *             if this handler fails to establish a connection
44     */
45    @Override
46    protected URLConnection openConnection(URL u) throws IOException {
47        return new HttpURLConnectionImpl(u, getDefaultPort());
48    }
49
50    /**
51     * Returns a connection, which is established via the <code>proxy</code>,
52     * to the HTTP server specified by this <code>URL</code>. If the
53     * <code>proxy</code> is DIRECT type, the connection is made in normal
54     * way.
55     *
56     * @param u
57     *            the URL which the connection is pointing to
58     * @param proxy
59     *            the proxy which is used to make the connection
60     * @return a connection to the resource pointed by this url.
61     *
62     * @throws IOException
63     *             if this handler fails to establish a connection.
64     * @throws IllegalArgumentException
65     *             if any argument is null or the type of proxy is wrong.
66     * @throws UnsupportedOperationException
67     *             if the protocol handler doesn't support this method.
68     */
69    @Override
70    protected URLConnection openConnection(URL u, Proxy proxy)
71            throws IOException {
72        if (null == u || null == proxy) {
73            throw new IllegalArgumentException(Msg.getString("K034b")); //$NON-NLS-1$
74        }
75        return new HttpURLConnectionImpl(u, getDefaultPort(), proxy);
76    }
77
78    /**
79     * Return the default port.
80     */
81    @Override
82    protected int getDefaultPort() {
83        return 80;
84    }
85}
86