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.Vector;
007: import java.util.zip.Adler32;
008: import java.util.zip.CheckedInputStream;
009: import java.util.zip.CheckedOutputStream;
010: import java.util.zip.ZipEntry;
011: import java.util.zip.ZipInputStream;
012: import java.util.zip.ZipOutputStream;
013:
014: import net.xoetrope.debug.DebugLogger;
015: import net.xoetrope.optional.service.ServiceProxy;
016: import net.xoetrope.optional.service.ServiceProxyException;
017: import com.xoetrope.carousel.build.BuildProperties;
018: import java.util.Hashtable;
019: import net.xoetrope.optional.service.ServiceContext;
020: import net.xoetrope.optional.service.ServiceProxyArgs;
021:
022: /**
023: * Compresses the output data in a zip stream
024: *
025: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
026: * the GNU Public License (GPL), please see license.txt for more details. If
027: * you make commercial use of this software you must purchase a commercial
028: * license from Xoetrope.</p>
029: * <p> $Revision: 1.9 $</p>
030: */
031: public class ZipDecoderService extends ServiceProxy {
032: public ZipDecoderService() {
033: status = OK;
034: }
035:
036: /**
037: * Deompresses the call, its arguments using the ZLib zip algorithm. The
038: * response is compressed. The call is then forwarded to the
039: * next service proxy in the chain.
040: * @param method the method name to be invoked
041: * @param context the context of the service call
042: * @return the response or result of the call
043: * @throws ServiceProxyException
044: */
045: public Object call(String method, ServiceContext context)
046: throws ServiceProxyException {
047: try {
048: status = STARTED;
049: //int numArgs = args.getNumArgs();
050: ServiceProxyArgs args = context.getArgs();
051:
052: // Decompress the output.
053: if (BuildProperties.DEBUG) {
054: if (args.getPassParam("ZipStream") == null) {
055: DebugLogger
056: .logError("Invalid ZipDecoderService argument");
057: throw new ServiceProxyException(
058: "Invalid ZipDecoderService argument");
059: }
060: }
061:
062: ByteArrayInputStream bis = new ByteArrayInputStream(
063: org.apache.catalina.util.Base64
064: .decode(((String) args
065: .getPassParam("ZipStream"))
066: .getBytes()));
067: CheckedInputStream cis = new CheckedInputStream(bis,
068: new Adler32());
069: ZipInputStream zis = new ZipInputStream(cis);
070: Vector zipArgNames = new Vector();
071: Vector zipArgValues = new Vector();
072:
073: final int BUFFER_SIZE = 2048;
074: int count;
075: byte[] buf = new byte[BUFFER_SIZE];
076: ZipEntry ze;
077: while ((ze = zis.getNextEntry()) != null) {
078: zipArgNames.addElement(ze.getName());
079: ByteArrayOutputStream bos = new ByteArrayOutputStream();
080: while ((zis.available() > 0)
081: && ((count = zis.read(buf, 0, BUFFER_SIZE)) != -1))
082: bos.write(buf, 0, count);
083:
084: zipArgValues.addElement(new String(bos.toByteArray()));
085: }
086:
087: // Call the next service proxy
088: ServiceContext callContext = new ServiceContext();
089: Hashtable passArgs = callContext.getPassArgs();
090: passArgs.clear();
091: int numDecodedArgs = zipArgNames.size();
092: String decodedArgNames[] = new String[numDecodedArgs];
093: for (int k = 0; k < numDecodedArgs; k++)
094: passArgs.put((String) zipArgNames.elementAt(k),
095: zipArgValues.elementAt(k));
096:
097: Object status = nextProxy.call(method, callContext);
098: Object result = callContext.getReturnArgs().get(
099: ServiceProxy.RETURN_VALUE);
100:
101: ByteArrayOutputStream bos = new ByteArrayOutputStream();
102: CheckedOutputStream cos = new CheckedOutputStream(bos,
103: new Adler32());
104: ZipOutputStream zos = new ZipOutputStream(cos);
105:
106: // Create a new entry and output
107: if (result != null) {
108: ZipEntry zeOut = new ZipEntry("ZipStream");
109: zos.putNextEntry(zeOut);
110: zos.write(result.toString().getBytes());
111: zeOut.setCrc(cos.getChecksum().getValue());
112: }
113: zos.close();
114:
115: status = COMPLETE;
116: context.setReturnValue(new String(
117: org.apache.catalina.util.Base64.encode(bos
118: .toByteArray())));
119: return status;
120: } catch (IOException ioex) {
121: status = FAILED;
122: if (BuildProperties.DEBUG)
123: ioex.printStackTrace();
124: } catch (ServiceProxyException ex) {
125: status = FAILED;
126: throw (ex);
127: }
128:
129: return null;
130: }
131: }
|