| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
package net.admin4j.ui.filters.consolidator; |
| 15 | |
|
| 16 | |
import java.util.Arrays; |
| 17 | |
import java.util.HashSet; |
| 18 | |
import java.util.Properties; |
| 19 | |
import java.util.Set; |
| 20 | |
|
| 21 | |
import javax.servlet.FilterConfig; |
| 22 | |
import javax.servlet.ServletConfig; |
| 23 | |
import javax.servlet.ServletException; |
| 24 | |
import javax.servlet.http.HttpServletRequest; |
| 25 | |
|
| 26 | |
import net.admin4j.deps.commons.lang3.StringUtils; |
| 27 | |
import net.admin4j.deps.commons.lang3.Validate; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | 18 | public class RestServiceHttpRequestConsolidator implements |
| 43 | |
HttpRequestConsolidator { |
| 44 | |
|
| 45 | |
protected static final String PARM_REST_RESOURCE_TOKENS = "rest.resource.tokens"; |
| 46 | |
protected static final String PARM_REST_SERVICE_URI_PREFIX = "rest.service.uri.prefix"; |
| 47 | 18 | private final Set<String> serviceResourceTokenSet = new HashSet<String>(); |
| 48 | 18 | private String restServiceURLPrefix = null; |
| 49 | |
|
| 50 | |
public String categorize(HttpServletRequest request) { |
| 51 | |
|
| 52 | 15 | if (!request.getRequestURI() |
| 53 | |
.contains(restServiceURLPrefix)) { |
| 54 | 0 | return request.getRequestURI(); |
| 55 | |
} |
| 56 | |
|
| 57 | |
String localUri = StringUtils.substringAfter(request.getRequestURI(), |
| 58 | |
restServiceURLPrefix); |
| 59 | |
String localPrefix = StringUtils.substringBefore( |
| 60 | |
request.getRequestURI(), restServiceURLPrefix) |
| 61 | |
+ restServiceURLPrefix; |
| 62 | 15 | if (localPrefix.endsWith("/")) { |
| 63 | |
localPrefix = StringUtils.substringBeforeLast(localPrefix, "/"); |
| 64 | |
} |
| 65 | |
|
| 66 | |
String[] requestUriParts = StringUtils.split(localUri, '/'); |
| 67 | 15 | StringBuilder builder = new StringBuilder(localPrefix); |
| 68 | 63 | for (int i = 0; i < requestUriParts.length; i++) { |
| 69 | 48 | builder.append("/"); |
| 70 | 48 | if (serviceResourceTokenSet.contains(requestUriParts[i])) { |
| 71 | 30 | builder.append(requestUriParts[i]); |
| 72 | |
} |
| 73 | |
else { |
| 74 | 18 | builder.append("*"); |
| 75 | |
} |
| 76 | |
} |
| 77 | |
|
| 78 | 15 | builder.append("--"); |
| 79 | 15 | builder.append(request.getMethod()); |
| 80 | |
|
| 81 | 15 | return builder.toString(); |
| 82 | |
} |
| 83 | |
|
| 84 | |
public void configure(FilterConfig config) throws ServletException { |
| 85 | 3 | this.configureRestResourceTokens(config.getInitParameter(PARM_REST_RESOURCE_TOKENS)); |
| 86 | 3 | this.configureRestServicePrefix(config.getInitParameter(PARM_REST_SERVICE_URI_PREFIX)); |
| 87 | 3 | } |
| 88 | |
|
| 89 | |
public void configure(ServletConfig config) throws ServletException { |
| 90 | 3 | this.configureRestResourceTokens(config.getInitParameter(PARM_REST_RESOURCE_TOKENS)); |
| 91 | 3 | this.configureRestServicePrefix(config.getInitParameter(PARM_REST_SERVICE_URI_PREFIX)); |
| 92 | |
|
| 93 | 3 | } |
| 94 | |
|
| 95 | |
public void configure(String namePrefix, Properties config) { |
| 96 | 12 | this.configureRestResourceTokens((String) config.get(PARM_REST_RESOURCE_TOKENS)); |
| 97 | 12 | this.configureRestServicePrefix((String) config.get(PARM_REST_SERVICE_URI_PREFIX)); |
| 98 | |
|
| 99 | 12 | } |
| 100 | |
|
| 101 | |
protected void configureRestResourceTokens(String restResourceTokenStr) { |
| 102 | |
Validate.notEmpty(restResourceTokenStr, |
| 103 | |
"Null or missing configuration property %s", |
| 104 | |
PARM_REST_RESOURCE_TOKENS); |
| 105 | |
|
| 106 | |
String[] resourceTokens = StringUtils.split(restResourceTokenStr, ','); |
| 107 | 72 | for (int i = 0; i < resourceTokens.length; i++) { |
| 108 | 54 | resourceTokens[i] = resourceTokens[i].trim(); |
| 109 | |
} |
| 110 | 18 | serviceResourceTokenSet.addAll(Arrays.asList(resourceTokens)); |
| 111 | |
Validate.notEmpty(serviceResourceTokenSet, |
| 112 | |
"Null or blank serviceResourceTokenSet not allowed."); |
| 113 | 18 | } |
| 114 | |
|
| 115 | |
protected void configureRestServicePrefix(String restServicePrefix) { |
| 116 | 18 | this.restServiceURLPrefix = restServicePrefix; |
| 117 | |
Validate.notEmpty(restServicePrefix, |
| 118 | |
"Null or missing configuration property %s", |
| 119 | |
PARM_REST_SERVICE_URI_PREFIX); |
| 120 | 18 | } |
| 121 | |
|
| 122 | |
} |