File Source: WeblogConfig.java

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.ui.struts2.editor;
    20  
    21  import java.util.ArrayList;
    22  import java.util.Arrays;
    23  import java.util.Collections;
    24  import java.util.Iterator;
    25  import java.util.List;
    26  import java.util.Map;
    27  import org.apache.commons.logging.Log;
    28  import org.apache.commons.logging.LogFactory;
    29  import org.apache.roller.weblogger.business.plugins.PluginManager;
    30  import org.apache.roller.weblogger.business.WebloggerFactory;
    31  import org.apache.roller.weblogger.business.UserManager;
    32  import org.apache.roller.weblogger.business.WeblogManager;
    33  import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
    34  import org.apache.roller.weblogger.pojos.WeblogPermission;
    35  import org.apache.roller.weblogger.pojos.Weblog;
    36  import org.apache.roller.weblogger.ui.core.RollerContext;
    37  import org.apache.roller.weblogger.ui.core.plugins.UIPluginManager;
    38  import org.apache.roller.weblogger.ui.struts2.util.UIAction;
    39  import org.apache.roller.weblogger.util.Blacklist;
    40  import org.apache.roller.weblogger.util.cache.CacheManager;
    41  import org.apache.struts2.interceptor.validation.SkipValidation;
    42  
    43  
    44  /**
    45   * Action for modifying weblog configuration.
    46   */
    47  public class WeblogConfig extends UIAction {
    48      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.struts2.editor.WeblogConfig__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    49      private static Log log = LogFactory.getLog(WeblogConfig.class);
    50      
    51      // bean for managing submitted data
    52      private WeblogConfigBean bean = new WeblogConfigBean();
    53      
    54      // categories list
    55      private List weblogCategories = Collections.EMPTY_LIST;
    56      
    57      // list of available editors
    58      private List editorsList = Collections.EMPTY_LIST;
    59      
    60      // list of available plugins
    61      private List pluginsList = Collections.EMPTY_LIST;
    62      
    63      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.struts2.editor.WeblogConfig()
              * 
              *  Presumptions:
              *    init'ed(java.util.Collections.EMPTY_LIST)
              * 
              *  Postconditions:
              *    this.actionName == &"weblogConfig"
              *    this.actionWeblog == null
              *    this.authenticatedUser == null
              *    this.weblog == null
              *    this.bean.about == null
              *    this.bean.blacklist == null
              *    this.bean.bloggerCategoryId == null
              *    this.bean.defaultCategoryId == null
              *    this.bean.defaultPlugins == null
              *    this.bean.defaultPluginsArray == null
              *    ...
              */
    64      public WeblogConfig() {
    65          this.actionName = "weblogConfig";
    66          this.desiredMenu = "editor";
    67          this.pageTitle = "websiteSettings.title";
    68      }
    69      
    70      
    71      // admin perms required
    72      public short requiredWeblogPermissions() {
                 /* 
    P/P           *  Method: short requiredWeblogPermissions()
                  * 
                  *  Presumptions:
                  *    init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.ADMIN)
                  * 
                  *  Postconditions:
                  *    return_value == org.apache.roller.weblogger.pojos.WeblogPermission.ADMIN
                  *    (soft) init'ed(return_value)
                  */
    73          return WeblogPermission.ADMIN;
    74      }
    75      
    76      
    77      public void myPrepare() {
    78          
    79          try {
                     /* 
    P/P               *  Method: void myPrepare()
                      * 
                      *  Preconditions:
                      *    (soft) log != null
                      *    (soft) init'ed(this.actionWeblog)
                      * 
                      *  Presumptions:
                      *    java.util.Map:values(...)@96 != null
                      *    org.apache.roller.weblogger.business.Weblogger:getPluginManager(...)@93 != null
                      *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@80 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@80 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@93 != null
                      *    ...
                      * 
                      *  Postconditions:
                      *    possibly_updated(this.editorsList)
                      *    this.pluginsList == One-of{&new ArrayList(myPrepare#1), old this.pluginsList}
                      *    possibly_updated(this.weblogCategories)
                      *    new ArrayList(myPrepare#1) num objects <= 1
                      * 
                      *  Test Vectors:
                      *    java.util.Iterator:hasNext(...)@97: {0}, {1}
                      *    org.apache.roller.weblogger.ui.core.plugins.UIPluginManager:getWeblogEntryEditors(...)@87: Addr_Set{null}, Inverse{null}
                      */
    80              WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
    81              
    82              // set categories list
    83              setWeblogCategories(wmgr.getWeblogCategories(getActionWeblog(), false));
    84              
    85              // set the Editor Page list
    86              UIPluginManager pmgr = RollerContext.getUIPluginManager();
    87              List editorsList = pmgr.getWeblogEntryEditors();
    88              if(editorsList != null) {
    89                  setEditorsList(editorsList);
    90              }
    91              
    92              // set plugins list
    93              PluginManager ppmgr = WebloggerFactory.getWeblogger().getPluginManager();
    94              Map pluginsMap = ppmgr.getWeblogEntryPlugins(getActionWeblog());
    95              List plugins = new ArrayList();
    96              Iterator iter = pluginsMap.values().iterator();
    97              while(iter.hasNext()) {
    98                  plugins.add(iter.next());
    99              }
   100              // sort
   101              setPluginsList(plugins);
   102  
   103          } catch (Exception ex) {
   104              log.error("Error preparing weblog config action", ex);
   105          }
   106      }
   107      
   108      
   109      @SkipValidation
   110      public String execute() {
   111          
   112          // load bean with data from weblog
                 /* 
    P/P           *  Method: String execute()
                  * 
                  *  Preconditions:
                  *    this.actionWeblog != null
                  *    this.bean != null
                  * 
                  *  Postconditions:
                  *    java.lang.StringBuilder:toString(...)._tainted == 0
                  *    return_value == &"input"
                  *    init'ed(this.bean.about)
                  *    init'ed(this.bean.active)
                  *    init'ed(this.bean.allowComments)
                  *    init'ed(this.bean.blacklist)
                  *    possibly_updated(this.bean.bloggerCategoryId)
                  *    init'ed(this.bean.commentModerationRequired)
                  *    init'ed(this.bean.defaultAllowComments)
                  *    possibly_updated(this.bean.defaultCategoryId)
                  *    ...
                  */
   113          getBean().copyFrom(getActionWeblog());
   114          
   115          return INPUT;
   116      }
   117      
   118      
   119      /**
   120       * Save weblog configuration.
   121       */
   122      public String save() {
   123          
   124          // run validation
                 /* 
    P/P           *  Method: String save()
                  * 
                  *  Preconditions:
                  *    this.bean != null
                  *    init'ed(this.bean.entryDisplayCount)
                  *    (soft) log != null
                  *    (soft) this.actionWeblog != null
                  *    (soft) init'ed(this.bean.about)
                  *    (soft) init'ed(this.bean.active)
                  *    (soft) init'ed(this.bean.allowComments)
                  *    (soft) init'ed(this.bean.applyCommentDefaults)
                  *    (soft) init'ed(this.bean.blacklist)
                  *    (soft) init'ed(this.bean.bloggerCategoryId)
                  *    ...
                  * 
                  *  Presumptions:
                  *    init'ed(java.lang.Boolean.FALSE)
                  *    org.apache.roller.weblogger.business.Weblogger:getRefererManager(...)@163 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@129 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@128 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@128 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    return_value == &"input"
                  * 
                  *  Test Vectors:
                  *    this.bean.applyCommentDefaults: {0}, {1}
                  *    this.bean.bloggerCategoryId: Addr_Set{null}, Inverse{null}
                  *    java.lang.Boolean:booleanValue(...)@142: {1}, {0}
                  *    java.lang.String:equals(...)@136: {1}, {0}
                  *    org.apache.roller.weblogger.pojos.Weblog:isEnableMultiLang(...)@150: {1}, {0}
                  *    org.apache.roller.weblogger.pojos.Weblog:isShowAllLangs(...)@150: {1}, {0}
                  *    org.apache.roller.weblogger.ui.struts2.editor.WeblogConfig:hasActionErrors(...)@127: {1}, {0}
                  */
   125          myValidate();
   126          
   127          if(!hasActionErrors()) try {
   128              WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
   129              UserManager umgr = WebloggerFactory.getWeblogger().getUserManager();
   130              
   131              Weblog weblog = getActionWeblog();
   132              
   133              getBean().copyTo(weblog);
   134              
   135              // if blogger category changed then lookup new cat and set it
   136              if(getBean().getBloggerCategoryId() != null && 
   137                      !weblog.getBloggerCategory().getId().equals(getBean().getBloggerCategoryId())) {
   138                  weblog.setBloggerCategory(wmgr.getWeblogCategory(getBean().getBloggerCategoryId()));
   139              }
   140              
   141              // ROL-485: comments not allowed on inactive weblogs
   142              if(!weblog.getActive()) {
   143                  weblog.setAllowComments(Boolean.FALSE);
   144                  addMessage("websiteSettings.commentsOffForInactiveWeblog");
   145              }
   146              
   147              // if blog has unchecked 'show all langs' then we must make sure
   148              // the multi-language blogging option is enabled.
   149              // TODO: this should be properly reflected via the UI
   150              if(!weblog.isShowAllLangs() && !weblog.isEnableMultiLang()) {
   151                  weblog.setEnableMultiLang(true);
   152              }
   153              
   154              // save config
   155              umgr.saveWebsite(weblog);
   156              
   157              // ROL-1050: apply comment defaults to existing entries
   158              if(getBean().getApplyCommentDefaults()) {
   159                  wmgr.applyCommentDefaultsToEntries(weblog);
   160              }
   161              
   162              // apply referer filters
   163              WebloggerFactory.getWeblogger().getRefererManager().applyRefererFilters(weblog);
   164              
   165              // flush
   166              WebloggerFactory.getWeblogger().flush();
   167              
   168              addMessage("websiteSettings.savedChanges");
   169              
   170              // Clear cache entries associated with website
   171              CacheManager.invalidate(weblog);
   172              
   173          } catch (Exception ex) {
   174              log.error("Error updating weblog config", ex);
   175              // TODO: i18n
   176              addError("Error updating configuration");
   177          }
   178          
   179          return  INPUT;
   180      }
   181      
   182      
   183      // validation
   184      private void myValidate() {
   185          
   186          // make sure user didn't enter an invalid entry display count
                 /* 
    P/P           *  Method: void myValidate()
                  * 
                  *  Preconditions:
                  *    this.bean != null
                  *    init'ed(this.bean.entryDisplayCount)
                  *    (soft) init'ed(this.bean.blacklist)
                  */
   187          int maxEntries = WebloggerRuntimeConfig.getIntProperty("site.pages.maxEntries");
   188          if(getBean().getEntryDisplayCount() > maxEntries) {
   189              addError("websiteSettings.error.entryDisplayCount");
   190          }
   191          
   192          // check blacklist
   193          List regexRules = new ArrayList();
   194          List stringRules = new ArrayList();
   195          try {
   196              // just for testing/counting, this does not persist rules in any way
   197              Blacklist.populateSpamRules(getBean().getBlacklist(), stringRules, regexRules, null);
   198              addMessage("websiteSettings.acceptedBlacklist",
   199                      Arrays.asList(new String[] {""+stringRules.size(), ""+regexRules.size()}));
   200          } catch (Throwable e) {
   201              addError("websiteSettings.error.processingBlacklist", e.getMessage());
   202          }
   203      }
   204      
   205      
   206      public WeblogConfigBean getBean() {
                 /* 
    P/P           *  Method: WeblogConfigBean getBean()
                  * 
                  *  Preconditions:
                  *    init'ed(this.bean)
                  * 
                  *  Postconditions:
                  *    return_value == this.bean
                  *    init'ed(return_value)
                  */
   207          return bean;
   208      }
   209  
   210      public void setBean(WeblogConfigBean bean) {
                 /* 
    P/P           *  Method: void setBean(WeblogConfigBean)
                  * 
                  *  Postconditions:
                  *    this.bean == bean
                  *    init'ed(this.bean)
                  */
   211          this.bean = bean;
   212      }
   213  
   214      public List getWeblogCategories() {
                 /* 
    P/P           *  Method: List getWeblogCategories()
                  * 
                  *  Preconditions:
                  *    init'ed(this.weblogCategories)
                  * 
                  *  Postconditions:
                  *    return_value == this.weblogCategories
                  *    init'ed(return_value)
                  */
   215          return weblogCategories;
   216      }
   217  
   218      public void setWeblogCategories(List weblogCategories) {
                 /* 
    P/P           *  Method: void setWeblogCategories(List)
                  * 
                  *  Postconditions:
                  *    this.weblogCategories == weblogCategories
                  *    init'ed(this.weblogCategories)
                  */
   219          this.weblogCategories = weblogCategories;
   220      }
   221  
   222      public List getEditorsList() {
                 /* 
    P/P           *  Method: List getEditorsList()
                  * 
                  *  Preconditions:
                  *    init'ed(this.editorsList)
                  * 
                  *  Postconditions:
                  *    return_value == this.editorsList
                  *    init'ed(return_value)
                  */
   223          return editorsList;
   224      }
   225      
   226      public void setEditorsList(List editorsList) {
                 /* 
    P/P           *  Method: void setEditorsList(List)
                  * 
                  *  Postconditions:
                  *    this.editorsList == editorsList
                  *    init'ed(this.editorsList)
                  */
   227          this.editorsList = editorsList;
   228      }
   229  
   230      public List getPluginsList() {
                 /* 
    P/P           *  Method: List getPluginsList()
                  * 
                  *  Preconditions:
                  *    init'ed(this.pluginsList)
                  * 
                  *  Postconditions:
                  *    return_value == this.pluginsList
                  *    init'ed(return_value)
                  */
   231          return pluginsList;
   232      }
   233  
   234      public void setPluginsList(List pluginsList) {
                 /* 
    P/P           *  Method: void setPluginsList(List)
                  * 
                  *  Postconditions:
                  *    this.pluginsList == pluginsList
                  *    init'ed(this.pluginsList)
                  */
   235          this.pluginsList = pluginsList;
   236      }
   237      
   238  }








SofCheck Inspector Build Version : 2.18479
WeblogConfig.java 2009-Jan-02 14:25:14
WeblogConfig.class 2009-Sep-04 03:12:45