001: package com.xoetrope.service;
002:
003: import java.util.Enumeration;
004: import java.util.Hashtable;
005: import net.xoetrope.optional.service.ServiceContext;
006: import net.xoetrope.optional.service.ServiceProxy;
007: import net.xoetrope.optional.service.ServiceProxyArgs;
008: import net.xoetrope.optional.service.ServiceProxyException;
009: import net.xoetrope.optional.service.XRouteManager;
010:
011: /**
012: * Encodes the argument values using Base64 encoding and decodes the response.
013: *
014: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
015: * the GNU Public License (GPL), please see license.txt for more details. If
016: * you make commercial use of this software you must purchase a commercial
017: * license from Xoetrope.</p>
018: * <p> $Revision: 1.10 $</p>
019: */
020: public class Base64EncoderService extends ServiceProxy {
021:
022: public static final String ARG_NAME_ENCODEPARAM = "base64encode:encodeparam";
023: public static final String ARG_NAME_OPERATION = "base64encode:operation";
024: public static final String ARG_VALUE_SAVE = "base64encode:save";
025: public static final String ARG_VALUE_OPEN = "base64encode:open";
026:
027: public Base64EncoderService() {
028: status = OK;
029: }
030:
031: /*public Object call() throws ServiceProxyException
032: {
033: try {
034: return new String( org.apache.commons.codec.binary.Base64.decodeBase64( nextProxy.call().toString().getBytes() ) );
035: }
036: catch ( ServiceProxyException e ) {
037: throw ( e );
038: }
039: }*/
040:
041: /**
042: * Base64 encodes the data before forwarding it to the next proxy.
043: * Services should place data
044: * they wish to return in the ServiceContext. The RETURN_PARAM is the default
045: * location for a single return value.
046: * @return the status of the service call
047: * @param context The ServiceContext contain pass and return parameters
048: * @param method the name of the service being called
049: * @throws net.xoetrope.optional.service.ServiceProxyException Throw an exception if there is a problem with the call
050: */
051: public Object call(String method, ServiceContext context)
052: throws ServiceProxyException {
053: try {
054: boolean client = (side == XRouteManager.CLIENT_SIDE);
055: ServiceProxyArgs args = context.getArgs();
056: String operation = (String) args
057: .getPassParam(ARG_NAME_OPERATION);
058: status = STARTED;
059:
060: if (client) {
061: if (operation == null) {
062: Hashtable passArgs = args.getPassArgs();
063: Enumeration passKeys = passArgs.keys();
064: while (passKeys.hasMoreElements()) {
065: String key = (String) passKeys.nextElement();
066: String data = (String) passArgs.get(key);
067: args.setPassParam(key, new String(
068: org.apache.catalina.util.Base64
069: .encode(data.getBytes())));
070: }
071: callNextProxy(method, context, null);
072: String resValue = (String) args
073: .getReturnParam(ARG_NAME_ENCODEPARAM);
074: args.setReturnParam(ARG_NAME_ENCODEPARAM,
075: new String(org.apache.catalina.util.Base64
076: .decode(resValue.getBytes()))
077: .trim());
078: } else {
079: String encodeParamName = (String) args
080: .getPassParam(ARG_NAME_ENCODEPARAM);
081: if (operation.equals(ARG_VALUE_SAVE)) {
082: String value = (String) args
083: .getPassParam(encodeParamName);
084: args.setPassParam(encodeParamName,
085: new String(
086: org.apache.catalina.util.Base64
087: .encode(value.trim()
088: .getBytes())));
089: callNextProxy(method, context, null);
090: } else {
091: callNextProxy(method, context, null);
092: String value = (String) args
093: .getReturnParam(encodeParamName);
094: args.setReturnParam(encodeParamName,
095: new String(
096: org.apache.catalina.util.Base64
097: .decode(value
098: .getBytes()))
099: .trim());
100: }
101: }
102: } else {
103: if (operation == null) {
104: Hashtable passArgs = args.getPassArgs();
105: Enumeration passKeys = passArgs.keys();
106: while (passKeys.hasMoreElements()) {
107: String key = (String) passKeys.nextElement();
108: String data = (String) passArgs.get(key);
109: args.setPassParam(key, new String(
110: org.apache.catalina.util.Base64
111: .decode(data.getBytes())));
112: }
113: callNextProxy(method, context, null);
114: String resValue = (String) args
115: .getReturnParam(ARG_NAME_ENCODEPARAM);
116: args.setReturnParam(ARG_NAME_ENCODEPARAM,
117: new String(org.apache.catalina.util.Base64
118: .encode(resValue.getBytes()))
119: .trim());
120: } else {
121: String encodeParamName = (String) args
122: .getPassParam(ARG_NAME_ENCODEPARAM);
123: if (operation.equals(ARG_VALUE_OPEN)) {
124: callNextProxy(method, context, null);
125: String value = (String) args
126: .getReturnParam(encodeParamName);
127: args.setReturnParam(encodeParamName,
128: new String(
129: org.apache.catalina.util.Base64
130: .encode(value
131: .getBytes())));
132: } else {
133: String value = (String) args
134: .getPassParam(encodeParamName);
135: args.setPassParam(encodeParamName, new String(
136: org.apache.catalina.util.Base64
137: .decode(value.getBytes()))
138: .trim());
139: callNextProxy(method, context, null);
140: }
141: }
142: }
143: status = COMPLETE;
144: } catch (ServiceProxyException ex) {
145: status = FAILED;
146: throw (ex);
147: }
148:
149: return null;
150: }
151: }
|