Coverage Report - net.admin4j.ui.filters.consolidator.RestServiceHttpRequestConsolidator
 
Classes in this File Line Coverage Branch Coverage Complexity
RestServiceHttpRequestConsolidator
96%
29/30
80%
8/10
2
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  *
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  *
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 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  
  * Consolidates RESTful web service requests for performance tracking purposes.
 31  
  *
 32  
  * <p>
 33  
  * Configuration options for this consolidator are as follows:
 34  
  * </p>
 35  
  * <li>rest.resource.tokens -- comma delimited list of REST resource identifiers
 36  
  * (e.g. "students/classes/charges").</li> <li>rest.service.uri.prefix -- URI
 37  
  * prefix used for publishing RESTful web services (e.g. "/services").</li>
 38  
  *
 39  
  * @author D. Ashmore
 40  
  * @since 1.0.4-rc1
 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  
                 // Only consolidate RESTful web service calls. Leave others alone.
 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 { // it's a REST resource identifier
 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  
 }