Coverage Report - net.admin4j.util.ServletUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ServletUtils
87%
27/31
87%
14/16
3.6
 
 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.util;
 15  
 
 16  
 import java.io.IOException;
 17  
 import java.util.ArrayList;
 18  
 import java.util.Enumeration;
 19  
 import java.util.List;
 20  
 
 21  
 import javax.servlet.ServletConfig;
 22  
 import javax.servlet.ServletException;
 23  
 import javax.servlet.http.HttpServletRequest;
 24  
 import javax.servlet.http.HttpSession;
 25  
 
 26  
 import net.admin4j.deps.commons.lang3.StringUtils;
 27  
 
 28  
 /**
 29  
  * Generic Servlet utilities
 30  
  * @author D. Ashmore
 31  
  *
 32  
  */
 33  0
 public class ServletUtils {
 34  
 
 35  
     /**
 36  
      * Will rethrow a caught Servlet Exception
 37  
      * @param t
 38  
      * @throws ServletException
 39  
      * @throws IOException
 40  
      */
 41  
     public static void reThrowServletFilterException(Throwable t) throws ServletException,
 42  
             IOException {
 43  24
         if (t instanceof ServletException) {
 44  3
             throw (ServletException)t;
 45  
         }
 46  21
         if (t instanceof IOException) {
 47  3
             throw (IOException) t;
 48  
         }
 49  18
         if (t instanceof RuntimeException) {
 50  18
             throw (RuntimeException) t;
 51  
         }
 52  0
         throw new ServletException(t);
 53  
     }
 54  
 
 55  
     /**
 56  
      * Will format session attributes as NavmeValuePairs.
 57  
      * @param session
 58  
      * @return
 59  
      */
 60  
     @SuppressWarnings("rawtypes")
 61  
     public static List<NameValuePair> listSessionAttributes(HttpSession session) {
 62  18
         List<NameValuePair> list = new ArrayList<NameValuePair>();
 63  18
             Enumeration attrNames = session.getAttributeNames();
 64  
             String name;
 65  
         Object value;
 66  
             
 67  24
             while (attrNames.hasMoreElements()) {
 68  6
                     name = (String)attrNames.nextElement();
 69  6
                     value = session.getAttribute(name);
 70  
                     
 71  6
                     list.add(new NameValuePair(name, value.toString()));
 72  
             }
 73  
             
 74  18
             return list;
 75  
     }
 76  
 
 77  
     /**
 78  
      * Will format request attributes as NameValuePairs.
 79  
      * @param request
 80  
      * @return
 81  
      */
 82  
     @SuppressWarnings("rawtypes")
 83  
     public static List<NameValuePair> listRequestAttributes(HttpServletRequest request) {
 84  
     
 85  27
             List<NameValuePair> list = new ArrayList<NameValuePair>();
 86  27
             Enumeration parmNames = request.getParameterNames();
 87  
             String name;
 88  
             Object value;
 89  
     
 90  36
             while (parmNames.hasMoreElements()) {
 91  
                     
 92  9
                     name = (String)parmNames.nextElement();
 93  9
                     if ("javax.faces.ViewState".equals(name))
 94  
                     {
 95  0
                             value = "[suppressed]";
 96  
                     }
 97  
                     else
 98  
                     {
 99  9
                             value = request.getParameter(name);
 100  
                     }
 101  
                     
 102  9
                     list.add(new NameValuePair(name, value.toString()));
 103  
             }
 104  
             
 105  27
             return list;
 106  
     }
 107  
 
 108  
     /**
 109  
      * Will parse the request and return the name of the current web application.
 110  
      * @param request
 111  
      * @return
 112  
      */
 113  
     public static String getApplicationName(HttpServletRequest request) {
 114  18
             return request.getRequestURI().substring(1, request.getRequestURI().indexOf("/", 1));
 115  
     }
 116  
     
 117  
     /**
 118  
      * Will return the configuration setting requested *in the order of the keys provided*.  The
 119  
      * first key to retrieve a non-empty value will be the value returned.
 120  
      * @param settingKeysInPrecedenceOrder
 121  
      * @param config
 122  
      * @return
 123  
      */
 124  
     public static String getConfigurationSetting(String[] settingKeysInPrecedenceOrder, ServletConfig config) {
 125  75
         String value = null;
 126  162
         for (String key: settingKeysInPrecedenceOrder) {
 127  114
             value = config.getInitParameter(key);
 128  0
             if (!StringUtils.isEmpty(value)) {
 129  27
                 return value;
 130  
             }
 131  
         }
 132  
         
 133  48
         return value;
 134  
     }
 135  
 
 136  
 }