001: // Copyright © 2002-2007 Canoo Engineering AG, Switzerland. Released under the Canoo Webtest license.
002: package com.canoo.webtest.util;
003:
004: import java.io.File;
005: import java.io.FileInputStream;
006: import java.io.FileOutputStream;
007: import java.io.IOException;
008: import java.io.InputStream;
009: import java.io.ObjectInputStream;
010: import java.io.ObjectOutputStream;
011:
012: import com.canoo.webtest.boundary.StreamBoundary;
013: import com.canoo.webtest.engine.StepExecutionException;
014: import com.canoo.webtest.steps.Step;
015: import com.gargoylesoftware.htmlunit.WebResponse;
016:
017: import org.apache.commons.io.IOUtils;
018: import org.apache.log4j.Logger;
019:
020: /**
021: * Utility class for working with files.
022: *
023: * @author Paul King
024: */
025: public class FileUtil {
026: private static final Logger LOG = Logger.getLogger(FileUtil.class);
027:
028: /**
029: * Reads a file into a String.
030: *
031: * @param file
032: * @param step
033: * @return the resulting String
034: */
035: public static String readFileToString(final File file,
036: final Step step) {
037: String canonicalPath = null;
038: String result = null;
039: InputStream inputStream = null;
040: try {
041: canonicalPath = file.getCanonicalPath();
042: inputStream = new FileInputStream(file);
043: result = IOUtils.toString(inputStream);
044: } catch (IOException e) {
045: throw new StepExecutionException("Could not find/read \""
046: + canonicalPath + "\".", step);
047: } finally {
048: IOUtils.closeQuietly(inputStream);
049: }
050: return result;
051: }
052:
053: /**
054: * Reads a file into a byte[].
055: *
056: * @param file
057: * @param step
058: * @return the resulting byte[]
059: */
060: public static byte[] readFileToByteArray(final File file,
061: final Step step) {
062: String canonicalPath = null;
063: byte[] result = null;
064: InputStream inputStream = null;
065: try {
066: canonicalPath = file.getCanonicalPath();
067: inputStream = new FileInputStream(file);
068: result = IOUtils.toByteArray(inputStream);
069: } catch (IOException e) {
070: throw new StepExecutionException("Could not find/read \""
071: + canonicalPath + "\".", step);
072: } finally {
073: IOUtils.closeQuietly(inputStream);
074: }
075: return result;
076: }
077:
078: /**
079: * Writes a String to a file.
080: *
081: * @param file
082: * @param content
083: * @param step
084: * @throws StepExecutionException if something goes wrong
085: */
086: public static void writeStringToFile(final File file,
087: final String content, final Step step) {
088: String canonicalPath = null;
089: FileOutputStream outputStream = null;
090: try {
091: canonicalPath = file.getCanonicalPath();
092: outputStream = new FileOutputStream(file);
093: IOUtils.write(content, outputStream);
094: } catch (IOException e) {
095: throw new StepExecutionException("Could not find/write \""
096: + canonicalPath + "\".", step);
097: } finally {
098: IOUtils.closeQuietly(outputStream);
099: }
100: }
101:
102: /**
103: * Writes a Stream to a file.
104: *
105: * @param inputResponse
106: * @param destfile
107: * @param step
108: * @throws StepExecutionException if something goes wrong
109: */
110: public static void writeResponseStreamToFile(
111: final WebResponse inputResponse, final File destfile,
112: final Step step) {
113: String canonicalPath = null;
114: FileOutputStream outputStream = null;
115: try {
116: canonicalPath = destfile.getCanonicalPath();
117: outputStream = new FileOutputStream(destfile);
118: IOUtils.copy(inputResponse.getContentAsStream(),
119: outputStream);
120: } catch (IOException e) {
121: throw new StepExecutionException("Could not find/write \""
122: + canonicalPath + "\".", step);
123: } finally {
124: IOUtils.closeQuietly(outputStream);
125: }
126: }
127:
128: /**
129: * Creates parent directory for file if required.
130: *
131: * @param file
132: */
133: public static void prepareDirs(final File file) {
134: if (file.getParentFile() != null
135: && !file.getParentFile().exists()) {
136: prepareDirs(file.getParentFile());
137: file.getParentFile().mkdirs();
138: }
139: }
140:
141: /**
142: * Helper method for reading objects from a file.
143: *
144: * @param file the file to read from
145: * @param step step requesting the operation
146: * @return the object from the file if everything worked correctly
147: */
148: public static Object tryReadObjectFromFile(final File file,
149: final Step step) {
150: FileInputStream fis = null;
151: ObjectInputStream ois = null;
152: String message = "finding";
153: try {
154: fis = new FileInputStream(file);
155: ois = new ObjectInputStream(fis);
156: message = "reading from";
157: return StreamBoundary.tryReadObject(ois, step);
158: } catch (IOException e) {
159: LOG.error(e.getMessage(), e);
160: throw new StepExecutionException("Error " + message
161: + " file: " + e.getMessage(), step);
162: } finally {
163: IOUtils.closeQuietly(ois);
164: IOUtils.closeQuietly(fis);
165: }
166: }
167:
168: /**
169: * Helper method for writing objects to a file.
170: *
171: * @param file the file to write to
172: * @param object the object to write
173: * @param step step requesting the operation
174: */
175: public static boolean tryWriteObjectToFile(final File file,
176: final Object object, final Step step) {
177: FileOutputStream fos = null;
178: ObjectOutputStream oos = null;
179: String message = "creating";
180: boolean success = false;
181: try {
182: fos = new FileOutputStream(file);
183: oos = new ObjectOutputStream(fos);
184: message = "filling";
185: oos.writeObject(object);
186: success = true;
187: } catch (IOException e) {
188: LOG.error("Error during write: " + e.getMessage(), e);
189: if (step != null) {
190: throw new StepExecutionException("Error " + message
191: + " file: " + e.getMessage(), step);
192: }
193: } finally {
194: StreamBoundary.closeOutputStream(oos);
195: StreamBoundary.closeOutputStream(fos);
196: }
197: return success;
198: }
199:
200: /**
201: * Creates a temporary file using a prefix and suffix and marks it for deletion on exit.
202: *
203: * @param prefix
204: * @param suffix
205: * @param step
206: * @throws StepExecutionException if an error occurs while creating the file
207: * @return the newly created temp file
208: */
209: public static File tryCreateTempFile(final String prefix,
210: final String suffix, final Step step) {
211: final File tmpFile;
212: try {
213: tmpFile = File.createTempFile(prefix, suffix);
214: } catch (Exception e) {
215: LOG.error(e.getMessage(), e);
216: throw new StepExecutionException(
217: "Error creating temporary file " + e.getMessage(),
218: step);
219: }
220: tmpFile.deleteOnExit();
221: return tmpFile;
222: }
223: }
|