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.publication;
019:
020: import java.util.Arrays;
021: import java.util.Map;
022:
023: import org.apache.avalon.framework.service.ServiceException;
024: import org.apache.avalon.framework.service.ServiceManager;
025: import org.apache.cocoon.environment.ObjectModelHelper;
026: import org.apache.cocoon.environment.Request;
027: import org.apache.lenya.cms.repository.RepositoryException;
028: import org.apache.lenya.cms.repository.RepositoryUtil;
029: import org.apache.lenya.cms.repository.Session;
030: import org.apache.lenya.util.ServletHelper;
031:
032: /**
033: * Publication utility.
034: */
035: public class PublicationUtil {
036:
037: /**
038: * Creates a new publication. The publication ID is resolved from the request URI. The servlet
039: * context path is resolved from the context object.
040: * @param manager The service manager.
041: * @param objectModel The object model of the Cocoon component.
042: * @return a <code>Publication</code>
043: * @throws PublicationException if there was a problem creating the publication.
044: */
045: public static Publication getPublication(ServiceManager manager,
046: Map objectModel) throws PublicationException {
047: return getPublication(manager, ObjectModelHelper
048: .getRequest(objectModel));
049: }
050:
051: /**
052: * Creates a new publication based on a request.
053: * @param manager The service manager.
054: * @param request A request.
055: * @return A publication.
056: * @throws PublicationException if there was a problem creating the publication.
057: */
058: public static Publication getPublication(ServiceManager manager,
059: Request request) throws PublicationException {
060: Session session;
061: try {
062: session = RepositoryUtil.getSession(manager, request);
063: } catch (RepositoryException e) {
064: throw new PublicationException(e);
065: }
066: DocumentFactory factory = DocumentUtil.createDocumentFactory(
067: manager, session);
068: String webappUrl = ServletHelper.getWebappURI(request);
069: return getPublicationFromUrl(manager, factory, webappUrl);
070: }
071:
072: /**
073: * Creates a publication from a webapp URL and a servlet context directory.
074: * @param manager The service manager.
075: * @param factory The factory.
076: * @param webappUrl The URL within the web application (without context prefix)
077: * @return A publication
078: * @throws PublicationException when something went wrong
079: */
080: public static Publication getPublicationFromUrl(
081: ServiceManager manager, DocumentFactory factory,
082: String webappUrl) throws PublicationException {
083: URLInformation info = new URLInformation(webappUrl);
084: String pubId = info.getPublicationId();
085: return factory.getPublication(pubId);
086: }
087:
088: /**
089: * Returns a list of all available publications.
090: * @param manager The service manager.
091: * @param factory The document factory.
092: * @return An array of publications.
093: * @throws PublicationException if an error occurs.
094: */
095: public static Publication[] getPublications(ServiceManager manager,
096: DocumentFactory factory) throws PublicationException {
097: PublicationManager pubManager = null;
098: try {
099: pubManager = (PublicationManager) manager
100: .lookup(PublicationManager.ROLE);
101: return pubManager.getPublications(factory);
102: } catch (ServiceException e) {
103: throw new PublicationException(e);
104: } finally {
105: if (pubManager != null) {
106: manager.release(pubManager);
107: }
108: }
109: }
110:
111: /**
112: * Checks if a publication id is valid.
113: * @param id
114: * @return true if the id contains only lowercase letters and/or numbers, and is not an empty
115: * string.
116: */
117: public static boolean isValidPublicationID(String id) {
118: return id.matches("[a-z0-9]+");
119: }
120:
121: private static final String[] areas = { Publication.AUTHORING_AREA,
122: Publication.DAV_AREA, Publication.STAGING_AREA,
123: Publication.LIVE_AREA, Publication.ARCHIVE_AREA,
124: Publication.TRASH_AREA };
125:
126: /**
127: * Returns if a given string is a valid area name.
128: * @param area The area string to test.
129: * @return A boolean value.
130: */
131: public static boolean isValidArea(String area) {
132: return area != null && Arrays.asList(areas).contains(area);
133: }
134:
135: }
|