-
Notifications
You must be signed in to change notification settings - Fork 37
Assembly paging prototype #273
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
carlosmunoz
wants to merge
2
commits into
redhataccess:master
Choose a base branch
from
carlosmunoz:assembly_paging_prototype
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
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
114 changes: 114 additions & 0 deletions
114
...ndle/src/main/java/com/redhat/pantheon/servlet/assembly/AssemblyPageRenderingServlet.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,114 @@ | ||
package com.redhat.pantheon.servlet.assembly; | ||
|
||
import com.google.common.base.Charsets; | ||
import com.redhat.pantheon.conf.GlobalConfig; | ||
import com.redhat.pantheon.html.Html; | ||
import com.redhat.pantheon.jcr.JcrQueryHelper; | ||
import com.redhat.pantheon.model.api.SlingModels; | ||
import com.redhat.pantheon.model.module.Module; | ||
import com.redhat.pantheon.model.module.ModuleVersion; | ||
import com.redhat.pantheon.servlet.ServletUtils; | ||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.api.SlingHttpServletResponse; | ||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.servlets.SlingSafeMethodsServlet; | ||
import org.apache.sling.servlets.annotations.SlingServletPaths; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
import org.jsoup.nodes.Node; | ||
import org.osgi.framework.Constants; | ||
import org.osgi.service.component.annotations.Component; | ||
|
||
import javax.jcr.RepositoryException; | ||
import javax.servlet.Servlet; | ||
import javax.servlet.ServletException; | ||
import java.io.IOException; | ||
import java.io.Writer; | ||
import java.util.Optional; | ||
import java.util.StringTokenizer; | ||
|
||
/** | ||
* @author Carlos Munoz | ||
*/ | ||
@Component( | ||
service = Servlet.class, | ||
property = { | ||
Constants.SERVICE_DESCRIPTION + "=Renders an assembly page in html", | ||
Constants.SERVICE_VENDOR + "=Red Hat Content Tooling team" | ||
}) | ||
@SlingServletPaths(value = "/api/assembly") | ||
public class AssemblyPageRenderingServlet extends SlingSafeMethodsServlet { | ||
|
||
private static final String SPLIT_TAG = "\\<\\!\\-\\-pantheon\\-module\\-start\\-\\-\\>"; | ||
|
||
@Override | ||
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException { | ||
String assemblyId = ServletUtils.paramValue(request, "id"); | ||
Long pageNum = ServletUtils.paramValueAsLong(request, "page", 1L); | ||
|
||
try { | ||
// First get the assembly | ||
// Since there are no assemblies yet, we will get modules | ||
JcrQueryHelper helper = new JcrQueryHelper(request.getResourceResolver()); | ||
Resource assemblyResource = helper.findById(assemblyId); | ||
Module assembly = SlingModels.getModel(assemblyResource, Module.class); | ||
|
||
// Get the page content | ||
// Split by sections with class 'sect1' as they seem to be the top level sections | ||
// Assuming latest draft or released version, and only in English for now | ||
Optional<ModuleVersion> versionOpt = assembly.getReleasedVersion(GlobalConfig.DEFAULT_MODULE_LOCALE); | ||
ModuleVersion version = versionOpt.orElse( assembly.getDraftVersion(GlobalConfig.DEFAULT_MODULE_LOCALE).get() ); | ||
String fullHtmlContent = version.content().get().cachedHtml().get().data().get(); | ||
// Now split it into the right page | ||
String pageHtml = | ||
//new TopSectionSplittingStrategy().split(fullHtmlContent, pageNum.intValue()); | ||
new CustomTagBasedSplittingStrategy().split(fullHtmlContent, pageNum.intValue()); | ||
|
||
// Render the page as html | ||
response.setContentType("text/html"); | ||
Writer w = response.getWriter(); | ||
w.write(pageHtml); | ||
} catch (RepositoryException e) { | ||
throw new ServletException(e); | ||
} | ||
} | ||
|
||
private interface HtmlSplittingStrategy { | ||
String split(String html, int page); | ||
} | ||
|
||
private class TopSectionSplittingStrategy implements HtmlSplittingStrategy { | ||
|
||
@Override | ||
public String split(String html, int pageNum) { | ||
return Html.parse(Charsets.UTF_8.name()) | ||
.andThen(Document::body) | ||
.andThen(element -> element.select("section.sect1")) | ||
.andThen(elements -> elements.listIterator(pageNum-1).next()) | ||
.andThen(element -> element.outerHtml()) | ||
.apply(html); | ||
} | ||
} | ||
|
||
private class CustomTagBasedSplittingStrategy implements HtmlSplittingStrategy { | ||
|
||
@Override | ||
public String split(String html, int page) { | ||
// extract just the body | ||
String fullBodyHtml = Html.parse(Charsets.UTF_8.name()) | ||
.andThen(Document::body) | ||
.andThen(Element::html) | ||
.apply(html); | ||
|
||
// TODO this is very rudimentary, the system could do a more scalable split and could also | ||
// TODO cache the pages after generation | ||
String[] pages = fullBodyHtml.split(SPLIT_TAG); | ||
|
||
// Send the html through Jsoup again so it's cleaned up | ||
return Html.parse(Charsets.UTF_8.name()) | ||
.andThen(Document::outerHtml) | ||
.apply(pages[page-1]); | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.