Coverage report

  %line %branch
org.apache.jetspeed.capabilities.impl.CapabilityValveImpl
0% 
0% 

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.jetspeed.capabilities.impl;
 19  
 
 20  
 import org.apache.commons.logging.Log;
 21  
 import org.apache.commons.logging.LogFactory;
 22  
 import org.apache.jetspeed.capabilities.Capabilities;
 23  
 import org.apache.jetspeed.capabilities.CapabilityMap;
 24  
 import org.apache.jetspeed.capabilities.MediaType;
 25  
 import org.apache.jetspeed.capabilities.MimeType;
 26  
 import org.apache.jetspeed.capabilities.UnableToBuildCapabilityMapException;
 27  
 import org.apache.jetspeed.pipeline.PipelineException;
 28  
 import org.apache.jetspeed.pipeline.valve.CapabilityValve;
 29  
 import org.apache.jetspeed.pipeline.valve.ValveContext;
 30  
 import org.apache.jetspeed.request.RequestContext;
 31  
 
 32  
 /**
 33  
  * Invokes the capability mapper in the request pipeline
 34  
  * 
 35  
  * @author <a href="mailto:roger.ruttimann@earthlink.net">Roger Ruttimann </a>
 36  
  * @version $Id: CapabilityValveImpl.java 517719 2007-03-13 15:05:48Z ate $
 37  
  */
 38  
 public class CapabilityValveImpl implements CapabilityValve
 39  
 {
 40  0
     private static final Log log = LogFactory.getLog(CapabilityValveImpl.class);
 41  
     String resourceDefault; // the default name for a resource
 42  
     private Capabilities capabilities;
 43  
 
 44  
     public CapabilityValveImpl( Capabilities capabilities )
 45  0
     {
 46  0
         this.capabilities = capabilities;
 47  0
     }
 48  
 
 49  
     /**
 50  
      * Initialize the valve before using in a pipeline.
 51  
      */
 52  
     public void initialize() throws PipelineException
 53  
     {
 54  
 
 55  0
     }
 56  
 
 57  
     public void invoke( RequestContext request, ValveContext context ) throws PipelineException
 58  
     {
 59  0
         String agent = request.getRequest().getHeader("User-Agent");
 60  
 
 61  
         // Get capability map
 62  
         CapabilityMap cm;
 63  
         try
 64  
         {
 65  0
             cm = capabilities.getCapabilityMap(agent);
 66  
         }
 67  0
         catch (UnableToBuildCapabilityMapException e)
 68  
         {
 69  0
            throw new PipelineException("Falied to create capabilitied:  "+e.getMessage(), e);
 70  0
         }
 71  
         
 72  0
         MediaType mediaType = cm.getPreferredMediaType();
 73  0
         MimeType mimeType = cm.getPreferredType();
 74  
 
 75  0
         if (mediaType == null)
 76  
         {
 77  0
             log.error("CapabilityMap returned a null media type");
 78  0
             throw new PipelineException("CapabilityMap returned a null media type");
 79  
         }
 80  
 
 81  0
         if (mimeType == null)
 82  
         {
 83  0
             log.error("CapabilityMap returned a null mime type");
 84  0
             throw new PipelineException("CapabilityMap returned a null mime type");
 85  
         }
 86  
 
 87  0
         String encoding = request.getRequest().getCharacterEncoding();
 88  
 
 89  0
         if (encoding == null)
 90  
         {
 91  0
             if (mediaType != null && mediaType.getCharacterSet() != class="keyword">null)
 92  
             {
 93  0
                 encoding = mediaType.getCharacterSet();
 94  
             }
 95  
         }
 96  
 
 97  0
         if (log.isDebugEnabled())
 98  
         {
 99  0
             log.debug("MediaType: " + mediaType.getName());
 100  0
             log.debug("Encoding: " + encoding);
 101  0
             log.debug("Mimetype: " + mimeType.getName());
 102  
         }
 103  
 
 104  
         // Put the encoding in the request
 105  0
         request.setCharacterEncoding(encoding);
 106  
 
 107  
         // Put the CapabilityMap into the request
 108  0
         request.setCapabilityMap(cm);
 109  
 
 110  
         // Put the Media Type into the request
 111  0
         request.setMediaType(mediaType.getName());
 112  
 
 113  
         // Put the Mime Type into the request
 114  0
         request.setMimeType(mimeType.getName());
 115  
 
 116  
         // Put the Mime Type and Charset into the response
 117  0
         StringBuffer contentType = new StringBuffer(mimeType.getName());
 118  0
         if (encoding != null)
 119  
         {
 120  0
             contentType.append("; charset=" + encoding);
 121  
         }
 122  0
         String type =  contentType.toString(); //mapContentType(request, contentType.toString());
 123  0
         request.getResponse().setContentType(type);
 124  
 
 125  
         // Pass control to the next Valve in the Pipeline
 126  0
         context.invokeNext(request);
 127  0
     }
 128  
 
 129  0
     static String[][] MIME_MAP =
 130  
     {     
 131  
         {".pdf", "application/pdf"}       
 132  
     };
 133  
     
 134  
     protected String mapContentType(RequestContext request, String contentType)
 135  
     {
 136  
         // TODO: get path from servlet request
 137  
         // this code below fails since the path is not yet parsed
 138  
         // to far up in the pipeline
 139  0
         String path = request.getPath();
 140  0
         if (path != null)
 141  
         {
 142  0
             for (int ix=0; ix < MIME_MAP.length; ix++)
 143  
             {
 144  0
                 if (path.endsWith(MIME_MAP[ix][0]))
 145  
                 {
 146  0
                     return MIME_MAP[ix][1];
 147  
                 }
 148  
             }            
 149  
         }
 150  0
         return contentType;
 151  
     }
 152  
     
 153  
     public String toString()
 154  
     {
 155  0
         return "CapabilityValveImpl";
 156  
     }
 157  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.