| <?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> > <a href="index.source.html" class="el_package">org.apache.turbine.util</a> > <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 |
| * "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. |
| */ |
| |
| 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 <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a> |
| * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> |
| * @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<String, Object> map) |
| throws Exception |
| { |
| <span class="nc" id="L61"> byte[] byteArray = null;</span> |
| <span class="nc" id="L62"> Map<String, Object> mapCopy = new HashMap<>(map);</span> |
| |
| // Remove all entries that are not serializable |
| <span class="nc bnc" id="L65" title="All 2 branches missed."> for (Iterator<Map.Entry<String, Object>> i = mapCopy.entrySet().iterator(); i.hasNext();)</span> |
| { |
| <span class="nc" id="L67"> Map.Entry<String, Object> 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("Skipping serialization, value is not serializable: " + 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 <T> type of the object to return |
| * @param objectData The serialized object. |
| * |
| * @return The deserialized object, or <code>null</code> on failure. |
| */ |
| @SuppressWarnings("unchecked") |
| public static <T> 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("Problem deserializing object.", 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> |