| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.apache.lenya.cms.cocoon.source; |
| |
| import java.io.IOException; |
| import java.net.MalformedURLException; |
| import java.util.Map; |
| |
| import javax.servlet.http.HttpServletRequest; |
| |
| import org.apache.avalon.framework.configuration.Configuration; |
| import org.apache.avalon.framework.configuration.DefaultConfiguration; |
| import org.apache.cocoon.components.flow.FlowHelper; |
| import org.apache.cocoon.components.modules.input.JXPathHelper; |
| import org.apache.cocoon.components.modules.input.JXPathHelperConfiguration; |
| import org.apache.cocoon.processing.ProcessInfoProvider; |
| import org.apache.cocoon.spring.configurator.WebAppContextUtils; |
| import org.apache.cocoon.util.AbstractLogEnabled; |
| import org.apache.excalibur.source.Source; |
| import org.apache.excalibur.source.SourceException; |
| import org.apache.excalibur.source.SourceFactory; |
| import org.apache.lenya.cms.publication.Publication; |
| import org.apache.lenya.cms.publication.Repository; |
| import org.apache.lenya.cms.publication.Session; |
| import org.apache.lenya.cms.repository.NodeFactory; |
| import org.apache.lenya.cms.repository.RepositoryException; |
| import org.apache.lenya.util.Query; |
| |
| /** |
| * A factory for the "lenya" scheme (virtual protocol), which is used to resolve any src="lenya:..." |
| * attributes in sitemaps. This implementation constructs the path to the source document from the |
| * page envelope and delegates any further resolving to the "context" source resolver of Cocoon. |
| * |
| * @version $Id$ |
| */ |
| public class LenyaSourceFactory extends AbstractLogEnabled implements SourceFactory { |
| |
| protected static final String SCHEME = "lenya:"; |
| |
| /** fallback if no configuration is available */ |
| protected static final String DEFAULT_DELEGATION_SCHEME = "context:"; |
| protected static final String DEFAULT_DELEGATION_PREFIX = "/" |
| + Publication.PUBLICATION_PREFIX_URI; |
| |
| private NodeFactory nodeFactory; |
| private Repository repository; |
| |
| /** |
| * @see org.apache.excalibur.source.SourceFactory#getSource(java.lang.String, java.util.Map) |
| */ |
| public Source getSource(final String location, final Map parameters) |
| throws MalformedURLException, IOException, SourceException { |
| |
| String sessionName = null; |
| |
| String[] uriAndQuery = location.split("\\?"); |
| if (uriAndQuery.length > 1) { |
| Query query = new Query(uriAndQuery[1]); |
| sessionName = query.getValue("session"); |
| } |
| |
| Session session; |
| try { |
| session = getSession(sessionName); |
| } catch (RepositoryException e) { |
| throw new RuntimeException(e); |
| } |
| |
| if (getLogger().isDebugEnabled()) { |
| getLogger().debug("Creating repository source for URI [" + location + "]"); |
| } |
| |
| return new RepositorySource(this.nodeFactory, location, session, getLogger()); |
| |
| } |
| |
| protected Session getSession(String sessionName) throws RepositoryException { |
| Session session; |
| ProcessInfoProvider process = (ProcessInfoProvider) WebAppContextUtils |
| .getCurrentWebApplicationContext().getBean(ProcessInfoProvider.ROLE); |
| if (sessionName == null) { |
| HttpServletRequest request = process.getRequest(); |
| session = this.repository.getSession(request); |
| } else if (sessionName.equals("usecase")) { |
| session = getUsecaseSession(process.getObjectModel()); |
| } else { |
| throw new RepositoryException("Invalid session: [" + sessionName + "]"); |
| } |
| |
| return session; |
| } |
| |
| protected Session getUsecaseSession(Map objectModel) throws RepositoryException { |
| try { |
| Configuration config = new DefaultConfiguration("foo"); |
| JXPathHelperConfiguration helperConfig = JXPathHelper.setup(config); |
| Object contextObject = FlowHelper.getContextObject(objectModel); |
| return (Session) JXPathHelper.getAttribute("usecase/session", config, helperConfig, |
| contextObject); |
| } catch (Exception e) { |
| throw new RepositoryException(e); |
| } |
| } |
| |
| /** |
| * Does nothing because the delegated factory does this. |
| * @see org.apache.excalibur.source.SourceFactory#release(org.apache.excalibur.source.Source) |
| */ |
| public void release(Source source) { |
| // do nothing |
| } |
| |
| public void setNodeFactory(NodeFactory nodeFactory) { |
| this.nodeFactory = nodeFactory; |
| } |
| |
| public void setRepository(Repository repository) { |
| this.repository = repository; |
| } |
| } |