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