components/visual-panels/apache/src/java/vpanels/app/apache/com/oracle/solaris/vp/panels/apache/client/swing/VirtualHost.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panels.apache.client.swing;
       
    27 
       
    28 import java.util.List;
       
    29 import javax.swing.*;
       
    30 import com.oracle.solaris.rad.client.RadObjectException;
       
    31 import com.oracle.solaris.scf.common.ScfException;
       
    32 import com.oracle.solaris.vp.panel.common.model.*;
       
    33 import com.oracle.solaris.vp.panel.common.smf.*;
       
    34 import com.oracle.solaris.vp.util.misc.*;
       
    35 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    36 import com.oracle.solaris.vp.util.misc.property.MutableProperty;
       
    37 import com.oracle.solaris.vp.util.swing.HasIcon;
       
    38 
       
    39 @SuppressWarnings({"serial"})
       
    40 public class VirtualHost extends AbstractManagedObject<ManagedObject>
       
    41     implements HasIcon, Cloneable, Enableable, ApacheInfo {
       
    42 
       
    43     //
       
    44     // Static data
       
    45     //
       
    46 
       
    47     protected static final List<ImageIcon> ICONS_ENABLED = Finder.getIcons(
       
    48 	"images/vhost-enabled-16.png",
       
    49 	"images/vhost-enabled-24.png",
       
    50 	"images/vhost-enabled-32.png");
       
    51 
       
    52     protected static final List<ImageIcon> ICONS_DISABLED = Finder.getIcons(
       
    53 	"images/vhost-disabled-16.png",
       
    54 	"images/vhost-disabled-24.png",
       
    55 	"images/vhost-disabled-32.png");
       
    56 
       
    57     public static final String SMF_GROUP_PREFIX = "vhost-";
       
    58 
       
    59     public static final String PROPERTY_CUSTOM_ENABLED = "custom_conf";
       
    60     public static final String PROPERTY_CUSTOM_FILE = "custom_file";
       
    61     public static final String PROPERTY_DOCROOT = "docroot";
       
    62     public static final String PROPERTY_DOMAIN = "domain";
       
    63     public static final String PROPERTY_ENABLED = "enabled";
       
    64     public static final String PROPERTY_PORT = "port";
       
    65     public static final String PROPERTY_SERVE_HOME_DIRS = "serve_home_dir";
       
    66     public static final String PROPERTY_SSL_CERT = "sslcert";
       
    67     public static final String PROPERTY_SSL_ENABLED = "sslengine";
       
    68     public static final String PROPERTY_SSL_IP = "sslip";
       
    69     public static final String PROPERTY_SSL_KEY = "sslkey";
       
    70     public static final String PROPERTY_SSL_PORT = "sslport";
       
    71 
       
    72     //
       
    73     // Instance data
       
    74     //
       
    75 
       
    76     private ApachePanelDescriptor parent;
       
    77     private String group;
       
    78     private MimeTypes mimeTypes;
       
    79 
       
    80     private BasicSmfMutableProperty<Boolean> customEnabledProperty =
       
    81 	new BooleanSmfProperty(PROPERTY_CUSTOM_ENABLED, this);
       
    82 
       
    83     private BasicSmfMutableProperty<String> customFileProperty =
       
    84 	new StringSmfProperty(PROPERTY_CUSTOM_FILE, this);
       
    85 
       
    86     private BasicSmfMutableProperty<String> docRootProperty =
       
    87 	new StringSmfProperty(PROPERTY_DOCROOT, this);
       
    88 
       
    89     private BasicSmfMutableProperty<String> domainProperty =
       
    90 	new StringSmfProperty(PROPERTY_DOMAIN, this);
       
    91 
       
    92     private BasicSmfMutableProperty<Boolean> enabledProperty =
       
    93 	new BooleanSmfProperty(PROPERTY_ENABLED, this);
       
    94 
       
    95     private BasicSmfMutableProperty<Integer> portProperty =
       
    96 	new CountSmfProperty(PROPERTY_PORT, this);
       
    97 
       
    98     private BasicSmfMutableProperty<Boolean> serveHomeDirsProperty =
       
    99 	new BooleanSmfProperty(PROPERTY_SERVE_HOME_DIRS, this);
       
   100 
       
   101     private BasicSmfMutableProperty<String> sslCertProperty =
       
   102 	new StringSmfProperty(PROPERTY_SSL_CERT, this);
       
   103 
       
   104     private BasicSmfMutableProperty<Boolean> sslEnabledProperty =
       
   105 	new BooleanSmfProperty(PROPERTY_SSL_ENABLED, this);
       
   106 
       
   107     private BasicSmfMutableProperty<String> sslIpProperty =
       
   108 	new StringSmfProperty(PROPERTY_SSL_IP, this);
       
   109 
       
   110     private BasicSmfMutableProperty<String> sslKeyProperty =
       
   111 	new StringSmfProperty(PROPERTY_SSL_KEY, this);
       
   112 
       
   113     private BasicSmfMutableProperty<Integer> sslPortProperty =
       
   114 	new CountSmfProperty(PROPERTY_SSL_PORT, this);
       
   115 
       
   116     //
       
   117     // Constructors
       
   118     //
       
   119 
       
   120     public VirtualHost(ApachePanelDescriptor parent) {
       
   121 	this.parent = parent;
       
   122 
       
   123 	customEnabledProperty.setFirstValue(false);
       
   124 	addProperties(customEnabledProperty);
       
   125 
       
   126 	customFileProperty.setFirstValue("");
       
   127 	addProperties(customFileProperty);
       
   128 
       
   129 	docRootProperty.setFirstValue("");
       
   130 	addProperties(docRootProperty);
       
   131 
       
   132 	domainProperty.setFirstValue("");
       
   133 	addProperties(domainProperty);
       
   134 
       
   135 	enabledProperty.setFirstValue(false);
       
   136 	addProperties(enabledProperty);
       
   137 
       
   138 	portProperty.setFirstValue(80);
       
   139 	addProperties(portProperty);
       
   140 
       
   141 	serveHomeDirsProperty.setFirstValue(false);
       
   142 	addProperties(serveHomeDirsProperty);
       
   143 
       
   144 	sslCertProperty.setFirstValue("");
       
   145 	addProperties(sslCertProperty);
       
   146 
       
   147 	sslEnabledProperty.setFirstValue(false);
       
   148 	addProperties(sslEnabledProperty);
       
   149 
       
   150 	String ip = "";
       
   151 	try {
       
   152 	    ip = getParent().getClientContext().getConnectionInfo().
       
   153 		getInetAddresses()[0].getHostAddress();
       
   154 	} catch (Throwable ignore) {
       
   155 	}
       
   156 
       
   157 	sslIpProperty.setFirstValue(ip);
       
   158 	addProperties(sslIpProperty);
       
   159 
       
   160 	sslKeyProperty.setFirstValue("");
       
   161 	addProperties(sslKeyProperty);
       
   162 
       
   163 	sslPortProperty.setFirstValue(443);
       
   164 	addProperties(sslPortProperty);
       
   165 
       
   166 	ChangeableAggregator aggregator = getChangeableAggregator();
       
   167 
       
   168 	addChildren(mimeTypes = new MimeTypes(this));
       
   169 	aggregator.addChangeables(mimeTypes.getChangeableAggregator());
       
   170     }
       
   171 
       
   172     public VirtualHost(ApachePanelDescriptor parent, String group)
       
   173 	throws ScfException {
       
   174 
       
   175 	this(parent);
       
   176 	this.group = group;
       
   177 	refresh(true);
       
   178     }
       
   179 
       
   180     //
       
   181     // ManagedObject methods
       
   182     //
       
   183 
       
   184     @Override
       
   185     public String getId() {
       
   186 	return getPropertyGroupName();
       
   187     }
       
   188 
       
   189     @Override
       
   190     public String getName() {
       
   191 	return domainProperty.getFirstValue();
       
   192     }
       
   193 
       
   194     @Override
       
   195     public String getStatusText() {
       
   196 	return docRootProperty.getFirstValue();
       
   197     }
       
   198 
       
   199     //
       
   200     // HasIcon methods
       
   201     //
       
   202 
       
   203     @Override
       
   204     public Icon getIcon(int height) {
       
   205 	return IconUtil.getClosestIcon(isEnabled() ?
       
   206 	    ICONS_ENABLED : ICONS_DISABLED, height);
       
   207     }
       
   208 
       
   209     //
       
   210     // HasService methods
       
   211     //
       
   212 
       
   213     @Override
       
   214     public AggregatedRefreshService getService() {
       
   215 	return parent.getService();
       
   216     }
       
   217 
       
   218     //
       
   219     // SmfPropertyGroupInfo methods
       
   220     //
       
   221 
       
   222     @Override
       
   223     public String getPropertyGroupName() {
       
   224 	if (group == null) {
       
   225 	    group = parent.getVirtualHostNamePool().get();
       
   226 	}
       
   227 
       
   228 	return group;
       
   229     }
       
   230 
       
   231     //
       
   232     // SmfPropertyInfo methods
       
   233     //
       
   234 
       
   235     @Override
       
   236     public String getPropertyName() {
       
   237 	// Use the default property names
       
   238 	return null;
       
   239     }
       
   240 
       
   241     //
       
   242     // ApacheInfo methods
       
   243     //
       
   244 
       
   245     @Override
       
   246     public ApachePanelDescriptor getPanelDescriptor() {
       
   247 	return getParent();
       
   248     }
       
   249 
       
   250     //
       
   251     // Enableable methods
       
   252     //
       
   253 
       
   254     @Override
       
   255     public boolean isEnabled() {
       
   256 	return enabledProperty.getFirstValue();
       
   257     }
       
   258 
       
   259     //
       
   260     // Object methods
       
   261     //
       
   262 
       
   263     @Override
       
   264     public VirtualHost clone() {
       
   265 	VirtualHost clone = new VirtualHost(getParent());
       
   266 	for (MutableProperty<?> property : getProperties()) {
       
   267 	    String name = property.getPropertyName();
       
   268 
       
   269 	    @SuppressWarnings({"unchecked"})
       
   270 	    MutableProperty<Object> oldProperty =
       
   271 		(MutableProperty<Object>)property;
       
   272 
       
   273 	    @SuppressWarnings({"unchecked"})
       
   274 	    MutableProperty<Object> newProperty =
       
   275 		(MutableProperty<Object>)clone.getProperty(name);
       
   276 
       
   277 	    newProperty.setValue(oldProperty.getValue());
       
   278 	}
       
   279 
       
   280 	return clone;
       
   281     }
       
   282 
       
   283     //
       
   284     // VirtualHost methods
       
   285     //
       
   286 
       
   287     public BasicSmfMutableProperty<Boolean> getCustomEnabledProperty() {
       
   288 	return customEnabledProperty;
       
   289     }
       
   290 
       
   291     public BasicSmfMutableProperty<String> getCustomFileProperty() {
       
   292 	return customFileProperty;
       
   293     }
       
   294 
       
   295     public BasicSmfMutableProperty<String> getDocRootProperty() {
       
   296 	return docRootProperty;
       
   297     }
       
   298 
       
   299     public BasicSmfMutableProperty<String> getDomainProperty() {
       
   300 	return domainProperty;
       
   301     }
       
   302 
       
   303     public BasicSmfMutableProperty<Boolean> getEnabledProperty() {
       
   304 	return enabledProperty;
       
   305     }
       
   306 
       
   307     public MimeTypes getMimeTypes() {
       
   308 	return mimeTypes;
       
   309     }
       
   310 
       
   311     public ApachePanelDescriptor getParent() {
       
   312 	return parent;
       
   313     }
       
   314 
       
   315     public BasicSmfMutableProperty<Integer> getPortProperty() {
       
   316 	return portProperty;
       
   317     }
       
   318 
       
   319     public BasicSmfMutableProperty<Boolean> getServeHomeDirsProperty() {
       
   320 	return serveHomeDirsProperty;
       
   321     }
       
   322 
       
   323     public BasicSmfMutableProperty<String> getSslCertProperty() {
       
   324 	return sslCertProperty;
       
   325     }
       
   326 
       
   327     public BasicSmfMutableProperty<Boolean> getSslEnabledProperty() {
       
   328 	return sslEnabledProperty;
       
   329     }
       
   330 
       
   331     public BasicSmfMutableProperty<String> getSslIpProperty() {
       
   332 	return sslIpProperty;
       
   333     }
       
   334 
       
   335     public BasicSmfMutableProperty<String> getSslKeyProperty() {
       
   336 	return sslKeyProperty;
       
   337     }
       
   338 
       
   339     public BasicSmfMutableProperty<Integer> getSslPortProperty() {
       
   340 	return sslPortProperty;
       
   341     }
       
   342 
       
   343     public boolean isInRepo() {
       
   344 	return group != null;
       
   345     }
       
   346 
       
   347     public void refresh(boolean force) throws ScfException {
       
   348 	for (MutableProperty<?> property : getProperties()) {
       
   349 	    if (property instanceof SmfMutableProperty) {
       
   350 		((SmfMutableProperty)property).updateFromRepo(force);
       
   351 	    }
       
   352 	}
       
   353 
       
   354 	mimeTypes.refresh(force);
       
   355     }
       
   356 
       
   357     protected void removeFromRepo() throws ScfException {
       
   358 	if (group != null) {
       
   359 	    ServiceBean service = getService();
       
   360 	    service.deletePropertyGroup(group);
       
   361 	    service.refresh();
       
   362 	    parent.getVirtualHostNamePool().put(group);
       
   363 	}
       
   364     }
       
   365 
       
   366     public void saveToRepo() throws ScfException {
       
   367 	ApacheUtil.saveToRepo(getService(),
       
   368 	    new ScfRunnable() {
       
   369 		@Override
       
   370 		public void run() throws ScfException {
       
   371 		    for (MutableProperty property : getProperties()) {
       
   372 			if (property.isChanged()) {
       
   373 			    if (property instanceof SmfMutableProperty) {
       
   374 				((SmfMutableProperty)property).saveToRepo();
       
   375 			    } else {
       
   376 				property.save();
       
   377 			    }
       
   378 			}
       
   379 		    }
       
   380 
       
   381 		    mimeTypes.saveToRepo();
       
   382 		}
       
   383 	    });
       
   384     }
       
   385 
       
   386     protected void validate() throws VirtualHostGeneralValidationException,
       
   387 	VirtualHostSSLValidationException {
       
   388 
       
   389 	validateDomain();
       
   390 	validateCustomFile();
       
   391 	validateDocRoot();
       
   392 	validateSSLIP();
       
   393 	validateSSLCert();
       
   394 	validateSSLKey();
       
   395     }
       
   396 
       
   397     //
       
   398     // Private methods
       
   399     //
       
   400 
       
   401     private void validateCustomFile()
       
   402 	throws VirtualHostGeneralValidationException {
       
   403 
       
   404 	if (customEnabledProperty.getFirstValue() &&
       
   405 	   (customEnabledProperty.isChanged() ||
       
   406 	    customFileProperty.isChanged()) &&
       
   407 	    customFileProperty.getFirstValue().isEmpty()) {
       
   408 	    throw new VirtualHostGeneralValidationException(getId(),
       
   409 		Finder.getString("vhost.general.error.customfile.empty"));
       
   410 	}
       
   411     }
       
   412 
       
   413     private void validateDomain() throws VirtualHostGeneralValidationException {
       
   414 	boolean domainChanged = domainProperty.isChanged();
       
   415 	String domain = domainProperty.getFirstValue();
       
   416 
       
   417 	if (domainChanged) {
       
   418 	    validateDomainName(domain, getId());
       
   419 	}
       
   420 
       
   421 	// Only one virtual host for a given domain may be enabled
       
   422         if (isEnabled() && (enabledProperty.isChanged() || domainChanged)) {
       
   423 	    for (VirtualHost peer : parent.getChildren()) {
       
   424 		if (peer != this && peer.isEnabled() &&
       
   425 		    ObjectUtil.equals(domain,
       
   426 			peer.getDomainProperty().getFirstValue())) {
       
   427 
       
   428 		    throw new VirtualHostGeneralValidationException(getId(),
       
   429 			Finder.getString(
       
   430 			"vhost.general.error.domain.duplicate", domain));
       
   431 		}
       
   432 	    }
       
   433 	}
       
   434     }
       
   435 
       
   436     private void validateDocRoot()
       
   437 	throws VirtualHostGeneralValidationException {
       
   438 
       
   439 	if (docRootProperty.isChanged() &&
       
   440 	    docRootProperty.getFirstValue().isEmpty()) {
       
   441 	    throw new VirtualHostGeneralValidationException(getId(),
       
   442 		Finder.getString("vhost.general.error.docroot.empty"));
       
   443 	}
       
   444     }
       
   445 
       
   446     private void validateSSLCert()
       
   447 	throws VirtualHostSSLValidationException {
       
   448 
       
   449 	if (sslEnabledProperty.getFirstValue() &&
       
   450            (sslEnabledProperty.isChanged() || sslCertProperty.isChanged()) &&
       
   451 	    sslCertProperty.getFirstValue().isEmpty()) {
       
   452 	    throw new VirtualHostSSLValidationException(getId(),
       
   453 		Finder.getString("vhost.ssl.error.cert.empty"));
       
   454 	}
       
   455     }
       
   456 
       
   457     private void validateSSLIP()
       
   458 	throws VirtualHostSSLValidationException {
       
   459 
       
   460 	if (sslEnabledProperty.getFirstValue() &&
       
   461            (sslEnabledProperty.isChanged() || sslIpProperty.isChanged())) {
       
   462 
       
   463 	    String sslIP = sslIpProperty.getFirstValue();
       
   464 	    if (sslIP.isEmpty()) {
       
   465 		throw new VirtualHostSSLValidationException(getId(),
       
   466 		    Finder.getString("vhost.ssl.error.ip.empty"));
       
   467 	    }
       
   468 
       
   469 	    try {
       
   470                 boolean isLocalAddress =
       
   471 		    parent.getNetworkBean().isLocalAddress(sslIP);
       
   472 
       
   473 		if (!isLocalAddress) {
       
   474 		    throw new VirtualHostSSLValidationException(getId(),
       
   475 			Finder.getString("vhost.ssl.error.ip.invalid", sslIP));
       
   476 		}
       
   477 	    } catch (RadObjectException e) {
       
   478 		throw new VirtualHostSSLValidationException(getId(),
       
   479 		    Finder.getString("vhost.ssl.error.ip.syserror", e));
       
   480 	    }
       
   481 	}
       
   482     }
       
   483 
       
   484     private void validateSSLKey()
       
   485 	throws VirtualHostSSLValidationException {
       
   486 
       
   487 	if (sslEnabledProperty.getFirstValue() &&
       
   488            (sslEnabledProperty.isChanged() || sslKeyProperty.isChanged()) &&
       
   489 	    sslKeyProperty.getFirstValue().isEmpty()) {
       
   490 	    throw new VirtualHostSSLValidationException(getId(),
       
   491 		Finder.getString("vhost.ssl.error.key.empty"));
       
   492 	}
       
   493     }
       
   494 
       
   495     //
       
   496     // Static methods
       
   497     //
       
   498 
       
   499     public static void validateDomainName(String domain, String id)
       
   500 	throws VirtualHostGeneralValidationException {
       
   501 
       
   502 	if (domain.isEmpty()) {
       
   503 	    throw new VirtualHostGeneralValidationException(id,
       
   504 		Finder.getString("vhost.general.error.domain.empty"));
       
   505 	}
       
   506 
       
   507 	if (!domain.matches("[\\w\\.\\-]+")) {
       
   508 	    throw new VirtualHostGeneralValidationException(id,
       
   509 		Finder.getString("vhost.general.error.domain.invalid",
       
   510 		domain));
       
   511 	}
       
   512     }
       
   513 }