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.providers;
020
021import org.apache.log4j.Logger;
022import org.apache.wiki.WikiPage;
023import org.apache.wiki.api.core.Attachment;
024import org.apache.wiki.api.core.Engine;
025import org.apache.wiki.api.core.Page;
026import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
027import org.apache.wiki.api.exceptions.ProviderException;
028import org.apache.wiki.api.providers.AttachmentProvider;
029import org.apache.wiki.api.search.QueryItem;
030import org.apache.wiki.util.ClassUtil;
031import org.apache.wiki.util.TextUtil;
032
033import java.io.IOException;
034import java.io.InputStream;
035import java.util.Arrays;
036import java.util.Collection;
037import java.util.Date;
038import java.util.List;
039import java.util.Properties;
040import java.util.stream.Collectors;
041
042
043/**
044 * This provider ensures backward compatibility with attachment providers not using the public API. As providers should use the public API
045 * directly, the use of this class is considered deprecated.
046 *
047 * @deprecated adapted provider should use {@link AttachmentProvider} instead.
048 * @see AttachmentProvider
049 */
050@Deprecated
051public class WikiAttachmentAdapterProvider implements AttachmentProvider {
052
053    private static final Logger LOG = Logger.getLogger( WikiAttachmentAdapterProvider.class );
054    private static final String PROP_ADAPTER_IMPL = "jspwiki.attachmentProvider.adapter.impl";
055
056    WikiAttachmentProvider provider;
057
058    /** {@inheritDoc} */
059    @Override
060    public void initialize( final Engine engine, final Properties properties ) throws NoRequiredPropertyException, IOException {
061        LOG.warn( "Using an attachment provider through org.apache.wiki.providers.WikiAttachmentAdapterProvider" );
062        LOG.warn( "Please contact the attachment provider's author so there can be a new release of the provider " +
063                  "implementing the new org.apache.wiki.api.providers.AttachmentProvider public API" );
064        final String classname = TextUtil.getRequiredProperty( properties, PROP_ADAPTER_IMPL );
065        try {
066            LOG.debug( "Page provider class: '" + classname + "'" );
067            final Class<?> providerclass = ClassUtil.findClass("org.apache.wiki.providers", classname);
068            provider = ( WikiAttachmentProvider ) providerclass.newInstance();
069        } catch( final IllegalAccessException | InstantiationException | ClassNotFoundException e ) {
070            LOG.error( "Could not instantiate " + classname, e );
071            throw new IOException( e.getMessage(), e );
072        }
073
074        LOG.debug( "Initializing attachment provider class " + provider );
075        provider.initialize( engine, properties );
076    }
077
078    /** {@inheritDoc} */
079    @Override
080    public String getProviderInfo() {
081        return provider.getProviderInfo();
082    }
083
084    /** {@inheritDoc} */
085    @Override
086    public void putAttachmentData( final Attachment att, final InputStream data ) throws ProviderException, IOException {
087        provider.putAttachmentData( ( org.apache.wiki.attachment.Attachment )att, data );
088    }
089
090    /** {@inheritDoc} */
091    @Override
092    public InputStream getAttachmentData( final Attachment att ) throws ProviderException, IOException {
093        return provider.getAttachmentData( ( org.apache.wiki.attachment.Attachment )att );
094    }
095
096    /** {@inheritDoc} */
097    @Override
098    public List< Attachment > listAttachments( final Page page ) throws ProviderException {
099        return provider.listAttachments( ( WikiPage )page ).stream().map( att -> ( Attachment )att ).collect( Collectors.toList() );
100    }
101
102    /** {@inheritDoc} */
103    @Override
104    public Collection< Attachment > findAttachments( final QueryItem[] query ) {
105        final org.apache.wiki.search.QueryItem[] queryItems = Arrays.stream( query )
106                                                                    .map( SearchAdapter::oldQueryItemfrom )
107                                                                    .toArray( org.apache.wiki.search.QueryItem[]::new );
108        return provider.findAttachments( queryItems ).stream().map( att -> ( Attachment )att ).collect( Collectors.toList() );
109    }
110
111    /** {@inheritDoc} */
112    @Override
113    public List< Attachment > listAllChanged( final Date timestamp ) throws ProviderException {
114        return provider.listAllChanged( timestamp ).stream().map( att -> ( Attachment )att ).collect( Collectors.toList() );
115    }
116
117    /** {@inheritDoc} */
118    @Override
119    public Attachment getAttachmentInfo( final Page page, final String name, final int version ) throws ProviderException {
120        return provider.getAttachmentInfo( ( WikiPage )page, name, version );
121    }
122
123    /** {@inheritDoc} */
124    @Override
125    public List< Attachment > getVersionHistory( final Attachment att ) {
126        return provider.getVersionHistory( ( org.apache.wiki.attachment.Attachment )att )
127                       .stream()
128                       .map( attr -> ( Attachment )attr )
129                       .collect( Collectors.toList() );
130    }
131
132    /** {@inheritDoc} */
133    @Override
134    public void deleteVersion( final Attachment att ) throws ProviderException {
135        provider.deleteVersion( ( org.apache.wiki.attachment.Attachment )att );
136    }
137
138    /** {@inheritDoc} */
139    @Override
140    public void deleteAttachment( final Attachment att ) throws ProviderException {
141        provider.deleteAttachment( ( org.apache.wiki.attachment.Attachment )att );
142    }
143
144    /** {@inheritDoc} */
145    @Override
146    public void moveAttachmentsForPage( final String oldParent, final String newParent ) throws ProviderException {
147        provider.moveAttachmentsForPage( oldParent, newParent );
148    }
149
150}