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: package org.apache.cocoon.environment.commandline;
018:
019: import org.apache.avalon.framework.logger.Logger;
020:
021: import org.apache.cocoon.Constants;
022: import org.apache.cocoon.environment.ObjectModelHelper;
023:
024: import java.io.File;
025: import java.io.OutputStream;
026: import java.net.MalformedURLException;
027: import java.util.Map;
028: import java.util.List;
029:
030: /**
031: * This environment is used to save the requested file to disk.
032: *
033: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
034: * @author <a href="mailto:uv@upaya.co.uk">Upayavira</a>
035: * @version $Id: FileSavingEnvironment.java 433543 2006-08-22 06:22:54Z crossley $
036: */
037: public class FileSavingEnvironment extends
038: AbstractCommandLineEnvironment {
039:
040: protected boolean modified = true;
041: protected long sourceLastModified = 0L;
042:
043: public FileSavingEnvironment(String uri, long lastModified,
044: File context, Map attributes, Map parameters, Map headers,
045: Map links, List gatheredLinks,
046: CommandLineContext cliContext, OutputStream stream,
047: Logger log) throws MalformedURLException {
048: super (uri, null, context, stream, log);
049: this .objectModel.put(ObjectModelHelper.REQUEST_OBJECT,
050: new CommandLineRequest(this , null, uri, null,
051: attributes, parameters, headers));
052: this .objectModel.put(ObjectModelHelper.RESPONSE_OBJECT,
053: new CommandLineResponse());
054: this .objectModel.put(ObjectModelHelper.CONTEXT_OBJECT,
055: cliContext);
056: this .sourceLastModified = lastModified;
057: if (links != null) {
058: this .objectModel.put(Constants.LINK_OBJECT, links);
059: }
060: if (gatheredLinks != null) {
061: this .objectModel.put(Constants.LINK_COLLECTION_OBJECT,
062: gatheredLinks);
063: }
064: }
065:
066: public FileSavingEnvironment(String uri, File context,
067: Map attributes, Map parameters, Map headers, Map links,
068: List gatheredLinks, CommandLineContext cliContext,
069: OutputStream stream, Logger log)
070: throws MalformedURLException {
071: this (uri, 0L, context, attributes, parameters, headers, links,
072: gatheredLinks, cliContext, stream, log);
073: }
074:
075: /**
076: * Check if the response has been modified since the same
077: * "resource" was requested.
078: * The caller has to test if it is really the same "resource"
079: * which is requested.
080: * @return true if the response is modified or if the
081: * environment is not able to test it
082: */
083: public boolean isResponseModified(long cacheLastModified) {
084: if (cacheLastModified != 0) {
085: return cacheLastModified / 1000 > sourceLastModified / 1000;
086: }
087: return true;
088: }
089:
090: /**
091: * Mark the response as not modified.
092: */
093: public void setResponseIsNotModified() {
094: this .modified = false;
095: }
096:
097: public boolean isModified() {
098: return this.modified;
099: }
100: }
|