    001/*
    002    Licensed to the Apache Software Foundation (ASF) under one
    003    or more contributor license agreements.  See the NOTICE file
    004    distributed with this work for additional information
    005    regarding copyright ownership.  The ASF licenses this file
    006    to you under the Apache License, Version 2.0 (the
    007    "License"); you may not use this file except in compliance
    008    with the License.  You may obtain a copy of the License at
    009
    010       http://www.apache.org/licenses/LICENSE-2.0
    011
    012    Unless required by applicable law or agreed to in writing,
    013    software distributed under the License is distributed on an
    014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015    KIND, either express or implied.  See the License for the
    016    specific language governing permissions and limitations
    017    under the License.
    018 */
    019package org.apache.wiki.api.providers;
    020
    021import org.apache.wiki.api.core.Attachment;
    022import org.apache.wiki.api.core.Page;
    023import org.apache.wiki.api.exceptions.ProviderException;
    024import org.apache.wiki.api.search.QueryItem;
    025
    026import java.io.IOException;
    027import java.io.InputStream;
    028import java.util.Collection;
    029import java.util.Date;
    030import java.util.List;
    031
    032
    033/**
    034 *  Defines an attachment provider - a class which is capable of saving binary data as attachments.
    035 *  <P>
    036 *  The difference between this class and WikiPageProvider is that there PageProviders handle Unicode text, whereas we handle binary data.
    037 *  While there are quite a lot of similarities in how we handle things, many providers can really use just one.  In addition,
    038 *  since binary files can be really large, we rely on Input/OutputStreams.
    039 */
    040public interface AttachmentProvider extends WikiProvider {
    041
    042    /** Property that supplies the directory used to store attachments. */
    043    String PROP_STORAGEDIR = "jspwiki.basicAttachmentProvider.storageDir";
    044
    045    /**
    046     *  Put new attachment data.
    047     *
    048     *  @param att Attachment object to add new data to
    049     *  @param data The stream from which the provider should read the data
    050     *  @throws IOException If writing fails
    051     *  @throws ProviderException If there are other errors.
    052     */
    053    void putAttachmentData( Attachment att, InputStream data ) throws ProviderException, IOException;
    054
    055    /**
    056     *  Get attachment data.
    057     *
    058     *  @param att The attachment
    059     *  @return An InputStream which contains the raw data of the object. It's your responsibility to close it.
    060     *  @throws ProviderException If the attachment cannot be found
    061     *  @throws IOException If the attachment cannot be opened
    062     */
    063    InputStream getAttachmentData( Attachment att ) throws ProviderException, IOException;
    064
    065    /**
    066     *  Lists all attachments attached to a page.
    067     *
    068     *  @param page The page to list the attachments from.
    069     *  @return A collection of Attachment objects.  May be empty, but never null.
    070     *  @throws ProviderException If something goes wrong when listing the attachments.
    071     */
    072    List< Attachment > listAttachments( Page page ) throws ProviderException;
    073
    074    /**
    075     * Finds attachments based on the query.
    076     *
    077     * @param query An array of QueryItem objects to search for
    078     * @return A Collection of Attachment objects.  May be empty, but never null.
    079     */
    080    Collection< Attachment > findAttachments( QueryItem[] query );
    081
    082    /**
    083     *  Lists changed attachments since given date.  Can also be used to fetch a list of all pages.
    084     *  <P>
    085     *  This is different from WikiPageProvider, where you basically get a list of all pages, then sort them locally.  However, since some
    086     *  providers can be more efficient in locating recently changed files (like any database) than our non-optimized Java code, it makes
    087     *  more sense to fetch the whole list this way.
    088     *  <P>
    089     *  To get all files, call this with Date(0L);
    090     *
    091     *  @param timestamp List all files from this date onward.
    092     *  @return A List of Attachment objects, in most-recently-changed first order.
    093     *  @throws ProviderException If something goes wrong.
    094     */
    095    List< Attachment > listAllChanged( Date timestamp ) throws ProviderException;
    096
    097    /**
    098     *  Returns info about an attachment.
    099     *
    100     *  @param page The parent page
    101     *  @param name The name of the attachment
    102     *  @param version The version of the attachment (it's okay to use {@link WikiProvider#.LATEST_VERSION} to find the latest one)
    103     *  @return An attachment object
    104     *  @throws ProviderException If the attachment cannot be found or some other error occurs.
    105     */
    106    Attachment getAttachmentInfo( Page page, String name, int version ) throws ProviderException;
    107
    108    /**
    109     *  Returns version history.  Each element should be an Attachment.
    110     *
    111     *  @param att The attachment for which to find the version history for.
    112     *  @return A List of Attachment objects.
    113     */
    114    List< Attachment > getVersionHistory( Attachment att );
    115
    116    /**
    117     *  Removes a specific version from the repository.  The implementations should really do no more security checks, since that is the
    118     *  domain of the AttachmentManager.  Just delete it as efficiently as you can.
    119     *
    120     *  @since 2.0.19.
    121     *  @param att Attachment to be removed.  The version field is checked, and thus only that version is removed.
    122     *  @throws ProviderException If the attachment cannot be removed for some reason.
    123     */
    124    void deleteVersion( Attachment att ) throws ProviderException;
    125
    126    /**
    127     *  Removes an entire page from the repository.  The implementations should really do no more security checks, since that is the domain
    128     *  of the AttachmentManager.  Just delete it as efficiently as you can.  You should also delete any auxiliary files and directories
    129     *  that belong to this attachment, IF they were created by this provider.
    130     *
    131     *  @since 2.0.17.
    132     *  @param att Attachment to delete.
    133     *  @throws ProviderException If the page could not be removed for some reason.
    134     */
    135    void deleteAttachment( Attachment att ) throws ProviderException;
    136
    137    /**
    138     * Move all the attachments for a given page so that they are attached to a new page.
    139     *
    140     * @param oldParent Name of the page we are to move the attachments from.
    141     * @param newParent Name of the page we are to move the attachments to.
    142     * @throws ProviderException If the attachments could not be moved for some reason.
    143     */
    144    void moveAttachmentsForPage( String oldParent, String newParent ) throws ProviderException;
    145
    146}
    147
    148