Coverage Report - org.mule.transport.servlet.JarResourceServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
JarResourceServlet
0%
0/28
0%
0/8
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.transport.servlet;
 8  
 
 9  
 import org.mule.util.IOUtils;
 10  
 
 11  
 import java.io.ByteArrayOutputStream;
 12  
 import java.io.IOException;
 13  
 import java.io.InputStream;
 14  
 
 15  
 import javax.servlet.ServletException;
 16  
 import javax.servlet.http.HttpServlet;
 17  
 import javax.servlet.http.HttpServletRequest;
 18  
 import javax.servlet.http.HttpServletResponse;
 19  
 
 20  
 /**
 21  
  * A servlet for loading resources loaded in jar files
 22  
  */
 23  0
 public class JarResourceServlet extends HttpServlet
 24  
 {
 25  
     public static final String DEFAULT_PATH_SPEC = "/mule-resource/*";
 26  
 
 27  
     public static final String DEFAULT_BASE_PATH = "";
 28  
 
 29  
     public static final String DEFAULT_MIME_TYPE = "application/octet-stream";
 30  
 
 31  0
     private String basepath = DEFAULT_BASE_PATH;
 32  
 
 33  
     @Override
 34  
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
 35  
     {
 36  0
         String file = getBasepath() + req.getPathInfo();
 37  
 
 38  0
         if(file.startsWith("/")) file = file.substring(1);
 39  0
         InputStream in = IOUtils.getResourceAsStream(file, getClass(), false, false);
 40  0
         if (in == null)
 41  
         {
 42  0
             resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
 43  0
             resp.getWriter().write("Unable to find file: " + req.getPathInfo());
 44  0
             return;
 45  
         }
 46  
         byte[] buffer;
 47  
         try
 48  
         {
 49  0
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 50  0
             IOUtils.copyLarge(in, baos);
 51  
 
 52  0
             buffer = baos.toByteArray();
 53  
 
 54  0
             String mimetype = DEFAULT_MIME_TYPE;
 55  0
             if (getServletContext() != null)
 56  
             {
 57  0
                 String temp = getServletContext().getMimeType(file);
 58  0
                 if (temp != null)
 59  
                 {
 60  0
                     mimetype = temp;
 61  
                 }
 62  
             }
 63  
 
 64  0
             resp.setContentType(mimetype);
 65  0
             resp.setContentLength(buffer.length);
 66  0
             resp.setHeader("Content-Disposition", "attachment; filename=\"" + req.getPathInfo() + "\"");
 67  0
             resp.getOutputStream().write(buffer);
 68  
         }
 69  
         finally
 70  
         {
 71  0
             in.close();
 72  0
             resp.getOutputStream().flush();
 73  0
         }
 74  0
     }
 75  
 
 76  
     public String getBasepath()
 77  
     {
 78  0
         return basepath;
 79  
     }
 80  
 
 81  
     public void setBasepath(String basepath)
 82  
     {
 83  0
         this.basepath = basepath;
 84  0
     }
 85  
 }