| Class | Line # | Actions | |||||
|---|---|---|---|---|---|---|---|
| DomainWikiReferenceExtractor | 40 | 13 | 0% | 7 | 2 |
| 1 | /* | |
| 2 | * See the NOTICE file distributed with this work for additional | |
| 3 | * information regarding copyright ownership. | |
| 4 | * | |
| 5 | * This is free software; you can redistribute it and/or modify it | |
| 6 | * under the terms of the GNU Lesser General Public License as | |
| 7 | * published by the Free Software Foundation; either version 2.1 of | |
| 8 | * the License, or (at your option) any later version. | |
| 9 | * | |
| 10 | * This software is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | * Lesser General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU Lesser General Public | |
| 16 | * License along with this software; if not, write to the Free | |
| 17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
| 18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
| 19 | */ | |
| 20 | package org.xwiki.url.internal.standard; | |
| 21 | ||
| 22 | import javax.inject.Named; | |
| 23 | import javax.inject.Singleton; | |
| 24 | ||
| 25 | import org.apache.commons.lang3.StringUtils; | |
| 26 | import org.xwiki.component.annotation.Component; | |
| 27 | import org.xwiki.model.reference.WikiReference; | |
| 28 | import org.xwiki.url.ExtendedURL; | |
| 29 | import org.xwiki.wiki.descriptor.WikiDescriptor; | |
| 30 | ||
| 31 | /** | |
| 32 | * Handles domain-based multiwiki configurations when extracting the wiki reference from the passed URL. | |
| 33 | * | |
| 34 | * @version $Id: aa55d5977aac7b390b218cb4e87bc847a299eb21 $ | |
| 35 | * @since 6.3M1 | |
| 36 | */ | |
| 37 | @Component | |
| 38 | @Named("domain") | |
| 39 | @Singleton | |
| 40 | public class DomainWikiReferenceExtractor extends AbstractWikiReferenceExtractor | |
| 41 | { | |
| 42 | 20973 | @Override |
| 43 | public WikiReference extract(ExtendedURL url) | |
| 44 | { | |
| 45 | // Note: we don't use url.getURI().getHost() since URI are more restricted in characters allowed in domain | |
| 46 | // names. For example the "_" character is not supported URIs but it is supported in URLs. | |
| 47 | 20980 | String wikiId = resolveDomainBasedWikiReference(url.getWrappedURL().getHost()); |
| 48 | ||
| 49 | 20948 | if (StringUtils.isEmpty(wikiId)) { |
| 50 | 0 | wikiId = getMainWikiId(); |
| 51 | } | |
| 52 | ||
| 53 | 20951 | return new WikiReference(wikiId.toLowerCase()); |
| 54 | } | |
| 55 | ||
| 56 | 20954 | private String resolveDomainBasedWikiReference(String alias) |
| 57 | { | |
| 58 | 20967 | String wikiId; |
| 59 | ||
| 60 | // Look for a Wiki Descriptor | |
| 61 | 20970 | WikiDescriptor wikiDescriptor = getWikiDescriptorByAlias(alias); |
| 62 | 20941 | if (wikiDescriptor != null) { |
| 63 | // Get the wiki id from the wiki descriptor | |
| 64 | 11509 | wikiId = wikiDescriptor.getId(); |
| 65 | } else { | |
| 66 | // Fallback: No definition found based on the full domain name, consider the alias as a | |
| 67 | // domain name and try to use the first part of the domain name as the wiki name. | |
| 68 | 9433 | String domainAlias = StringUtils.substringBefore(alias, "."); |
| 69 | ||
| 70 | // As a convenience, we do not require the creation of an XWiki.XWikiServerXwiki page for the main | |
| 71 | // wiki and automatically go to the main wiki in certain cases: | |
| 72 | // - "www.<rest of domain name>" | |
| 73 | // - "localhost" | |
| 74 | // - IP address | |
| 75 | 9427 | if ("www".equals(domainAlias) || "localhost".equals(alias) |
| 76 | || alias.matches("[0-9]{1,3}(?:\\.[0-9]{1,3}){3}")) | |
| 77 | { | |
| 78 | 9422 | wikiId = getMainWikiId(); |
| 79 | } else { | |
| 80 | 5 | wikiId = normalizeWikiIdForNonExistentWikiDescriptor(domainAlias); |
| 81 | } | |
| 82 | ||
| 83 | // Create a virtual descriptor and save it so that next call will resolve to it directly without needing | |
| 84 | // to query the entity store. | |
| 85 | // this.wikiDescriptorCache.add(new WikiDescriptor(wikiId, alias)); | |
| 86 | // TODO: uncomment theses lines, find a solution | |
| 87 | } | |
| 88 | ||
| 89 | 20949 | return wikiId; |
| 90 | } | |
| 91 | } |