001/*
002 *  Copyright 2016 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.core.cocoon;
017
018import java.io.IOException;
019import java.io.OutputStream;
020import java.io.Serializable;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.cocoon.ProcessingException;
024import org.apache.excalibur.source.Source;
025import org.apache.excalibur.source.SourceValidity;
026
027/**
028 * Interface used to handle resources
029 */
030public interface ResourceHandler
031{
032    /** Minimum priority. */
033    public static final int MIN_PRIORITY = Integer.MAX_VALUE;
034    /** Maximum priority. */
035    public static final int MAX_PRIORITY = 0;
036    
037    /**
038     * Initialize the resource handler with a resource.
039     * @param source The source uri
040     * @param par The parameters
041     * @return the resolved source
042     * @throws IOException If an error occurs
043     * @throws ProcessingException If an error occurs
044     */
045    public Source setup(String source, Parameters par) throws IOException, ProcessingException;
046    
047    /**
048     * Generate the resource configured during setup, and output it
049     * @param source The source
050     * @param out The output stream to write to
051     * @param parameters The parameters
052     * @throws IOException If an error occurs
053     * @throws ProcessingException If an error occurs
054     */
055    public void generateResource(Source source, OutputStream out, Parameters parameters) throws IOException, ProcessingException;
056    
057    /**
058     * Determines if the resource is supported by this handler
059     * @param src The uri of resource
060     * @return <code>true</code> if the resource is supported
061     */
062    public boolean isSupported(String src);
063    
064    /**
065     * Get the priority of this handler
066     * @return the priority
067     */
068    public int getPriority();
069    
070    /**
071     * Return the mime type of the configured resource.
072     * @param source The source
073     * @param parameters The parameters
074     * @return The mime type.
075     */
076    public String getMimeType(Source source, Parameters parameters);
077    
078    /**
079     * Get the unique key for this resource, for cache purpose.
080     * @param source The source
081     * @param parameters The parameters
082     * @return The cache key.
083     */
084    public Serializable getKey(Source source, Parameters parameters);
085    
086    /**
087     * Get the resource validity, for cache purpose.
088     * @param source The source
089     * @param parameters The parameters
090     * @return The resource validity.
091     */
092    public SourceValidity getValidity(Source source, Parameters parameters);
093    
094    /**
095     * Get the resource size, if available.
096     * @param source The source
097     * @param parameters The parameters
098     * @return The resource size.
099     */
100    public long getSize(Source source, Parameters parameters);
101    
102    /**
103     * Get the resource last modified time
104     * @param source The source
105     * @param parameters The parameters
106     * @return The last modified
107     */
108    public long getLastModified(Source source, Parameters parameters);
109}