Skip to content

WW-5256 compress html output #1272

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions core/src/main/java/org/apache/struts2/components/Compress.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. 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.
*/
package org.apache.struts2.components;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.util.ValueStack;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import java.io.Writer;

/**
* <p>
* Used to compress HTML output. Just wrap a given section with the tag.
* </p>
*
* <p>
* Configurable attributes are:
* </p>
*
* <ul>
* <li>force (true/false) - always compress output, this can be useful in DevMode as devMode disables compression</li>
* </ul>
*
* <p><b>Examples</b></p>
* <pre>
* <!-- START SNIPPET: example -->
* &lt;s:compress&gt;
* &lt;s:form action="submit"&gt;
* &lt;s:text name="name" /&gt;
* ...
* &lt;/s:form&gt;
* &lt;/s:compress&gt;
* <!-- END SNIPPET: example -->
* </pre>
*
* <p>Uses conditional compression depending on action</p>
* <pre>
* <!-- START SNIPPET: example -->
* &lt;s:compress force="shouldCompress"&gt;
* &lt;s:form action="submit"&gt;
* &lt;s:text name="name" /&gt;
* ...
* &lt;/s:form&gt;
* &lt;/s:compress&gt;
* <!-- END SNIPPET: example -->
* </pre>
* "shouldCompress" is a field with getter define on action used in expression evaluation
*/
@StrutsTag(name = "compress", tldTagClass = "org.apache.struts2.views.jsp.CompressTag",
description = "Compress wrapped content")
public class Compress extends Component {

private static final Logger LOG = LogManager.getLogger(Compress.class);

private String force;

public Compress(ValueStack stack) {
super(stack);
}

@Override
public boolean end(Writer writer, String body) {
Object forceValue = findValue(force, Boolean.class);
boolean forced = forceValue != null && Boolean.parseBoolean(forceValue.toString());
if (devMode && !forced) {
LOG.debug("Avoids compressing output: {} in DevMode", body);
return super.end(writer, body, true);
}
LOG.trace("Compresses: {}", body);
String compressed = body.trim().replaceAll(">\\s+<", "><");
LOG.trace("Compressed: {}", compressed);
return super.end(writer, compressed, true);
}

@Override
public boolean usesBody() {
return true;
}

@StrutsTagAttribute(description = "Force output compression")
public void setForce(String force) {
this.force = force;
}
}
55 changes: 55 additions & 0 deletions core/src/main/java/org/apache/struts2/views/jsp/CompressTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. 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.
*/
package org.apache.struts2.views.jsp;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.struts2.components.Component;
import org.apache.struts2.components.Compress;
import org.apache.struts2.util.ValueStack;

import java.io.Serial;

/**
* @see org.apache.struts2.components.Compress
*/
public class CompressTag extends ComponentTagSupport {

@Serial
private static final long serialVersionUID = 7572566991679717145L;

private String force;

@Override
public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
return new Compress(stack);
}

@Override
protected void populateParams() {
super.populateParams();

Compress compress = (Compress) component;
compress.setForce(force);
}

public void setForce(String force) {
this.force = force;
}
}
17 changes: 7 additions & 10 deletions core/src/main/resources/template/css_xhtml/controlfooter.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,26 @@
*/
-->
${attributes.after!}<#t/>
<#lt/>
<#if !attributes.labelPosition?? && (attributes.form.labelPosition)??>
<#assign labelPos = attributes.form.labelPosition/>
<#elseif attributes.labelPosition??>
<#assign labelPos = attributes.labelPosition/>
</#if>
<#if (labelPos!"top") == 'top'>
</div> <#rt/>
</div><#rt/>
<#else>
</span> <#rt/>
</span><#rt/>
</#if>
<#if (attributes.errorposition!"top") == 'bottom'>
<#assign hasFieldErrors = attributes.name?? && fieldErrors?? && fieldErrors.get(attributes.name)??/>
<#if hasFieldErrors>
<div <#rt/><#if attributes.id??>id="wwerr_${attributes.id}"<#rt/></#if> class="wwerr">
<#list fieldErrors.get(attributes.name) as error>
<div<#rt/>
<#if attributes.id??>
errorFor="${attributes.id}"<#rt/>
</#if>
class="errorMessage">
${error}
</div><#t/>
<div<#rt/>
<#if attributes.id??>
errorFor="${attributes.id}"<#rt/>
</#if>
class="errorMessage">${error}</div><#rt/>
</#list>
</div><#t/>
</#if>
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/resources/template/css_xhtml/form-validate.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
-->
<#if attributes.validate!false == true>
<@s.script src="${base}${attributes.staticContentPath}/css_xhtml/validation.js"/>
<#if attributes.onsubmit??>
${tag.addParameter('onsubmit', "${attributes.onsubmit}; return validateForm_${attributes.escapedId}();")}
<#else>
${tag.addParameter('onsubmit', "return validateForm_${attributes.escapedId}();")}
</#if>
<#if attributes.onsubmit??>
${tag.addParameter('onsubmit', "${attributes.onsubmit}; return validateForm_${attributes.escapedId}();")}
<#else>
${tag.addParameter('onsubmit', "return validateForm_${attributes.escapedId}();")}
</#if>
</#if>
1 change: 0 additions & 1 deletion core/src/main/resources/template/css_xhtml/label.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* under the License.
*/
-->
<#--include "/${attributes.templateDir}/css_xhtml/controlheader.ftl" /-->
<#include "/${attributes.templateDir}/${attributes.expandTheme}/controlheader.ftl" />
<label<#rt/>
<#if attributes.id??>
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/resources/template/simple/actionerror.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
-->
<#if (actionErrors?? && actionErrors?size > 0)>
<ul<#rt/>
<ul<#rt/>
<#if attributes.id??>
id="${attributes.id}"<#rt/>
</#if>
Expand All @@ -32,10 +32,10 @@
style="${attributes.cssStyle}"<#rt/>
</#if>
>
<#list actionErrors as error>
<#if error??>
<li><span><#if attributes.escape>${error!}<#else>${error!?no_esc}</#if></span><#rt/></li><#rt/>
</#if>
</#list>
</ul>
<#list actionErrors as error>
<#if error??>
<li><span><#if attributes.escape>${error!}<#else>${error!?no_esc}</#if></span><#rt/></li><#rt/>
</#if>
</#list>
</ul>
</#if>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* under the License.
*/
-->

<#--
Code that will add javascript needed for tooltips
--><#t/>
Expand Down
2 changes: 0 additions & 2 deletions core/src/main/resources/template/simple/form-close.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
-->
</form>

<#if (attributes.customOnsubmitEnabled??)>
<@s.script>
<#--
Expand Down Expand Up @@ -98,5 +97,4 @@
</#if>
</@s.script>
</#if>

<#include "/${attributes.templateDir}/${attributes.expandTheme}/form-close-tooltips.ftl" />
12 changes: 6 additions & 6 deletions core/src/main/resources/template/xhtml/controlheader.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
*/
-->
<#include "/${attributes.templateDir}/${attributes.expandTheme}/controlheader-core.ftl" />
<td
<#if attributes.align?? >
class="align-${attributes.align}"
<#else >
class="tdInput"
</#if>
<td<#rt/>
<#if attributes.align?? >
class="align-${attributes.align}"<#rt/>
<#else >
class="tdInput"<#rt/>
</#if>
><#t/>
12 changes: 6 additions & 6 deletions core/src/main/resources/template/xhtml/form-validate.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
*/
-->
<#if attributes.validate!false == true>
<@s.script src="${base}${attributes.staticContentPath}/xhtml/validation.js" />
<#if attributes.onsubmit??>
${tag.addParameter('onsubmit', "${attributes.onsubmit}; return validateForm_${attributes.escapedId}();")}
<#else>
${tag.addParameter('onsubmit', "return validateForm_${attributes.escapedId}();")}
</#if>
<@s.script src="${base}${attributes.staticContentPath}/xhtml/validation.js" />
<#if attributes.onsubmit??>
${tag.addParameter('onsubmit', "${attributes.onsubmit}; return validateForm_${attributes.escapedId}();")}
<#else>
${tag.addParameter('onsubmit', "return validateForm_${attributes.escapedId}();")}
</#if>
</#if>
32 changes: 32 additions & 0 deletions core/src/site/resources/tags/compress-attributes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<table class="tag-reference">
<tr>
<td colspan="6"><h4>Dynamic Attributes Allowed:</h4> false</td>
</tr>
<tr>
<td colspan="6"><hr/></td>
</tr>
<tr>
<th class="tag-header"><h4>Name</h4></th>
<th class="tag-header"><h4>Required</h4></th>
<th class="tag-header"><h4>Default</h4></th>
<th class="tag-header"><h4>Evaluated</h4></th>
<th class="tag-header"><h4>Type</h4></th>
<th class="tag-header"><h4>Description</h4></th>
</tr>
<tr>
<td class="tag-attribute">force</td>
<td class="tag-attribute">false</td>
<td class="tag-attribute"></td>
<td class="tag-attribute">false</td>
<td class="tag-attribute">String</td>
<td class="tag-attribute">Force output compression</td>
</tr>
<tr>
<td class="tag-attribute">performClearTagStateForTagPoolingServers</td>
<td class="tag-attribute">false</td>
<td class="tag-attribute">false</td>
<td class="tag-attribute">false</td>
<td class="tag-attribute">Boolean</td>
<td class="tag-attribute">Whether to clear all tag state during doEndTag() processing (if applicable)</td>
</tr>
</table>
1 change: 1 addition & 0 deletions core/src/site/resources/tags/compress-description.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Compress wrapped content
Loading