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;
037import java.util.stream.Collectors;
038
039import com.vaadin.ui.FormLayout;
040import com.vaadin.v7.data.Property.ValueChangeEvent;
041import com.vaadin.v7.data.Property.ValueChangeListener;
042import com.vaadin.v7.data.util.BeanItemContainer;
043import com.vaadin.v7.ui.AbstractSelect.NewItemHandler;
044import com.vaadin.v7.ui.ComboBox;
045
046/**
047 * Layout for workplace server configuration.<p>
048 */
049public class CmsWorkplaceServerWidget extends FormLayout {
050
051    /**vaadin serial id. */
052    private static final long serialVersionUID = -2972167045879745058L;
053
054    /**vaadin component. */
055    private ComboBox m_encryption;
056
057    /**vaadin component. */
058    private ComboBox m_server;
059
060    /**ItemContainer. */
061    BeanItemContainer<CmsSite> m_serverContainer;
062
063    /**Flag to protect changes of ValueChangeListener. */
064    protected boolean m_do_not_change = false;
065
066    /**
067     * Public constructor.<p>
068     *
069     * @param sites all sites
070     * @param server current server
071     */
072    public CmsWorkplaceServerWidget(List<CmsSite> sites, String server) {
073
074        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
075        CmsSSLMode sslMode = OpenCms.getSiteManager().getSSLModeForWorkplaceServer(server);
076        m_encryption.setContainerDataSource(CmsEditSiteForm.getSSLModeContainer("caption", false, sslMode));
077        m_encryption.setItemCaptionPropertyId("caption");
078        m_encryption.setNullSelectionAllowed(false);
079        m_encryption.setNewItemsAllowed(false);
080        m_encryption.select(CmsSSLMode.NO);
081
082        List<CmsSite> sitesWithUrl = sites.stream().filter(site -> site.getSiteMatcher() != null).collect(
083            Collectors.toList());
084        m_serverContainer = setUpWorkplaceComboBox(sitesWithUrl, m_server, false, server, sslMode);
085
086        m_encryption.select(sslMode);
087        m_encryption.addValueChangeListener(new ValueChangeListener() {
088
089            private static final long serialVersionUID = -2628646995757479728L;
090
091            public void valueChange(ValueChangeEvent event) {
092
093                if (m_do_not_change) {
094                    return;
095                }
096                m_do_not_change = true;
097                adjustServerPrefix();
098                m_do_not_change = false;
099
100            }
101
102        });
103        m_server.addValueChangeListener(new ValueChangeListener() {
104
105            private static final long serialVersionUID = 6670411745575147230L;
106
107            public void valueChange(ValueChangeEvent event) {
108
109                if (m_do_not_change) {
110                    return;
111                }
112                m_do_not_change = true;
113                adjustSSL();
114                m_do_not_change = false;
115
116            }
117        });
118        adjustSSL();
119    }
120
121    /**
122     * Sets the combo box for workplace.<p>
123     *
124     * @param allSites alls available sites
125     * @param combo combo box to fill
126     * @param nullselect if true, nothing is selected
127     * @param defaultValue if set, this value gets chosen
128     * @param sslMode CmsSSLMode
129     * @return BeanItemContainer
130     */
131    private static BeanItemContainer<CmsSite> setUpWorkplaceComboBox(
132        List<CmsSite> allSites,
133        final ComboBox combo,
134        boolean nullselect,
135        String defaultValue,
136        CmsSSLMode sslMode) {
137
138        final List<CmsSite> modSites = new ArrayList<CmsSite>();
139        CmsSite siteWithDefaultURL = null;
140
141        String defaultURL = defaultValue;
142
143        for (CmsSite site : allSites) {
144            CmsSite si = new CmsSite("dummy", site.getUrl());
145            si.setSSLMode(site.getSSLMode());
146            modSites.add(si);
147            if (defaultValue != null) {
148                if (defaultURL.equals(si.getUrl())) { //SSL sensitive ('http'!='https')
149                    siteWithDefaultURL = si;
150                }
151            }
152        }
153        if (defaultValue != null) {
154            if (siteWithDefaultURL == null) {
155                siteWithDefaultURL = new CmsSite("dummy", defaultURL);
156                siteWithDefaultURL.setSSLMode(sslMode);
157                modSites.add(0, siteWithDefaultURL);
158            }
159        }
160
161        final BeanItemContainer<CmsSite> objects = new BeanItemContainer<CmsSite>(CmsSite.class, modSites);
162        combo.setContainerDataSource(objects);
163        combo.setNullSelectionAllowed(nullselect);
164        combo.setItemCaptionPropertyId("url");
165        combo.setValue(siteWithDefaultURL);
166        combo.setNewItemsAllowed(true);
167        combo.setImmediate(true);
168        combo.setNewItemHandler(new NewItemHandler() {
169
170            private static final long serialVersionUID = -4760590374697520609L;
171
172            public void addNewItem(String newItemCaption) {
173
174                CmsSite newItem = new CmsSite("dummy", newItemCaption);
175                newItem.setSSLMode(newItemCaption.contains("https:") ? CmsSSLMode.MANUAL : CmsSSLMode.NO);
176                objects.addBean(newItem);
177                combo.select(newItem);
178            }
179        });
180        return objects;
181    }
182
183    /**
184     * Gets the server url.<p>
185     *
186     * @return String
187     */
188    public String getServer() {
189
190        if (m_server.getValue() == null) {
191            return "";
192        }
193        return ((CmsSite)m_server.getValue()).getUrl();
194    }
195
196    /**
197     * Gets the SSL Mode.<p>
198     *
199     * @return CmsSSLMode
200     */
201    public CmsSSLMode getSSLMode() {
202
203        return (CmsSSLMode)m_encryption.getValue();
204    }
205
206    /**
207     * Adjustes the server prefixes according to SSL setting.<p>
208     */
209    protected void adjustServerPrefix() {
210
211        CmsSSLMode mode = (CmsSSLMode)m_encryption.getValue();
212        if (simpleReturn(mode)) {
213            return;
214        }
215        String toBeReplaced = "http:";
216        String newString = "https:";
217
218        if (mode.equals(CmsSSLMode.NO) | mode.equals(CmsSSLMode.SECURE_SERVER)) {
219            toBeReplaced = "https:";
220            newString = "http:";
221        }
222
223        if (((CmsSite)m_server.getValue()).getUrl().contains(toBeReplaced)) {
224
225            String newURL = ((CmsSite)m_server.getValue()).getUrl().replaceAll(toBeReplaced, newString);
226            CmsSite newSite = new CmsSite("dummy", newURL);
227            if (!m_serverContainer.containsId(newSite)) {
228                m_serverContainer.addItem(newSite);
229            }
230            m_server.select(newSite);
231        }
232
233    }
234
235    /**
236     * Adjustes the SSL according to server name.<p>
237     */
238    protected void adjustSSL() {
239
240        if (simpleReturn(null)) {
241            return;
242        }
243        CmsSSLMode siteMode = ((CmsSite)m_server.getValue()).getSSLMode();
244        m_encryption.setValue(siteMode.equals(CmsSSLMode.SECURE_SERVER) ? CmsSSLMode.NO : siteMode); //Set mode according to site, don't allow Secure Server
245
246    }
247
248    /**
249     * Checks if adjust methods can early return.<p>
250     *
251     * @param mode to be checked.
252     * @return true if no adjustment is needed
253     */
254    private boolean simpleReturn(CmsSSLMode mode) {
255
256        if (m_server.getValue() == null) {
257            return true;
258        }
259
260        if (mode != null) {
261
262            if (mode.equals(CmsSSLMode.NO)) {
263                if (((CmsSite)m_server.getValue()).getUrl().contains("http:")) {
264                    return true;
265                }
266            } else {
267                if (((CmsSite)m_server.getValue()).getUrl().contains("https:")) {
268                    return true;
269                }
270            }
271        }
272        return false;
273    }
274
275}