001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.war.filter;
034:
035: import com.flexive.shared.FxSharedUtils;
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: import java.io.ByteArrayOutputStream;
040: import java.io.IOException;
041: import java.io.PrintWriter;
042: import java.io.Writer;
043:
044: /**
045: * Print Writer for the FxResponseWrapper.
046: * <p/>
047: * This file is part of the fleXive 3.x framework.
048: *
049: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
050: * @version $Rev: 1 $
051: */
052:
053: final class FxWriter extends Writer {
054: private static final transient Log LOG = LogFactory
055: .getLog(FxWriter.class);
056: private final PrintWriter pw;
057: private final boolean catchContent;
058: private final ByteArrayOutputStream output;
059:
060: private int len = 0;
061: private boolean commited = false;
062:
063: protected boolean isCommited() {
064: return commited;
065: }
066:
067: public FxWriter(PrintWriter pw, boolean catchContent) {
068: this .pw = pw;
069: this .catchContent = catchContent;
070: output = this .catchContent ? new ByteArrayOutputStream(10000)
071: : null;
072: }
073:
074: /**
075: * Returns the byte count sent to the client.
076: *
077: * @return the byte count sent to the client.
078: */
079: public long getContentLength() {
080: return this .len;
081: }
082:
083: /**
084: * Returns the data sent to the client if the catchContent option was true.
085: * <p/>
086: * if the catchContent option was false a empty array is returned.
087: *
088: * @return the data sent to the client.
089: */
090: public byte[] getData() {
091: if (output == null)
092: return new byte[0];
093: return output.toByteArray();
094: }
095:
096: @Override
097: public void write(char buf[], int off, int len) {
098: if (pw != null)
099: pw.write(buf, off, len);
100: this .len += len;
101: if (catchContent)
102: writeToBuffer(FxSharedUtils.getBytes(new String(buf, off,
103: len)));
104: }
105:
106: @Override
107: public void flush() {
108: try {
109: if (output != null)
110: output.flush();
111: } catch (IOException exc) {
112: LOG.error(this .getClass() + " output buffer flush error:"
113: + exc.getMessage(), exc);
114: }
115: if (pw != null)
116: pw.flush();
117: }
118:
119: @Override
120: public void close() {
121: if (pw != null)
122: pw.close();
123: try {
124: if (output != null)
125: output.close();
126: } catch (IOException exc) {
127: LOG.error(this .getClass() + " output buffer close error:"
128: + exc.getMessage(), exc);
129: }
130: commited = true;
131: }
132:
133: private void writeToBuffer(byte[] b) {
134: try {
135: output.write(b);
136: } catch (IOException exc) {
137: LOG.error(this .getClass()
138: + " is unable to write to output buffer: "
139: + exc.getMessage(), exc);
140: }
141: }
142:
143: }
|