1 31 32 package org.opencms.site; 33 34 import org.opencms.configuration.CmsConfigurationException; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsPropertyDefinition; 37 import org.opencms.file.CmsResource; 38 import org.opencms.main.CmsException; 39 import org.opencms.main.CmsLog; 40 import org.opencms.main.CmsRuntimeException; 41 import org.opencms.main.OpenCms; 42 import org.opencms.security.CmsPermissionSet; 43 import org.opencms.security.CmsRole; 44 import org.opencms.util.CmsStringUtil; 45 46 import java.util.ArrayList ; 47 import java.util.Collections ; 48 import java.util.HashMap ; 49 import java.util.HashSet ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 import java.util.Map ; 53 import java.util.Set ; 54 55 import javax.servlet.http.HttpServletRequest ; 56 57 import org.apache.commons.logging.Log; 58 59 68 public final class CmsSiteManager implements Cloneable { 69 70 71 private static final Log LOG = CmsLog.getLog(CmsSiteManager.class); 72 73 75 private List m_aliases; 76 77 78 private CmsSite m_defaultSite; 79 80 81 private String m_defaultUri; 82 83 84 private boolean m_frozen; 85 86 87 private Set m_siteRoots; 88 89 90 private Map m_sites; 91 92 93 private String m_workplaceServer; 94 95 96 private CmsSiteMatcher m_workplaceSiteMatcher; 97 98 102 public CmsSiteManager() { 103 104 m_sites = new HashMap (); 105 m_siteRoots = new HashSet (); 106 m_aliases = new ArrayList (); 107 108 if (CmsLog.INIT.isInfoEnabled()) { 109 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_START_SITE_CONFIG_0)); 110 } 111 } 112 113 121 public static List getAvailableSites(CmsObject cms, boolean workplaceMode) { 122 123 Map sites = OpenCms.getSiteManager().getSites(); 124 List siteroots = new ArrayList (sites.size() + 1); 125 Map siteServers = new HashMap (sites.size() + 1); 126 List result = new ArrayList (sites.size() + 1); 127 128 Iterator i; 129 i = sites.keySet().iterator(); 131 while (i.hasNext()) { 132 CmsSite site = (CmsSite)sites.get(i.next()); 133 String folder = site.getSiteRoot() + "/"; 134 if (!siteroots.contains(folder)) { 135 siteroots.add(folder); 136 siteServers.put(folder, site.getSiteMatcher()); 137 } 138 } 139 if (workplaceMode && OpenCms.getSiteManager().getDefaultSite() != null) { 141 String folder = OpenCms.getSiteManager().getDefaultSite().getSiteRoot() + "/"; 142 if (!siteroots.contains(folder)) { 143 siteroots.add(folder); 144 } 145 } 146 147 String currentRoot = cms.getRequestContext().getSiteRoot(); 148 cms.getRequestContext().saveSiteRoot(); 149 try { 150 cms.getRequestContext().setSiteRoot("/"); 152 if (workplaceMode && cms.hasRole(CmsRole.ROOT_FOLDER_ACCESS)) { 153 if (!siteroots.contains("/")) { 154 siteroots.add("/"); 155 } 156 if (!siteroots.contains(currentRoot + "/")) { 157 siteroots.add(currentRoot + "/"); 158 } 159 } 160 Collections.sort(siteroots); 161 i = siteroots.iterator(); 162 while (i.hasNext()) { 163 String folder = (String )i.next(); 164 try { 165 CmsResource res = cms.readResource(folder); 166 if (!workplaceMode || cms.hasPermissions(res, CmsPermissionSet.ACCESS_VIEW)) { 167 String title = cms.readPropertyObject(res, CmsPropertyDefinition.PROPERTY_TITLE, false).getValue(); 168 if (title == null) { 169 title = folder; 170 } 171 result.add(new CmsSite( 172 folder, 173 res.getStructureId(), 174 title, 175 (CmsSiteMatcher)siteServers.get(folder))); 176 } 177 178 } catch (CmsException e) { 179 } 181 } 182 } catch (Throwable t) { 183 LOG.error(Messages.get().getBundle().key(Messages.LOG_READ_SITE_PROP_FAILED_0), t); 184 } finally { 185 cms.getRequestContext().restoreSiteRoot(); 187 } 188 return result; 189 } 190 191 197 public static CmsSite getCurrentSite(CmsObject cms) { 198 199 String siteRoot = cms.getRequestContext().getSiteRoot(); 200 CmsSite site = getSite(siteRoot); 201 if (site == null) { 202 return OpenCms.getSiteManager().getDefaultSite(); 203 } else { 204 return site; 205 } 206 } 207 208 216 public static CmsSite getSite(String siteRoot) { 217 218 Map sites = OpenCms.getSiteManager().getSites(); 219 Iterator i = sites.keySet().iterator(); 220 while (i.hasNext()) { 221 CmsSite site = (CmsSite)sites.get(i.next()); 222 if (siteRoot.equals(site.getSiteRoot())) { 223 return site; 224 } 225 } 226 return null; 227 } 228 229 236 public static String getSiteRoot(String path) { 237 238 Set roots = OpenCms.getSiteManager().getSiteRoots(); 239 int pos = path.indexOf('/', 7); 241 if (pos > 0) { 242 String candidate = path.substring(0, pos); 243 if (roots.contains(candidate)) { 244 return candidate; 245 } 246 } 247 Iterator i = roots.iterator(); 249 while (i.hasNext()) { 250 String siteRoot = (String )i.next(); 251 if (path.startsWith(siteRoot)) { 252 return siteRoot; 253 } 254 } 255 return null; 256 } 257 258 263 public void addAliasToConfigSite(String alias) { 264 265 CmsSiteMatcher siteMatcher = new CmsSiteMatcher(alias); 266 m_aliases.add(siteMatcher); 267 } 268 269 285 public void addSite(String server, String uri, String secureServer, String exclusive, String error) 286 throws CmsConfigurationException { 287 288 if (m_frozen) { 289 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONFIG_FROZEN_0)); 290 } 291 CmsSiteMatcher matcher = new CmsSiteMatcher(server); 292 CmsSite site = new CmsSite(uri, matcher); 293 addServer(matcher, site); 294 if (CmsStringUtil.isNotEmpty(secureServer)) { 295 matcher = new CmsSiteMatcher(secureServer); 296 site.setSecureServer(matcher); 297 site.setExclusiveUrl(Boolean.valueOf(exclusive).booleanValue()); 298 site.setExclusiveError(Boolean.valueOf(error).booleanValue()); 299 addServer(matcher, site); 300 } 301 302 site.setAliases(m_aliases); 305 Iterator i = m_aliases.iterator(); 306 while (i.hasNext()) { 307 matcher = (CmsSiteMatcher)i.next(); 308 addServer(matcher, site); 309 } 310 m_aliases = new ArrayList (); 311 m_siteRoots.add(site.getSiteRoot()); 312 if (CmsLog.INIT.isInfoEnabled()) { 313 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SITE_ROOT_ADDED_1, site.toString())); 314 } 315 } 316 317 322 public CmsSite getDefaultSite() { 323 324 return m_defaultSite; 325 } 326 327 332 public String getDefaultUri() { 333 334 return m_defaultUri; 335 } 336 337 342 public Set getSiteRoots() { 343 344 return m_siteRoots; 345 } 346 347 354 public Map getSites() { 355 356 return m_sites; 357 } 358 359 364 public String getWorkplaceServer() { 365 366 return m_workplaceServer; 367 } 368 369 374 public CmsSiteMatcher getWorkplaceSiteMatcher() { 375 376 return m_workplaceSiteMatcher; 377 } 378 379 384 public void initialize(CmsObject cms) { 385 386 if (CmsLog.INIT.isInfoEnabled()) { 387 CmsLog.INIT.info(Messages.get().getBundle().key( 388 Messages.INIT_NUM_SITE_ROOTS_CONFIGURED_1, 389 new Integer ((m_sites.size() + ((m_defaultUri != null) ? 1 : 0))))); 390 } 391 392 Iterator i = m_sites.values().iterator(); 394 while (i.hasNext()) { 395 CmsSite site = (CmsSite)i.next(); 396 if (site != null) { 397 try { 398 cms.readResource(site.getSiteRoot()); 399 } catch (Throwable t) { 400 if (CmsLog.INIT.isWarnEnabled()) { 401 CmsLog.INIT.warn(Messages.get().getBundle().key(Messages.INIT_NO_ROOT_FOLDER_1, site)); 402 } 403 } 404 } 405 } 406 407 if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_defaultUri)) { 409 m_defaultSite = null; 410 } else { 411 m_defaultSite = new CmsSite(m_defaultUri, CmsSiteMatcher.DEFAULT_MATCHER); 412 try { 413 cms.readResource(m_defaultSite.getSiteRoot()); 414 } catch (Throwable t) { 415 if (CmsLog.INIT.isWarnEnabled()) { 416 CmsLog.INIT.warn(Messages.get().getBundle().key( 417 Messages.INIT_NO_ROOT_FOLDER_DEFAULT_SITE_1, 418 m_defaultSite)); 419 } 420 } 421 } 422 if (m_defaultSite == null) { 423 m_defaultSite = new CmsSite("/", CmsSiteMatcher.DEFAULT_MATCHER); 424 } 425 if (CmsLog.INIT.isInfoEnabled()) { 426 if (m_defaultSite != null) { 427 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_DEFAULT_SITE_ROOT_1, m_defaultSite)); 428 } else { 429 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_DEFAULT_SITE_ROOT_0)); 430 } 431 } 432 m_workplaceSiteMatcher = new CmsSiteMatcher(m_workplaceServer); 433 if (CmsLog.INIT.isInfoEnabled()) { 434 if (m_workplaceSiteMatcher != null) { 435 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_WORKPLACE_SITE_1, m_workplaceSiteMatcher)); 436 } else { 437 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_WORKPLACE_SITE_0)); 438 } 439 } 440 441 m_sites = Collections.unmodifiableMap(m_sites); 443 m_siteRoots = Collections.unmodifiableSet(m_siteRoots); 444 445 m_frozen = true; 447 } 448 449 455 public boolean isMatching(CmsSiteMatcher matcher) { 456 457 return m_sites.get(matcher) != null; 458 } 459 460 467 public boolean isMatchingCurrentSite(CmsObject cms, CmsSiteMatcher matcher) { 468 469 return m_sites.get(matcher) == getCurrentSite(cms); 470 } 471 472 478 public boolean isWorkplaceRequest(HttpServletRequest req) { 479 480 if (req == null) { 481 return false; 483 } 484 CmsSiteMatcher matcher = new CmsSiteMatcher(req.getScheme(), req.getServerName(), req.getServerPort()); 485 return m_workplaceSiteMatcher.equals(matcher); 486 } 487 488 495 public CmsSite matchRequest(HttpServletRequest req) { 496 497 CmsSiteMatcher matcher = new CmsSiteMatcher(req.getScheme(), req.getServerName(), req.getServerPort()); 498 CmsSite site = matchSite(matcher); 499 if (LOG.isDebugEnabled()) { 500 String requestServer = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort(); 501 LOG.debug(Messages.get().getBundle().key( 502 Messages.LOG_MATCHING_REQUEST_TO_SITE_2, 503 requestServer, 504 site.toString())); 505 } 506 return site; 507 } 508 509 516 public CmsSite matchSite(CmsSiteMatcher matcher) { 517 518 CmsSite site = (CmsSite)m_sites.get(matcher); 519 if (site == null) { 520 site = m_defaultSite; 522 } 523 return site; 524 } 525 526 534 public void setDefaultUri(String defaultUri) { 535 536 if (m_frozen) { 537 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONFIG_FROZEN_0)); 538 } 539 m_defaultUri = defaultUri; 540 } 541 542 550 public void setWorkplaceServer(String workplaceServer) { 551 552 if (m_frozen) { 553 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONFIG_FROZEN_0)); 554 } 555 m_workplaceServer = workplaceServer; 556 } 557 558 566 private void addServer(CmsSiteMatcher server, CmsSite site) throws CmsConfigurationException { 567 568 if (m_sites.containsKey(server)) { 569 throw new CmsConfigurationException(Messages.get().container( 570 Messages.ERR_DUPLICATE_SERVER_NAME_1, 571 server.getUrl())); 572 } 573 m_sites.put(server, site); 574 } 575 } | Popular Tags |