You are viewing a plain text version of this content. The canonical link for it is here.
Posted to wsrp4j-dev@portals.apache.org by dl...@apache.org on 2005/08/29 21:11:50 UTC

cvs commit: ws-wsrp4j/sandbox/wsrp4j/commons-producer/src/java/org/apache/wsrp4j/commons/producer/util Base64.java MapDeserializer.java MapSerializer.java ServletAccess.java

dlouzan     2005/08/29 12:11:50

  Added:       sandbox/wsrp4j/commons-producer/src/java/org/apache/wsrp4j/commons/producer/util
                        Base64.java MapDeserializer.java MapSerializer.java
                        ServletAccess.java
  Log:
  Initial commit.
  
  Revision  Changes    Path
  1.1                  ws-wsrp4j/sandbox/wsrp4j/commons-producer/src/java/org/apache/wsrp4j/commons/producer/util/Base64.java
  
  Index: Base64.java
  ===================================================================
  /*
   * Copyright 2003-2005 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.wsrp4j.commons.producer.util;
  
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.Writer;
  
  import org.apache.axis.utils.Messages;
  
  
  /**
   * This is taken from the axis source tree.
   * Just adapted the base64 alphaphet for use in url's.
   *
   *
   * @author TAMURA Kent <kent@trl.ibm.co.jp>
   * @version $Id: Base64.java,v 1.1 2005/08/29 19:11:50 dlouzan Exp $
   */
  public class Base64 {
      private static final char[] S_BASE64CHAR =
      {
          'A','B','C','D','E','F','G','H',
                  'I','J','K','L','M','N','O','P',
                  'Q','R','S','T','U','V','W','X',
                  'Y','Z','a','b','c','d','e','f',
                  'g','h','i','j','k','l','m','n',
                  'o','p','q','r','s','t','u','v',
                  'w','x','y','z','0','1','2','3',
                  '4','5','6','7','8','9','-','_'
      };
      
      private static final char S_BASE64PAD = '*';
      private static final byte[] S_DECODETABLE = new byte[128];
      static {
          for (int i = 0; i < S_DECODETABLE.length; i++)
              S_DECODETABLE[i] = Byte.MAX_VALUE; // 127
          for (int i = 0; i < S_BASE64CHAR.length; i++) // 0 to 63
              S_DECODETABLE[S_BASE64CHAR[i]] = (byte)i;
      }
      
      private static int decode0(char[] ibuf, byte[] obuf, int wp) {
          int outlen = 3;
          if (ibuf[3] == S_BASE64PAD)
              outlen = 2;
          if (ibuf[2] == S_BASE64PAD)
              outlen = 1;
          int b0 = S_DECODETABLE[ibuf[0]];
          int b1 = S_DECODETABLE[ibuf[1]];
          int b2 = S_DECODETABLE[ibuf[2]];
          int b3 = S_DECODETABLE[ibuf[3]];
          switch (outlen) {
              case 1 :
                  obuf[wp] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
                  return 1;
              case 2 :
                  obuf[wp++] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
                  obuf[wp] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
                  return 2;
              case 3 :
                  obuf[wp++] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
                  obuf[wp++] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
                  obuf[wp] = (byte) (b2 << 6 & 0xc0 | b3 & 0x3f);
                  return 3;
              default :
                  throw new RuntimeException(
                          Messages.getMessage("internalError00"));
          }
      }
      
      /**
       *
       */
      public static byte[] decode(char[] data, int off, int len) {
          char[] ibuf = new char[4];
          int ibufcount = 0;
          byte[] obuf = new byte[len / 4 * 3 + 3];
          int obufcount = 0;
          for (int i = off; i < off + len; i++) {
              char ch = data[i];
              if (ch == S_BASE64PAD || ch < S_DECODETABLE.length &&
                      S_DECODETABLE[ch] != Byte.MAX_VALUE) {
                  ibuf[ibufcount++] = ch;
                  if (ibufcount == ibuf.length) {
                      ibufcount = 0;
                      obufcount += decode0(ibuf, obuf, obufcount);
                  }
              }
          }
          if (obufcount == obuf.length)
              return obuf;
          byte[] ret = new byte[obufcount];
          System.arraycopy(obuf, 0, ret, 0, obufcount);
          return ret;
      }
      
      /**
       *
       */
      public static byte[] decode(String data) {
          char[] ibuf = new char[4];
          int ibufcount = 0;
          byte[] obuf = new byte[data.length() / 4 * 3 + 3];
          int obufcount = 0;
          for (int i = 0; i < data.length(); i++) {
              char ch = data.charAt(i);
              if (ch == S_BASE64PAD || ch < S_DECODETABLE.length &&
                      S_DECODETABLE[ch] != Byte.MAX_VALUE) {
                  ibuf[ibufcount++] = ch;
                  if (ibufcount == ibuf.length) {
                      ibufcount = 0;
                      obufcount += decode0(ibuf, obuf, obufcount);
                  }
              }
          }
          if (obufcount == obuf.length)
              return obuf;
          byte[] ret = new byte[obufcount];
          System.arraycopy(obuf, 0, ret, 0, obufcount);
          return ret;
      }
      
      /**
       *
       */
      public static void decode(char[] data, int off, int len,
              OutputStream ostream)
              throws IOException {
          char[] ibuf = new char[4];
          int ibufcount = 0;
          byte[] obuf = new byte[3];
          for (int i = off; i < off + len; i++) {
              char ch = data[i];
              if (ch == S_BASE64PAD || ch < S_DECODETABLE.length && 
                      S_DECODETABLE[ch] != Byte.MAX_VALUE) {
                  ibuf[ibufcount++] = ch;
                  if (ibufcount == ibuf.length) {
                      ibufcount = 0;
                      int obufcount = decode0(ibuf, obuf, 0);
                      ostream.write(obuf, 0, obufcount);
                  }
              }
          }
      }
      
      /**
       *
       */
      public static void decode(String data, OutputStream ostream) 
      throws IOException {
          char[] ibuf = new char[4];
          int ibufcount = 0;
          byte[] obuf = new byte[3];
          for (int i = 0; i < data.length(); i++) {
              char ch = data.charAt(i);
              if (ch == S_BASE64PAD || ch < S_DECODETABLE.length && 
                      S_DECODETABLE[ch] != Byte.MAX_VALUE) {
                  ibuf[ibufcount++] = ch;
                  if (ibufcount == ibuf.length) {
                      ibufcount = 0;
                      int obufcount = decode0(ibuf, obuf, 0);
                      ostream.write(obuf, 0, obufcount);
                  }
              }
          }
      }
      
      /**
       * Returns base64 representation of specified byte array.
       */
      public static String encode(byte[] data) {
          return encode(data, 0, data.length);
      }
      
      /**
       * Returns base64 representation of specified byte array.
       */
      public static String encode(byte[] data, int off, int len) {
          if (len <= 0)
              return "";
          char[] out = new char[len / 3 * 4 + 4];
          int rindex = off;
          int windex = 0;
          int rest = len - off;
          while (rest >= 3) {
              int i = ((data[rindex] & 0xff) << 16) + 
                      ((data[rindex + 1] & 0xff) << 8) + 
                      (data[rindex + 2] & 0xff);
              out[windex++] = S_BASE64CHAR[i >> 18];
              out[windex++] = S_BASE64CHAR[(i >> 12) & 0x3f];
              out[windex++] = S_BASE64CHAR[(i >> 6) & 0x3f];
              out[windex++] = S_BASE64CHAR[i & 0x3f];
              rindex += 3;
              rest -= 3;
          }
          if (rest == 1) {
              int i = data[rindex] & 0xff;
              out[windex++] = S_BASE64CHAR[i >> 2];
              out[windex++] = S_BASE64CHAR[(i << 4) & 0x3f];
              out[windex++] = S_BASE64PAD;
              out[windex++] = S_BASE64PAD;
          } else if (rest == 2) {
              int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
              out[windex++] = S_BASE64CHAR[i >> 10];
              out[windex++] = S_BASE64CHAR[(i >> 4) & 0x3f];
              out[windex++] = S_BASE64CHAR[(i << 2) & 0x3f];
              out[windex++] = S_BASE64PAD;
          }
          return new String(out, 0, windex);
      }
      
      /**
       * Outputs base64 representation of the specified byte array to a byte 
       * stream.
       */
      public static void encode(byte[] data, int off, int len, 
              OutputStream ostream)
      throws IOException {
          if (len <= 0)
              return;
          byte[] out = new byte[4];
          int rindex = off;
          int rest = len - off;
          while (rest >= 3) {
              int i = ((data[rindex] & 0xff) << 16) + 
                      ((data[rindex + 1] & 0xff) << 8) + 
                      (data[rindex + 2] & 0xff);
              out[0] = (byte)S_BASE64CHAR[i >> 18];
              out[1] = (byte)S_BASE64CHAR[(i >> 12) & 0x3f];
              out[2] = (byte)S_BASE64CHAR[(i >> 6) & 0x3f];
              out[3] = (byte)S_BASE64CHAR[i & 0x3f];
              ostream.write(out, 0, 4);
              rindex += 3;
              rest -= 3;
          }
          if (rest == 1) {
              int i = data[rindex] & 0xff;
              out[0] = (byte)S_BASE64CHAR[i >> 2];
              out[1] = (byte)S_BASE64CHAR[(i << 4) & 0x3f];
              out[2] = (byte)S_BASE64PAD;
              out[3] = (byte)S_BASE64PAD;
              ostream.write(out, 0, 4);
          } else if (rest == 2) {
              int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
              out[0] = (byte)S_BASE64CHAR[i >> 10];
              out[1] = (byte)S_BASE64CHAR[(i >> 4) & 0x3f];
              out[2] = (byte)S_BASE64CHAR[(i << 2) & 0x3f];
              out[3] = (byte)S_BASE64PAD;
              ostream.write(out, 0, 4);
          }
      }
      
      /**
       * Outputs base64 representation of the specified byte array to a character 
       * stream.
       */
      public static void encode(byte[] data, int off, int len, Writer writer) 
      throws IOException {
          if (len <= 0)
              return;
          char[] out = new char[4];
          int rindex = off;
          int rest = len - off;
          int output = 0;
          while (rest >= 3) {
              int i = ((data[rindex] & 0xff) << 16) + 
                      ((data[rindex + 1] & 0xff) << 8) + 
                      (data[rindex + 2] & 0xff);
              out[0] = S_BASE64CHAR[i >> 18];
              out[1] = S_BASE64CHAR[(i >> 12) & 0x3f];
              out[2] = S_BASE64CHAR[(i >> 6) & 0x3f];
              out[3] = S_BASE64CHAR[i & 0x3f];
              writer.write(out, 0, 4);
              rindex += 3;
              rest -= 3;
              output += 4;
              if (output % 76 == 0)
                  writer.write("\n");
          }
          if (rest == 1) {
              int i = data[rindex] & 0xff;
              out[0] = S_BASE64CHAR[i >> 2];
              out[1] = S_BASE64CHAR[(i << 4) & 0x3f];
              out[2] = S_BASE64PAD;
              out[3] = S_BASE64PAD;
              writer.write(out, 0, 4);
          } else if (rest == 2) {
              int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
              out[0] = S_BASE64CHAR[i >> 10];
              out[1] = S_BASE64CHAR[(i >> 4) & 0x3f];
              out[2] = S_BASE64CHAR[(i << 2) & 0x3f];
              out[3] = S_BASE64PAD;
              writer.write(out, 0, 4);
          }
      }
  }
  
  
  1.1                  ws-wsrp4j/sandbox/wsrp4j/commons-producer/src/java/org/apache/wsrp4j/commons/producer/util/MapDeserializer.java
  
  Index: MapDeserializer.java
  ===================================================================
  /*
   * Copyright 2003-2005 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.wsrp4j.commons.producer.util;
  
  import java.io.ByteArrayInputStream;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.util.Map;
  
  import org.apache.wsrp4j.commons.log.LogManager;
  import org.apache.wsrp4j.commons.log.Logger;
  
  
  /**
   *
   * @author Stephan.Laertz@de.ibm.com
   *
   * Helper to deserialize the parameter map of the portlet url
   * @version $Id: MapDeserializer.java,v 1.1 2005/08/29 19:11:50 dlouzan Exp $
   **/
  public class MapDeserializer {
      
      //     for logging and exception support
      private static Logger logger = 
              LogManager.getLogManager().getLogger(MapDeserializer.class);
      
      public static Map deserialize(byte[] map) 
      throws IOException, ClassNotFoundException {
          
          String MN = "deserialize(String)";
          if (logger.isLogging(Logger.TRACE_HIGH)) {
              logger.entry(Logger.TRACE_HIGH, MN, map);
          }
          
          Map result = null;
          
          ByteArrayInputStream bytes = new ByteArrayInputStream(map);
          ObjectInputStream in = new ObjectInputStream(bytes);
          Object obj = in.readObject();
          in.close();
          
          if (obj instanceof Map) {
              
              result = (Map)obj;
              
          } else {
              
              logger.text(Logger.ERROR, MN, 
                      "Deserialized object is not a map.", obj);
          }
          
          if (logger.isLogging(Logger.TRACE_HIGH)) {
              logger.exit(Logger.TRACE_HIGH, MN, result);
          }
          
          return result;
      }
  }
  
  
  
  1.1                  ws-wsrp4j/sandbox/wsrp4j/commons-producer/src/java/org/apache/wsrp4j/commons/producer/util/MapSerializer.java
  
  Index: MapSerializer.java
  ===================================================================
  /*
   * Copyright 2003-2005 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.wsrp4j.commons.producer.util;
  
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.ObjectOutputStream;
  import java.io.Serializable;
  import java.util.Map;
  
  import org.apache.wsrp4j.commons.log.LogManager;
  import org.apache.wsrp4j.commons.log.Logger;
  
  
  /**
   * @author Stephan.Laertz@de.ibm.com
   *
   * Helper to serialize the parameter map of the portlet url
   * @version $Id: MapSerializer.java,v 1.1 2005/08/29 19:11:50 dlouzan Exp $
   **/
  public class MapSerializer {
      
      // for logging and exception support
      private static Logger logger = 
              LogManager.getLogManager().getLogger(MapSerializer.class);
      
      public static byte[] serialize(Map parameters) throws IOException {
          
          String MN = "serialize(Map)";
          if (logger.isLogging(Logger.TRACE_HIGH)) {
              logger.entry(Logger.TRACE_HIGH, MN, parameters);
          }
          
          byte[] result = null;
          
          if (parameters instanceof Serializable) {
              
              ByteArrayOutputStream bytes = new ByteArrayOutputStream();
              ObjectOutputStream out = new ObjectOutputStream(bytes);
              out.writeObject(parameters);
              out.flush();
              result = bytes.toByteArray();
              out.close();
              
          } else {
              
              if (logger.isLogging(Logger.WARN)) {
                  logger.text(Logger.WARN, MN, 
                          "Unable to serialize parameter map.");
              }
          }
          
          if (logger.isLogging(Logger.TRACE_HIGH)) {
              logger.exit(Logger.TRACE_HIGH, MN, result);
          }
          
          return result;
      }
  }
  
  
  
  1.1                  ws-wsrp4j/sandbox/wsrp4j/commons-producer/src/java/org/apache/wsrp4j/commons/producer/util/ServletAccess.java
  
  Index: ServletAccess.java
  ===================================================================
  /*
   * Copyright 2003-2005 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.wsrp4j.commons.producer.util;
  
  import javax.servlet.ServletContext;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  
  import org.apache.axis.AxisEngine;
  import org.apache.axis.MessageContext;
  import org.apache.axis.transport.http.HTTPConstants;
  
  
  /**
   * @version $Id: ServletAccess.java,v 1.1 2005/08/29 19:11:50 dlouzan Exp $
   */
  public class ServletAccess {
      
      public static HttpServlet getServlet() {
          MessageContext msgContext = AxisEngine.getCurrentMessageContext();
          HttpServlet servlet = (HttpServlet) msgContext.getProperty(
                  HTTPConstants.MC_HTTP_SERVLET);
          return servlet;
      }
      
      public static ServletContext getServletContext() {
          ServletContext servletContext = getServlet().getServletContext();
          return servletContext;
      }
      
      public static HttpServletRequest getServletRequest() {
          MessageContext msgContext = AxisEngine.getCurrentMessageContext();
          HttpServletRequest servletRequest = (HttpServletRequest) msgContext.
                  getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
          return servletRequest;
      }
      
  }