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;
022
023/**
024 *  A DynamicAttachment is an attachment which does not really exist, but is
025 *  created dynamically by a JSPWiki component.
026 *  <p>
027 *  Note that a DynamicAttachment might not be available before it is actually
028 *  created by a component (e.g. plugin), and therefore trying to access it
029 *  before that component has been invoked, might result in a surprising 404.
030 *  <p>
031 *  DynamicAttachments are not listed among regular attachments in the current
032 *  version.
033 *  <p>
034 *  Usage:
035 *
036 *  <pre>
037 *
038 *  class MyDynamicComponent implements DynamicAttachmentProvider
039 *  {
040 *  ...
041 *
042 *     DynamicAttachment destatt = mgr.getDynamicAttachment( destattname );
043 *
044 *     if( destatt == null )
045 *     {
046 *         destatt = new DynamicAttachment( context.getEngine(),
047 *                                          context.getPage().getName(),
048 *                                          destfilename,
049 *                                          this );
050 *         destatt.setCacheable( false );
051 *     }
052 *
053 *     // This is used to check whether the attachment is modified or not
054 *     // so don't forget to update this if your attachment source changes!
055 *     // Else JSPWiki will be serving 304s to anyone who asks...
056 *
057 *     destatt.setLastModified( context.getPage().getLastModified() );
058 *     mgr.storeDynamicAttachment( context,  destatt );
059 *  ...
060 *
061 *      public InputStream getAttachmentData( WikiContext context, Attachment att )
062 *          throws IOException
063 *      {
064 *          byte[] bytes = "This is a test".getBytes();
065 *
066 *          return new ByteArrayInputStream( bytes );
067 *      }
068 *  </pre>
069 *
070 *  @since 2.5.34
071 */
072public class DynamicAttachment extends Attachment
073{
074    private DynamicAttachmentProvider m_provider  = null;
075
076    /**
077     *  Creates a DynamicAttachment.
078     *
079     *  @param engine  The engine which owns this attachment
080     *  @param parentPage The page which owns this attachment
081     *  @param fileName The filename of the attachment
082     *  @param provider The provider which will be used to generate the attachment.
083     */
084    public DynamicAttachment(WikiEngine engine,
085                             String parentPage,
086                             String fileName,
087                             DynamicAttachmentProvider provider)
088    {
089        super(engine, parentPage, fileName);
090        m_provider = provider;
091    }
092
093    /**
094     *  Returns the provider which is used to generate this attachment.
095     *
096     *  @return A Provider component for this attachment.
097     */
098    public DynamicAttachmentProvider getProvider()
099    {
100        return m_provider;
101    }
102}