ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/catalina/util/CustomObjectInputStream.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/catalina/util/CustomObjectInputStream.java
Status
scanned
Type
file
Name
CustomObjectInputStream.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
7617 (7.4 KB)
MD5
d7766cc19b475b4516d21739f7568095
SHA1
76e4f07ca2bba7d95c0cf88e930028eefb6d43b8
SHA256
6cf7ebd59eeb054f13ef008497cb0987780ac33b74d35a580d57ab8abfd16c91
SHA512

      
    
SHA1_git
b18e58a71256f0465590e41496f02967ca828e5c
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
CustomObjectInputStream.java | 7.4 KB |

/* * 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.catalina.util; import java.io.IOException; import java.io.InputStream; import java.io.InvalidClassException; import java.io.ObjectInputStream; import java.io.ObjectStreamClass; import java.lang.reflect.Proxy; import java.util.Set; import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; import org.apache.juli.logging.Log; import org.apache.tomcat.util.res.StringManager; /** * Custom subclass of <code>ObjectInputStream</code> that loads from the class loader for this web application. This * allows classes defined only with the web application to be found correctly. */ public final class CustomObjectInputStream extends ObjectInputStream { private static final StringManager sm = StringManager.getManager(CustomObjectInputStream.class); private static final WeakHashMap<ClassLoader,Set<String>> reportedClassCache = new WeakHashMap<>(); /** * The class loader we will use to resolve classes. */ private final ClassLoader classLoader; private final Set<String> reportedClasses; private final Log log; private final Pattern allowedClassNamePattern; private final String allowedClassNameFilter; private final boolean warnOnFailure; /** * Construct a new instance of CustomObjectInputStream without any filtering of deserialized classes. * * @param stream The input stream we will read from * @param classLoader The class loader used to instantiate objects * * @exception IOException if an input/output error occurs */ public CustomObjectInputStream(InputStream stream, ClassLoader classLoader) throws IOException { this(stream, classLoader, null, null, false); } /** * Construct a new instance of CustomObjectInputStream with filtering of deserialized classes. * * @param stream The input stream we will read from * @param classLoader The class loader used to instantiate objects * @param log The logger to use to report any issues. It may only be null if the filterMode does * not require logging * @param allowedClassNamePattern The regular expression to use to filter deserialized classes. The fully qualified * class name must match this pattern for deserialization to be allowed if * filtering is enabled. * @param warnOnFailure Should any failures be logged? * * @exception IOException if an input/output error occurs */ public CustomObjectInputStream(InputStream stream, ClassLoader classLoader, Log log, Pattern allowedClassNamePattern, boolean warnOnFailure) throws IOException { super(stream); if (log == null && allowedClassNamePattern != null && warnOnFailure) { throw new IllegalArgumentException(sm.getString("customObjectInputStream.logRequired")); } this.classLoader = classLoader; this.log = log; this.allowedClassNamePattern = allowedClassNamePattern; if (allowedClassNamePattern == null) { this.allowedClassNameFilter = null; } else { this.allowedClassNameFilter = allowedClassNamePattern.toString(); } this.warnOnFailure = warnOnFailure; Set<String> reportedClasses; synchronized (reportedClassCache) { reportedClasses = reportedClassCache.get(classLoader); } if (reportedClasses == null) { reportedClasses = ConcurrentHashMap.newKeySet(); Set<String> original; synchronized (reportedClassCache) { original = reportedClassCache.putIfAbsent(classLoader, reportedClasses); } if (original != null) { // Concurrent attempts to create the new Set. Make sure all // threads use the first successfully added Set. reportedClasses = original; } } this.reportedClasses = reportedClasses; } /** * Load the local class equivalent of the specified stream class description, by using the class loader assigned to * this Context. * * @param classDesc Class description from the input stream * * @exception ClassNotFoundException if this class cannot be found * @exception IOException if an input/output error occurs */ @Override public Class<?> resolveClass(ObjectStreamClass classDesc) throws ClassNotFoundException, IOException { String name = classDesc.getName(); if (allowedClassNamePattern != null) { boolean allowed = allowedClassNamePattern.matcher(name).matches(); if (!allowed) { boolean doLog = warnOnFailure && reportedClasses.add(name); String msg = sm.getString("customObjectInputStream.nomatch", name, allowedClassNameFilter); if (doLog) { log.warn(msg); } else if (log.isDebugEnabled()) { log.debug(msg); } throw new InvalidClassException(msg); } } try { return Class.forName(name, false, classLoader); } catch (ClassNotFoundException e) { try { // Try also the superclass because of primitive types return super.resolveClass(classDesc); } catch (ClassNotFoundException e2) { // Rethrow original exception, as it can have more information // about why the class was not found. BZ 48007 throw e; } } } /** * Return a proxy class that implements the interfaces named in a proxy class descriptor. Do this using the class * loader assigned to this Context. */ @Override protected Class<?> resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException { Class<?>[] cinterfaces = new Class[interfaces.length]; for (int i = 0; i < interfaces.length; i++) { cinterfaces[i] = classLoader.loadClass(interfaces[i]); } try { // No way to avoid this at the moment @SuppressWarnings("deprecation") Class<?> proxyClass = Proxy.getProxyClass(classLoader, cinterfaces); return proxyClass; } catch (IllegalArgumentException e) { throw new ClassNotFoundException(null, e); } } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
16.64
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9