Skip to content

Commit f1f0848

Browse files
committed
WW-5256 Implements dedicated tag to compress output
1 parent f06bc51 commit f1f0848

File tree

16 files changed

+441
-54
lines changed

16 files changed

+441
-54
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.struts2.components;
20+
21+
import org.apache.logging.log4j.LogManager;
22+
import org.apache.logging.log4j.Logger;
23+
import org.apache.struts2.util.ValueStack;
24+
import org.apache.struts2.views.annotations.StrutsTag;
25+
import org.apache.struts2.views.annotations.StrutsTagAttribute;
26+
27+
import java.io.Writer;
28+
29+
/**
30+
* <p>
31+
* Used to compress HTML output. Just wrap a given section with the tag.
32+
* </p>
33+
*
34+
* <p>
35+
* Configurable attributes are:
36+
* </p>
37+
*
38+
* <ul>
39+
* <li>force (true/false) - always compress output, this can be useful in DevMode as devMode disables compression</li>
40+
* </ul>
41+
*
42+
* <p><b>Examples</b></p>
43+
* <pre>
44+
* <!-- START SNIPPET: example -->
45+
* &lt;s:compress&gt;
46+
* &lt;s:form action="submit"&gt;
47+
* &lt;s:text name="name" /&gt;
48+
* ...
49+
* &lt;/s:form&gt;
50+
* &lt;/s:compress&gt;
51+
* <!-- END SNIPPET: example -->
52+
* </pre>
53+
*
54+
* <p>Uses conditional compression depending on action</p>
55+
* <pre>
56+
* <!-- START SNIPPET: example -->
57+
* &lt;s:compress force="shouldCompress"&gt;
58+
* &lt;s:form action="submit"&gt;
59+
* &lt;s:text name="name" /&gt;
60+
* ...
61+
* &lt;/s:form&gt;
62+
* &lt;/s:compress&gt;
63+
* <!-- END SNIPPET: example -->
64+
* </pre>
65+
* "shouldCompress" is a field with getter define on action used in expression evaluation
66+
*/
67+
@StrutsTag(name = "compress", tldTagClass = "org.apache.struts2.views.jsp.CompressTag",
68+
description = "Compress wrapped content")
69+
public class Compress extends Component {
70+
71+
private static final Logger LOG = LogManager.getLogger(Compress.class);
72+
73+
private String force;
74+
75+
public Compress(ValueStack stack) {
76+
super(stack);
77+
}
78+
79+
@Override
80+
public boolean end(Writer writer, String body) {
81+
Object forceValue = findValue(force, Boolean.class);
82+
boolean forced = forceValue != null && Boolean.parseBoolean(forceValue.toString());
83+
if (devMode && !forced) {
84+
LOG.debug("Avoids compressing output: {} in DevMode", body);
85+
return super.end(writer, body, true);
86+
}
87+
LOG.trace("Compresses: {}", body);
88+
String compressed = body.trim().replaceAll(">\\s+<", "><");
89+
LOG.trace("Compressed: {}", compressed);
90+
return super.end(writer, compressed, true);
91+
}
92+
93+
@Override
94+
public boolean usesBody() {
95+
return true;
96+
}
97+
98+
@StrutsTagAttribute(description = "Force output compression")
99+
public void setForce(String force) {
100+
this.force = force;
101+
}
102+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.struts2.views.jsp;
20+
21+
import jakarta.servlet.http.HttpServletRequest;
22+
import jakarta.servlet.http.HttpServletResponse;
23+
import org.apache.struts2.components.Component;
24+
import org.apache.struts2.components.Compress;
25+
import org.apache.struts2.util.ValueStack;
26+
27+
import java.io.Serial;
28+
29+
/**
30+
* @see org.apache.struts2.components.Compress
31+
*/
32+
public class CompressTag extends ComponentTagSupport {
33+
34+
@Serial
35+
private static final long serialVersionUID = 7572566991679717145L;
36+
37+
private String force;
38+
39+
@Override
40+
public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
41+
return new Compress(stack);
42+
}
43+
44+
@Override
45+
protected void populateParams() {
46+
super.populateParams();
47+
48+
Compress compress = (Compress) component;
49+
compress.setForce(force);
50+
}
51+
52+
public void setForce(String force) {
53+
this.force = force;
54+
}
55+
}

core/src/main/resources/template/css_xhtml/controlfooter.ftl

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,26 @@
1919
*/
2020
-->
2121
${attributes.after!}<#t/>
22-
<#lt/>
2322
<#if !attributes.labelPosition?? && (attributes.form.labelPosition)??>
2423
<#assign labelPos = attributes.form.labelPosition/>
2524
<#elseif attributes.labelPosition??>
2625
<#assign labelPos = attributes.labelPosition/>
2726
</#if>
2827
<#if (labelPos!"top") == 'top'>
29-
</div> <#rt/>
28+
</div><#rt/>
3029
<#else>
31-
</span> <#rt/>
30+
</span><#rt/>
3231
</#if>
3332
<#if (attributes.errorposition!"top") == 'bottom'>
3433
<#assign hasFieldErrors = attributes.name?? && fieldErrors?? && fieldErrors.get(attributes.name)??/>
3534
<#if hasFieldErrors>
3635
<div <#rt/><#if attributes.id??>id="wwerr_${attributes.id}"<#rt/></#if> class="wwerr">
3736
<#list fieldErrors.get(attributes.name) as error>
38-
<div<#rt/>
39-
<#if attributes.id??>
40-
errorFor="${attributes.id}"<#rt/>
41-
</#if>
42-
class="errorMessage">
43-
${error}
44-
</div><#t/>
37+
<div<#rt/>
38+
<#if attributes.id??>
39+
errorFor="${attributes.id}"<#rt/>
40+
</#if>
41+
class="errorMessage">${error}</div><#rt/>
4542
</#list>
4643
</div><#t/>
4744
</#if>

core/src/main/resources/template/css_xhtml/form-validate.ftl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
-->
2121
<#if attributes.validate!false == true>
2222
<@s.script src="${base}${attributes.staticContentPath}/css_xhtml/validation.js"/>
23-
<#if attributes.onsubmit??>
24-
${tag.addParameter('onsubmit', "${attributes.onsubmit}; return validateForm_${attributes.escapedId}();")}
25-
<#else>
26-
${tag.addParameter('onsubmit', "return validateForm_${attributes.escapedId}();")}
27-
</#if>
23+
<#if attributes.onsubmit??>
24+
${tag.addParameter('onsubmit', "${attributes.onsubmit}; return validateForm_${attributes.escapedId}();")}
25+
<#else>
26+
${tag.addParameter('onsubmit', "return validateForm_${attributes.escapedId}();")}
27+
</#if>
2828
</#if>

core/src/main/resources/template/css_xhtml/label.ftl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
* under the License.
1919
*/
2020
-->
21-
<#--include "/${attributes.templateDir}/css_xhtml/controlheader.ftl" /-->
2221
<#include "/${attributes.templateDir}/${attributes.expandTheme}/controlheader.ftl" />
2322
<label<#rt/>
2423
<#if attributes.id??>

core/src/main/resources/template/simple/actionerror.ftl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
-->
2121
<#if (actionErrors?? && actionErrors?size > 0)>
22-
<ul<#rt/>
22+
<ul<#rt/>
2323
<#if attributes.id??>
2424
id="${attributes.id}"<#rt/>
2525
</#if>
@@ -32,10 +32,10 @@
3232
style="${attributes.cssStyle}"<#rt/>
3333
</#if>
3434
>
35-
<#list actionErrors as error>
36-
<#if error??>
37-
<li><span><#if attributes.escape>${error!}<#else>${error!?no_esc}</#if></span><#rt/></li><#rt/>
38-
</#if>
39-
</#list>
40-
</ul>
35+
<#list actionErrors as error>
36+
<#if error??>
37+
<li><span><#if attributes.escape>${error!}<#else>${error!?no_esc}</#if></span><#rt/></li><#rt/>
38+
</#if>
39+
</#list>
40+
</ul>
4141
</#if>

core/src/main/resources/template/simple/form-close-tooltips.ftl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
* under the License.
1919
*/
2020
-->
21-
2221
<#--
2322
Code that will add javascript needed for tooltips
2423
--><#t/>

core/src/main/resources/template/simple/form-close.ftl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
-->
2121
</form>
22-
2322
<#if (attributes.customOnsubmitEnabled??)>
2423
<@s.script>
2524
<#--
@@ -98,5 +97,4 @@
9897
</#if>
9998
</@s.script>
10099
</#if>
101-
102100
<#include "/${attributes.templateDir}/${attributes.expandTheme}/form-close-tooltips.ftl" />

core/src/main/resources/template/xhtml/controlheader.ftl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
*/
2020
-->
2121
<#include "/${attributes.templateDir}/${attributes.expandTheme}/controlheader-core.ftl" />
22-
<td
23-
<#if attributes.align?? >
24-
class="align-${attributes.align}"
25-
<#else >
26-
class="tdInput"
27-
</#if>
22+
<td<#rt/>
23+
<#if attributes.align?? >
24+
class="align-${attributes.align}"<#rt/>
25+
<#else >
26+
class="tdInput"<#rt/>
27+
</#if>
2828
><#t/>

core/src/main/resources/template/xhtml/form-validate.ftl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
*/
2020
-->
2121
<#if attributes.validate!false == true>
22-
<@s.script src="${base}${attributes.staticContentPath}/xhtml/validation.js" />
23-
<#if attributes.onsubmit??>
24-
${tag.addParameter('onsubmit', "${attributes.onsubmit}; return validateForm_${attributes.escapedId}();")}
25-
<#else>
26-
${tag.addParameter('onsubmit', "return validateForm_${attributes.escapedId}();")}
27-
</#if>
22+
<@s.script src="${base}${attributes.staticContentPath}/xhtml/validation.js" />
23+
<#if attributes.onsubmit??>
24+
${tag.addParameter('onsubmit', "${attributes.onsubmit}; return validateForm_${attributes.escapedId}();")}
25+
<#else>
26+
${tag.addParameter('onsubmit', "return validateForm_${attributes.escapedId}();")}
27+
</#if>
2828
</#if>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<table class="tag-reference">
2+
<tr>
3+
<td colspan="6"><h4>Dynamic Attributes Allowed:</h4> false</td>
4+
</tr>
5+
<tr>
6+
<td colspan="6"><hr/></td>
7+
</tr>
8+
<tr>
9+
<th class="tag-header"><h4>Name</h4></th>
10+
<th class="tag-header"><h4>Required</h4></th>
11+
<th class="tag-header"><h4>Default</h4></th>
12+
<th class="tag-header"><h4>Evaluated</h4></th>
13+
<th class="tag-header"><h4>Type</h4></th>
14+
<th class="tag-header"><h4>Description</h4></th>
15+
</tr>
16+
<tr>
17+
<td class="tag-attribute">force</td>
18+
<td class="tag-attribute">false</td>
19+
<td class="tag-attribute"></td>
20+
<td class="tag-attribute">false</td>
21+
<td class="tag-attribute">String</td>
22+
<td class="tag-attribute">Force output compression</td>
23+
</tr>
24+
<tr>
25+
<td class="tag-attribute">performClearTagStateForTagPoolingServers</td>
26+
<td class="tag-attribute">false</td>
27+
<td class="tag-attribute">false</td>
28+
<td class="tag-attribute">false</td>
29+
<td class="tag-attribute">Boolean</td>
30+
<td class="tag-attribute">Whether to clear all tag state during doEndTag() processing (if applicable)</td>
31+
</tr>
32+
</table>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Compress wrapped content

0 commit comments

Comments
 (0)