| 15 |
*/ |
*/ |
| 16 |
package jp.sf.pal.pooptimizer; |
package jp.sf.pal.pooptimizer; |
| 17 |
|
|
| 18 |
import java.io.BufferedWriter; |
import java.io.BufferedOutputStream; |
| 19 |
import java.io.IOException; |
import java.io.IOException; |
| 20 |
import java.io.InputStream; |
import java.io.InputStream; |
| 21 |
|
|
| 44 |
*/ |
*/ |
| 45 |
private static final Log log = LogFactory.getLog(OptimizerFilter.class); |
private static final Log log = LogFactory.getLog(OptimizerFilter.class); |
| 46 |
|
|
| 47 |
private static final int BLOCK_SIZE = 4096; |
private static final int DEFAULT_BLOCK_SIZE = 4096; |
| 48 |
|
|
| 49 |
|
private static final String BLOCK_SIZE_KEY = "block-size"; |
| 50 |
|
|
| 51 |
|
private int blockSize = DEFAULT_BLOCK_SIZE; |
| 52 |
|
|
| 53 |
/* |
/* |
| 54 |
* (non-Javadoc) |
* (non-Javadoc) |
| 65 |
* @see org.apache.portals.bridges.portletfilter.PortletFilter#init(org.apache.portals.bridges.portletfilter.PortletFilterConfig) |
* @see org.apache.portals.bridges.portletfilter.PortletFilter#init(org.apache.portals.bridges.portletfilter.PortletFilterConfig) |
| 66 |
*/ |
*/ |
| 67 |
public void init(PortletFilterConfig config) throws PortletException { |
public void init(PortletFilterConfig config) throws PortletException { |
| 68 |
|
String blockSizeStr = config.getInitParameter(BLOCK_SIZE_KEY); |
| 69 |
|
if (blockSizeStr != null) { |
| 70 |
|
try { |
| 71 |
|
blockSize = Integer.parseInt(blockSizeStr); |
| 72 |
|
} catch (NumberFormatException e) { |
| 73 |
|
log.warn("Could not parse " + blockSizeStr, e); |
| 74 |
|
} |
| 75 |
|
} |
| 76 |
} |
} |
| 77 |
|
|
| 78 |
/* |
/* |
| 123 |
bufferedResponseStream.commit(); |
bufferedResponseStream.commit(); |
| 124 |
|
|
| 125 |
InputStream reader = bufferedResponseStream.getInputStream(); |
InputStream reader = bufferedResponseStream.getInputStream(); |
| 126 |
BufferedWriter writer = new BufferedWriter(bufferedResponseStream |
BufferedOutputStream outputStream = new BufferedOutputStream( |
| 127 |
.getWriter()); |
bufferedResponseStream.getOutputStream()); |
| 128 |
|
|
| 129 |
byte[] bytes = new byte[BLOCK_SIZE]; |
byte[] bytes = new byte[blockSize]; |
| 130 |
try { |
try { |
| 131 |
int length = reader.read(bytes); |
int length = reader.read(bytes); |
| 132 |
if (length != -1) { |
if (length != -1) { |
| 133 |
String str = new String(bytes, 0, length, |
String str = new String(bytes, 0, length, "ISO-8859-1"); |
|
bufferedResponseStream.getEncoding()); |
|
| 134 |
int beginBodyIndex = str.indexOf("<body"); |
int beginBodyIndex = str.indexOf("<body"); |
| 135 |
|
if (beginBodyIndex == -1) { |
| 136 |
|
beginBodyIndex = str.indexOf("<BODY"); |
| 137 |
|
} |
| 138 |
if (beginBodyIndex >= 0) { |
if (beginBodyIndex >= 0) { |
| 139 |
str = str.substring(beginBodyIndex).replaceFirst( |
int beginBodyIndexEnd = str |
| 140 |
"<body[^>]*>", ""); |
.indexOf(">", beginBodyIndex); |
| 141 |
int endBodyIndex = str.indexOf("</body"); |
int endBodyIndex = str.indexOf("</body"); |
| 142 |
if (endBodyIndex >= 0) { |
if (endBodyIndex == -1) { |
| 143 |
writer.write(str.substring(0, endBodyIndex)); |
endBodyIndex = str.indexOf("</BODY"); |
| 144 |
|
} |
| 145 |
|
if (endBodyIndex > beginBodyIndex) { |
| 146 |
|
outputStream.write(bytes, beginBodyIndexEnd + 1, |
| 147 |
|
endBodyIndex - beginBodyIndexEnd - 1); |
| 148 |
} else { |
} else { |
| 149 |
|
outputStream.write(bytes, beginBodyIndexEnd + 1, |
| 150 |
|
length - beginBodyIndexEnd - 1); |
| 151 |
length = reader.read(bytes); |
length = reader.read(bytes); |
| 152 |
while (length != -1) { |
while (length != -1) { |
| 153 |
str = new String(bytes, 0, length, |
str = new String(bytes, 0, length, "ISO-8859-1"); |
|
bufferedResponseStream.getEncoding()); |
|
| 154 |
endBodyIndex = str.indexOf("</body"); |
endBodyIndex = str.indexOf("</body"); |
| 155 |
|
if (endBodyIndex == -1) { |
| 156 |
|
endBodyIndex = str.indexOf("</BODY"); |
| 157 |
|
} |
| 158 |
if (endBodyIndex >= 0) { |
if (endBodyIndex >= 0) { |
| 159 |
writer |
outputStream.write(bytes, 0, endBodyIndex); |
|
.write(str.substring(0, |
|
|
endBodyIndex)); |
|
| 160 |
break; |
break; |
| 161 |
} else { |
} else { |
| 162 |
if (length != 0) { |
if (length > 0) { |
| 163 |
writer.write(str); |
outputStream.write(bytes, 0, length); |
| 164 |
} |
} |
| 165 |
length = reader.read(bytes); |
length = reader.read(bytes); |
| 166 |
} |
} |
| 168 |
} |
} |
| 169 |
} else { |
} else { |
| 170 |
if (length != -1) { |
if (length != -1) { |
| 171 |
writer.write(str); |
outputStream.write(bytes, 0, length); |
| 172 |
} |
} |
| 173 |
length = reader.read(bytes); |
length = reader.read(bytes); |
| 174 |
while (length != -1) { |
while (length != -1) { |
| 175 |
str = new String(bytes, 0, length, |
str = new String(bytes, 0, length, |
| 176 |
bufferedResponseStream.getEncoding()); |
bufferedResponseStream.getEncoding()); |
| 177 |
if (length != 0) { |
if (length > 0) { |
| 178 |
writer.write(str); |
outputStream.write(bytes, 0, length); |
| 179 |
} |
} |
| 180 |
length = reader.read(bytes); |
length = reader.read(bytes); |
| 181 |
} |
} |
| 182 |
} |
} |
| 183 |
} |
} |
| 184 |
writer.flush(); |
outputStream.flush(); |
| 185 |
} finally { |
} finally { |
| 186 |
bytes = null; |
bytes = null; |
| 187 |
} |
} |