001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ui.apps.sitemanager; 029 030import org.opencms.main.OpenCms; 031import org.opencms.site.CmsSSLMode; 032import org.opencms.site.CmsSite; 033import org.opencms.ui.CmsVaadinUtils; 034 035import java.util.ArrayList; 036import java.util.List; 037 038import com.vaadin.v7.data.Property.ValueChangeEvent; 039import com.vaadin.v7.data.Property.ValueChangeListener; 040import com.vaadin.v7.data.util.BeanItemContainer; 041import com.vaadin.v7.ui.AbstractSelect.NewItemHandler; 042import com.vaadin.v7.ui.ComboBox; 043import com.vaadin.ui.FormLayout; 044 045/** 046 * Layout for workplace server configuration.<p> 047 */ 048public class CmsWorkplaceServerWidget extends FormLayout { 049 050 /**vaadin serial id. */ 051 private static final long serialVersionUID = -2972167045879745058L; 052 053 /**vaadin component. */ 054 private ComboBox m_encryption; 055 056 /**vaadin component. */ 057 private ComboBox m_server; 058 059 /**ItemContainer. */ 060 BeanItemContainer<CmsSite> m_serverContainer; 061 062 /**Flag to protect changes of ValueChangeListener. */ 063 protected boolean m_do_not_change = false; 064 065 /** 066 * Public constructor.<p> 067 * 068 * @param sites all sites 069 * @param server current server 070 */ 071 public CmsWorkplaceServerWidget(List<CmsSite> sites, String server) { 072 073 CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null); 074 CmsSSLMode sslMode = OpenCms.getSiteManager().getSSLModeForWorkplaceServer(server); 075 m_encryption.setContainerDataSource(CmsEditSiteForm.getSSLModeContainer("caption", false, sslMode)); 076 m_encryption.setItemCaptionPropertyId("caption"); 077 m_encryption.setNullSelectionAllowed(false); 078 m_encryption.setNewItemsAllowed(false); 079 m_encryption.select(CmsSSLMode.NO); 080 081 m_serverContainer = setUpWorkplaceComboBox(sites, m_server, false, server, sslMode); 082 083 m_encryption.select(sslMode); 084 m_encryption.addValueChangeListener(new ValueChangeListener() { 085 086 private static final long serialVersionUID = -2628646995757479728L; 087 088 public void valueChange(ValueChangeEvent event) { 089 090 if (m_do_not_change) { 091 return; 092 } 093 m_do_not_change = true; 094 adjustServerPrefix(); 095 m_do_not_change = false; 096 097 } 098 099 }); 100 m_server.addValueChangeListener(new ValueChangeListener() { 101 102 private static final long serialVersionUID = 6670411745575147230L; 103 104 public void valueChange(ValueChangeEvent event) { 105 106 if (m_do_not_change) { 107 return; 108 } 109 m_do_not_change = true; 110 adjustSSL(); 111 m_do_not_change = false; 112 113 } 114 }); 115 adjustSSL(); 116 } 117 118 /** 119 * Sets the combo box for workplace.<p> 120 * 121 * @param allSites alls available sites 122 * @param combo combo box to fill 123 * @param nullselect if true, nothing is selected 124 * @param defaultValue if set, this value gets chosen 125 * @param sslMode CmsSSLMode 126 * @return BeanItemContainer 127 */ 128 private static BeanItemContainer<CmsSite> setUpWorkplaceComboBox( 129 List<CmsSite> allSites, 130 final ComboBox combo, 131 boolean nullselect, 132 String defaultValue, 133 CmsSSLMode sslMode) { 134 135 final List<CmsSite> modSites = new ArrayList<CmsSite>(); 136 CmsSite siteWithDefaultURL = null; 137 138 String defaultURL = defaultValue; 139 140 for (CmsSite site : allSites) { 141 CmsSite si = new CmsSite("dummy", site.getUrl()); 142 si.setSSLMode(site.getSSLMode()); 143 modSites.add(si); 144 if (defaultValue != null) { 145 if (defaultURL.equals(si.getUrl())) { //SSL sensitive ('http'!='https') 146 siteWithDefaultURL = si; 147 } 148 } 149 } 150 if (defaultValue != null) { 151 if (siteWithDefaultURL == null) { 152 siteWithDefaultURL = new CmsSite("dummy", defaultURL); 153 siteWithDefaultURL.setSSLMode(sslMode); 154 modSites.add(0, siteWithDefaultURL); 155 } 156 } 157 158 final BeanItemContainer<CmsSite> objects = new BeanItemContainer<CmsSite>(CmsSite.class, modSites); 159 combo.setContainerDataSource(objects); 160 combo.setNullSelectionAllowed(nullselect); 161 combo.setItemCaptionPropertyId("url"); 162 combo.setValue(siteWithDefaultURL); 163 combo.setNewItemsAllowed(true); 164 combo.setImmediate(true); 165 combo.setNewItemHandler(new NewItemHandler() { 166 167 private static final long serialVersionUID = -4760590374697520609L; 168 169 public void addNewItem(String newItemCaption) { 170 171 CmsSite newItem = new CmsSite("dummy", newItemCaption); 172 newItem.setSSLMode(newItemCaption.contains("https:") ? CmsSSLMode.MANUAL : CmsSSLMode.NO); 173 objects.addBean(newItem); 174 combo.select(newItem); 175 } 176 }); 177 return objects; 178 } 179 180 /** 181 * Gets the server url.<p> 182 * 183 * @return String 184 */ 185 public String getServer() { 186 187 if (m_server.getValue() == null) { 188 return ""; 189 } 190 return ((CmsSite)m_server.getValue()).getUrl(); 191 } 192 193 /** 194 * Gets the SSL Mode.<p> 195 * 196 * @return CmsSSLMode 197 */ 198 public CmsSSLMode getSSLMode() { 199 200 return (CmsSSLMode)m_encryption.getValue(); 201 } 202 203 /** 204 * Adjustes the server prefixes according to SSL setting.<p> 205 */ 206 protected void adjustServerPrefix() { 207 208 CmsSSLMode mode = (CmsSSLMode)m_encryption.getValue(); 209 if (simpleReturn(mode)) { 210 return; 211 } 212 String toBeReplaced = "http:"; 213 String newString = "https:"; 214 215 if (mode.equals(CmsSSLMode.NO) | mode.equals(CmsSSLMode.SECURE_SERVER)) { 216 toBeReplaced = "https:"; 217 newString = "http:"; 218 } 219 220 if (((CmsSite)m_server.getValue()).getUrl().contains(toBeReplaced)) { 221 222 String newURL = ((CmsSite)m_server.getValue()).getUrl().replaceAll(toBeReplaced, newString); 223 CmsSite newSite = new CmsSite("dummy", newURL); 224 if (!m_serverContainer.containsId(newSite)) { 225 m_serverContainer.addItem(newSite); 226 } 227 m_server.select(newSite); 228 } 229 230 } 231 232 /** 233 * Adjustes the SSL according to server name.<p> 234 */ 235 protected void adjustSSL() { 236 237 if (simpleReturn(null)) { 238 return; 239 } 240 CmsSSLMode siteMode = ((CmsSite)m_server.getValue()).getSSLMode(); 241 m_encryption.setValue(siteMode.equals(CmsSSLMode.SECURE_SERVER) ? CmsSSLMode.NO : siteMode); //Set mode according to site, don't allow Secure Server 242 243 } 244 245 /** 246 * Checks if adjust methods can early return.<p> 247 * 248 * @param mode to be checked. 249 * @return true if no adjustment is needed 250 */ 251 private boolean simpleReturn(CmsSSLMode mode) { 252 253 if (m_server.getValue() == null) { 254 return true; 255 } 256 257 if (mode != null) { 258 259 if (mode.equals(CmsSSLMode.NO)) { 260 if (((CmsSite)m_server.getValue()).getUrl().contains("http:")) { 261 return true; 262 } 263 } else { 264 if (((CmsSite)m_server.getValue()).getUrl().contains("https:")) { 265 return true; 266 } 267 } 268 } 269 return false; 270 } 271 272}