blob: 745e766b6a892016e41eadb9ad8974338bf3406a [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>ObjectUtils.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Turbine</a> &gt; <a href="index.source.html" class="el_package">org.apache.turbine.util</a> &gt; <span class="el_source">ObjectUtils.java</span></div><h1>ObjectUtils.java</h1><pre class="source lang-java linenums">package org.apache.turbine.util;
/*
* 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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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.
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputFilter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.turbine.Turbine;
import org.apache.turbine.TurbineConstants;
/**
* This is where common Object manipulation routines should go.
*
* @author &lt;a href=&quot;mailto:nissim@nksystems.com&quot;&gt;Nissim Karpenstein&lt;/a&gt;
* @author &lt;a href=&quot;mailto:hps@intermeta.de&quot;&gt;Henning P. Schmiedehausen&lt;/a&gt;
* @version $Id$
*/
<span class="nc" id="L45">public abstract class ObjectUtils</span>
{
<span class="nc" id="L47"> private static final Logger log = LogManager.getLogger(ObjectUtils.class);</span>
/**
* Converts a map to a byte array for storage/serialization.
*
* @param map The Map to convert.
*
* @return A byte[] with the converted Map.
*
* @throws Exception A generic exception.
*/
public static byte[] serializeMap(Map&lt;String, Object&gt; map)
throws Exception
{
<span class="nc" id="L61"> byte[] byteArray = null;</span>
<span class="nc" id="L62"> Map&lt;String, Object&gt; mapCopy = new HashMap&lt;&gt;(map);</span>
// Remove all entries that are not serializable
<span class="nc bnc" id="L65" title="All 2 branches missed."> for (Iterator&lt;Map.Entry&lt;String, Object&gt;&gt; i = mapCopy.entrySet().iterator(); i.hasNext();)</span>
{
<span class="nc" id="L67"> Map.Entry&lt;String, Object&gt; entry = i.next();</span>
<span class="nc bnc" id="L68" title="All 2 branches missed."> if (! (entry.getValue() instanceof Serializable))</span>
{
<span class="nc" id="L70"> i.remove();</span>
<span class="nc" id="L71"> log.warn(&quot;Skipping serialization, value is not serializable: &quot; + entry.getValue());</span>
}
<span class="nc" id="L73"> }</span>
<span class="nc" id="L75"> try (ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);</span>
<span class="nc" id="L76"> ObjectOutputStream out = new ObjectOutputStream(baos))</span>
{
<span class="nc" id="L78"> out.writeObject(mapCopy);</span>
<span class="nc" id="L79"> out.flush();</span>
<span class="nc" id="L81"> byteArray = baos.toByteArray();</span>
}
<span class="nc" id="L84"> return byteArray;</span>
}
/**
* Deserializes a single object from an array of bytes.
*
* @param &lt;T&gt; type of the object to return
* @param objectData The serialized object.
*
* @return The deserialized object, or &lt;code&gt;null&lt;/code&gt; on failure.
*/
@SuppressWarnings(&quot;unchecked&quot;)
public static &lt;T&gt; T deserialize(byte[] objectData)
{
<span class="nc" id="L98"> T object = null;</span>
<span class="nc bnc" id="L100" title="All 2 branches missed."> if (objectData != null)</span>
{
<span class="nc" id="L102"> final String filterPattern = Turbine.getConfiguration().getString(TurbineConstants.SESSION_OBJECTINPUTFILTER);</span>
<span class="nc" id="L104"> try (ByteArrayInputStream bin = new ByteArrayInputStream(objectData);</span>
<span class="nc" id="L105"> ObjectInputStream in = new ObjectInputStream(bin))</span>
{
// Set filter to limit what can be deserialized if so configured
<span class="nc bnc" id="L108" title="All 2 branches missed."> if (StringUtils.isNotEmpty(filterPattern))</span>
{
<span class="nc" id="L110"> in.setObjectInputFilter(ObjectInputFilter.Config.createFilter(filterPattern));</span>
}
// If objectData has not been initialized, an
// exception will occur.
<span class="nc" id="L115"> object = (T)in.readObject();</span>
}
<span class="nc" id="L117"> catch (Exception e)</span>
{
<span class="nc" id="L119"> log.warn(&quot;Problem deserializing object.&quot;, e);</span>
<span class="nc" id="L120"> }</span>
}
<span class="nc" id="L123"> return object;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html>