View Javadoc
1   /*
2    * Copyright (C) 2018-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation;
5   
6   
7   import java.util.ArrayList;
8   import java.util.List;
9   import java.util.Objects;
10  
11  
12  /**
13   * General validation utilities.
14   *
15   * @author Kai Hofmann
16   */
17  // @SuppressFBWarnings({"CLI_CONSTANT_LIST_INDEX", "DCN_NULLPOINTER_EXCEPTION", "EXS_EXCEPTION_SOFTENING_RETURN_FALSE"})
18  @SuppressWarnings("java:S1696")
19  public final class ValidationUtils
20   {
21    /* *
22     * Logger.
23     */
24    // private static final Logger LOGGER = LogManager.getLogger(ValidationUtils.class);
25  
26    /**
27     * URI separator.
28     */
29    private static final char URI_SEPARATOR = '/';
30  
31  
32    /**
33     * Private default constructor.
34     */
35    private ValidationUtils()
36     {
37      super();
38     }
39  
40  
41    /**
42     * Sanitize url path.
43     *
44     * @param urlPath URL path
45     * @return Sanitized url path
46     * @throws NullPointerException when urlPath is null
47     */
48    public static String sanitizeUrlPath(final String urlPath)
49     {
50      Objects.requireNonNull(urlPath, "urlPath"); //$NON-NLS-1$
51      final var result = new StringBuilder(urlPath.length() + 1);
52      if (urlPath.isEmpty()) // absolute vs relative ?
53       {
54        result.append(ValidationUtils.URI_SEPARATOR);
55       }
56      else
57       {
58        if (urlPath.charAt(0) != ValidationUtils.URI_SEPARATOR)
59         {
60          result.append(ValidationUtils.URI_SEPARATOR);
61         }
62        // TODO check illegal characters(?) and sanitize them.
63        // /urlpath + ? param=value & param=value
64        // ? & = # % +
65        // https://de.wikipedia.org/wiki/URL-Encoding
66        // reserviert: : / ? # [ ] @ ! $ & ' ( ) * + , ; =
67        // nicht reserviert: A–Z, a–z, 0-9, - . _ ~
68        result.append(urlPath);
69       }
70      return result.toString();
71     }
72  
73  
74    /**
75     * Split hostname and port.
76     *
77     * @param hostnamePort "hostname:port", "ipv4:port", "[ipv6]:port"
78     * @return String List 0: hostname; 1: port
79     * @throws NullPointerException If hostnamePort is null
80     * @throws IllegalArgumentException If it is not a host:port combination
81     */
82    public static List<String> splitHostnamePort(final String hostnamePort)
83     {
84      Objects.requireNonNull(hostnamePort, "hostnamePort"); //$NON-NLS-1$
85      final int pos = hostnamePort.lastIndexOf(':');
86      if (pos == -1)
87       {
88        throw new IllegalArgumentException("Not a host:port combination!"); //$NON-NLS-1$
89       }
90      final List<String> result = new ArrayList<>();
91      if (hostnamePort.contains("[")) // hostname/IP V4/IP V6 //$NON-NLS-1$
92       {
93        result.add(hostnamePort.substring(1, pos - 1)); // remove [] from IP V6 address
94       }
95      else
96       {
97        result.add(hostnamePort.substring(0, pos));
98       }
99      result.add(hostnamePort.substring(pos + 1)); // port
100     return result;
101    }
102 
103  }