Skip to content

Commit 6a11de7

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

File tree

16 files changed

+411
-50
lines changed

16 files changed

+411
-50
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.StrutsConstants;
24+
import org.apache.struts2.inject.Inject;
25+
import org.apache.struts2.util.ValueStack;
26+
import org.apache.struts2.views.annotations.StrutsTag;
27+
import org.apache.struts2.views.annotations.StrutsTagAttribute;
28+
29+
import java.io.Writer;
30+
import java.util.regex.Pattern;
31+
32+
/**
33+
* Used to compress html output.
34+
*/
35+
@StrutsTag(name = "compress", tldTagClass = "org.apache.struts2.views.jsp.CompressTag",
36+
description = "Compress wrapped content")
37+
public class Compress extends Component {
38+
39+
private static final Logger LOG = LogManager.getLogger(Compress.class);
40+
41+
private boolean devMode;
42+
private String force;
43+
44+
public Compress(ValueStack stack) {
45+
super(stack);
46+
}
47+
48+
@Inject(StrutsConstants.STRUTS_DEVMODE)
49+
public void setDevMode(String devMode) {
50+
this.devMode = Boolean.parseBoolean(devMode);
51+
}
52+
53+
@Override
54+
public boolean end(Writer writer, String body) {
55+
Object forceValue = findValue(force, Boolean.class);
56+
boolean forced = forceValue != null && Boolean.parseBoolean(forceValue.toString());
57+
if (devMode && !forced) {
58+
LOG.debug("Avoids compressing output: {} in DevMode", body);
59+
return super.end(writer, body, true);
60+
}
61+
LOG.trace("Compresses: {}", body);
62+
String compressed = body.trim().replaceAll(">\\s+<", "><");
63+
LOG.trace("Compressed: {}", compressed);
64+
return super.end(writer, compressed, true);
65+
}
66+
67+
@Override
68+
public boolean usesBody() {
69+
return true;
70+
}
71+
72+
@StrutsTagAttribute(description="Force output compression")
73+
public void setForce(String force) {
74+
this.force = force;
75+
}
76+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.components.ContextBean;
26+
import org.apache.struts2.util.ValueStack;
27+
28+
import java.io.Serial;
29+
30+
/**
31+
* @see org.apache.struts2.components.Compress
32+
*/
33+
public class CompressTag extends ComponentTagSupport {
34+
35+
@Serial
36+
private static final long serialVersionUID = 7572566991679717145L;
37+
38+
private String force;
39+
40+
@Override
41+
public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
42+
return new Compress(stack);
43+
}
44+
45+
@Override
46+
protected void populateParams() {
47+
super.populateParams();
48+
49+
Compress compress = (Compress) component;
50+
compress.setForce(force);
51+
}
52+
53+
public void setForce(String force) {
54+
this.force = force;
55+
}
56+
}

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>

0 commit comments

Comments
 (0)