|
| 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 | + |
| 31 | +/** |
| 32 | + * <p> |
| 33 | + * Used to compress HTML output. Just wrap a given section with the tag. |
| 34 | + * </p> |
| 35 | + * |
| 36 | + * <p> |
| 37 | + * Configurable attributes are: |
| 38 | + * </p> |
| 39 | + * |
| 40 | + * <ul> |
| 41 | + * <li>force (true/false) - always compress output, this can be useful in DevMode as devMode disables compression</li> |
| 42 | + * </ul> |
| 43 | + * |
| 44 | + * <p><b>Examples</b></p> |
| 45 | + * <pre> |
| 46 | + * <!-- START SNIPPET: example --> |
| 47 | + * <s:compress> |
| 48 | + * <s:form action="submit"> |
| 49 | + * <s:text name="name" /> |
| 50 | + * ... |
| 51 | + * </s:form> |
| 52 | + * </s:compress> |
| 53 | + * <!-- END SNIPPET: example --> |
| 54 | + * </pre> |
| 55 | + * |
| 56 | + * <p>Uses conditional compression depending on action</p> |
| 57 | + * <pre> |
| 58 | + * <!-- START SNIPPET: example --> |
| 59 | + * <s:compress force="shouldCompress"> |
| 60 | + * <s:form action="submit"> |
| 61 | + * <s:text name="name" /> |
| 62 | + * ... |
| 63 | + * </s:form> |
| 64 | + * </s:compress> |
| 65 | + * <!-- END SNIPPET: example --> |
| 66 | + * </pre> |
| 67 | + * "shouldCompress" is a field with getter define on action used in expression evaluation |
| 68 | + */ |
| 69 | +@StrutsTag(name = "compress", tldTagClass = "org.apache.struts2.views.jsp.CompressTag", |
| 70 | + description = "Compress wrapped content") |
| 71 | +public class Compress extends Component { |
| 72 | + |
| 73 | + private static final Logger LOG = LogManager.getLogger(Compress.class); |
| 74 | + |
| 75 | + private String force; |
| 76 | + |
| 77 | + public Compress(ValueStack stack) { |
| 78 | + super(stack); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean end(Writer writer, String body) { |
| 83 | + Object forceValue = findValue(force, Boolean.class); |
| 84 | + boolean forced = forceValue != null && Boolean.parseBoolean(forceValue.toString()); |
| 85 | + if (devMode && !forced) { |
| 86 | + LOG.debug("Avoids compressing output: {} in DevMode", body); |
| 87 | + return super.end(writer, body, true); |
| 88 | + } |
| 89 | + LOG.trace("Compresses: {}", body); |
| 90 | + String compressed = body.trim().replaceAll(">\\s+<", "><"); |
| 91 | + LOG.trace("Compressed: {}", compressed); |
| 92 | + return super.end(writer, compressed, true); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean usesBody() { |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + @StrutsTagAttribute(description = "Force output compression") |
| 101 | + public void setForce(String force) { |
| 102 | + this.force = force; |
| 103 | + } |
| 104 | +} |
0 commit comments