This is an automated email from the ASF dual-hosted git repository. juanpablo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit b365691bcccb625e85fd027229f2aced0b92f0bc Author: juanpablo <[email protected]> AuthorDate: Wed Feb 26 19:23:02 2020 +0100 JSPWIKI-120: remove use of WikiEngine throughout the code as much as possible and use Engine instead (3) --- .../org/apache/wiki/rpc/atom/AtomAPIServlet.java | 214 +++++++-------------- .../java/org/apache/wiki/rss/RSSGenerator.java | 29 +-- .../main/java/org/apache/wiki/rss/RSSThread.java | 67 ++----- 3 files changed, 109 insertions(+), 201 deletions(-) diff --git a/jspwiki-main/src/main/java/org/apache/wiki/rpc/atom/AtomAPIServlet.java b/jspwiki-main/src/main/java/org/apache/wiki/rpc/atom/AtomAPIServlet.java index 084ecd2..66add1a 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/rpc/atom/AtomAPIServlet.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/rpc/atom/AtomAPIServlet.java @@ -22,6 +22,7 @@ import org.apache.log4j.Logger; import org.apache.wiki.WikiContext; import org.apache.wiki.WikiEngine; import org.apache.wiki.WikiPage; +import org.apache.wiki.api.core.Engine; import org.apache.wiki.api.exceptions.ProviderException; import org.apache.wiki.api.exceptions.WikiException; import org.apache.wiki.pages.PageManager; @@ -35,6 +36,7 @@ import org.intabulas.sandler.elements.Entry; import org.intabulas.sandler.elements.Feed; import org.intabulas.sandler.elements.Link; import org.intabulas.sandler.elements.Person; +import org.intabulas.sandler.elements.impl.LinkImpl; import org.intabulas.sandler.exceptions.FeedMarshallException; import javax.servlet.ServletConfig; @@ -45,30 +47,27 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Collection; import java.util.Date; -import java.util.Iterator; /** - * Handles incoming requests for the Atom API. This class uses the - * "sandler" Atom API implementation. + * Handles incoming requests for the Atom API. This class uses the "sandler" Atom API implementation. * * @since 2.1.97 */ // FIXME: Rewrite using some other library -public class AtomAPIServlet extends HttpServlet -{ - static final Logger log = Logger.getLogger( AtomAPIServlet.class ); +public class AtomAPIServlet extends HttpServlet { + + private static final Logger log = Logger.getLogger( AtomAPIServlet.class ); private static final long serialVersionUID = 0L; - private WikiEngine m_engine; + private Engine m_engine; /** * {@inheritDoc} */ - @Override public void init( final ServletConfig config ) - throws ServletException - { + @Override + public void init( final ServletConfig config ) throws ServletException { m_engine = WikiEngine.getInstance( config ); } @@ -77,22 +76,15 @@ public class AtomAPIServlet extends HttpServlet * The initial slash is also removed. If there is no page, * returns null. */ - private String getPageName( final HttpServletRequest request ) - { + private String getPageName( final HttpServletRequest request ) { String name = request.getPathInfo(); - - if( name == null || name.length() <= 1 ) - { + if( name == null || name.length() <= 1 ) { return null; - } - else if( name.charAt(0) == '/' ) - { - name = name.substring(1); + } else if( name.charAt( 0 ) == '/' ) { + name = name.substring( 1 ); } - name = TextUtil.urlDecodeUTF8( name ); - - return name; + return TextUtil.urlDecodeUTF8( name ); } /** @@ -104,61 +96,43 @@ public class AtomAPIServlet extends HttpServlet * <li>Assumes that incoming code is plain text or WikiMarkup, not html. * </ul> * - * @param request {@inheritDoc} - * @param response {@inheritDoc} - * @throws ServletException {@inheritDoc} + * {@inheritDoc} */ - @Override public void doPost( final HttpServletRequest request, final HttpServletResponse response ) - throws ServletException - { - log.debug("Received POST to AtomAPIServlet"); + @Override + public void doPost( final HttpServletRequest request, final HttpServletResponse response ) throws ServletException { + log.debug( "Received POST to AtomAPIServlet" ); - try - { + try { final String blogid = getPageName( request ); - - final WikiPage page = m_engine.getManager( PageManager.class ).getPage( blogid ); - - if( page == null ) - { - throw new ServletException("Page "+blogid+" does not exist, cannot add blog post."); + final WikiPage page = m_engine.getManager( PageManager.class ).getPage( blogid ); + if( page == null ) { + throw new ServletException( "Page " + blogid + " does not exist, cannot add blog post." ); } - //FIXME: Do authentication here + // FIXME: Do authentication here final Entry entry = Sandler.unmarshallEntry( request.getInputStream() ); - // // Fetch the obligatory parts of the content. - // - final Content title = entry.getTitle(); - final Content content = entry.getContent(0); - - final Person author = entry.getAuthor(); - - //FIXME: Sandler 0.5 does not support generator + final Content title = entry.getTitle(); + final Content content = entry.getContent( 0 ); + final Person author = entry.getAuthor(); - // - // Generate new blog entry. - // + // FIXME: Sandler 0.5 does not support generator + // Generate new blog entry. final WeblogEntryPlugin plugin = new WeblogEntryPlugin(); - final String pageName = plugin.getNewEntryPage( m_engine, blogid ); final String username = author.getName(); - final WikiPage entryPage = new WikiPage( m_engine, pageName ); entryPage.setAuthor( username ); final WikiContext context = new WikiContext( m_engine, request, entryPage ); - final StringBuilder text = new StringBuilder(); - text.append( "!" + title.getBody() ); - text.append( "\n\n" ); - text.append( content.getBody() ); - - log.debug("Writing entry: "+text); - + text.append( "!" ) + .append( title.getBody() ) + .append( "\n\n" ) + .append( content.getBody() ); + log.debug( "Writing entry: " + text ); m_engine.getManager( PageManager.class ).saveText( context, text.toString() ); - } catch( final FeedMarshallException e ) { log.error("Received faulty Atom entry",e); throw new ServletException("Faulty Atom entry",e); @@ -177,71 +151,53 @@ public class AtomAPIServlet extends HttpServlet * * {@inheritDoc} */ - @Override public void doGet( final HttpServletRequest request, final HttpServletResponse response ) - throws ServletException - { - log.debug("Received HTTP GET to AtomAPIServlet"); - + @Override + public void doGet( final HttpServletRequest request, final HttpServletResponse response ) throws ServletException { + log.debug( "Received HTTP GET to AtomAPIServlet" ); final String blogid = getPageName( request ); - - log.debug("Requested page "+blogid); - - try - { - if( blogid == null ) - { + log.debug( "Requested page " + blogid ); + try { + if( blogid == null ) { final Feed feed = listBlogs(); - - response.setContentType("application/x.atom+xml; charset=UTF-8"); - response.getWriter().println( Sandler.marshallFeed(feed) ); - - response.getWriter().flush(); - } - else - { + response.setContentType( "application/x.atom+xml; charset=UTF-8" ); + response.getWriter().println( Sandler.marshallFeed( feed ) ); + } else { final Entry entry = getBlogEntry( blogid ); - - response.setContentType("application/x.atom+xml; charset=UTF-8"); - response.getWriter().println( Sandler.marshallEntry(entry) ); - - response.getWriter().flush(); + response.setContentType( "application/x.atom+xml; charset=UTF-8" ); + response.getWriter().println( Sandler.marshallEntry( entry ) ); } + response.getWriter().flush(); + } catch( final Exception e ) { + log.error( "Unable to generate response", e ); + throw new ServletException( "Internal problem - whack Janne on the head to get a better error report", e ); } - catch( final Exception e ) - { - log.error("Unable to generate response",e); - throw new ServletException("Internal problem - whack Janne on the head to get a better error report",e); - } - } private Entry getBlogEntry( final String entryid ) { final WikiPage page = m_engine.getManager( PageManager.class ).getPage( entryid ); final WikiPage firstVersion = m_engine.getManager( PageManager.class ).getPage( entryid, 1 ); - final Entry entry = SyndicationFactory.newSyndicationEntry(); - final String pageText = m_engine.getManager( PageManager.class ).getText(page.getName()); - String title = ""; final int firstLine = pageText.indexOf('\n'); - if( firstLine > 0 ) - { + String title = ""; + if( firstLine > 0 ) { title = pageText.substring( 0, firstLine ); } - if( title.trim().length() == 0 ) title = page.getName(); + if( title.trim().length() == 0 ) { + title = page.getName(); + } // Remove wiki formatting - while( title.startsWith("!") ) title = title.substring(1); + while( title.startsWith("!") ) { + title = title.substring(1); + } entry.setTitle( title ); entry.setCreated( firstVersion.getLastModified() ); entry.setModified( page.getLastModified() ); - entry.setAuthor( SyndicationFactory.createPerson( page.getAuthor(), - null, - null ) ); - + entry.setAuthor( SyndicationFactory.createPerson( page.getAuthor(), null, null ) ); entry.addContent( SyndicationFactory.createEscapedContent(pageText) ); return entry; @@ -252,46 +208,25 @@ public class AtomAPIServlet extends HttpServlet */ private Feed listBlogs() throws ProviderException { final Collection< WikiPage > pages = m_engine.getManager( PageManager.class ).getAllPages(); - final Feed feed = SyndicationFactory.newSyndicationFeed(); feed.setTitle("List of blogs at this site"); feed.setModified( new Date() ); - for( final Iterator< WikiPage > i = pages.iterator(); i.hasNext(); ) - { - final WikiPage p = i.next(); - - // + for( final WikiPage p : pages ) { // List only weblogs - // FIXME: Unfortunately, a weblog is not known until it has - // been executed once, because plugins are off during - // the initial startup phase. - // - - log.debug( p.getName()+" = "+p.getAttribute(WeblogPlugin.ATTR_ISWEBLOG)) ; + // FIXME: Unfortunately, a weblog is not known until it has een executed once, because plugins are off during the initial startup phase. + log.debug( p.getName() + " = " + p.getAttribute( WeblogPlugin.ATTR_ISWEBLOG ) ); - if( !("true".equals(p.getAttribute(WeblogPlugin.ATTR_ISWEBLOG)) ) ) { + if( !( "true".equals( p.getAttribute( WeblogPlugin.ATTR_ISWEBLOG ) ) ) ) { continue; } final String encodedName = TextUtil.urlEncodeUTF8( p.getName() ); - final WikiContext context = new WikiContext( m_engine, p ); - - final String title = TextUtil.replaceEntities(org.apache.wiki.rss.Feed.getSiteName(context)); - - final Link postlink = createLink( "service.post", - m_engine.getBaseURL()+"atom/"+encodedName, - title ); - - final Link editlink = createLink( "service.edit", - m_engine.getBaseURL()+"atom/"+encodedName, - title ); - - final Link feedlink = createLink( "service.feed", - m_engine.getBaseURL()+"atom.jsp?page="+encodedName, - title ); - + final String title = TextUtil.replaceEntities( org.apache.wiki.rss.Feed.getSiteName( context ) ); + final Link postlink = createLink( "service.post", m_engine.getBaseURL() + "atom/" + encodedName, title ); + final Link editlink = createLink( "service.edit", m_engine.getBaseURL() + "atom/" + encodedName, title ); + final Link feedlink = createLink( "service.feed", m_engine.getBaseURL() + "atom.jsp?page=" + encodedName, title ); feed.addLink( postlink ); feed.addLink( feedlink ); @@ -301,12 +236,8 @@ public class AtomAPIServlet extends HttpServlet return feed; } - private Link createLink( final String rel, - final String href, - final String title ) - { - final org.intabulas.sandler.elements.impl.LinkImpl link = new org.intabulas.sandler.elements.impl.LinkImpl(); - + private Link createLink( final String rel, final String href, final String title ) { + final LinkImpl link = new LinkImpl(); link.setRelationship( rel ); link.setTitle( title ); link.setType( "application/x.atom+xml" ); @@ -318,14 +249,17 @@ public class AtomAPIServlet extends HttpServlet /** * {@inheritDoc} */ - @Override public void doDelete( final HttpServletRequest request, final HttpServletResponse response ) { - log.debug("Received HTTP DELETE"); + @Override + public void doDelete( final HttpServletRequest request, final HttpServletResponse response ) { + log.debug( "Received HTTP DELETE" ); } /** * {@inheritDoc} */ - @Override public void doPut( final HttpServletRequest request, final HttpServletResponse response ) { - log.debug("Received HTTP PUT"); + @Override + public void doPut( final HttpServletRequest request, final HttpServletResponse response ) { + log.debug( "Received HTTP PUT" ); } + } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSGenerator.java b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSGenerator.java index cb48eb9..44730a8 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSGenerator.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSGenerator.java @@ -20,16 +20,19 @@ package org.apache.wiki.rss; import org.apache.log4j.Logger; import org.apache.wiki.WikiContext; -import org.apache.wiki.WikiEngine; import org.apache.wiki.WikiPage; import org.apache.wiki.WikiProvider; import org.apache.wiki.WikiSession; +import org.apache.wiki.api.core.Engine; import org.apache.wiki.attachment.Attachment; +import org.apache.wiki.auth.AuthorizationManager; import org.apache.wiki.auth.permissions.PagePermission; import org.apache.wiki.diff.DifferenceManager; import org.apache.wiki.pages.PageManager; import org.apache.wiki.pages.PageTimeComparator; +import org.apache.wiki.render.RenderingManager; import org.apache.wiki.util.TextUtil; +import org.apache.wiki.variables.VariableManager; import java.util.Iterator; import java.util.List; @@ -55,7 +58,7 @@ import java.util.Set; public class RSSGenerator { private static final Logger log = Logger.getLogger( RSSGenerator.class ); - private WikiEngine m_engine; + private Engine m_engine; private String m_channelDescription = ""; private String m_channelLanguage = "en-us"; @@ -121,12 +124,12 @@ public class RSSGenerator { private static final int MAX_CHARACTERS = Integer.MAX_VALUE-1; /** - * Initialize the RSS generator for a given WikiEngine. + * Initialize the RSS generator for a given Engine. * - * @param engine The WikiEngine. + * @param engine The Engine. * @param properties The properties. */ - public RSSGenerator( final WikiEngine engine, final Properties properties ) { + public RSSGenerator( final Engine engine, final Properties properties ) { m_engine = engine; m_channelDescription = properties.getProperty( PROP_CHANNEL_DESCRIPTION, m_channelDescription ); m_channelLanguage = properties.getProperty( PROP_CHANNEL_LANGUAGE, m_channelLanguage ); @@ -190,7 +193,7 @@ public class RSSGenerator { buf.append( diff ); } else { buf.append( author ).append( " created this page on " ).append( page.getLastModified() ).append( ":<br /><hr /><br />" ); - buf.append( m_engine.getRenderingManager().getHTML( page.getName() ) ); + buf.append( m_engine.getManager( RenderingManager.class ).getHTML( page.getName() ) ); } return buf.toString(); @@ -321,7 +324,7 @@ public class RSSGenerator { final WikiPage page = i.next(); // Check if the anonymous user has view access to this page. - if( !m_engine.getAuthorizationManager().checkPermission(session, new PagePermission(page,PagePermission.VIEW_ACTION) ) ) { + if( !m_engine.getManager( AuthorizationManager.class ).checkPermission(session, new PagePermission(page,PagePermission.VIEW_ACTION) ) ) { // No permission, skip to the next one. continue; } @@ -357,14 +360,14 @@ public class RSSGenerator { protected String generateWikiPageRSS( final WikiContext wikiContext, final List< WikiPage > changed, final Feed feed ) { feed.setChannelTitle( m_engine.getApplicationName()+": "+wikiContext.getPage().getName() ); feed.setFeedURL( wikiContext.getViewURL( wikiContext.getPage().getName() ) ); - final String language = m_engine.getVariableManager().getVariable( wikiContext, PROP_CHANNEL_LANGUAGE ); + final String language = m_engine.getManager( VariableManager.class ).getVariable( wikiContext, PROP_CHANNEL_LANGUAGE ); if( language != null ) { feed.setChannelLanguage( language ); } else { feed.setChannelLanguage( m_channelLanguage ); } - final String channelDescription = m_engine.getVariableManager().getVariable( wikiContext, PROP_CHANNEL_DESCRIPTION ); + final String channelDescription = m_engine.getManager( VariableManager.class ).getVariable( wikiContext, PROP_CHANNEL_DESCRIPTION ); if( channelDescription != null ) { feed.setChannelDescription( channelDescription ); @@ -412,7 +415,7 @@ public class RSSGenerator { log.debug( "Generating RSS for blog, size=" + changed.size() ); } - final String ctitle = m_engine.getVariableManager().getVariable( wikiContext, PROP_CHANNEL_TITLE ); + final String ctitle = m_engine.getManager( VariableManager.class ).getVariable( wikiContext, PROP_CHANNEL_TITLE ); if( ctitle != null ) { feed.setChannelTitle( ctitle ); } else { @@ -421,14 +424,14 @@ public class RSSGenerator { feed.setFeedURL( wikiContext.getViewURL( wikiContext.getPage().getName() ) ); - final String language = m_engine.getVariableManager().getVariable( wikiContext, PROP_CHANNEL_LANGUAGE ); + final String language = m_engine.getManager( VariableManager.class ).getVariable( wikiContext, PROP_CHANNEL_LANGUAGE ); if( language != null ) { feed.setChannelLanguage( language ); } else { feed.setChannelLanguage( m_channelLanguage ); } - final String channelDescription = m_engine.getVariableManager().getVariable( wikiContext, PROP_CHANNEL_DESCRIPTION ); + final String channelDescription = m_engine.getManager( VariableManager.class ).getVariable( wikiContext, PROP_CHANNEL_DESCRIPTION ); if( channelDescription != null ) { feed.setChannelDescription( channelDescription ); } @@ -477,7 +480,7 @@ public class RSSGenerator { if( maxlen > MAX_CHARACTERS ) { maxlen = MAX_CHARACTERS; } - pageText = m_engine.getRenderingManager().textToHTML( wikiContext, pageText.substring( firstLine + 1, maxlen ).trim() ); + pageText = m_engine.getManager( RenderingManager.class ).textToHTML( wikiContext, pageText.substring( firstLine + 1, maxlen ).trim() ); if( maxlen == MAX_CHARACTERS ) { pageText += "..."; } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSThread.java b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSThread.java index 9707314..1168f84 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSThread.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSThread.java @@ -18,11 +18,10 @@ */ package org.apache.wiki.rss; - import org.apache.log4j.Logger; import org.apache.wiki.WatchDog; import org.apache.wiki.WikiBackgroundThread; -import org.apache.wiki.WikiEngine; +import org.apache.wiki.api.core.Engine; import org.apache.wiki.util.FileUtil; import java.io.BufferedWriter; @@ -33,31 +32,30 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.io.StringReader; import java.io.Writer; +import java.nio.charset.StandardCharsets; + /** * Runs the RSS generation thread. * FIXME: MUST be somewhere else, this is not a good place. */ -public class RSSThread extends WikiBackgroundThread -{ - static Logger log = Logger.getLogger( RSSThread.class ); - +public class RSSThread extends WikiBackgroundThread { + + private static final Logger log = Logger.getLogger( RSSThread.class ); private final File m_rssFile; private final RSSGenerator m_generator; - private WatchDog m_watchdog; /** * Create a new RSS thread. * - * @param engine A WikiEngine to own this thread. + * @param engine A Engine to own this thread. * @param rssFile A File to write the RSS data to. * @param rssInterval How often the RSS should be generated. */ - public RSSThread( final WikiEngine engine, final File rssFile, final int rssInterval ) - { + public RSSThread( final Engine engine, final File rssFile, final int rssInterval ) { super( engine, rssInterval ); - m_generator = engine.getRSSGenerator(); + m_generator = engine.getManager( RSSGenerator.class ); m_rssFile = rssFile; setName("JSPWiki RSS Generator"); log.debug( "RSS file will be at "+m_rssFile.getAbsolutePath() ); @@ -81,47 +79,20 @@ public class RSSThread extends WikiBackgroundThread * @throws Exception All exceptions are thrown upwards. */ @Override - public void backgroundTask() throws Exception - { - if ( m_generator.isEnabled() ) - { - Writer out = null; - Reader in = null; - + public void backgroundTask() throws Exception { + if( m_generator.isEnabled() ) { m_watchdog.enterState( "Generating RSS feed", 60 ); - - try - { - // - // Generate RSS file, output it to - // default "rss.rdf". - // - log.debug("Regenerating RSS feed to "+m_rssFile); - - final String feed = m_generator.generate(); - - in = new StringReader(feed); - out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( m_rssFile ), "UTF-8") ); + final String feed = m_generator.generate(); + log.debug( "Regenerating RSS feed to " + m_rssFile ); + // Generate RSS file, output it to default "rss.rdf". + try( final Reader in = new StringReader( feed ); + final Writer out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( m_rssFile ), StandardCharsets.UTF_8 ) ) ) { FileUtil.copyContents( in, out ); - } - catch( final IOException e ) - { - log.error("Cannot generate RSS feed to "+m_rssFile.getAbsolutePath(), e ); + } catch( final IOException e ) { + log.error( "Cannot generate RSS feed to " + m_rssFile.getAbsolutePath(), e ); m_generator.setEnabled( false ); - } - finally - { - try - { - if( in != null ) in.close(); - if( out != null ) out.close(); - } - catch( final IOException e ) - { - log.fatal("Could not close I/O for RSS", e ); - m_generator.setEnabled( false ); - } + } finally { m_watchdog.exitState(); }
