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            final Class<?> providerclass = ClassUtil.findClass("org.apache.wiki.providers", classname);
070            provider = ( WikiPageProvider ) providerclass.newInstance();
071        } catch( final IllegalAccessException | InstantiationException | ClassNotFoundException e ) {
072            LOG.error( "Could not instantiate " + classname, e );
073            throw new IOException( e.getMessage(), e );
074        }
075
076        LOG.debug( "Initializing page provider class " + provider );
077        provider.initialize( engine, properties );
078    }
079
080    /** {@inheritDoc} */
081    @Override
082    public String getProviderInfo() {
083        return provider.getProviderInfo();
084    }
085
086    /** {@inheritDoc} */
087    @Override
088    public void putPageText( final Page page, final String text ) throws ProviderException {
089        provider.putPageText( ( WikiPage )page, text );
090    }
091
092    /** {@inheritDoc} */
093    @Override
094    public boolean pageExists( final String page ) {
095        return provider.pageExists( page );
096    }
097
098    /** {@inheritDoc} */
099    @Override
100    public boolean pageExists( final String page, final int version ) {
101        return provider.pageExists( page, version );
102    }
103
104    /** {@inheritDoc} */
105    @Override
106    public Collection< SearchResult > findPages( final QueryItem[] query ) {
107        final org.apache.wiki.search.QueryItem[] queryItems = Arrays.stream( query )
108                                                                    .map( SearchAdapter::oldQueryItemfrom )
109                                                                    .toArray( org.apache.wiki.search.QueryItem[]::new );
110        final Collection< org.apache.wiki.search.SearchResult > results = provider.findPages( queryItems );
111        return results.stream()
112                      .map( SearchAdapter::newSearchResultFrom )
113                      .collect( Collectors.toCollection( () -> new TreeSet<>( new SearchResultComparator() ) ) );
114    }
115
116    /** {@inheritDoc} */
117    @Override
118    public Page getPageInfo( final String page, final int version ) throws ProviderException {
119        return provider.getPageInfo( page, version );
120    }
121
122    /** {@inheritDoc} */
123    @Override
124    public Collection< Page > getAllPages() throws ProviderException {
125        return provider.getAllPages().stream().map( wikiPage -> ( Page )wikiPage ).collect( Collectors.toList() );
126    }
127
128    /** {@inheritDoc} */
129    @Override
130    public Collection< Page > getAllChangedSince( final Date date ) {
131        return provider.getAllChangedSince( date ).stream().map( wikiPage -> ( Page )wikiPage ).collect( Collectors.toList() );
132    }
133
134    /** {@inheritDoc} */
135    @Override
136    public int getPageCount() throws ProviderException {
137        return provider.getPageCount();
138    }
139
140    /** {@inheritDoc} */
141    @Override
142    public List< Page > getVersionHistory( final String page ) throws ProviderException {
143        return provider.getVersionHistory( page ).stream().map( wikiPage -> ( Page )wikiPage ).collect( Collectors.toList() );
144    }
145
146    /** {@inheritDoc} */
147    @Override
148    public String getPageText( final String page, final int version ) throws ProviderException {
149        return provider.getPageText( page, version );
150    }
151
152    /** {@inheritDoc} */
153    @Override
154    public void deleteVersion( final String pageName, final int version ) throws ProviderException {
155        provider.deleteVersion( pageName, version );
156    }
157
158    /** {@inheritDoc} */
159    @Override
160    public void deletePage( final String pageName ) throws ProviderException {
161        provider.deletePage( pageName );
162    }
163
164    /** {@inheritDoc} */
165    @Override
166    public void movePage( final String from, final String to ) throws ProviderException {
167        provider.movePage( from, to );
168    }
169
170}