001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package com.sun.servicetag;
043:
044: import java.io.*;
045: import java.net.URISyntaxException;
046: import java.net.URL;
047: import java.net.HttpURLConnection;
048: import java.net.MalformedURLException;
049: import java.io.OutputStreamWriter;
050: import java.util.Locale;
051: import javax.net.ssl.HttpsURLConnection;
052:
053: /**
054: * Sun Connection Class for Product Registration.
055: *
056: * Registration Web Application Interface
057: * 1) POST the product registry to the output stream of the registration
058: * relay service.
059: * 2) Open the webapp URL from a browser with the following parameters:
060: * registry-urn
061: * product=jdk
062: * locale=<locale-lang>
063: *
064: * @see https://sn-tools.central.sun.com/twiki/pub/ServiceTags/RegistrationRelayService/
065: *
066: */
067: class SunConnection {
068:
069: private static String JDK_REGISTRATION_URL = "https://inventory.sun.com/RegistrationWeb/register";
070: private static String SANDBOX_TESTING_URL = "https://connection-tst.sun.com/RegistrationWeb/register";
071:
072: // System properties for testing
073: private static String SVCTAG_REGISTER_TESTING = "servicetag.register.testing";
074: private static String SVCTAG_REGISTRATION_URL = "servicetag.registration.url";
075: private static String SVCTAG_CONNECTION_TIMEOUT = "servicetag.connection.timeout";
076:
077: private SunConnection() {
078: }
079:
080: /**
081: * Returns a URL for JDK registration interfacing with the Sun Connection
082: * registration relay service in this form:
083: * <registration-url>/<registry_urn>?product=jdk&locale=<locale-lang>
084: *
085: * The <registration-url> can be overridden by an environment
086: * variable or a system property.
087: *
088: * 1) "servicetag.register.testing" system property to switch to the
089: * Sun Connection registration sandbox testing.
090: * 2) "servicetag.registration.url" system property to override
091: * the URL
092: * 3) Default production URL
093: *
094: */
095: static URL getRegistrationURL(String registrationURN) {
096: String url = System.getProperty(SVCTAG_REGISTRATION_URL);
097: if (url == null) {
098: if (System.getProperty(SVCTAG_REGISTER_TESTING) != null) {
099: url = SANDBOX_TESTING_URL;
100: } else {
101: url = JDK_REGISTRATION_URL;
102: }
103: }
104:
105: // trim whitespaces
106: url = url.trim();
107: if (url.length() == 0) {
108: throw new InternalError("Empty registration url set");
109: }
110:
111: // Add the registry_urn in the URL's query
112: String registerURL = rewriteURL(url, registrationURN);
113: try {
114: return new URL(registerURL);
115: } catch (MalformedURLException ex) {
116: // should never reach here
117: InternalError x = new InternalError(ex.getMessage());
118: x.initCause(ex);
119: throw x;
120: }
121: }
122:
123: private static String rewriteURL(String url, String registryURN) {
124: StringBuilder sb = new StringBuilder(url.trim());
125: int len = sb.length();
126: if (sb.charAt(len - 1) != '/') {
127: sb.append('/');
128: }
129: sb.append(registryURN);
130: sb.append("?");
131: sb.append("product=jdk");
132: sb.append("&");
133: sb.append("locale=").append(Locale.getDefault().getLanguage());
134: return sb.toString();
135: }
136:
137: /**
138: * Registers all products in the given product registry. If it fails
139: * to post the service tag registry, open the browser with the offline
140: * registration page.
141: *
142: * @param regData registration data to be posted to the Sun Connection
143: * for registration.
144: *
145: * @throws IOException if I/O error occurs in this operation
146: */
147: public static void register(RegistrationData regData)
148: throws IOException {
149: // Gets the URL for SunConnection registration relay service
150: URL url = getRegistrationURL(regData.getRegistrationURN());
151:
152: // Post the Product Registry to Sun Connection
153: boolean succeed = postRegistrationData(url, regData);
154: if (succeed) {
155: // service tags posted successfully
156: // now prompt for registration
157: openBrowser(url);
158: } else {
159: // open browser with the offline registration page
160: openOfflineRegisterPage();
161: }
162: }
163:
164: /**
165: * Opens a browser for JDK product registration.
166: * @param url Registration Webapp URL
167: */
168: private static void openBrowser(URL url) throws IOException {
169: if (!BrowserSupport.isSupported()) {
170: if (Util.isVerbose()) {
171: System.out.println("Browser is not supported");
172: }
173: return;
174: }
175:
176: try {
177: BrowserSupport.browse(url.toURI());
178: } catch (URISyntaxException ex) {
179: InternalError x = new InternalError(
180: "Error in registering: " + ex.getMessage());
181: x.initCause(ex);
182: throw x;
183: } catch (IllegalArgumentException ex) {
184: if (Util.isVerbose()) {
185: ex.printStackTrace();
186: }
187: } catch (UnsupportedOperationException ex) {
188: // ignore if not supported
189: if (Util.isVerbose()) {
190: ex.printStackTrace();
191: }
192: }
193: }
194:
195: /**
196: * POST service tag registry to Sun Connection
197: * @param loc the URL of the webapp to handle the POST request
198: * @param streg the Service Tag registry
199: * @return true if posting succeeds; otherwise, false.
200: */
201: private static boolean postRegistrationData(URL url,
202: RegistrationData registration) {
203: try {
204: HttpsURLConnection con = (HttpsURLConnection) url
205: .openConnection();
206: con.setDoInput(true);
207: con.setDoOutput(true);
208: con.setUseCaches(false);
209: con.setAllowUserInteraction(false);
210:
211: // default 10 seconds timeout
212: String timeout = System.getProperty(
213: SVCTAG_CONNECTION_TIMEOUT, "10");
214: con.setConnectTimeout(Util.getIntValue(timeout) * 1000);
215:
216: if (Util.isVerbose()) {
217: System.out
218: .println("Connecting to post registration data at "
219: + url);
220: }
221:
222: con.setRequestMethod("POST");
223: con.setRequestProperty("Content-Type",
224: "text/xml;charset=\"utf-8\"");
225: con.connect();
226:
227: OutputStream out = con.getOutputStream();
228: registration.storeToXML(out);
229: out.flush();
230: out.close();
231:
232: int returnCode = con.getResponseCode();
233: if (Util.isVerbose()) {
234: System.out
235: .println("POST return status = " + returnCode);
236: printReturnData(con, returnCode);
237: }
238: return (returnCode == HttpURLConnection.HTTP_OK);
239: } catch (MalformedURLException me) {
240: // should never reach here
241: InternalError x = new InternalError(
242: "Error in registering: " + me.getMessage());
243: x.initCause(me);
244: throw x;
245: } catch (Exception ioe) {
246: // SocketTimeoutException, IOException or UnknownHostException
247: if (Util.isVerbose()) {
248: ioe.printStackTrace();
249: }
250: return false;
251: }
252: }
253:
254: /**
255: * Opens the offline registratioin page in the browser.
256: *
257: */
258: private static void openOfflineRegisterPage() throws IOException {
259: if (!BrowserSupport.isSupported()) {
260: if (Util.isVerbose()) {
261: System.out.println("Browser is not supported");
262: }
263: return;
264: }
265:
266: File registerPage = Installer.getRegistrationHtmlPage();
267: try {
268: BrowserSupport.browse(registerPage.toURI());
269: } catch (FileNotFoundException ex) {
270: // should never reach here
271: InternalError x = new InternalError("Error in launching "
272: + registerPage + ": " + ex.getMessage());
273: x.initCause(ex);
274: throw x;
275: } catch (IllegalArgumentException ex) {
276: if (Util.isVerbose()) {
277: ex.printStackTrace();
278: }
279: } catch (UnsupportedOperationException ex) {
280: // ignore if not supported
281: if (Util.isVerbose()) {
282: ex.printStackTrace();
283: }
284: }
285: }
286:
287: private static void printReturnData(HttpURLConnection con,
288: int returnCode) throws IOException {
289: BufferedReader reader = null;
290: try {
291: if (returnCode < 400) {
292: reader = new BufferedReader(new InputStreamReader(con
293: .getInputStream()));
294: } else {
295: reader = new BufferedReader(new InputStreamReader(con
296: .getErrorStream()));
297: }
298: StringBuilder sb = new StringBuilder();
299: String line;
300: while ((line = reader.readLine()) != null) {
301: sb.append(line).append("\n");
302: }
303: System.out.println("Response is : ");
304: System.out.println(sb.toString());
305: } finally {
306: if (reader != null) {
307: reader.close();
308: }
309: }
310: }
311: }
|