001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ServeContent.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.cmf.elements;
009:
010: import com.uwyn.rife.cmf.dam.ContentManager;
011: import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContentFactory;
012: import com.uwyn.rife.cmf.dam.exceptions.ContentManagerException;
013: import com.uwyn.rife.config.Config;
014: import com.uwyn.rife.database.Datasource;
015: import com.uwyn.rife.database.Datasources;
016: import com.uwyn.rife.engine.Element;
017: import com.uwyn.rife.engine.annotations.Elem;
018: import com.uwyn.rife.tools.ExceptionUtils;
019: import java.net.URLDecoder;
020: import java.util.logging.Logger;
021: import javax.servlet.http.HttpServletResponse;
022:
023: @Elem
024: public class ServeContent extends Element {
025: private String mRepositoryName = null;
026:
027: public void setRepositoryName(String repositoryName) {
028: mRepositoryName = repositoryName;
029: }
030:
031: public String getRepositoryName() {
032: return mRepositoryName;
033: }
034:
035: public void processElement() {
036: Datasource datasource = getPropertyTyped("datasource",
037: Datasource.class);
038: if (null == datasource) {
039: String datasource_name = Config.getRepInstance().getString(
040: "DATASOURCE", "datasource");
041: datasource = Datasources.getRepInstance().getDatasource(
042: datasource_name);
043: if (null == datasource) {
044: throw new MissingDatasourceException(getElementInfo()
045: .getId());
046: }
047: }
048:
049: // obtain the optional repository name
050: setRepositoryName(getPropertyString("repository"));
051:
052: // retrieve and output the content that corresponds to the path info
053: ContentManager manager = DatabaseContentFactory
054: .getInstance(datasource);
055:
056: // get the content path
057: String content_path = getEmbedValue();
058: try {
059: // get the content path from the embed value
060: if (content_path != null) {
061: print(manager.getContentForHtml(content_path, this ,
062: "serve"));
063: return;
064: }
065:
066: // get the content path from the path info
067: content_path = getPathInfo();
068: if (content_path != null) {
069: content_path = URLDecoder.decode(content_path);
070: }
071:
072: // filter the content path
073: content_path = filterPath(content_path);
074:
075: // prepend the repository name if it was provided
076: if (mRepositoryName != null) {
077: StringBuilder buffer = new StringBuilder(
078: mRepositoryName);
079: buffer.append(":");
080: buffer.append(content_path);
081: content_path = buffer.toString();
082: }
083:
084: // serve the content for the path, if the path is valid
085: if (content_path != null && !content_path.equals("/")) {
086: manager.serveContentData(this , content_path);
087: return;
088: }
089: } catch (ContentManagerException e) {
090: Logger.getLogger("com.uwyn.rife.cmf").severe(
091: ExceptionUtils.getExceptionStackTrace(e));
092: setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
093: return;
094: }
095:
096: defer();
097: }
098:
099: public String filterPath(String path) {
100: return path;
101: }
102: }
|