jdk/src/share/classes/com/sun/servicetag/SunConnection.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 10355 a976ff46116b
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10355
diff changeset
     2
 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     4
 *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
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: 1694
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1694
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    10
 *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    15
 * accompanied this code).
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    16
 *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1694
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1694
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1694
diff changeset
    23
 * questions.
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    24
 */
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    25
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    26
package com.sun.servicetag;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    27
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    28
import java.io.*;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    29
import java.net.URISyntaxException;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    30
import java.net.URL;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    31
import java.net.HttpURLConnection;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    32
import java.net.MalformedURLException;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    33
import java.io.OutputStreamWriter;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    34
import java.util.Locale;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    35
import javax.net.ssl.HttpsURLConnection;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    36
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    37
/**
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    38
 * Sun Connection Class for Product Registration.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    39
 *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    40
 * Registration Web Application Interface
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    41
 * 1) POST the product registry to the output stream of the registration
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    42
 *    relay service.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    43
 * 2) Open the webapp URL from a browser with the following parameters:
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    44
 *    registry-urn
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    45
 *    product=jdk
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    46
 *    locale=<default-locale>
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    47
 *    version=<version>
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    48
 *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    49
 * @see https://sn-tools.central.sun.com/twiki/pub/ServiceTags/RegistrationRelayService/
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    50
 *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    51
 */
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    52
class SunConnection {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    53
7165
cf3291ed2016 6985875: Change servicetags registration to refer to Oracle SSO and MSO 5.2 release
mchung
parents: 5506
diff changeset
    54
    private static String JDK_REGISTRATION_URL = "https://hs-ws1.oracle.com/";
cf3291ed2016 6985875: Change servicetags registration to refer to Oracle SSO and MSO 5.2 release
mchung
parents: 5506
diff changeset
    55
    private static String SANDBOX_TESTING_URL = "https://hs-ws1-tst.oracle.com/";
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    56
    private static String REGISTRATION_WEB_PATH = "RegistrationWeb/register";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    57
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    58
    // System properties for testing
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    59
    private static String SVCTAG_REGISTER_TESTING = "servicetag.register.testing";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    60
    private static String SVCTAG_REGISTRATION_URL = "servicetag.registration.url";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    61
    private static String SVCTAG_CONNECTION_TIMEOUT = "servicetag.connection.timeout";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    62
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    63
    private SunConnection() {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    64
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    65
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    66
    /**
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    67
     * Returns a URL for JDK registration interfacing with the Sun Connection
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    68
     * registration relay service in this form:
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    69
     *   <registration-url>/<registry_urn>?product=jdk&locale=<locale>
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    70
     *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    71
     * The <registration-url> can be overridden by an environment
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    72
     * variable or a system property.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    73
     *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    74
     * 1) "servicetag.register.testing" system property to switch to the
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    75
     *    Sun Connection registration sandbox testing.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    76
     * 2) "servicetag.registration.url" system property to override
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    77
     *    the URL
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    78
     * 3) Default production URL
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    79
     *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    80
     */
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    81
    static URL getRegistrationURL(String registrationURN, Locale locale, String version) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    82
        String url = System.getProperty(SVCTAG_REGISTRATION_URL);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    83
        if (url == null) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    84
            if (System.getProperty(SVCTAG_REGISTER_TESTING) != null) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    85
                url = SANDBOX_TESTING_URL;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    86
            } else {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    87
                url = JDK_REGISTRATION_URL;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    88
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    89
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    90
        url += REGISTRATION_WEB_PATH;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    91
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    92
        // trim whitespaces
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    93
        url = url.trim();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    94
        if (url.length() == 0) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    95
            throw new InternalError("Empty registration url set");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    96
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    97
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    98
        // Add the registry_urn in the URL's query
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    99
        String registerURL = rewriteURL(url, registrationURN, locale, version);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   100
        try {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   101
            return new URL(registerURL);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   102
        } catch (MalformedURLException ex) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   103
            // should never reach here
10355
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 7668
diff changeset
   104
            throw new InternalError(ex.getMessage(), ex);
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   105
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   106
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   107
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   108
    private static String rewriteURL(String url, String registryURN, Locale locale, String version) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   109
        StringBuilder sb = new StringBuilder(url.trim());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   110
        int len = sb.length();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   111
        if (sb.charAt(len-1) != '/') {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   112
            sb.append('/');
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   113
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   114
        sb.append(registryURN);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   115
        sb.append("?");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   116
        sb.append("product=jdk");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   117
        sb.append("&");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   118
        sb.append("locale=").append(locale.toString());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   119
        sb.append("&");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   120
        sb.append("version=").append(version);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   121
        return sb.toString();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   122
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   123
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   124
    /**
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   125
     * Registers all products in the given product registry.  If it fails
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   126
     * to post the service tag registry, open the browser with the offline
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   127
     * registration page.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   128
     *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   129
     * @param regData registration data to be posted to the Sun Connection
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   130
     *             for registration.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   131
     * @param locale Locale
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   132
     * @param version JDK version
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   133
     *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   134
     * @throws IOException if I/O error occurs in this operation
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   135
     */
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   136
    public static void register(RegistrationData regData,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   137
                                Locale locale,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   138
                                String version) throws IOException {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   139
        // Gets the URL for SunConnection registration relay service
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   140
        URL url = getRegistrationURL(regData.getRegistrationURN(),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   141
                                     locale,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   142
                                     version);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   143
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   144
        // Post the Product Registry to Sun Connection
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   145
        boolean succeed = postRegistrationData(url, regData);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   146
        if (succeed) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   147
            // service tags posted successfully
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   148
            // now prompt for registration
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   149
            openBrowser(url);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   150
        } else {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   151
            // open browser with the offline registration page
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   152
            openOfflineRegisterPage();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   153
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   154
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   155
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   156
    /**
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   157
     * Opens a browser for JDK product registration.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   158
     * @param url Registration Webapp URL
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   159
     */
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   160
    private static void openBrowser(URL url) throws IOException {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   161
        if (!BrowserSupport.isSupported()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   162
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   163
                System.out.println("Browser is not supported");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   164
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   165
            return;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   166
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   167
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   168
        try {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   169
            BrowserSupport.browse(url.toURI());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   170
        } catch (URISyntaxException ex) {
10355
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 7668
diff changeset
   171
            throw new InternalError("Error in registering: " + ex.getMessage(), ex);
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   172
        } catch (IllegalArgumentException ex) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   173
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   174
                ex.printStackTrace();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   175
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   176
        } catch (UnsupportedOperationException ex) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   177
            // ignore if not supported
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   178
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   179
                ex.printStackTrace();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   180
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   181
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   182
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   183
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   184
    /**
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   185
     * POST service tag registry to Sun Connection
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   186
     * @param loc the URL of the webapp to handle the POST request
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   187
     * @param streg the Service Tag registry
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   188
     * @return true if posting succeeds; otherwise, false.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   189
     */
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   190
    private static boolean postRegistrationData(URL url,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   191
                                                RegistrationData registration) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   192
        try {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   193
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   194
            con.setDoInput(true);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   195
            con.setDoOutput(true);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   196
            con.setUseCaches(false);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   197
            con.setAllowUserInteraction(false);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   198
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   199
            // default 10 seconds timeout
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   200
            String timeout = System.getProperty(SVCTAG_CONNECTION_TIMEOUT, "10");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   201
            con.setConnectTimeout(Util.getIntValue(timeout) * 1000);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   202
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   203
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   204
                System.out.println("Connecting to post registration data at " + url);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   205
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   206
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   207
            con.setRequestMethod("POST");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   208
            con.setRequestProperty("Content-Type", "text/xml;charset=\"utf-8\"");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   209
            con.connect();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   210
1694
3fae9f25fa1e 6764062: Revise usage of java.io.*.close
mchung
parents: 1327
diff changeset
   211
            OutputStream out = null;
3fae9f25fa1e 6764062: Revise usage of java.io.*.close
mchung
parents: 1327
diff changeset
   212
            try {
3fae9f25fa1e 6764062: Revise usage of java.io.*.close
mchung
parents: 1327
diff changeset
   213
                out = con.getOutputStream();
3fae9f25fa1e 6764062: Revise usage of java.io.*.close
mchung
parents: 1327
diff changeset
   214
                registration.storeToXML(out);
3fae9f25fa1e 6764062: Revise usage of java.io.*.close
mchung
parents: 1327
diff changeset
   215
                out.flush();
3fae9f25fa1e 6764062: Revise usage of java.io.*.close
mchung
parents: 1327
diff changeset
   216
            } finally {
3fae9f25fa1e 6764062: Revise usage of java.io.*.close
mchung
parents: 1327
diff changeset
   217
                if (out != null) {
3fae9f25fa1e 6764062: Revise usage of java.io.*.close
mchung
parents: 1327
diff changeset
   218
                    out.close();
3fae9f25fa1e 6764062: Revise usage of java.io.*.close
mchung
parents: 1327
diff changeset
   219
                }
3fae9f25fa1e 6764062: Revise usage of java.io.*.close
mchung
parents: 1327
diff changeset
   220
            }
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   221
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   222
            int returnCode = con.getResponseCode();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   223
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   224
                System.out.println("POST return status = " + returnCode);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   225
                printReturnData(con, returnCode);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   226
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   227
            return (returnCode == HttpURLConnection.HTTP_OK);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   228
        } catch (MalformedURLException me) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   229
            // should never reach here
10355
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 7668
diff changeset
   230
            throw new InternalError("Error in registering: " + me.getMessage(), me);
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   231
        } catch (Exception ioe) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   232
            // SocketTimeoutException, IOException or UnknownHostException
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   233
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   234
                ioe.printStackTrace();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   235
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   236
            return false;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   237
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   238
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   239
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   240
    /**
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   241
     * Opens the offline registratioin page in the browser.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   242
     *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   243
     */
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   244
    private static void openOfflineRegisterPage()
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   245
            throws IOException {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   246
        if (!BrowserSupport.isSupported()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   247
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   248
                System.out.println("Browser is not supported");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   249
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   250
            return;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   251
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   252
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   253
        File registerPage = Installer.getRegistrationHtmlPage();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   254
        try {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   255
            BrowserSupport.browse(registerPage.toURI());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   256
        } catch (FileNotFoundException ex) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   257
            // should never reach here
10355
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 7668
diff changeset
   258
            throw new InternalError(
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 7668
diff changeset
   259
                    "Error in launching " + registerPage + ": " + ex.getMessage()
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 7668
diff changeset
   260
                    , ex);
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   261
        } catch (IllegalArgumentException ex) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   262
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   263
                ex.printStackTrace();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   264
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   265
        } catch (UnsupportedOperationException ex) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   266
            // ignore if not supported
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   267
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   268
                ex.printStackTrace();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   269
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   270
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   271
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   272
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   273
    private static void printReturnData(HttpURLConnection con, int returnCode)
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   274
            throws IOException {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   275
        BufferedReader reader = null;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   276
        try {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   277
            if (returnCode < 400) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   278
                reader = new BufferedReader(
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   279
                             new InputStreamReader(con.getInputStream()));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   280
            } else {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   281
                reader = new BufferedReader(
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   282
                             new InputStreamReader(con.getErrorStream()));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   283
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   284
            StringBuilder sb = new StringBuilder();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   285
            String line;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   286
            while ((line = reader.readLine()) != null) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   287
                sb.append(line).append("\n");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   288
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   289
            System.out.println("Response is : ");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   290
            System.out.println(sb.toString());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   291
        } finally {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   292
            if (reader != null) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   293
                reader.close();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   294
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   295
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   296
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   297
}