/* 
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you 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.wiki.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;

/**
 *  Provides static helper functions for serializing different objects.
 *  
 *  @since 2.8
 */
public final class Serializer
{
    /**
     * Prefix used to indicated that a serialized item was encoded with Base64.
     */
    protected static final String BASE64_PREFIX = "base64 ";

    /**
     *  Prevent instantiation.
     */
    private Serializer()
    {}
    
    /**
     * Deserializes a Base64-encoded String into a HashMap. Both the keys and values
     * must implement {@link java.io.Serializable}.
     * @param rawString the String contents containing the map to be deserialized
     * @return the attributes, parsed into a Map
     * @throws IOException if the contents cannot be parsed for any reason
     */
    @SuppressWarnings("unchecked")
    public static Map<String,? extends Serializable> deserializeFromBase64( String rawString ) throws IOException
    {
        // Decode from Base64-encoded String to byte array
        byte[] decodedBytes = Base64.decodeBase64( rawString.getBytes("UTF-8") );
        
        // Deserialize from the input stream to the Map
        InputStream bytesIn = new ByteArrayInputStream( decodedBytes );
        ObjectInputStream in = new ObjectInputStream( bytesIn );
        HashMap<String,Serializable> attributes;
        try
        {
            attributes = (HashMap<String,Serializable>)in.readObject();
        }
        catch ( ClassNotFoundException e )
        {
            throw new IOException( "Could not deserialiaze user profile attributes. Reason: " + e.getMessage() );
        }
        finally
        {
            in.close();
        }
        return attributes;
    }

    /**
     * Serializes a Map and formats it into a Base64-encoded String. For ease of serialization, the Map contents
     * are first copied into a HashMap, then serialized into a byte array that is encoded as a Base64 String.
     * @param map the Map to serialize
     * @return a String representing the serialized form of the Map
     * @throws IOException If serialization cannot be done
     */
    public static String serializeToBase64( Map<String,Serializable> map ) throws IOException
    {
        // Load the Map contents into a defensive HashMap
        HashMap<String,Serializable> serialMap = new HashMap<String,Serializable>();
        serialMap.putAll( map );
        
        // Serialize the Map to an output stream
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream( bytesOut );
        out.writeObject( serialMap );
        out.close();
        
        // Transform to Base64-encoded String
        byte[] result = Base64.encodeBase64( bytesOut.toByteArray() );
        return new String( result ) ;
    }

}

@@ -1091,20 +1091,36 @@
 ava.
-util.HashMap
+nio.charset.StandardCharsets
 ;%0Aim
@@ -1138,21 +1138,23 @@
 til.
-Map;%0A
+Base64;
 %0Aimport 
 org.
@@ -1153,47 +1153,49 @@
 ort 
-org.apache.commons.codec.binary.Base64;
+java.util.HashMap;%0Aimport java.util.Map;%0A
 %0A%0A/*
@@ -1982,23 +1982,25 @@
 tic Map%3C
+ 
 String,
+ 
 ? extend
@@ -2013,16 +2013,17 @@
 alizable
+ 
 %3E deseri
@@ -2068,36 +2068,32 @@
 rows IOException
-%0A   
  %7B%0A        // De
@@ -2179,20 +2179,27 @@
 e64.
-d
+getD
 ecode
-Base64
+r().decode
 ( ra
@@ -2219,15 +2219,32 @@
 tes(
-%22UTF-8%22
+ StandardCharsets.UTF_8 
 ) );
@@ -2384,24 +2384,29 @@
 s );%0A       
+ try(
  ObjectInput
@@ -2453,78 +2453,10 @@
 In )
-;%0A        HashMap%3CString,Serializable%3E attributes;%0A        try%0A      
  
+)
  %7B%0A 
@@ -2470,30 +2470,25 @@
     
-attributes =
+return
  (
+ 
 HashMap%3C
 Stri
@@ -2475,39 +2475,41 @@
 eturn ( HashMap%3C
+ 
 String,
+ 
 Serializable%3E)in
@@ -2504,17 +2504,19 @@
 alizable
-%3E
+ %3E 
 )in.read
@@ -2534,24 +2534,16 @@
        %7D
-%0A       
  catch (
@@ -2569,24 +2569,16 @@
 tion e )
-%0A       
  %7B%0A     
@@ -2700,95 +2700,8 @@
   %7D%0A
-        finally%0A        %7B%0A            in.close();%0A        %7D%0A        return attributes;%0A
     
@@ -3152,31 +3152,33 @@
 Base64( Map%3C
+ 
 String,
+ 
 Serializable
@@ -3177,16 +3177,17 @@
 alizable
+ 
 %3E map ) 
@@ -3204,20 +3204,16 @@
 xception
-%0A   
  %7B%0A     
@@ -3273,31 +3273,29 @@
         
-Hash
 Map%3C
+ 
 String,
+ 
 Serializ
@@ -3298,16 +3298,17 @@
 alizable
+ 
 %3E serial
@@ -3329,27 +3329,8 @@
 Map%3C
-String,Serializable
 %3E();
@@ -3709,20 +3709,27 @@
 e64.
+getEncoder().
 encode
-Base64
 ( by

use try with resources