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.site.usecases;
019:
020: import java.io.IOException;
021:
022: import org.apache.avalon.framework.service.ServiceException;
023: import org.apache.cocoon.servlet.multipart.Part;
024: import org.apache.lenya.cms.metadata.MetaDataException;
025: import org.apache.lenya.cms.publication.Document;
026: import org.apache.lenya.cms.publication.DocumentException;
027: import org.apache.lenya.cms.publication.ResourceWrapper;
028: import org.apache.lenya.cms.repository.RepositoryException;
029: import org.apache.lenya.util.ServletHelper;
030:
031: /**
032: * Create a new resource document.
033: */
034: public class CreateResource extends CreateDocument {
035:
036: protected static final String PARAMETER_FILE = "file";
037:
038: protected static final String MESSAGE_UPLOAD_DISABLED = "upload-disabled";
039: protected static final String MESSAGE_UPLOAD_ENTER_TITLE = "upload-enter-title";
040: protected static final String MESSAGE_UPLOAD_CHOOSE_FILE = "upload-choose-file";
041: protected static final String MESSAGE_UPLOAD_SIZE_EXCEEDED = "upload-size-exceeded";
042: protected static final String MESSAGE_UPLOAD_RESET = "upload-reset";
043: protected static final String MESSAGE_UPLOAD_MISSING_EXTENSION = "upload-missing-extension";
044:
045: protected void doCheckPreconditions() throws Exception {
046: super .doCheckPreconditions();
047: if (!ServletHelper.isUploadEnabled(manager)) {
048: addErrorMessage(MESSAGE_UPLOAD_DISABLED);
049: }
050: }
051:
052: /**
053: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
054: */
055: protected void doCheckExecutionConditions() throws Exception {
056:
057: super .doCheckExecutionConditions();
058:
059: Part file = getPart(PARAMETER_FILE);
060: if (hasErrors() && file != null) {
061: resetUploadField();
062: } else {
063: if (file == null) {
064: addErrorMessage(MESSAGE_UPLOAD_CHOOSE_FILE);
065: } else if (file.isRejected()) {
066: String[] params = { Integer.toString(file.getSize()) };
067: addErrorMessage(MESSAGE_UPLOAD_SIZE_EXCEEDED, params);
068: }
069: }
070: }
071:
072: /**
073: * The browser can't set the value of the file upload widget for security reasons, so we have to
074: * remove the file parameter and the user has to select the file again.
075: */
076: protected void resetUploadField() {
077: deleteParameter(PARAMETER_FILE);
078: addErrorMessage(MESSAGE_UPLOAD_RESET);
079: }
080:
081: /**
082: * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
083: */
084: protected void initParameters() {
085: super .initParameters();
086: deleteParameter(RESOURCE_TYPES);
087: }
088:
089: /**
090: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
091: */
092: protected void doExecute() throws Exception {
093: super .doExecute();
094: addResource();
095: }
096:
097: /**
098: * Adds the ressource. If asset upload is not enabled, an error message is added.
099: * @throws IOException
100: * @throws ServiceException
101: * @throws DocumentException
102: * @throws MetaDataException
103: * @throws RepositoryException
104: */
105: protected void addResource() throws ServiceException, IOException,
106: DocumentException, RepositoryException, MetaDataException {
107:
108: if (getLogger().isDebugEnabled())
109: getLogger().debug("Assets::addAsset() called");
110:
111: Part file = getPart(PARAMETER_FILE);
112: Document document = getNewDocument();
113: ResourceWrapper wrapper = new ResourceWrapper(document,
114: this .manager, getLogger());
115: wrapper.write(file);
116: }
117:
118: protected String getSourceExtension() {
119: String extension = "";
120:
121: Part file = getPart(PARAMETER_FILE);
122: String fileName = file.getFileName();
123: int lastDotIndex = fileName.lastIndexOf(".");
124: if (lastDotIndex > -1) {
125: extension = fileName.substring(lastDotIndex + 1);
126: } else {
127: addErrorMessage(MESSAGE_UPLOAD_MISSING_EXTENSION);
128: }
129: return extension.toLowerCase();
130: }
131:
132: }
|