|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.jmeter.samplers |
| 19 | + |
| 20 | +import org.apache.jmeter.samplers.decoders.DeflateDecoder |
| 21 | +import org.apache.jmeter.samplers.decoders.GzipDecoder |
| 22 | +import org.apache.jmeter.util.JMeterUtils |
| 23 | +import org.apache.jorphan.reflect.LogAndIgnoreServiceLoadExceptionHandler |
| 24 | +import org.apiguardian.api.API |
| 25 | +import org.slf4j.LoggerFactory |
| 26 | +import java.io.IOException |
| 27 | +import java.io.InputStream |
| 28 | +import java.util.Locale |
| 29 | +import java.util.ServiceLoader |
| 30 | +import java.util.concurrent.ConcurrentHashMap |
| 31 | + |
| 32 | +/** |
| 33 | + * Registry for [ResponseDecoder] implementations. |
| 34 | + * Provides centralized management of response decoders for different content encodings. |
| 35 | + * |
| 36 | + * Decoders are discovered via: |
| 37 | + * - Built-in decoders (gzip, deflate) |
| 38 | + * - ServiceLoader mechanism (META-INF/services) |
| 39 | + * |
| 40 | + * Thread-safe singleton registry. |
| 41 | + * |
| 42 | + * @since 6.0.0 |
| 43 | + */ |
| 44 | +@API(status = API.Status.EXPERIMENTAL, since = "6.0.0") |
| 45 | +public object ResponseDecoderRegistry { |
| 46 | + |
| 47 | + private val log = LoggerFactory.getLogger(ResponseDecoderRegistry::class.java) |
| 48 | + |
| 49 | + /** |
| 50 | + * Map of encoding name (lowercase) to decoder implementation. |
| 51 | + * Uses ConcurrentHashMap for thread-safe access. |
| 52 | + */ |
| 53 | + private val decoders = ConcurrentHashMap<String, ResponseDecoder>() |
| 54 | + |
| 55 | + init { |
| 56 | + // Register built-in decoders, this ensures the decoders are there even if service registration fails |
| 57 | + registerDecoder(GzipDecoder()) |
| 58 | + registerDecoder(DeflateDecoder()) |
| 59 | + |
| 60 | + // Load decoders via ServiceLoader |
| 61 | + loadServiceLoaderDecoders() |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Loads decoders using ServiceLoader mechanism. |
| 66 | + */ |
| 67 | + private fun loadServiceLoaderDecoders() { |
| 68 | + try { |
| 69 | + JMeterUtils.loadServicesAndScanJars( |
| 70 | + ResponseDecoder::class.java, |
| 71 | + ServiceLoader.load(ResponseDecoder::class.java), |
| 72 | + Thread.currentThread().contextClassLoader, |
| 73 | + LogAndIgnoreServiceLoadExceptionHandler(log) |
| 74 | + ).forEach { registerDecoder(it) } |
| 75 | + } catch (e: Exception) { |
| 76 | + log.error("Error loading ResponseDecoder services", e) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Registers a decoder for all its encoding types. |
| 82 | + * If a decoder already exists for an encoding, the one with higher priority is kept. |
| 83 | + * |
| 84 | + * @param decoder the decoder to register |
| 85 | + */ |
| 86 | + @JvmStatic |
| 87 | + public fun registerDecoder(decoder: ResponseDecoder) { |
| 88 | + val encodings = decoder.encodings |
| 89 | + if (encodings.isEmpty()) { |
| 90 | + log.warn("Decoder {} has null or empty encodings list, skipping registration", decoder.javaClass.name) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + for (encoding in encodings) { |
| 95 | + val key = encoding.lowercase(Locale.ROOT) |
| 96 | + |
| 97 | + decoders.merge(key, decoder) { existing, newDecoder -> |
| 98 | + // Keep the decoder with higher priority |
| 99 | + if (newDecoder.priority > existing.priority) { |
| 100 | + log.info( |
| 101 | + "Replacing decoder for '{}': {} (priority {}) -> {} (priority {})", |
| 102 | + encoding, |
| 103 | + existing.javaClass.simpleName, existing.priority, |
| 104 | + newDecoder.javaClass.simpleName, newDecoder.priority |
| 105 | + ) |
| 106 | + newDecoder |
| 107 | + } else { |
| 108 | + log.debug( |
| 109 | + "Keeping existing decoder for '{}': {} (priority {}) over {} (priority {})", |
| 110 | + encoding, |
| 111 | + existing.javaClass.simpleName, existing.priority, |
| 112 | + newDecoder.javaClass.simpleName, newDecoder.priority |
| 113 | + ) |
| 114 | + existing |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Decodes the given data using the decoder registered for the specified encoding. |
| 122 | + * If no decoder is found for the encoding, returns the data unchanged. |
| 123 | + * |
| 124 | + * @param encoding the content encoding (e.g., "gzip", "deflate", "br") |
| 125 | + * @param data the data to decode |
| 126 | + * @return decoded data, or original data if no decoder found or encoding is null |
| 127 | + * @throws IOException if decoding fails |
| 128 | + */ |
| 129 | + @JvmStatic |
| 130 | + @Throws(IOException::class) |
| 131 | + public fun decode(encoding: String?, data: ByteArray?): ByteArray { |
| 132 | + if (encoding.isNullOrEmpty() || data == null || data.isEmpty()) { |
| 133 | + return data ?: ByteArray(0) |
| 134 | + } |
| 135 | + |
| 136 | + val decoder = decoders[encoding] ?: decoders[encoding.lowercase(Locale.ROOT)] |
| 137 | + |
| 138 | + if (decoder == null) { |
| 139 | + log.debug("No decoder found for encoding '{}', returning data unchanged", encoding) |
| 140 | + return data |
| 141 | + } |
| 142 | + |
| 143 | + return decoder.decode(data) |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Creates a decompressing InputStream that wraps the given input stream using the decoder |
| 148 | + * registered for the specified encoding. |
| 149 | + * |
| 150 | + * This enables streaming decompression without buffering the entire response in memory, |
| 151 | + * which is useful for computing checksums on decompressed data or processing large responses. |
| 152 | + * |
| 153 | + * If no decoder is found for the encoding, returns the original input stream unchanged. |
| 154 | + * |
| 155 | + * @param encoding the content encoding (e.g., "gzip", "deflate", "br") |
| 156 | + * @param input the input stream to wrap with decompression |
| 157 | + * @return a decompressing InputStream, or the original stream if no decoder found or encoding is null |
| 158 | + * @throws IOException if the decompressing stream cannot be created |
| 159 | + * @since 6.0.0 |
| 160 | + */ |
| 161 | + @JvmStatic |
| 162 | + @Throws(IOException::class) |
| 163 | + public fun decodeStream(encoding: String?, input: InputStream): InputStream { |
| 164 | + if (encoding.isNullOrEmpty()) { |
| 165 | + return input |
| 166 | + } |
| 167 | + |
| 168 | + val decoder = decoders[encoding] ?: decoders[encoding.lowercase(Locale.ROOT)] |
| 169 | + |
| 170 | + if (decoder == null) { |
| 171 | + log.debug("No decoder found for encoding '{}', returning input stream unchanged", encoding) |
| 172 | + return input |
| 173 | + } |
| 174 | + |
| 175 | + return decoder.decodeStream(input) |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Checks if a decoder is registered for the given encoding. |
| 180 | + * Primarily for testing purposes. |
| 181 | + * |
| 182 | + * @param encoding the encoding to check |
| 183 | + * @return true if a decoder is registered for this encoding |
| 184 | + */ |
| 185 | + @JvmStatic |
| 186 | + public fun hasDecoder(encoding: String): Boolean = |
| 187 | + decoders.containsKey(encoding.lowercase(Locale.ROOT)) |
| 188 | +} |
0 commit comments