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