001: // Copyright © 2004-2005 ASERT. Released under the Canoo Webtest license.
002: package com.canoo.webtest.boundary;
003:
004: import com.canoo.webtest.engine.StepExecutionException;
005: import com.canoo.webtest.engine.Context;
006: import com.canoo.webtest.steps.Step;
007: import org.apache.log4j.Logger;
008: import org.apache.commons.io.IOUtils;
009:
010: import java.io.IOException;
011: import java.io.InputStream;
012: import java.io.OutputStream;
013: import java.io.ObjectInputStream;
014:
015: /**
016: * Helper class for working with streams.
017: *
018: * @author Paul King
019: */
020: public final class StreamBoundary {
021: private static final Logger LOG = Logger
022: .getLogger(StreamBoundary.class);
023:
024: private StreamBoundary() {
025: }
026:
027: /**
028: * Close an InputStream ignoring an errors.
029: *
030: * Wraps IOException's.
031: *
032: * @param inStream
033: */
034: public static void closeInputStream(final InputStream inStream) {
035: if (inStream != null) {
036: try {
037: inStream.close();
038: } catch (IOException e) {
039: LOG.warn("Error closing stream: " + e.getMessage(), e);
040: }
041: }
042: }
043:
044: /**
045: * Close an OutputStream ignoring an errors.
046: *
047: * Wraps IOException's.
048: *
049: * @param outStream
050: */
051: public static void closeOutputStream(final OutputStream outStream) {
052: if (outStream != null) {
053: try {
054: outStream.close();
055: } catch (IOException e) {
056: LOG.warn("Error closing stream: " + e.getMessage(), e);
057: }
058: }
059: }
060:
061: /**
062: * Close an InputStream and abort step if an error occur.
063: *
064: * Wraps IOException's.
065: *
066: * @param is
067: * @param step
068: */
069: public static void tryCloseInputStreamWithFail(
070: final InputStream is, final Step step) {
071: if (is != null) {
072: try {
073: is.close();
074: } catch (IOException e) {
075: LOG.error(e.getMessage(), e);
076: throw new StepExecutionException(
077: "Error closing stream: " + e.getMessage(), step);
078: }
079: }
080: }
081:
082: /**
083: * Close an OutputStream and abort step if an error occur.
084: *
085: * Wraps IOException's.
086: *
087: * @param os
088: * @param step
089: */
090: public static void tryCloseOutputStream(final OutputStream os,
091: final Step step) {
092: if (os != null) {
093: try {
094: os.close();
095: } catch (IOException e) {
096: LOG.error(e.getMessage(), e);
097: throw new StepExecutionException(
098: "Error closing stream: " + e.getMessage(), step);
099: }
100: }
101: }
102:
103: public static Object tryReadObject(final ObjectInputStream ois,
104: final Step step) throws IOException {
105: try {
106: return ois.readObject();
107: } catch (ClassNotFoundException e) {
108: LOG.error(e.getMessage(), e);
109: throw new StepExecutionException(
110: "Error reading object from file: " + e.getMessage(),
111: step);
112: }
113: }
114:
115: public static byte[] tryGetBytes(final Context context,
116: final Step step) {
117: try {
118: return IOUtils.toByteArray(context.getCurrentResponse()
119: .getWebResponse().getContentAsStream());
120: } catch (IOException e) {
121: throw new StepExecutionException(
122: "Error processing content: " + e.getMessage(), step);
123: }
124: }
125: }
|