jdk/src/share/classes/com/sun/servicetag/SunConnection.java
author mchung
Thu, 02 Oct 2008 16:50:41 -0700
changeset 1327 912486b03832
child 1694 3fae9f25fa1e
permissions -rw-r--r--
6581243: Service Tag and Product Registration Support in JDK 7 Summary: Include service tag creation and product registration support in JDK Reviewed-by: 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
/*
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
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
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
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
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    23
 * have any questions.
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
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    54
    private static String JDK_REGISTRATION_URL = "https://inventory.sun.com/";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    55
    private static String SANDBOX_TESTING_URL = "https://inventory-beta.sun.com/";
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
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   104
            InternalError x =
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   105
                new InternalError(ex.getMessage());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   106
            x.initCause(ex);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   107
            throw x;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   108
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   109
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   110
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   111
    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
   112
        StringBuilder sb = new StringBuilder(url.trim());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   113
        int len = sb.length();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   114
        if (sb.charAt(len-1) != '/') {
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
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   117
        sb.append(registryURN);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   118
        sb.append("?");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   119
        sb.append("product=jdk");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   120
        sb.append("&");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   121
        sb.append("locale=").append(locale.toString());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   122
        sb.append("&");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   123
        sb.append("version=").append(version);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   124
        return sb.toString();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   125
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   126
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   127
    /**
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   128
     * 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
   129
     * 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
   130
     * registration page.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   131
     *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   132
     * @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
   133
     *             for registration.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   134
     * @param locale Locale
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   135
     * @param version JDK version
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   136
     *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   137
     * @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
   138
     */
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   139
    public static void register(RegistrationData regData,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   140
                                Locale locale,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   141
                                String version) throws IOException {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   142
        // Gets the URL for SunConnection registration relay service
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   143
        URL url = getRegistrationURL(regData.getRegistrationURN(),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   144
                                     locale,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   145
                                     version);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   146
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   147
        // Post the Product Registry to Sun Connection
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   148
        boolean succeed = postRegistrationData(url, regData);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   149
        if (succeed) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   150
            // service tags posted successfully
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   151
            // now prompt for registration
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   152
            openBrowser(url);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   153
        } else {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   154
            // open browser with the offline registration page
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   155
            openOfflineRegisterPage();
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
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   158
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
     * Opens a browser for JDK product registration.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   161
     * @param url Registration Webapp URL
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   162
     */
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   163
    private static void openBrowser(URL url) throws IOException {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   164
        if (!BrowserSupport.isSupported()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   165
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   166
                System.out.println("Browser is not supported");
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
            return;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   169
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   170
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   171
        try {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   172
            BrowserSupport.browse(url.toURI());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   173
        } catch (URISyntaxException ex) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   174
            InternalError x = new InternalError("Error in registering: " + ex.getMessage());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   175
            x.initCause(ex);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   176
            throw x;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   177
        } catch (IllegalArgumentException ex) {
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
        } catch (UnsupportedOperationException ex) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   182
            // ignore if not supported
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   183
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   184
                ex.printStackTrace();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   185
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   186
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   187
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   188
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
     * POST service tag registry to Sun Connection
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   191
     * @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
   192
     * @param streg the Service Tag registry
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   193
     * @return true if posting succeeds; otherwise, false.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   194
     */
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   195
    private static boolean postRegistrationData(URL url,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   196
                                                RegistrationData registration) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   197
        try {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   198
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   199
            con.setDoInput(true);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   200
            con.setDoOutput(true);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   201
            con.setUseCaches(false);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   202
            con.setAllowUserInteraction(false);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   203
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   204
            // default 10 seconds timeout
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   205
            String timeout = System.getProperty(SVCTAG_CONNECTION_TIMEOUT, "10");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   206
            con.setConnectTimeout(Util.getIntValue(timeout) * 1000);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   207
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   208
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   209
                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
   210
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   211
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   212
            con.setRequestMethod("POST");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   213
            con.setRequestProperty("Content-Type", "text/xml;charset=\"utf-8\"");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   214
            con.connect();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   215
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   216
            OutputStream out = con.getOutputStream();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   217
            registration.storeToXML(out);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   218
            out.flush();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   219
            out.close();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   220
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   221
            int returnCode = con.getResponseCode();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   222
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   223
                System.out.println("POST return status = " + returnCode);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   224
                printReturnData(con, returnCode);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   225
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   226
            return (returnCode == HttpURLConnection.HTTP_OK);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   227
        } catch (MalformedURLException me) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   228
            // should never reach here
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   229
            InternalError x = new InternalError("Error in registering: " + me.getMessage());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   230
            x.initCause(me);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   231
            throw x;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   232
        } catch (Exception ioe) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   233
            // SocketTimeoutException, IOException or UnknownHostException
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   234
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   235
                ioe.printStackTrace();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   236
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   237
            return false;
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
    /**
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   242
     * Opens the offline registratioin page in the browser.
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
     */
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   245
    private static void openOfflineRegisterPage()
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   246
            throws IOException {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   247
        if (!BrowserSupport.isSupported()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   248
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   249
                System.out.println("Browser is not supported");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   250
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   251
            return;
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
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   254
        File registerPage = Installer.getRegistrationHtmlPage();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   255
        try {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   256
            BrowserSupport.browse(registerPage.toURI());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   257
        } catch (FileNotFoundException ex) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   258
            // should never reach here
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   259
            InternalError x =
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   260
                new InternalError("Error in launching " + registerPage + ": " + ex.getMessage());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   261
            x.initCause(ex);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   262
            throw x;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   263
        } catch (IllegalArgumentException ex) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   264
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   265
                ex.printStackTrace();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   266
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   267
        } catch (UnsupportedOperationException ex) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   268
            // ignore if not supported
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   269
            if (Util.isVerbose()) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   270
                ex.printStackTrace();
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
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   274
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   275
    private static void printReturnData(HttpURLConnection con, int returnCode)
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   276
            throws IOException {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   277
        BufferedReader reader = null;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   278
        try {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   279
            if (returnCode < 400) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   280
                reader = new BufferedReader(
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   281
                             new InputStreamReader(con.getInputStream()));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   282
            } else {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   283
                reader = new BufferedReader(
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   284
                             new InputStreamReader(con.getErrorStream()));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   285
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   286
            StringBuilder sb = new StringBuilder();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   287
            String line;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   288
            while ((line = reader.readLine()) != null) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   289
                sb.append(line).append("\n");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   290
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   291
            System.out.println("Response is : ");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   292
            System.out.println(sb.toString());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   293
        } finally {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   294
            if (reader != null) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   295
                reader.close();
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
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   298
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   299
}