01: /*
02: * soapUI, copyright (C) 2004-2007 eviware.com
03: *
04: * soapUI is free software; you can redistribute it and/or modify it under the
05: * terms of version 2.1 of the GNU Lesser General Public License as published by
06: * the Free Software Foundation.
07: *
08: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
09: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: * See the GNU Lesser General Public License for more details at gnu.org.
11: */
12:
13: package com.eviware.soapui.impl.wsdl.support;
14:
15: import java.io.ByteArrayInputStream;
16: import java.io.ByteArrayOutputStream;
17: import java.io.IOException;
18: import java.util.zip.GZIPInputStream;
19: import java.util.zip.GZIPOutputStream;
20:
21: import sun.misc.BASE64Decoder;
22: import sun.misc.BASE64Encoder;
23:
24: import com.eviware.soapui.SoapUI;
25: import com.eviware.soapui.config.CompressedStringConfig;
26: import com.eviware.soapui.settings.WsdlSettings;
27: import com.eviware.soapui.support.Tools;
28:
29: /**
30: * Utility class for compressing/decompressing strings stored with CompressedString
31: *
32: * @author ole.matzura
33: */
34:
35: public class CompressedStringSupport {
36: public static String getString(
37: CompressedStringConfig compressedStringConfig) {
38: String compression = compressedStringConfig.getCompression();
39: if ("gzip".equals(compression)) {
40: try {
41: BASE64Decoder decoder = new BASE64Decoder();
42: byte[] bytes = decoder
43: .decodeBuffer(compressedStringConfig
44: .getStringValue());
45: GZIPInputStream in = new GZIPInputStream(
46: new ByteArrayInputStream(bytes));
47: return Tools.readAll(in, -1).toString();
48: } catch (IOException e) {
49: SoapUI.logError(e);
50: }
51: }
52:
53: return compressedStringConfig.getStringValue();
54: }
55:
56: public static void setString(
57: CompressedStringConfig compressedStringConfig, String value) {
58: long limit = SoapUI.getSettings().getLong(
59: WsdlSettings.COMPRESSION_LIMIT, 0);
60: if (limit > 0 && value.length() >= limit) {
61: try {
62: ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
63: GZIPOutputStream out = new GZIPOutputStream(byteOut);
64: out.write(value.getBytes());
65: out.finish();
66: BASE64Encoder encoder = new BASE64Encoder();
67: value = encoder.encode(byteOut.toByteArray());
68: compressedStringConfig.setCompression("gzip");
69: } catch (IOException e) {
70: SoapUI.logError(e);
71: compressedStringConfig.unsetCompression();
72: }
73: } else if (compressedStringConfig.isSetCompression()) {
74: compressedStringConfig.unsetCompression();
75: }
76:
77: compressedStringConfig.setStringValue(value);
78: }
79: }
|