You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2020/03/29 09:39:03 UTC

[jspwiki] 03/36: Deprecate WikiContext#findContext and move it to Context. Use the new method from the public api throughout the code

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 d8d8df3914f86ee46c3bbb1f70d7d969aa08aa8a
Author: juanpablo <ju...@apache.org>
AuthorDate: Thu Mar 26 21:07:28 2020 +0100

    Deprecate WikiContext#findContext and move it to Context. Use the new method from the public api throughout the code
---
 jspwiki-api/pom.xml                                     | 12 ++++++++++++
 .../src/main/java/org/apache/wiki/api/core/Context.java | 14 ++++++++++++++
 .../src/main/java/org/apache/wiki/WikiContext.java      | 16 +++++++++-------
 .../java/org/apache/wiki/preferences/Preferences.java   |  3 +--
 .../java/org/apache/wiki/tags/AdminBeanIteratorTag.java |  4 ++--
 .../org/apache/wiki/tags/AttachmentsIteratorTag.java    |  3 +--
 .../java/org/apache/wiki/tags/EditorIteratorTag.java    |  8 +++++---
 .../java/org/apache/wiki/tags/HistoryIteratorTag.java   |  5 +++--
 .../src/main/java/org/apache/wiki/tags/IteratorTag.java | 17 +++++------------
 .../org/apache/wiki/tags/SearchResultIteratorTag.java   | 10 +++++++---
 .../java/org/apache/wiki/ui/DefaultTemplateManager.java |  3 +--
 .../src/main/java/org/apache/wiki/ui/Editor.java        |  8 +++++---
 12 files changed, 65 insertions(+), 38 deletions(-)

diff --git a/jspwiki-api/pom.xml b/jspwiki-api/pom.xml
index 67b7a20..b6678cc 100644
--- a/jspwiki-api/pom.xml
+++ b/jspwiki-api/pom.xml
@@ -37,6 +37,12 @@
     </dependency>
 
     <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>jspwiki-util</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+
+    <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
     </dependency>
@@ -53,6 +59,12 @@
     </dependency>
 
     <dependency>
+      <groupId>javax.servlet.jsp</groupId>
+      <artifactId>javax.servlet.jsp-api</artifactId>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
       <groupId>org.junit.jupiter</groupId>
       <artifactId>junit-jupiter-api</artifactId>
       <scope>test</scope>
diff --git a/jspwiki-api/src/main/java/org/apache/wiki/api/core/Context.java b/jspwiki-api/src/main/java/org/apache/wiki/api/core/Context.java
index 36efd87..6846b9b 100644
--- a/jspwiki-api/src/main/java/org/apache/wiki/api/core/Context.java
+++ b/jspwiki-api/src/main/java/org/apache/wiki/api/core/Context.java
@@ -19,6 +19,7 @@
 package org.apache.wiki.api.core;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.jsp.PageContext;
 import java.security.Principal;
 
 
@@ -250,4 +251,17 @@ public interface Context extends Cloneable, Command {
     /** {@inheritDoc} */
     Context clone();
 
+    /**
+     *  This method can be used to find the WikiContext programmatically from a JSP PageContext. We check the request context.
+     *  The wiki context, if it exists, is looked up using the key {@link #ATTR_CONTEXT}.
+     *
+     *  @since 2.4
+     *  @param pageContext the JSP page context
+     *  @return Current WikiContext, or null, of no context exists.
+     */
+    static Context findContext( final PageContext pageContext ) {
+        final HttpServletRequest request = ( HttpServletRequest )pageContext.getRequest();
+        return ( Context )request.getAttribute( ATTR_CONTEXT );
+    }
+
 }
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WikiContext.java b/jspwiki-main/src/main/java/org/apache/wiki/WikiContext.java
index 2255a8d..a075874 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/WikiContext.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/WikiContext.java
@@ -104,8 +104,7 @@ public class WikiContext implements Context, Command {
     /** User is previewing the changes he just made. */
     public static final String PREVIEW = PageCommand.PREVIEW.getRequestContext();
 
-    /** User has an internal conflict, and does quite not know what to
-        do. Please provide some counseling. */
+    /** User has an internal conflict, and does quite not know what to do. Please provide some counseling. */
     public static final String CONFLICT = PageCommand.CONFLICT.getRequestContext();
 
     /** An error has been encountered and the user needs to be informed. */
@@ -671,13 +670,16 @@ public class WikiContext implements Context, Command {
     }
 
     /**
-     *  This method can be used to find the WikiContext programmatically from a JSP PageContext. We check the request context.
-     *  The wiki context, if it exists, is looked up using the key {@link #ATTR_CONTEXT}.
+     * This method can be used to find the WikiContext programmatically from a JSP PageContext. We check the request context.
+     * The wiki context, if it exists, is looked up using the key {@link #ATTR_CONTEXT}.
      *
-     *  @since 2.4
-     *  @param pageContext the JSP page context
-     *  @return Current WikiContext, or null, of no context exists.
+     * @since 2.4
+     * @param pageContext the JSP page context
+     * @return Current WikiContext, or null, of no context exists.
+     * @deprecated use {@Context#findContext( PageContext )} instead.
+     * @see Context#findContext( PageContext )
      */
+    @Deprecated
     public static WikiContext findContext( final PageContext pageContext ) {
         final HttpServletRequest request = ( HttpServletRequest )pageContext.getRequest();
         return ( WikiContext )request.getAttribute( ATTR_CONTEXT );
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/preferences/Preferences.java b/jspwiki-main/src/main/java/org/apache/wiki/preferences/Preferences.java
index b1ae4c2..23edefa 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/preferences/Preferences.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/preferences/Preferences.java
@@ -23,7 +23,6 @@ import org.apache.commons.lang3.LocaleUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.log4j.Logger;
 import org.apache.wiki.InternalWikiException;
-import org.apache.wiki.WikiContext;
 import org.apache.wiki.api.core.Context;
 import org.apache.wiki.i18n.InternationalizationManager;
 import org.apache.wiki.util.HttpUtil;
@@ -91,7 +90,7 @@ public class Preferences extends HashMap< String,String > {
     public static void reloadPreferences( final PageContext pageContext ) {
         final Preferences prefs = new Preferences();
         final Properties props = PropertyReader.loadWebAppProps( pageContext.getServletContext() );
-        final Context ctx = WikiContext.findContext( pageContext );
+        final Context ctx = Context.findContext( pageContext );
         final String dateFormat = ctx.getEngine().getManager( InternationalizationManager.class )
                                            .get( InternationalizationManager.CORE_BUNDLE, getLocale( ctx ), "common.datetimeformat" );
 
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/tags/AdminBeanIteratorTag.java b/jspwiki-main/src/main/java/org/apache/wiki/tags/AdminBeanIteratorTag.java
index d36a2fe..4913aea 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/tags/AdminBeanIteratorTag.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/tags/AdminBeanIteratorTag.java
@@ -18,7 +18,7 @@
  */
 package org.apache.wiki.tags;
 
-import org.apache.wiki.WikiContext;
+import org.apache.wiki.api.core.Context;
 import org.apache.wiki.ui.admin.AdminBean;
 import org.apache.wiki.ui.admin.AdminBeanManager;
 
@@ -42,7 +42,7 @@ public class AdminBeanIteratorTag extends IteratorTag {
      */
     public void setType( final String type ) {
     	if (m_wikiContext == null) {
-    		m_wikiContext = WikiContext.findContext(pageContext);
+    		m_wikiContext = Context.findContext(pageContext);
     	}
         m_type = m_wikiContext.getEngine().getManager( AdminBeanManager.class ).getTypeFromString( type );
     }
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/tags/AttachmentsIteratorTag.java b/jspwiki-main/src/main/java/org/apache/wiki/tags/AttachmentsIteratorTag.java
index 4314b33..e2dbe76 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/tags/AttachmentsIteratorTag.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/tags/AttachmentsIteratorTag.java
@@ -19,7 +19,6 @@
 package org.apache.wiki.tags;
 
 import org.apache.log4j.Logger;
-import org.apache.wiki.WikiContext;
 import org.apache.wiki.api.core.Attachment;
 import org.apache.wiki.api.core.Context;
 import org.apache.wiki.api.core.Engine;
@@ -55,7 +54,7 @@ public class AttachmentsIteratorTag extends IteratorTag {
      */
     @Override
     public final int doStartTag()  {
-        m_wikiContext = (WikiContext) pageContext.getAttribute( WikiContext.ATTR_CONTEXT, PageContext.REQUEST_SCOPE );
+        m_wikiContext = (Context) pageContext.getAttribute( Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE );
         final Engine engine = m_wikiContext.getEngine();
         final AttachmentManager mgr = engine.getManager( AttachmentManager.class );
         final Page page;
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/tags/EditorIteratorTag.java b/jspwiki-main/src/main/java/org/apache/wiki/tags/EditorIteratorTag.java
index d1f6882..cea8853 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/tags/EditorIteratorTag.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/tags/EditorIteratorTag.java
@@ -18,7 +18,7 @@
  */
 package org.apache.wiki.tags;
 
-import org.apache.wiki.WikiContext;
+import org.apache.wiki.api.core.Context;
 import org.apache.wiki.api.core.Engine;
 import org.apache.wiki.ui.Editor;
 import org.apache.wiki.ui.EditorManager;
@@ -36,8 +36,10 @@ public class EditorIteratorTag extends IteratorTag  {
 
     private static final long serialVersionUID = 0L;
 
-    @Override public final int doStartTag() {
-        m_wikiContext = WikiContext.findContext(pageContext);
+    /** {@inheritDoc} */
+    @Override
+    public final int doStartTag() {
+        m_wikiContext = Context.findContext(pageContext);
         final Engine engine = m_wikiContext.getEngine();
         final EditorManager mgr = engine.getManager( EditorManager.class );
         final String[] editorList = mgr.getEditorList();
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/tags/HistoryIteratorTag.java b/jspwiki-main/src/main/java/org/apache/wiki/tags/HistoryIteratorTag.java
index 30d1dc7..13a5577 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/tags/HistoryIteratorTag.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/tags/HistoryIteratorTag.java
@@ -19,7 +19,6 @@
 package org.apache.wiki.tags;
 
 import org.apache.log4j.Logger;
-import org.apache.wiki.WikiContext;
 import org.apache.wiki.api.core.Context;
 import org.apache.wiki.api.core.Engine;
 import org.apache.wiki.api.core.Page;
@@ -47,9 +46,10 @@ public class HistoryIteratorTag extends IteratorTag  {
     private static final long serialVersionUID = 0L;
     private static final Logger LOG = Logger.getLogger( HistoryIteratorTag.class );
 
+    /** {@inheritDoc} */
     @Override
     public final int doStartTag() {
-        m_wikiContext = (WikiContext) pageContext.getAttribute( WikiContext.ATTR_CONTEXT, PageContext.REQUEST_SCOPE );
+        m_wikiContext = (Context) pageContext.getAttribute( Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE );
         final Engine engine = m_wikiContext.getEngine();
         final Page page = m_wikiContext.getPage();
 
@@ -83,6 +83,7 @@ public class HistoryIteratorTag extends IteratorTag  {
         return SKIP_BODY;
     }
 
+    /** {@inheritDoc} */
     @Override
     public final int doAfterBody() {
         if( bodyContent != null ) {
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/tags/IteratorTag.java b/jspwiki-main/src/main/java/org/apache/wiki/tags/IteratorTag.java
index 08ac9fc..1b4eeec 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/tags/IteratorTag.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/tags/IteratorTag.java
@@ -19,7 +19,6 @@
 package org.apache.wiki.tags;
 
 import org.apache.log4j.Logger;
-import org.apache.wiki.WikiContext;
 import org.apache.wiki.api.core.Context;
 import org.apache.wiki.api.core.Page;
 
@@ -48,7 +47,7 @@ public abstract class IteratorTag extends BodyTagSupport implements TryCatchFina
 	private static final long serialVersionUID = 8945334759300595321L;
 	protected String m_pageName;
     protected Iterator< ? > m_iterator;
-    protected WikiContext m_wikiContext;
+    protected Context m_wikiContext;
 
     private static final Logger log = Logger.getLogger( IteratorTag.class );
 
@@ -88,12 +87,10 @@ public abstract class IteratorTag extends BodyTagSupport implements TryCatchFina
         // No operation here
     }
     
-    /**
-     *  {@inheritDoc}
-     */
+    /** {@inheritDoc} */
     @Override
     public int doStartTag() {
-        m_wikiContext = WikiContext.findContext(pageContext);
+        m_wikiContext = Context.findContext(pageContext);
         resetIterator();
         if( m_iterator == null ) {
             return SKIP_BODY;
@@ -119,9 +116,7 @@ public abstract class IteratorTag extends BodyTagSupport implements TryCatchFina
         pageContext.setAttribute( getId(), o );
     }
 
-    /**
-     *  {@inheritDoc}
-     */
+    /** {@inheritDoc} */
     @Override
     public int doEndTag() {
         // Return back to the original.
@@ -130,9 +125,7 @@ public abstract class IteratorTag extends BodyTagSupport implements TryCatchFina
         return EVAL_PAGE;
     }
 
-    /**
-     *  {@inheritDoc}
-     */
+    /** {@inheritDoc} */
     @Override
     public int doAfterBody() {
         if( bodyContent != null ) {
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/tags/SearchResultIteratorTag.java b/jspwiki-main/src/main/java/org/apache/wiki/tags/SearchResultIteratorTag.java
index 144b2ee..1a6ad51 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/tags/SearchResultIteratorTag.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/tags/SearchResultIteratorTag.java
@@ -52,7 +52,8 @@ public class SearchResultIteratorTag extends IteratorTag {
     private   int         m_start = 0;
     
     private static final Logger log = Logger.getLogger(SearchResultIteratorTag.class);
-    
+
+    /** {@inheritDoc} */
     @Override
     public void release() {
         super.release();
@@ -68,7 +69,8 @@ public class SearchResultIteratorTag extends IteratorTag {
     {
         m_start = arg;
     }
-    
+
+    /** {@inheritDoc} */
     @Override
     public final int doStartTag() {
         //  Do lazy eval if the search results have not been set.
@@ -86,7 +88,7 @@ public class SearchResultIteratorTag extends IteratorTag {
         }
 
         m_count = 0;
-        m_wikiContext = ( WikiContext )pageContext.getAttribute( WikiContext.ATTR_CONTEXT, PageContext.REQUEST_SCOPE );
+        m_wikiContext = ( Context )pageContext.getAttribute( Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE );
 
         return nextResult();
     }
@@ -111,6 +113,7 @@ public class SearchResultIteratorTag extends IteratorTag {
         return SKIP_BODY;
     }
 
+    /** {@inheritDoc} */
     @Override
     public int doAfterBody() {
         if( bodyContent != null ) {
@@ -127,6 +130,7 @@ public class SearchResultIteratorTag extends IteratorTag {
         return nextResult();
     }
 
+    /** {@inheritDoc} */
     @Override
     public int doEndTag() {
         m_iterator = null;
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/ui/DefaultTemplateManager.java b/jspwiki-main/src/main/java/org/apache/wiki/ui/DefaultTemplateManager.java
index 5e672d9..9a39ff9 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/ui/DefaultTemplateManager.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/ui/DefaultTemplateManager.java
@@ -21,7 +21,6 @@ package org.apache.wiki.ui;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.log4j.Logger;
 import org.apache.wiki.InternalWikiException;
-import org.apache.wiki.WikiContext;
 import org.apache.wiki.api.core.Context;
 import org.apache.wiki.api.core.Engine;
 import org.apache.wiki.modules.BaseModuleManager;
@@ -263,7 +262,7 @@ public class DefaultTemplateManager extends BaseModuleManager implements Templat
     /** {@inheritDoc} */
     @Override
     public Map< String, String > listTimeFormats( final PageContext pageContext ) {
-        final WikiContext context = WikiContext.findContext( pageContext );
+        final Context context = Context.findContext( pageContext );
         final Properties props = m_engine.getWikiProperties();
         final ArrayList< String > tfArr = new ArrayList<>(40);
         final LinkedHashMap< String, String > resultMap = new LinkedHashMap<>();
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/ui/Editor.java b/jspwiki-main/src/main/java/org/apache/wiki/ui/Editor.java
index 17df105..b71c3fd 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/ui/Editor.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/ui/Editor.java
@@ -18,7 +18,8 @@
  */
 package org.apache.wiki.ui;
 
-import org.apache.wiki.WikiContext;
+import org.apache.wiki.api.core.Context;
+
 
 /**
  *  Describes an editor.
@@ -28,10 +29,10 @@ import org.apache.wiki.WikiContext;
 public class Editor {
 
     private final String m_editorName;
-    private final WikiContext m_wikiContext;
+    private final Context m_wikiContext;
     private final EditorManager m_editorManager;
 
-    public Editor( final WikiContext wikiContext, final String editorName ) {
+    public Editor( final Context wikiContext, final String editorName ) {
         m_wikiContext = wikiContext;
         m_editorName = editorName;
         m_editorManager = wikiContext.getEngine().getManager( EditorManager.class );
@@ -62,6 +63,7 @@ public class Editor {
         return ifNotSelected;
     }
 
+    /** {@inheritDoc} */
     @Override
     public String toString() {
         return m_editorName;