ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/catalina/valves/rewrite/RandomizedTextRewriteMap.java

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

      
    
Rootfs path

      
    
Size
4077 (4.0 KB)
MD5
bc358ce0437785b5b2505a9e63483951
SHA1
9c9566461fda0d0f7131a2d5c4fee3c360d238b6
SHA256
d0f267392c16fdc88b9a62081f147be895cc2d67883f314a3ecd792acd1b4b27
SHA512

      
    
SHA1_git
a2ed97335df4daec4c164313d909f99e7b33119f
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
RandomizedTextRewriteMap.java | 4.0 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.valves.rewrite; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.Random; import org.apache.tomcat.util.file.ConfigFileLoader; import org.apache.tomcat.util.file.ConfigurationSource.Resource; import org.apache.tomcat.util.res.StringManager; /** * Implement a map for the txt: and rnd: mod_rewrite capabilities. */ public class RandomizedTextRewriteMap implements RewriteMap { protected static final StringManager sm = StringManager.getManager(RandomizedTextRewriteMap.class); private static final Random random = new Random(); private final Map<String,String[]> map = new HashMap<>(); /** * Create a map from a text file according to the mod_rewrite syntax. * * @param txtFilePath the text file path * @param useRandom if the map should produce random results */ public RandomizedTextRewriteMap(String txtFilePath, boolean useRandom) { String line; try (Resource txtResource = ConfigFileLoader.getSource().getResource(txtFilePath); BufferedReader reader = new BufferedReader(new InputStreamReader(txtResource.getInputStream()))) { while ((line = reader.readLine()) != null) { if (line.startsWith("#") || line.isEmpty()) { // Ignore comment line or empty lines continue; } else if (line.indexOf('#') > 0) { // Ignore comment characters after '#' line = line.substring(0, line.indexOf('#')).trim(); } String[] keyValuePair = line.split(" ", 2); if (keyValuePair.length > 1) { String key = keyValuePair[0]; String value = keyValuePair[1]; String[] possibleValues; if (useRandom && value.contains("|")) { possibleValues = value.split("\\|"); } else { possibleValues = new String[1]; possibleValues[0] = value; } map.put(key, possibleValues); } else { throw new IllegalArgumentException(sm.getString("rewriteMap.txtInvalidLine", line, txtFilePath)); } } } catch (IOException ioe) { throw new IllegalArgumentException(sm.getString("rewriteMap.txtReadError", txtFilePath), ioe); } } @Override public String setParameters(String params) { throw new IllegalArgumentException( StringManager.getManager(RewriteMap.class).getString("rewriteMap.tooManyParameters")); } @Override public String lookup(String key) { String[] possibleValues = map.get(key); if (possibleValues != null) { if (possibleValues.length > 1) { return possibleValues[random.nextInt(possibleValues.length)]; } else { return possibleValues[0]; } } return null; } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
30.13
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