001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.linking;
019:
020: import org.apache.lenya.cms.publication.Area;
021: import org.apache.lenya.cms.publication.Document;
022: import org.apache.lenya.cms.publication.DocumentFactory;
023:
024: /**
025: * <p>
026: * Converts webapp URLs to UUID-based internal links. If the URL
027: * doesn't refer to a document, the original URL is returned.
028: * </p>
029: * <p>
030: * Objects of this class are not thread-safe.
031: * </p>
032: */
033: public class UrlToUuidRewriter implements LinkRewriter {
034:
035: private Area area;
036:
037: /**
038: * @param area The area to operate in.
039: */
040: public UrlToUuidRewriter(Area area) {
041: this .area = area;
042: }
043:
044: public boolean matches(String url) {
045: return url.startsWith("/" + this .area.getPublication().getId()
046: + "/" + this .area.getName());
047: }
048:
049: public String rewrite(String webappUrl) {
050:
051: String anchor = null;
052: String url = null;
053:
054: int anchorIndex = webappUrl.indexOf("#");
055: if (anchorIndex > -1) {
056: url = webappUrl.substring(0, anchorIndex);
057: anchor = webappUrl.substring(anchorIndex + 1);
058: } else {
059: url = webappUrl;
060: }
061:
062: String[] linkUrlAndQuery = url.split("\\?");
063: String linkUrl = linkUrlAndQuery[0];
064: String queryString = null;
065: if (linkUrlAndQuery.length > 1) {
066: queryString = linkUrlAndQuery[1];
067: }
068:
069: DocumentFactory factory = this .area.getPublication()
070: .getFactory();
071:
072: String rewrittenUrl;
073:
074: try {
075: if (factory.isDocument(linkUrl)) {
076: Document targetDocument = factory.getFromURL(linkUrl);
077: rewrittenUrl = getUuidBasedUri(targetDocument, anchor,
078: queryString);
079: } else {
080: rewrittenUrl = webappUrl;
081: }
082: } catch (Exception e) {
083: throw new RuntimeException(e);
084: }
085: return rewrittenUrl;
086: }
087:
088: /**
089: * Rewrites a link.
090: *
091: * @param targetDocument The target document.
092: * @param anchor The anchor (the string after the # character in the URL).
093: * @param queryString The query string without question mark.
094: * @return A UUID-based link URI.
095: */
096: protected String getUuidBasedUri(Document targetDocument,
097: String anchor, String queryString) {
098:
099: Link link = new Link();
100: link.setUuid(targetDocument.getUUID());
101:
102: String linkUri = link.getUri();
103:
104: if (anchor != null) {
105: linkUri += "#" + anchor;
106: }
107:
108: if (queryString != null) {
109: linkUri += "?" + queryString;
110: }
111:
112: return linkUri;
113: }
114:
115: }
|