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:
019: /* $Id: URLInformation.java 550461 2007-06-25 11:55:02Z andreas $ */
020:
021: package org.apache.lenya.cms.publication;
022:
023: /**
024: * This class resolves all Lenya-specific information from a webapp URL.
025: */
026: public class URLInformation {
027:
028: private String publicationId = null;
029: private String area = null;
030: private String completeArea = null;
031: private String documentUrl = null;
032:
033: private String url;
034:
035: /**
036: * Returns the area (without the "webdav" prefix).
037: * @return A string.
038: */
039: public String getArea() {
040: if (this .area == null) {
041: String completeArea = getCompleteArea();
042: if (Publication.DAV_AREA.equals(completeArea)) {
043: this .area = Publication.AUTHORING_AREA;
044: } else {
045: this .area = completeArea;
046: }
047: }
048: return this .area;
049: }
050:
051: /**
052: * Returns the complete area (including the "webdav" prefix).
053: * @return A string.
054: */
055: public String getCompleteArea() {
056: String pubId = getPublicationId();
057: if (this .completeArea == null && pubId != null) {
058: String pubUrl = this .url.substring(pubId.length());
059: if (pubUrl.startsWith("/")) {
060: this .completeArea = extractBeforeSlash(pubUrl
061: .substring(1));
062: } else {
063: this .completeArea = null;
064: }
065: }
066: return this .completeArea;
067: }
068:
069: /**
070: * Returns the document URL.
071: * @return A string.
072: */
073: public String getDocumentUrl() {
074: if (this .documentUrl == null) {
075: String pubId = getPublicationId();
076: String area = getCompleteArea();
077: if (pubId != null && area != null) {
078: String prefix = pubId + "/" + area;
079: this .documentUrl = this .url.substring(prefix.length());
080: }
081: }
082: return this .documentUrl;
083: }
084:
085: /**
086: * Returns the publication ID.
087: * @return A string.
088: */
089: public String getPublicationId() {
090: if (this .publicationId == null) {
091: this .publicationId = extractBeforeSlash(this .url);
092: }
093: return this .publicationId;
094: }
095:
096: protected String extractBeforeSlash(String remaining) {
097:
098: if (remaining.length() == 0) {
099: return null;
100: }
101:
102: String step;
103: int slashIndex = remaining.indexOf('/');
104: if (slashIndex == -1) {
105: step = remaining;
106: } else {
107: step = remaining.substring(0, slashIndex);
108: }
109: return step;
110: }
111:
112: /**
113: * Ctor.
114: * @param webappUrl A webapp URL (without context prefix).
115: */
116: public URLInformation(String webappUrl) {
117:
118: if (!webappUrl.startsWith("/")) {
119: throw new RuntimeException("The URL [" + webappUrl
120: + "] doesn't start with a slash!");
121: }
122:
123: this .url = webappUrl.substring(1);
124: }
125: }
|