001/* 002 * File : $Source$ 003 * Date : $Date$ 004 * Version: $Revision$ 005 * 006 * This library is part of OpenCms - 007 * the Open Source Content Management System 008 * 009 * Copyright (C) 2002 - 2009 Alkacon Software (http://www.alkacon.com) 010 * 011 * This library is free software; you can redistribute it and/or 012 * modify it under the terms of the GNU Lesser General Public 013 * License as published by the Free Software Foundation; either 014 * version 2.1 of the License, or (at your option) any later version. 015 * 016 * This library is distributed in the hope that it will be useful, 017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 019 * Lesser General Public License for more details. 020 * 021 * For further information about Alkacon Software, please see the 022 * company website: http://www.alkacon.com 023 * 024 * For further information about OpenCms, please see the 025 * project website: http://www.opencms.org 026 * 027 * You should have received a copy of the GNU Lesser General Public 028 * License along with this library; if not, write to the Free Software 029 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 030 */ 031 032package org.opencms.workplace.tools.sites; 033 034import org.opencms.main.CmsLog; 035import org.opencms.main.OpenCms; 036import org.opencms.site.CmsAlternativeSiteRootMapping; 037import org.opencms.site.CmsSSLMode; 038import org.opencms.site.CmsSite; 039import org.opencms.site.CmsSiteManagerImpl; 040import org.opencms.site.CmsSiteMatcher; 041import org.opencms.site.CmsSiteMatcher.RedirectMode; 042import org.opencms.util.CmsStringUtil; 043import org.opencms.util.CmsUUID; 044 045import java.io.Serializable; 046import java.util.ArrayList; 047import java.util.LinkedHashMap; 048import java.util.List; 049import java.util.Optional; 050import java.util.SortedMap; 051import java.util.TreeMap; 052import java.util.stream.Collectors; 053 054import org.apache.commons.lang3.builder.EqualsBuilder; 055import org.apache.commons.logging.Log; 056 057/** 058 * Dialog object for a single site.<p> 059 * 060 * @since 9.0.0 061 */ 062public class CmsSiteBean implements Serializable { 063 064 /** Logger instance for this class. */ 065 private static final Log LOG = CmsLog.getLog(CmsSiteBean.class); 066 067 /** The serial version id. */ 068 private static final long serialVersionUID = 7202674198996429791L; 069 070 /** Alternative site root mapping. */ 071 private CmsAlternativeSiteRootMapping m_alternativeSiteRootMapping; 072 073 /** The URI used as error page. */ 074 private String m_errorPage; 075 076 /** The exclusive flag. */ 077 private boolean m_exclusiveError; 078 079 /** The exclusive URL. */ 080 private boolean m_exclusiveUrl; 081 082 /** The favicon. */ 083 private String m_favicon; 084 085 /** The SSL Mode of the site.*/ 086 private String m_mode; 087 088 /** The original site. */ 089 private transient CmsSite m_originalSite; 090 091 /** The parameters. */ 092 private SortedMap<String, String> m_parameters = new TreeMap<String, String>(); 093 094 /** The port (e.g. 8080) which is required to access this site. */ 095 private int m_port; 096 097 /** The position. */ 098 private float m_position; 099 100 /** An ordered map of the aliases (site URLs as keys, redirect modes as values). */ 101 private LinkedHashMap<String, CmsSiteMatcher.RedirectMode> m_aliases = new LinkedHashMap<>(); 102 103 /** The secure server. */ 104 private boolean m_secureServer; 105 106 /** The secure URL. */ 107 private String m_secureUrl; 108 109 /** The servers URL. */ 110 private String m_server; 111 112 /** The host name (e.g. localhost) which is required to access this site. */ 113 private String m_serverName; 114 115 /** The servers protocol (e.g. "http", "https") which is required to access this site. */ 116 private String m_serverProtocol; 117 118 /** The site root of this site. */ 119 private String m_siteRoot; 120 121 /** True if subsite selection should be enabled. */ 122 private boolean m_subsiteSelection; 123 124 /** The offset time in ms. */ 125 private long m_timeOffset; 126 127 /** The title of this site. */ 128 private String m_title; 129 130 /** Indicates whether this site should be considered when writing the web server configuration. */ 131 private boolean m_webserver = true; 132 133 /** 134 * Default constructor.<p> 135 */ 136 public CmsSiteBean() { 137 138 // noop 139 } 140 141 /** 142 * Public constructor with a CmsSite as parameter.<p> 143 * 144 * @param site the site 145 */ 146 public CmsSiteBean(CmsSite site) { 147 148 m_siteRoot = site.getSiteRoot(); 149 if (site.getSiteMatcher() != null) { 150 m_originalSite = site; 151 m_title = site.getTitle(); 152 m_server = site.getUrl(); 153 154 CmsSiteMatcher matcher = site.getSiteMatcher(); 155 m_serverProtocol = matcher.getServerProtocol(); 156 m_serverName = matcher.getServerName(); 157 m_port = matcher.getServerPort(); 158 m_timeOffset = matcher.getTimeOffset(); 159 m_secureServer = site.hasSecureServer(); 160 if (site.hasSecureServer()) { 161 m_secureUrl = site.getSecureUrl(); 162 m_exclusiveUrl = site.isExclusiveUrl(); 163 m_exclusiveError = site.isExclusiveError(); 164 } 165 for (CmsSiteMatcher aMatcher : site.getAliases()) { 166 if ((aMatcher != null) && CmsStringUtil.isNotEmptyOrWhitespaceOnly(aMatcher.getUrl())) { 167 m_aliases.put(aMatcher.getUrl(), aMatcher.getRedirectMode()); 168 } 169 } 170 m_position = site.getPosition(); 171 m_errorPage = site.getErrorPage(); 172 m_webserver = site.isWebserver(); 173 m_parameters = site.getParameters(); 174 m_mode = site.getSSLMode().name(); 175 m_subsiteSelection = site.isSubsiteSelectionEnabled(); 176 setAlternativeSiteRootMapping(site.getAlternativeSiteRootMapping().orElse(null)); 177 } 178 } 179 180 /** 181 * @see java.lang.Object#equals(java.lang.Object) 182 */ 183 @Override 184 public boolean equals(Object obj) { 185 186 return EqualsBuilder.reflectionEquals(this, obj); 187 } 188 189 /** 190 * Returns the aliases.<p> 191 * 192 * @return the aliases 193 */ 194 public List<String> getAliases() { 195 196 return new ArrayList<>(m_aliases.keySet()); 197 } 198 199 /** 200 * Gets the alternative site root mapping. 201 * 202 * @return the alternative site root mapping 203 */ 204 public CmsAlternativeSiteRootMapping getAlternativeSiteRootMapping() { 205 206 return m_alternativeSiteRootMapping; 207 } 208 209 /** 210 * Returns the errorPage.<p> 211 * 212 * @return the errorPage 213 */ 214 public String getErrorPage() { 215 216 return m_errorPage; 217 } 218 219 /** 220 * Returns the favicon.<p> 221 * 222 * @return the favicon 223 */ 224 public String getFavicon() { 225 226 return m_favicon; 227 } 228 229 /** 230 * Returns the originalSite.<p> 231 * 232 * @return the originalSite 233 */ 234 public CmsSite getOriginalSite() { 235 236 return m_originalSite; 237 } 238 239 /** 240 * Gets the parameters.<p> 241 * 242 * @return the site parameters 243 */ 244 public SortedMap<String, String> getParameters() { 245 246 return m_parameters; 247 } 248 249 /** 250 * Returns the port.<p> 251 * 252 * @return the port 253 */ 254 public int getPort() { 255 256 return m_port; 257 } 258 259 /** 260 * Returns the position.<p> 261 * 262 * @return the position 263 */ 264 public float getPosition() { 265 266 return m_position; 267 } 268 269 /** 270 * Returns the list of aliases that are configured to redirect to the site's main URL.<p> 271 * 272 * @return the redirect aliases 273 */ 274 public List<String> getRedirectAliases() { 275 276 return m_aliases.entrySet().stream().filter(entry -> entry.getValue() != RedirectMode.none).map( 277 entry -> entry.getKey()).collect(Collectors.toList()); 278 } 279 280 /** 281 * Returns the secureUrl.<p> 282 * 283 * @return the secureUrl 284 */ 285 public String getSecureUrl() { 286 287 return m_secureUrl; 288 } 289 290 /** 291 * Returns the server.<p> 292 * 293 * @return the server 294 */ 295 public String getServer() { 296 297 return m_server; 298 } 299 300 /** 301 * Returns the serverName.<p> 302 * 303 * @return the serverName 304 */ 305 public String getServerName() { 306 307 return m_serverName; 308 } 309 310 /** 311 * Returns the serverProtocol.<p> 312 * 313 * @return the serverProtocol 314 */ 315 public String getServerProtocol() { 316 317 return m_serverProtocol; 318 } 319 320 /** 321 * Returns the siteRoot.<p> 322 * 323 * @return the siteRoot 324 */ 325 public String getSiteRoot() { 326 327 return m_siteRoot; 328 } 329 330 /** 331 * Gets the SSL mode.<p> 332 * 333 * @return the SSL mode 334 */ 335 public String getSslMode() { 336 337 return m_mode; 338 } 339 340 /** 341 * Returns the timeOffset.<p> 342 * 343 * @return the timeOffset 344 */ 345 public long getTimeOffset() { 346 347 return m_timeOffset; 348 } 349 350 /** 351 * Returns the title.<p> 352 * 353 * @return the title 354 */ 355 public String getTitle() { 356 357 return m_title; 358 } 359 360 /** 361 * @see java.lang.Object#hashCode() 362 */ 363 @Override 364 public int hashCode() { 365 366 final int prime = 31; 367 int result = 1; 368 result = (prime * result) + ((m_server == null) ? 0 : m_server.hashCode()); 369 result = (prime * result) + ((m_siteRoot == null) ? 0 : m_siteRoot.hashCode()); 370 result = (prime * result) + ((m_title == null) ? 0 : m_title.hashCode()); 371 return result; 372 } 373 374 /** 375 * Returns the secureServer.<p> 376 * 377 * @return the secureServer 378 */ 379 public boolean hasSecureServer() { 380 381 return m_secureServer; 382 } 383 384 /** 385 * Returns the exclusiveError.<p> 386 * 387 * @return the exclusiveError 388 */ 389 public boolean isExclusiveError() { 390 391 return m_exclusiveError; 392 } 393 394 /** 395 * Returns the exclusiveUrl.<p> 396 * 397 * @return the exclusiveUrl 398 */ 399 public boolean isExclusiveUrl() { 400 401 return m_exclusiveUrl; 402 } 403 404 /** 405 * Returns the secureServer.<p> 406 * 407 * @return the secureServer 408 */ 409 public boolean isSecureServer() { 410 411 return m_secureServer; 412 } 413 414 /** 415 * Returns the web server.<p> 416 * 417 * @return the web server 418 */ 419 public boolean isWebserver() { 420 421 return m_webserver; 422 } 423 424 /** 425 * Sets the alternative site root mapping. 426 * 427 * @param alternativeSiteRootMapping the site root mapping 428 */ 429 public void setAlternativeSiteRootMapping(CmsAlternativeSiteRootMapping alternativeSiteRootMapping) { 430 431 m_alternativeSiteRootMapping = alternativeSiteRootMapping; 432 } 433 434 /** 435 * Sets the errorPage.<p> 436 * 437 * @param errorPage the errorPage to set 438 */ 439 public void setErrorPage(String errorPage) { 440 441 m_errorPage = errorPage; 442 } 443 444 /** 445 * Sets the exclusiveError.<p> 446 * 447 * @param exclusiveError the exclusiveError to set 448 */ 449 public void setExclusiveError(boolean exclusiveError) { 450 451 m_exclusiveError = exclusiveError; 452 } 453 454 /** 455 * Sets the exclusiveUrl.<p> 456 * 457 * @param exclusiveUrl the exclusiveUrl to set 458 */ 459 public void setExclusiveUrl(boolean exclusiveUrl) { 460 461 m_exclusiveUrl = exclusiveUrl; 462 } 463 464 /** 465 * Sets the favicon.<p> 466 * 467 * @param favicon the favicon to set 468 */ 469 public void setFavicon(String favicon) { 470 471 m_favicon = favicon; 472 } 473 474 /** 475 * Sets the parameters.<p> 476 * 477 * @param params the parameters 478 */ 479 public void setParameters(SortedMap<String, String> params) { 480 481 m_parameters = params; 482 } 483 484 /** 485 * Sets the port.<p> 486 * 487 * @param port the port to set 488 */ 489 public void setPort(int port) { 490 491 m_port = port; 492 } 493 494 /** 495 * Sets the position.<p> 496 * 497 * @param position the position to set 498 */ 499 public void setPosition(float position) { 500 501 m_position = position; 502 } 503 504 /** 505 * Sets the secureServer.<p> 506 * 507 * @param secureServer the secureServer to set 508 */ 509 public void setSecureServer(boolean secureServer) { 510 511 m_secureServer = secureServer; 512 } 513 514 /** 515 * Sets the secureUrl.<p> 516 * 517 * @param secureUrl the secureUrl to set 518 */ 519 public void setSecureUrl(String secureUrl) { 520 521 m_secureUrl = secureUrl; 522 } 523 524 /** 525 * Sets the server.<p> 526 * 527 * @param server the server to set 528 */ 529 public void setServer(String server) { 530 531 m_server = server; 532 } 533 534 /** 535 * Sets the serverName.<p> 536 * 537 * @param serverName the serverName to set 538 */ 539 public void setServerName(String serverName) { 540 541 m_serverName = serverName; 542 } 543 544 /** 545 * Sets the serverProtocol.<p> 546 * 547 * @param serverProtocol the serverProtocol to set 548 */ 549 public void setServerProtocol(String serverProtocol) { 550 551 m_serverProtocol = serverProtocol; 552 } 553 554 /** 555 * Sets the siteRoot.<p> 556 * 557 * @param siteRoot the siteRoot to set 558 */ 559 public void setSiteRoot(String siteRoot) { 560 561 m_siteRoot = siteRoot; 562 } 563 564 /** 565 * Sets the SSL mode.<p> 566 * 567 * @param mode the SSL mode 568 */ 569 public void setSslMode(String mode) { 570 571 m_mode = mode; 572 } 573 574 /** 575 * Sets the timeOffset.<p> 576 * 577 * @param timeOffset the timeOffset to set 578 */ 579 public void setTimeOffset(long timeOffset) { 580 581 m_timeOffset = timeOffset; 582 } 583 584 /** 585 * Sets the title.<p> 586 * 587 * @param title the title to set 588 */ 589 public void setTitle(String title) { 590 591 m_title = title; 592 } 593 594 /** 595 * Sets the web server.<p> 596 * 597 * @param webserver the web server to set 598 */ 599 public void setWebserver(boolean webserver) { 600 601 m_webserver = webserver; 602 } 603 604 /** 605 * Creates a new site object based on the members.<p> 606 * 607 * @return a new site object based on the members 608 */ 609 public CmsSite toCmsSite() { 610 611 return toCmsSite(OpenCms.getSiteManager()); 612 613 } 614 615 /** 616 * Creates a new site object based on the members.<p> 617 * 618 * This method with the siteManager parameter is mostly useful for testing, since usually only the global site manager instance is used. 619 * 620 * @param siteManager the site manager to use 621 * 622 * @return a new site object based on the members 623 */ 624 public CmsSite toCmsSite(CmsSiteManagerImpl siteManager) { 625 626 m_siteRoot = m_siteRoot.endsWith("/") ? m_siteRoot.substring(0, m_siteRoot.length() - 1) : m_siteRoot; 627 CmsSiteMatcher matcher = CmsStringUtil.isNotEmpty(m_secureUrl) ? new CmsSiteMatcher(m_secureUrl) : null; 628 CmsSite site = siteManager.getSiteForSiteRoot(m_siteRoot); 629 CmsUUID uuid = new CmsUUID(); 630 if ((site != null) && (site.getSiteMatcher() != null)) { 631 uuid = (CmsUUID)site.getSiteRootUUID().clone(); 632 } 633 String errorPage = CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_errorPage) ? m_errorPage : null; 634 List<CmsSiteMatcher> aliases = new ArrayList<CmsSiteMatcher>(); 635 for (String alias : m_aliases.keySet()) { 636 RedirectMode redirectMode = m_aliases.get(alias); 637 CmsSiteMatcher aliasMatcher = new CmsSiteMatcher(alias); 638 aliasMatcher.setRedirectMode(redirectMode); 639 aliases.add(aliasMatcher); 640 } 641 CmsSite result = new CmsSite( 642 m_siteRoot, 643 uuid, 644 m_title, 645 new CmsSiteMatcher(m_server), 646 String.valueOf(m_position), 647 errorPage, 648 matcher, 649 m_exclusiveUrl, 650 m_exclusiveError, 651 m_webserver, 652 aliases, 653 m_subsiteSelection); 654 result.setAlternativeSiteRootMapping(Optional.ofNullable(getAlternativeSiteRootMapping())); 655 result.setParameters(m_parameters); 656 try { 657 result.setSSLMode(CmsSSLMode.valueOf(m_mode)); 658 } catch (Exception e) { 659 result.setSSLMode(CmsSSLMode.NO); 660 LOG.error(e.getLocalizedMessage(), e); 661 } 662 return result; 663 664 } 665 666 /** 667 * @see java.lang.Object#toString() 668 */ 669 @Override 670 public String toString() { 671 672 return "Site [m_siteRoot=" + m_siteRoot + ", m_title=" + m_title + ", m_server=" + m_server + "]"; 673 } 674}