Coverage Report - net.admin4j.ui.servlets.AdminDisplayServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
AdminDisplayServlet
85%
18/21
66%
4/6
2.25
 
 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.servlets;
 15  
 
 16  
 import java.io.ByteArrayInputStream;
 17  
 import java.io.ByteArrayOutputStream;
 18  
 import java.io.IOException;
 19  
 import java.io.OutputStreamWriter;
 20  
 import java.util.Enumeration;
 21  
 import java.util.Map;
 22  
 
 23  
 import javax.servlet.http.HttpServletRequest;
 24  
 import javax.servlet.http.HttpServletResponse;
 25  
 
 26  
 import net.admin4j.deps.commons.io.IOUtils;
 27  
 import net.admin4j.util.Admin4jRuntimeException;
 28  
 import net.admin4j.util.FreemarkerUtils;
 29  
 import freemarker.template.Template;
 30  
 
 31  
 /**
 32  
  * Base class for Administrative display servlets.
 33  
  * @author D. Ashmore
 34  
  * @since 1.0
 35  
  */
 36  48
 public abstract class AdminDisplayServlet extends Admin4JServlet {
 37  
 
 38  
     public static final String ADMIN4J_SESSION_VARIABLE_PREFIX = "Admin4j";
 39  
     private static final long serialVersionUID = -7324380011384838424L;
 40  
     
 41  
     /**
 42  
      * Will generate Freemarker template results and write it to the output stream.
 43  
      * @param response
 44  
      * @param templateName
 45  
      * @param variableMap
 46  
      * @throws IOException
 47  
      */
 48  
     protected void displayFreeMarkerPage(HttpServletRequest request, 
 49  
             HttpServletResponse response, String templateName,
 50  
             Map<String, Object> variableMap) throws IOException {
 51  27
                 response.setContentType("text/html");
 52  
                 
 53  27
                 displayFreeMarkerResponse(request, response, templateName, variableMap);
 54  27
             }
 55  
 
 56  
     @SuppressWarnings("unchecked")
 57  
     protected void displayFreeMarkerResponse(HttpServletRequest request, 
 58  
             HttpServletResponse response,
 59  
             String templateName, Map<String, Object> variableMap)
 60  
             throws IOException {
 61  27
         Enumeration<String> attrNameEnum = request.getSession().getAttributeNames();
 62  
         String attrName;
 63  30
         while (attrNameEnum.hasMoreElements()) {
 64  3
             attrName = attrNameEnum.nextElement();
 65  3
             if (attrName != null && attrName.startsWith(ADMIN4J_SESSION_VARIABLE_PREFIX)) {
 66  3
                 variableMap.put("Session" +attrName, request.getSession().getAttribute(attrName));
 67  
             }
 68  
         }
 69  27
         variableMap.put("RequestAdmin4jCurrentUri", request.getRequestURI());
 70  
         
 71  27
         Template temp = FreemarkerUtils.createConfiguredTemplate(this.getClass(), templateName);
 72  27
         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
 73  
 
 74  
         try {
 75  27
             temp.process(variableMap, new OutputStreamWriter(outStream));
 76  27
             response.setContentLength(outStream.size());
 77  
             IOUtils.copy(new ByteArrayInputStream(outStream.toByteArray()), response.getOutputStream());
 78  
             
 79  27
             response.getOutputStream().flush();
 80  27
             response.getOutputStream().close();
 81  
         }
 82  0
         catch (Exception e) {
 83  0
             throw new Admin4jRuntimeException(e);
 84  27
         }
 85  27
     }
 86  
 
 87  
     /* (non-Javadoc)
 88  
      * @see net.admin4j.ui.servlets.Admin4JServlet#hasDisplay()
 89  
      */
 90  
     @Override
 91  
     public boolean hasDisplay() {
 92  0
         return true;
 93  
     }
 94  
     
 95  
     public abstract String getServletLabel();
 96  
 
 97  
 }