|
| 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 | +} |
0 commit comments