Coverage Report - sk.baka.webvm.wicket.WicketUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
WicketUtils
15%
3/20
0%
0/8
2.273
WicketUtils$ResourceLinkStream
0%
0/23
0%
0/2
2.273
 
 1  
 /**
 2  
  * Copyright 2009 Martin Vysny.
 3  
  *
 4  
  * This file is part of WebVM.
 5  
  *
 6  
  * WebVM is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License, or
 9  
  * (at your option) any later version.
 10  
  *
 11  
  * WebVM is distributed in the hope that it will be useful,
 12  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  
  * GNU General Public License for more details.
 15  
  *
 16  
  * You should have received a copy of the GNU General Public License
 17  
  * along with WebVM.  If not, see <http://www.gnu.org/licenses/>.
 18  
  */
 19  
 package sk.baka.webvm.wicket;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.IOException;
 23  
 import java.io.InputStream;
 24  
 import java.net.URLConnection;
 25  
 import java.util.ArrayList;
 26  
 import java.util.List;
 27  
 import java.util.Locale;
 28  
 import java.util.logging.Level;
 29  
 import java.util.logging.Logger;
 30  
 import org.apache.wicket.RequestCycle;
 31  
 import org.apache.wicket.request.target.resource.ResourceStreamRequestTarget;
 32  
 import org.apache.wicket.util.resource.FileResourceStream;
 33  
 import org.apache.wicket.util.resource.IResourceStream;
 34  
 import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
 35  
 import org.apache.wicket.util.time.Time;
 36  
 import sk.baka.tools.IOUtils;
 37  
 import sk.baka.webvm.analyzer.classloader.ResourceLink;
 38  
 
 39  
 /**
 40  
  * Contains Wicket utilities.
 41  
  * @author Martin Vysny
 42  
  */
 43  0
 public final class WicketUtils {
 44  
 
 45  
     /**
 46  
      * Redirects to (and starts download process of) given resource.
 47  
      * @param resLink the link to download, must not be null
 48  
      * @return true if the redirect was succesfull, false if given link is not a downloadable content.
 49  
      */
 50  
     public static boolean redirectTo(final ResourceLink resLink) {
 51  0
         final RequestCycle rc = RequestCycle.get();
 52  0
         if (resLink.isRoot()) {
 53  0
             final File container = resLink.getContainer();
 54  0
             if (container.isFile()) {
 55  0
                 rc.setRequestTarget(new ResourceStreamRequestTarget(new FileResourceStream(container), container.getName()));
 56  0
                 rc.setRedirect(true);
 57  0
                 return true;
 58  
             }
 59  0
             return false;
 60  
         }
 61  0
         if (resLink.isPackage()) {
 62  0
             return false;
 63  
         }
 64  0
         rc.setRequestTarget(new ResourceStreamRequestTarget(toStream(resLink), resLink.getName()));
 65  0
         rc.setRedirect(true);
 66  0
         return true;
 67  
     }
 68  
 
 69  1
     private WicketUtils() {
 70  1
         throw new AssertionError();
 71  
     }
 72  1
     private static final Logger LOG = Logger.getLogger(WicketUtils.class.getName());
 73  
 
 74  
     /**
 75  
      * Converts a resource link to a resource stream.
 76  
      * @param link the link to convert. Must not be a package nor a root link.
 77  
      * @return converted resource stream.
 78  
      */
 79  
     public static IResourceStream toStream(final ResourceLink link) {
 80  0
         if (link.isPackage()) {
 81  0
             throw new IllegalArgumentException(link.getFullName() + " is a package");
 82  
         }
 83  0
         return new ResourceLinkStream(link);
 84  
     }
 85  
 
 86  
     /**
 87  
      * Provides a Wicket resource stream for given resource link.
 88  
      */
 89  
     private static class ResourceLinkStream implements IResourceStream {
 90  
 
 91  
         private static final long serialVersionUID = 1L;
 92  
         private final ResourceLink link;
 93  
 
 94  0
         public ResourceLinkStream(ResourceLink link) {
 95  0
             this.link = link;
 96  0
         }
 97  
 
 98  
         public String getContentType() {
 99  0
             return URLConnection.getFileNameMap().getContentTypeFor(link.getName());
 100  
         }
 101  
 
 102  
         public long length() {
 103  
             try {
 104  0
                 return link.getLength();
 105  0
             } catch (IOException ex) {
 106  0
                 LOG.log(Level.WARNING, null, ex);
 107  0
                 return -1;
 108  
             }
 109  
         }
 110  
 
 111  
         public InputStream getInputStream() throws ResourceStreamNotFoundException {
 112  
             final InputStream result;
 113  
             try {
 114  0
                 result = link.open();
 115  0
             } catch (IOException ex) {
 116  0
                 throw new ResourceStreamNotFoundException(ex);
 117  0
             }
 118  0
             streams.add(result);
 119  0
             return result;
 120  
         }
 121  0
         private final List<InputStream> streams = new ArrayList<InputStream>();
 122  
 
 123  
         public void close() throws IOException {
 124  0
             for (final InputStream is : streams) {
 125  0
                 IOUtils.closeQuietly(is);
 126  
             }
 127  0
             streams.clear();
 128  0
         }
 129  
 
 130  
         public Locale getLocale() {
 131  0
             return locale;
 132  
         }
 133  
         private Locale locale;
 134  
 
 135  
         public void setLocale(Locale locale) {
 136  0
             this.locale = locale;
 137  0
         }
 138  
 
 139  
         public Time lastModifiedTime() {
 140  0
             return Time.milliseconds(1);
 141  
         }
 142  
     }
 143  
 }