-
Notifications
You must be signed in to change notification settings - Fork 161
Move frontpage selection into the administrator setup workflow #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
snoopdave
wants to merge
2
commits into
master
Choose a base branch
from
frontpage-setup-workflow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
app/src/main/java/org/apache/roller/weblogger/business/FrontpageSettings.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. The ASF licenses this file to You | ||
| * under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
| * implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. For additional | ||
| * information regarding copyright in this work, please see the NOTICE | ||
| * file in the top level directory of this distribution. | ||
| */ | ||
| package org.apache.roller.weblogger.business; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.roller.weblogger.WebloggerException; | ||
| import org.apache.roller.weblogger.pojos.RuntimeConfigProperty; | ||
| import org.apache.roller.weblogger.pojos.Weblog; | ||
|
|
||
| /** | ||
| * Reads and writes the site frontpage weblog settings. | ||
| * | ||
| * <p>Two screens change these values: the one-time setup screen used to choose | ||
| * a frontpage while the site is being installed, and the global configuration | ||
| * screen used afterwards. Both go through here so that the handle is resolved | ||
| * and validated the same way and both properties move together. | ||
| */ | ||
| public final class FrontpageSettings { | ||
|
|
||
| public static final String HANDLE_PROPERTY = "site.frontpage.weblog.handle"; | ||
| public static final String AGGREGATED_PROPERTY = "site.frontpage.weblog.aggregated"; | ||
|
|
||
| private FrontpageSettings() { | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a submitted handle to a weblog that actually exists and is | ||
| * enabled. | ||
| * | ||
| * @return the weblog, or null when the handle is blank, unknown or refers | ||
| * to a disabled weblog | ||
| */ | ||
| public static Weblog resolveWeblog(String handle) throws WebloggerException { | ||
| if (StringUtils.isBlank(handle) || !isValidHandle(handle.trim())) { | ||
| return null; | ||
| } | ||
| return WebloggerFactory.getWeblogger().getWeblogManager() | ||
| .getWeblogByHandle(handle.trim(), Boolean.TRUE); | ||
| } | ||
|
|
||
| /** @return the configured frontpage handle, or null when none is set. */ | ||
| public static String getConfiguredHandle() throws WebloggerException { | ||
| RuntimeConfigProperty prop = WebloggerFactory.getWeblogger() | ||
| .getPropertiesManager().getProperty(HANDLE_PROPERTY); | ||
| if (prop == null || StringUtils.isBlank(prop.getValue())) { | ||
| return null; | ||
| } | ||
| return prop.getValue(); | ||
| } | ||
|
|
||
| /** @return true when a frontpage weblog has already been chosen. */ | ||
| public static boolean isConfigured() throws WebloggerException { | ||
| return resolveWeblog(getConfiguredHandle()) != null; | ||
| } | ||
|
|
||
| /** | ||
| * Validates and stores the frontpage selection. | ||
| * | ||
| * <p>Both properties are written before the single flush so the pair cannot | ||
| * be left half-applied, and the canonical handle from the resolved weblog is | ||
| * stored rather than the submitted text. A missing aggregation value is | ||
| * treated as false, which is what an unchecked checkbox means. | ||
| * | ||
| * @param handle submitted weblog handle | ||
| * @param aggregated submitted aggregation flag; null means false | ||
| * @throws InvalidFrontpageWeblogException when the handle does not name an | ||
| * existing, enabled weblog | ||
| */ | ||
| public static boolean applyInitial(String handle, Boolean aggregated) | ||
| throws WebloggerException { | ||
|
|
||
| Weblog weblog = resolveWeblog(handle); | ||
| if (weblog == null) { | ||
| throw new InvalidFrontpageWeblogException(handle); | ||
| } | ||
|
|
||
| PropertiesManager mgr = WebloggerFactory.getWeblogger().getPropertiesManager(); | ||
|
|
||
| RuntimeConfigProperty handleProp = mgr.getProperty(HANDLE_PROPERTY); | ||
| String currentValue = handleProp == null ? null : handleProp.getValue(); | ||
| if (resolveWeblog(currentValue) != null | ||
| || !mgr.compareAndSetProperty(HANDLE_PROPERTY, currentValue, weblog.getHandle())) { | ||
| return false; | ||
| } | ||
|
|
||
| RuntimeConfigProperty aggregatedProp = mgr.getProperty(AGGREGATED_PROPERTY); | ||
| aggregatedProp.setValue(Boolean.toString(Boolean.TRUE.equals(aggregated))); | ||
| mgr.saveProperty(aggregatedProp); | ||
|
|
||
| WebloggerFactory.getWeblogger().flush(); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private static boolean isValidHandle(String handle) { | ||
| for (int i = 0; i < handle.length(); i++) { | ||
| if (!Character.isLetterOrDigit(handle.charAt(i)) && handle.charAt(i) != '_') { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** Raised when a submitted frontpage handle cannot be used. */ | ||
| public static class InvalidFrontpageWeblogException extends WebloggerException { | ||
| private final String handle; | ||
|
|
||
| public InvalidFrontpageWeblogException(String handle) { | ||
| super("Not an existing, enabled weblog handle: " + handle); | ||
| this.handle = handle; | ||
| } | ||
|
|
||
| public String getHandle() { | ||
| return handle; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getWeblogByHandlethrowsWebloggerException("Invalid handle")for anything outside[A-Za-z0-9_], so a POST withfrontpageBlog=my-blogtakes the genericWebloggerExceptionbranch (stack trace at ERROR, "Error saving properties") instead of the invalid-weblog message this method documents. Pre-check the handle or catch that case here.