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.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.PageProvider;
029import org.apache.wiki.api.search.QueryItem;
030import org.apache.wiki.api.search.SearchResult;
031import org.apache.wiki.search.SearchResultComparator;
032import org.apache.wiki.util.ClassUtil;
033import org.apache.wiki.util.TextUtil;
034
035import java.io.IOException;
036import java.util.Arrays;
037import java.util.Collection;
038import java.util.Date;
039import java.util.List;
040import java.util.Properties;
041import java.util.TreeSet;
042import java.util.stream.Collectors;
043
044
045/**
046 * This provider ensures backward compatibility with page providers not using the public API. As providers should use the public API
047 * directly, the use of this class is considered deprecated.
048 *
049 * @deprecated adapted provider should use {@link PageProvider} instead.
050 * @see PageProvider
051 */
052@Deprecated
053public class WikiPageAdapterProvider implements PageProvider {
054
055    private static final Logger LOG = LogManager.getLogger( WikiPageAdapterProvider.class );
056    private static final String PROP_ADAPTER_IMPL = "jspwiki.pageProvider.adapter.impl";
057
058    WikiPageProvider provider;
059
060    /** {@inheritDoc} */
061    @Override
062    public void initialize( final Engine engine, final Properties properties ) throws NoRequiredPropertyException, IOException {
063        LOG.warn( "Using a page provider through org.apache.wiki.providers.WikiPageAdapterProviderAdapterProvider" );
064        LOG.warn( "Please contact the page provider's author so there can be a new release of the provider " +
065                  "implementing the new org.apache.wiki.api.providers.PageProvider public API" );
066        final String classname = TextUtil.getRequiredProperty( properties, PROP_ADAPTER_IMPL );
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 page 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 putPageText( final Page page, final String text ) throws ProviderException {
088        provider.putPageText( ( WikiPage )page, text );
089    }
090
091    /** {@inheritDoc} */
092    @Override
093    public boolean pageExists( final String page ) {
094        return provider.pageExists( page );
095    }
096
097    /** {@inheritDoc} */
098    @Override
099    public boolean pageExists( final String page, final int version ) {
100        return provider.pageExists( page, version );
101    }
102
103    /** {@inheritDoc} */
104    @Override
105    public Collection< SearchResult > findPages( 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        final Collection< org.apache.wiki.search.SearchResult > results = provider.findPages( queryItems );
110        return results.stream()
111                      .map( SearchAdapter::newSearchResultFrom )
112                      .collect( Collectors.toCollection( () -> new TreeSet<>( new SearchResultComparator() ) ) );
113    }
114
115    /** {@inheritDoc} */
116    @Override
117    public Page getPageInfo( final String page, final int version ) throws ProviderException {
118        return provider.getPageInfo( page, version );
119    }
120
121    /** {@inheritDoc} */
122    @Override
123    public Collection< Page > getAllPages() throws ProviderException {
124        return provider.getAllPages().stream().map( wikiPage -> ( Page )wikiPage ).collect( Collectors.toList() );
125    }
126
127    /** {@inheritDoc} */
128    @Override
129    public Collection< Page > getAllChangedSince( final Date date ) {
130        return provider.getAllChangedSince( date ).stream().map( wikiPage -> ( Page )wikiPage ).collect( Collectors.toList() );
131    }
132
133    /** {@inheritDoc} */
134    @Override
135    public int getPageCount() throws ProviderException {
136        return provider.getPageCount();
137    }
138
139    /** {@inheritDoc} */
140    @Override
141    public List< Page > getVersionHistory( final String page ) throws ProviderException {
142        return provider.getVersionHistory( page ).stream().map( wikiPage -> ( Page )wikiPage ).collect( Collectors.toList() );
143    }
144
145    /** {@inheritDoc} */
146    @Override
147    public String getPageText( final String page, final int version ) throws ProviderException {
148        return provider.getPageText( page, version );
149    }
150
151    /** {@inheritDoc} */
152    @Override
153    public void deleteVersion( final String pageName, final int version ) throws ProviderException {
154        provider.deleteVersion( pageName, version );
155    }
156
157    /** {@inheritDoc} */
158    @Override
159    public void deletePage( final String pageName ) throws ProviderException {
160        provider.deletePage( pageName );
161    }
162
163    /** {@inheritDoc} */
164    @Override
165    public void movePage( final String from, final String to ) throws ProviderException {
166        provider.movePage( from, to );
167    }
168
169}