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.usecases.webdav;
019:
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.avalon.framework.service.ServiceSelector;
023: import org.apache.excalibur.source.SourceResolver;
024: import org.apache.lenya.cms.metadata.MetaData;
025: import org.apache.lenya.cms.metadata.MetaDataException;
026: import org.apache.lenya.cms.metadata.dublincore.DublinCore;
027: import org.apache.lenya.cms.publication.Document;
028: import org.apache.lenya.cms.publication.DocumentFactory;
029: import org.apache.lenya.cms.publication.DocumentManager;
030: import org.apache.lenya.cms.publication.ResourceType;
031: import org.apache.lenya.cms.site.usecases.Create;
032:
033: /**
034: * Supports WebDAV PUT.
035: *
036: */
037: public class Mkcol extends Create {
038: // default is xhtml and xml but you can override it with the config
039: protected String TYPE = "xhtml";
040: protected String EXTENSION = "xml";
041: protected static final String ATTRIBUTE_TYPE = "resource-type";
042: protected static final String ELEMENT_EXTENSION = "extension";
043:
044: public void configure(Configuration config)
045: throws ConfigurationException {
046: super .configure(config);
047: Configuration typeConfig = config.getChild(ELEMENT_EXTENSION,
048: false);
049: if (typeConfig != null) {
050: this .EXTENSION = typeConfig.getValue();
051: this .TYPE = typeConfig.getAttribute(ATTRIBUTE_TYPE);
052: }
053: }
054:
055: /**
056: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
057: */
058: protected void doExecute() throws Exception {
059: // super.doExecute();
060: SourceResolver resolver = null;
061: ResourceType resourceType = null;
062:
063: try {
064: resolver = (SourceResolver) this .manager
065: .lookup(SourceResolver.ROLE);
066:
067: Document doc = getSourceDocument();
068: // sanity check
069: if (doc == null)
070: throw new IllegalArgumentException(
071: "illegal usage, source document may not be null");
072:
073: if (!doc.exists()) {
074: DocumentManager documentManager = null;
075: ServiceSelector selector = null;
076: try {
077: selector = (ServiceSelector) this .manager
078: .lookup(ResourceType.ROLE + "Selector");
079:
080: documentManager = (DocumentManager) this .manager
081: .lookup(DocumentManager.ROLE);
082:
083: DocumentFactory map = getDocumentFactory();
084: String path = doc.getPath();
085:
086: resourceType = (ResourceType) selector.select(TYPE);
087: ResourceType.Sample sample = resourceType
088: .getSample(resourceType.getSampleNames()[0]);
089: doc = documentManager.add(map, resourceType, sample
090: .getUri(), getPublication(), doc.getArea(),
091: path, doc.getLanguage(), EXTENSION, doc
092: .getName(), true);
093: doc.setMimeType(sample.getMimeType());
094:
095: setMetaData(doc);
096: } finally {
097: if (documentManager != null) {
098: this .manager.release(documentManager);
099: }
100: if (selector != null) {
101: if (resourceType != null) {
102: selector.release(resourceType);
103: }
104: this .manager.release(selector);
105: }
106: }
107: }
108:
109: } finally {
110: if (resolver != null) {
111: this .manager.release(resolver);
112: }
113: }
114: }
115:
116: /**
117: * Sets the meta data of the created document.
118: *
119: * @param document The document.
120: * @throws MetaDataException if an error occurs.
121: */
122: protected void setMetaData(Document document)
123: throws MetaDataException {
124:
125: if (document == null)
126: throw new IllegalArgumentException(
127: "parameter document may not be null");
128:
129: MetaData dcMetaData = document
130: .getMetaData(DublinCore.DC_NAMESPACE);
131: dcMetaData.setValue(DublinCore.ELEMENT_TITLE, document
132: .getName());
133: dcMetaData.setValue(DublinCore.ELEMENT_CREATOR, "");
134: dcMetaData.setValue(DublinCore.ELEMENT_PUBLISHER, "");
135: dcMetaData.setValue(DublinCore.ELEMENT_SUBJECT, "");
136: dcMetaData.setValue(DublinCore.ELEMENT_DATE, "");
137: dcMetaData.setValue(DublinCore.ELEMENT_RIGHTS, "");
138: dcMetaData.setValue(DublinCore.ELEMENT_LANGUAGE, document
139: .getLanguage());
140: }
141:
142: protected String getNewDocumentName() {
143: // TODO Auto-generated method stub
144: return null;
145: }
146:
147: protected String getNewDocumentPath() {
148: // TODO Auto-generated method stub
149: return null;
150: }
151:
152: protected String getDocumentTypeName() {
153: // TODO Auto-generated method stub
154: return null;
155: }
156:
157: protected String getSourceExtension() {
158: return EXTENSION;
159: }
160:
161: protected boolean createVersion() {
162: return false;
163: }
164:
165: }
|