001: package com.xoetrope.service;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.IOException;
006: import java.util.zip.Adler32;
007: import java.util.zip.CheckedInputStream;
008: import java.util.zip.CheckedOutputStream;
009: import java.util.zip.ZipEntry;
010: import java.util.zip.ZipInputStream;
011: import java.util.zip.ZipOutputStream;
012:
013: import net.xoetrope.optional.service.ServiceProxy;
014: import net.xoetrope.optional.service.ServiceProxyException;
015: import com.xoetrope.carousel.build.BuildProperties;
016: import java.util.Enumeration;
017: import java.util.Hashtable;
018: import net.xoetrope.optional.service.ServiceContext;
019: import net.xoetrope.optional.service.ServiceProxyArgs;
020:
021: /**
022: * Compresses the output data in a zip stream
023: * <p>License: This class is part of a commercial software release. Please see the
024: * license.txt file that ships with the product for further details.</p>
025: *
026: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
027: * the GNU Public License (GPL), please see license.txt for more details. If
028: * you make commercial use of this software you must purchase a commercial
029: * license from Xoetrope.</p>
030: * <p> $Revision: 1.9 $</p>
031: */
032: public class ZipEncoderService extends ServiceProxy {
033: public ZipEncoderService() {
034: status = OK;
035: }
036:
037: /**
038: * Compresses the call, its arguments using the ZLib zip algorithm. The
039: * response is uncompressed. The call is then forwarded to the
040: * next service proxy in the chain.
041: * @param method the method name to be invoked
042: * @param context the context object of the service call
043: * @return the response or result of the call
044: * @throws ServiceProxyException
045: */
046: public Object call(String method, ServiceContext context)
047: throws ServiceProxyException {
048: try {
049: status = STARTED;
050:
051: ByteArrayOutputStream bos = new ByteArrayOutputStream();
052: CheckedOutputStream cos = new CheckedOutputStream(bos,
053: new Adler32());
054: ZipOutputStream zos = new ZipOutputStream(cos);
055: Hashtable passArgs = context.getPassArgs();
056: Enumeration keys = passArgs.keys();
057: while (keys.hasMoreElements()) {
058: String name = (String) keys.nextElement();
059: String value = passArgs.get(name).toString();
060: if (value != null) {
061: // Create a new entry and output
062: byte[] bytes = value.getBytes();
063: ZipEntry ze = new ZipEntry(name);
064: zos.putNextEntry(ze);
065: zos.write(bytes, 0, bytes.length);
066: ze.setSize(bytes.length);
067: ze.setCrc(cos.getChecksum().getValue());
068: }
069: }
070: zos.finish();
071: zos.flush();
072: zos.close();
073:
074: // Extract the byte buffer from the stream.
075: ServiceContext callContext = new ServiceContext();
076: ServiceProxyArgs zipArgs = callContext.getArgs();
077: passArgs = callContext.getPassArgs();
078: passArgs.clear();
079: passArgs.put("ZipStream", new String(
080: org.apache.catalina.util.Base64.encode(bos
081: .toByteArray())));
082:
083: // Call the next service proxy
084: Object callState = nextProxy.call(method, callContext);
085:
086: // Decompress the output.
087: if (callContext.hasReturnValue()) {
088: String result = callContext.getReturnValue().toString();
089: ByteArrayInputStream bis = new ByteArrayInputStream(
090: org.apache.catalina.util.Base64.decode(result
091: .getBytes()));
092: CheckedInputStream cis = new CheckedInputStream(bis,
093: new Adler32());
094: ZipInputStream zis = new ZipInputStream(cis);
095: bos = new ByteArrayOutputStream();
096: final int BUFFER_SIZE = 2048;
097: int count;
098: byte[] buf = new byte[BUFFER_SIZE];
099: ZipEntry ze;
100: while ((ze = zis.getNextEntry()) != null) {
101: while ((zis.available() > 0)
102: && ((count = zis.read(buf, 0, BUFFER_SIZE)) != -1))
103: bos.write(buf, 0, count);
104: }
105: context.setReturnValue(new String(bos.toByteArray()));
106: }
107: status = COMPLETE;
108: } catch (IOException ioex) {
109: status = FAILED;
110: if (BuildProperties.DEBUG)
111: ioex.printStackTrace();
112: } catch (ServiceProxyException ex) {
113: status = FAILED;
114: throw (ex);
115: }
116:
117: return status;
118: }
119: }
|