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.attachment;
020
021import org.apache.wiki.WikiEngine;
022import org.apache.wiki.WikiPage;
023import org.apache.wiki.api.core.Engine;
024
025
026/**
027 *  Describes an attachment.  Attachments are actually derivatives of a WikiPage, since they do actually have a WikiName as well.
028 */
029public class Attachment extends WikiPage implements org.apache.wiki.api.core.Attachment {
030
031    private String m_fileName;
032    private final String m_parentName;
033    private boolean m_cacheable = true;
034
035    /**
036     * Creates a new attachment.  The final name of the attachment will be a synthesis of the parent page name and the file name.
037     *
038     * @param engine     The Engine which is hosting this attachment.
039     * @param parentPage The page which will contain this attachment.
040     * @param fileName   The file name for the attachment.
041     */
042    public Attachment( final Engine engine, final String parentPage, final String fileName ) {
043        super( engine, parentPage + "/" + fileName );
044        m_parentName = parentPage;
045        m_fileName = fileName;
046    }
047
048    /**
049     * Creates a new attachment.  The final name of the attachment will be a synthesis of the parent page name and the file name.
050     *
051     * @param engine     The Engine which is hosting this attachment.
052     * @param parentPage The page which will contain this attachment.
053     * @param fileName   The file name for the attachment.
054     * @deprecated kept for compatibility with page/attachment providers not using public API. Use {@code Attachment(Engine, String, String)} instead.
055     */
056    @Deprecated
057    public Attachment( final WikiEngine engine, final String parentPage, final String fileName ) {
058        this( ( Engine )engine, parentPage, fileName );
059    }
060
061    /**
062     * Returns a human-readable, only-debugging-suitable description.
063     *
064     * @return A debugging string
065     */
066    @Override
067    public String toString() {
068        return "Attachment [" + getName() + ";mod=" + getLastModified() + "]";
069    }
070
071    /**
072     * Returns the file name of the attachment.
073     *
074     * @return A String with the file name.
075     */
076    public String getFileName() {
077        return m_fileName;
078    }
079
080    /**
081     * Sets the file name of this attachment.
082     *
083     * @param name The name of the attachment.  Must be a legal file name without  the path.
084     */
085    public void setFileName( final String name ) {
086        m_fileName = name;
087    }
088
089    /**
090     * Returns the name of the parent of this Attachment, i.e. the page which contains this attachment.
091     *
092     * @return String depicting the parent of the attachment.
093     */
094    public String getParentName() {
095        return m_parentName;
096    }
097
098    /**
099     * Returns true, if this attachment can be cached by the user agent.  By default attachments are cacheable.
100     *
101     * @return False, if the attachment should not be cached by the user agent.
102     * @since 2.5.34
103     */
104    public boolean isCacheable() {
105        return m_cacheable;
106    }
107
108    /**
109     * Sets this attachment to be cacheable or not.  This mostly concerns things like DynamicAttachments, but it may be useful for
110     * certain AttachmentProviders as well.
111     *
112     * @param value True or false, depending on whether you want this attachment to be cacheable or not.
113     * @since 2.5.34
114     */
115    public void setCacheable( final boolean value ) {
116        m_cacheable = value;
117    }
118
119}