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.editors;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.apache.cocoon.environment.Request;
027: import org.apache.lenya.ac.User;
028: import org.apache.lenya.cms.cocoon.components.context.ContextUtility;
029: import org.apache.lenya.cms.linking.LinkRewriter;
030: import org.apache.lenya.cms.linking.OutgoingLinkRewriter;
031: import org.apache.lenya.cms.publication.Document;
032: import org.apache.lenya.cms.publication.DocumentException;
033: import org.apache.lenya.cms.usecase.UsecaseException;
034: import org.apache.lenya.cms.usecase.UsecaseInvoker;
035: import org.apache.lenya.cms.usecase.UsecaseMessage;
036: import org.apache.lenya.util.ServletHelper;
037: import org.apache.lenya.cms.site.usecases.CreateResource;
038:
039: /**
040: * Usecase to insert an image into a document.
041: *
042: * @version $Id: InsertAsset.java 567644 2007-08-20 10:30:20Z andreas $
043: */
044: public class InsertAsset extends CreateResource {
045:
046: protected static final String DOCUMENT = "document";
047:
048: /**
049: * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
050: */
051: protected void initParameters() {
052: super .initParameters();
053:
054: deleteParameter(RELATIONS);
055: loadResources();
056:
057: setParameter(DOCUMENT, getSourceDocument());
058: try {
059: User user = getSession().getIdentity().getUser();
060: if (user != null) {
061: setParameter("creator", user.getId());
062: }
063: } catch (final Exception e) {
064: throw new RuntimeException(e);
065: }
066: }
067:
068: protected void doCheckPreconditions() throws Exception {
069: if (!ServletHelper.isUploadEnabled(manager)) {
070: addErrorMessage("Upload is not enabled please check local.build.properties!");
071: }
072: }
073:
074: protected Document[] getResourceDocuments()
075: throws DocumentException {
076: String mimeTypePrefix = getParameterAsString("mimeTypePrefix",
077: "");
078: List list = new ArrayList();
079: Document[] docs = getSourceDocument().area().getDocuments();
080: for (int i = 0; i < docs.length; i++) {
081: if (docs[i].getResourceType().getName().equals("resource")) {
082: String resMimeType = docs[i].getMimeType();
083: if (resMimeType == null) {
084: resMimeType = "unknown";
085: }
086: if (resMimeType.startsWith(mimeTypePrefix)) {
087: list.add(docs[i]);
088: }
089: }
090: }
091: return (Document[]) list.toArray(new Document[list.size()]);
092: }
093:
094: protected void loadResources() {
095: ContextUtility context = null;
096: try {
097: context = (ContextUtility) this .manager
098: .lookup(ContextUtility.ROLE);
099: Request request = context.getRequest();
100: boolean ssl = request.isSecure();
101:
102: LinkRewriter rewriter = new OutgoingLinkRewriter(
103: this .manager, getSession(), getSourceURL(), ssl,
104: false, false);
105: Map asset2proxyUrl = new HashMap();
106: setParameter("asset2proxyUrl", asset2proxyUrl);
107:
108: Document[] resources = getResourceDocuments();
109:
110: for (int i = 0; i < resources.length; i++) {
111:
112: String originalUrl = resources[i]
113: .getCanonicalWebappURL();
114: int lastDotIndex = originalUrl.lastIndexOf('.');
115: String extension = resources[i].getSourceExtension();
116: String url = originalUrl.substring(0, lastDotIndex)
117: + "." + extension;
118:
119: String proxyUrl = rewriter.rewrite(url);
120: asset2proxyUrl.put(resources[i], proxyUrl);
121:
122: }
123:
124: setParameter("assets", resources);
125: } catch (final Exception e) {
126: throw new RuntimeException(e);
127: } finally {
128: if (context != null) {
129: this .manager.release(context);
130: }
131: }
132: }
133:
134: /**
135: * Delegates to the main assets usecase; the name of the usecase being
136: * delegated to is set in the configuration parameter "asset-usecase".
137: *
138: * @see org.apache.lenya.cms.usecase.Usecase#advance()
139: */
140: public void advance() throws UsecaseException {
141: super .advance();
142: if (getParameterAsBoolean("upload", false)) {
143: UsecaseInvoker invoker = null;
144: try {
145: invoker = (UsecaseInvoker) this .manager
146: .lookup(UsecaseInvoker.ROLE);
147: String usecaseName = getParameterAsString("asset-usecase");
148:
149: if (getLogger().isDebugEnabled())
150: getLogger().debug(
151: "InsertAsset::advance() calling invoker with usecaseName ["
152: + usecaseName + "]");
153: invoker.invoke(getSourceURL(), usecaseName,
154: getParameters());
155: if (invoker.getResult() == UsecaseInvoker.SUCCESS) {
156: loadResources();
157: deleteParameter("title");
158: deleteParameter("creator");
159: deleteParameter("rights");
160: } else {
161: List messages = invoker.getErrorMessages();
162: for (Iterator i = messages.iterator(); i.hasNext();) {
163: UsecaseMessage message = (UsecaseMessage) i
164: .next();
165: addErrorMessage(message.getMessage());
166: }
167: }
168: /*
169: * The <input type="file"/> value cannot be passed to the next
170: * screen because the browser doesn't allow this for security
171: * reasons.
172: */
173: deleteParameter("file");
174: } catch (Exception e) {
175: throw new UsecaseException(e);
176: } finally {
177: if (invoker != null) {
178: this.manager.release(invoker);
179: }
180: }
181: }
182: }
183:
184: }
|