/* * Copyright 2000-2014 Vaadin Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.vaadin.tools; import static java.lang.Integer.parseInt; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.text.MessageFormat; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.ResourceBundle; import java.util.prefs.Preferences; import org.apache.commons.io.IOUtils; import elemental.json.JsonException; import elemental.json.JsonNull; import elemental.json.JsonObject; import elemental.json.impl.JsonUtil; /** * This class is able to validate the vaadin CVAL license. * * It reads the developer license file and asks the server to validate the * licenseKey. If the license is invalid it throws an exception with the * information about the problem and the server response. * * @since 7.3 */ public final class CvalChecker { /* * Class used for binding the JSON gotten from server. * * It is not in a separate f le, so as it is easier to copy into any product * which does not depend on vaadin core. * * We are using elemental.json in order not to use additional dependency * like auto-beans, gson, etc. */ public static class CvalInfo { public static class Product { private JsonObject o; public Product(JsonObject o) { this.o = o; } public String getName() { return get(o, "name", String.class); } public Integer getVersion() { return get(o, "version", Integer.class); } } @SuppressWarnings("unchecked") private static T get(JsonObject o, String k, Class clz) { Object ret = null; try { if (o == null || o.get(k) == null || o.get(k) instanceof JsonNull) { return null; } if (clz == String.class) { ret = o.getString(k); } else if (clz == JsonObject.class) { ret = o.getObject(k); } else if (clz == Integer.class) { ret = Integer.valueOf((int) o.getNumber(k)); } else if (clz == Date.class) { ret = new Date((long) o.getNumber(k)); } else if (clz == Boolean.class) { ret = o.getBoolean(k); } } catch (JsonException e) { } return (T) ret; } private JsonObject o; private Product product; public CvalInfo(JsonObject o) { this.o = o; product = new Product(get(o, "product", JsonObject.class)); } public Boolean getExpired() { return get(o, "expired", Boolean.class); } public Date getExpiredEpoch() { return get(o, "expiredEpoch", Date.class); } public String getLicensee() { return get(o, "licensee", String.class); } public String getLicenseKey() { return get(o, "licenseKey", String.class); } public String getMessage() { return get(o, "message", String.class); } public Product getProduct() { return product; } public String getType() { return get(o, "type", String.class); } public void setExpiredEpoch(Date expiredEpoch) { o.put("expiredEpoch", expiredEpoch.getTime()); } public void setMessage(String msg) { o.put("message", msg); } @Override public String toString() { return o.toString(); } public boolean isLicenseExpired() { return (getExpired() != null && getExpired()) || (getExpiredEpoch() != null && getExpiredEpoch().before( new Date())); } public boolean isValidVersion(int majorVersion) { return getProduct().getVersion() == null || getProduct().getVersion() >= majorVersion; } private boolean isValidInfo(String name, String key) { return getProduct() != null && getProduct().getName() != null && getLicenseKey() != null && getProduct().getName().equals(name) && getLicenseKey().equals(key); } } /* * The class with the method for getting json from server side. It is here * and protected just for replacing it in tests. */ public static class CvalServer { protected String licenseUrl = LICENSE_URL_PROD; String askServer(String productName, String productKey, int timeoutMs) throws IOException { String url = licenseUrl + productKey; URLConnection con; try { // Send some additional info in the User-Agent string. String ua = "Cval " + productName + " " + productKey + " " + getFirstLaunch(); for (String prop : Arrays.asList("java.vendor.url", "java.version", "os.name", "os.version", "os.arch")) { ua += " " + System.getProperty(prop, "-").replace(" ", "_"); } con = new URL(url).openConnection(); con.setRequestProperty("User-Agent", ua); con.setConnectTimeout(timeoutMs); con.setReadTimeout(timeoutMs); String r = IOUtils.toString(con.getInputStream()); return r; } catch (MalformedURLException e) { e.printStackTrace(); return null; } } /* * Get the GWT firstLaunch timestamp. */ String getFirstLaunch() { try { Class clz = Class .forName("com.google.gwt.dev.shell.CheckForUpdates"); return Preferences.userNodeForPackage(clz).get("firstLaunch", "-"); } catch (ClassNotFoundException e) { return "-"; } } } /** * Exception thrown when the user does not have a valid cval license. */ public static class InvalidCvalException extends Exception { private static final long serialVersionUID = 1L; public final CvalInfo info; public final String name; public final String key; public final String version; public final String title; public InvalidCvalException(String name, String version, String title, String key, CvalInfo info) { super(composeMessage(title, version, key, info)); this.info = info; this.name = name; this.key = key; this.version = version; this.title = title; } static String composeMessage(String title, String version, String key, CvalInfo info) { String msg = ""; int majorVers = computeMajorVersion(version); if (info != null && !info.isValidVersion(majorVers)) { msg = getErrorMessage("invalid", title, majorVers); } else if (info != null && info.getMessage() != null) { msg = info.getMessage().replace("\\n", "\n"); } else if (info != null && info.isLicenseExpired()) { String type = "evaluation".equals(info.getType()) ? "Evaluation license" : "License"; msg = getErrorMessage("expired", title, majorVers, type); } else if (key == null) { msg = getErrorMessage("none", title, majorVers); } else { msg = getErrorMessage("invalid", title, majorVers); } return msg; } } /** * Exception thrown when the license server is unreachable */ public st
/*
 * jQuery UI Progressbar @VERSION
 *
 * Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * http://docs.jquery.com/UI/Progressbar
 *
 * Depends:
 *   jquery.ui.core.js
 *   jquery.ui.widget.js
 */
(function( $, undefined ) {

$.widget( "ui.progressbar", {
	version: "@VERSION",
	options: {
		value: 0,
		max: 100
	},

	min: 0,

	_create: function() {
		this.element
			.addClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
			.attr({
				role: "progressbar",
				"aria-valuemin": this.min,
				"aria-valuemax": this.options.max,
				"aria-valuenow": this._value()
			});

		this.valueDiv = $( "<div class='ui-progressbar-value ui-widget-header ui-corner-left'></div>" )
			.appendTo( this.element );

		this.oldValue = this._value();
		this._refreshValue();
	},

	_destroy: function() {
		this.element
			.removeClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
			.removeAttr( "role" )
			.removeAttr( "aria-valuemin" )
			.removeAttr( "aria-valuemax" )
			.removeAttr( "aria-valuenow" );

		this.valueDiv.remove();
	},

	value: function( newValue ) {
		if ( newValue === undefined ) {
			return this._value();
		}

		this._setOption( "value", newValue );
		return this;
	},

	_setOption: function( key, value ) {
		if ( key === "value" ) {
			this.options.value = value;
			this._refreshValue();
			if ( this._value() === this.options.max ) {
				this._trigger( "complete" );
			}
		}

		this._super( "_setOption", key, value );
	},

	_value: function() {
		var val = this.options.value;
		// normalize invalid value
		if ( typeof val !== "number" ) {
			val = 0;
		}
		return Math.min( this.options.max, Math.max( this.min, val ) );
	},

	_percentage: function() {
		return 100 * this._value() / this.options.max;
	},

	_refreshValue: function() {
		var value = this.value();
		var percentage = this._percentage();

		if ( this.oldValue !== value ) {
			this.oldValue = value;
			this._trigger( "change" );
		}

		this.valueDiv
			.toggle( value > this.min )
			.toggleClass( "ui-corner-right", value === this.options.max )
			.width( percentage.toFixed(0) + "%" );
		this.element.attr( "aria-valuenow", value );
	}
});

})( jQuery );