Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2002-2003,2009 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package cn.wensiqun.utils;
 
 import java.io.File;
 import java.net.URL;
 import java.util.List;
 
Helper class to extract file paths from different urls
 
 public class URLUtil {
 
     private static final Log LOG = LogFactory.getLog(URLUtil.class);
     public static final String JBOSS5_VFS = "vfs";
     public static final String JBOSS5_VFSZIP = "vfszip";
     public static final String JBOSS5_VFSMEMORY = "vfsmemory";
     public static final String JBOSS5_VFSFILE = "vfsfile";
 
     private static final Pattern JAR_PATTERN = Pattern.compile("^(jar:|wsjar:|zip:|vfsfile:|code-source:)?(file:)?(.*?)(\\!/|\\.jar/)(.*)");
     private static final int JAR_FILE_PATH = 3;

    
Convert URLs to URLs with "file" protocol

Parameters:
url URL to convert to a jar url
Returns:
a URL to a file, or null if the URL external form cannot be parsed
 
     public static URL normalizeToFileProtocol(URL url) {
         String fileName = url.toExternalForm();
         Matcher jarMatcher = .matcher(fileName);
         try {
             if (isJBossUrl(url)){
                 return getJBossPhysicalUrl(url);
             } else  if (jarMatcher.matches()) {
                 String path = jarMatcher.group();
                 return new URL("file"""path);
             } else {
                 //it is not a jar or zip file
                 return null;
             }
         } catch (MalformedURLException e) {
             //can this ever happen?
             return null;
         } catch (IOException e) {
             .warn("Error opening JBoss vfs file"e);
             return null;
         }
     }

    
Verify That the given String is in valid URL format.

Parameters:
url The url string to verify.
Returns:
a boolean indicating whether the URL seems to be incorrect.
 
     public static boolean verifyUrl(String url) {
         if (url == null) {
             return false;
         }
 
         if (url.startsWith("https://")) {
             // URL doesn't understand the https protocol, hack it
             url = "http://" + url.substring(8);
         }
 
         try {
             new URL(url);
 
             return true;
         } catch (MalformedURLException e) {
             return false;
         }
     }

    
Check if given URL is matching Jar pattern for different servers

Parameters:
fileUrl
Returns:
    public static boolean isJarURL(URL fileUrl) {
        Matcher jarMatcher = ..matcher(fileUrl.getPath());
        return jarMatcher.matches(); 
    }

    
Check if given URL is pointing to JBoss 5 VFS resource

Parameters:
fileUrl
Returns:
    public static boolean isJBossUrl(URL fileUrl) {
        final String protocol = fileUrl.getProtocol();
        return .equals(protocol) || .equals(protocol) || .equals(protocol)
                || ("true".equals(System.getProperty("jboss.vfs.forceVfsJar")) && .equals(protocol));
    }

    
Try to determine physical file location.

Parameters:
url JBoss VFS URL
Returns:
URL pointing to physical file or original URL
Throws:
java.io.IOException If conversion fails
    public static URL getJBossPhysicalUrl(URL urlthrows IOException {
        Object content = url.openConnection().getContent();
        try {
            String s = content.getClass().toString();
            if (s.startsWith("class org.jboss.vfs.VirtualFile")) { // JBoss 7 and probably JBoss 6
                File physicalFile = readJBossPhysicalFile(content);
                return physicalFile.toURI().toURL();
            } else if (s.startsWith("class org.jboss.virtual.VirtualFile")) { // JBoss 5
                String fileName = url.toExternalForm();
                return new URL("file"nullfileName.substring(fileName.indexOf(":") + 1));
            }
        } catch (Exception e) {
            .warn("Error calling getPhysicalFile() on JBoss VirtualFile."e);
        }
        return url;
    }
    public static List<URLgetAllJBossPhysicalUrls(URL urlthrows IOException {
        List<URLurls = new ArrayList<URL>();
        Object content = url.openConnection().getContent();
        try {
            if (content.getClass().toString().startsWith("class org.jboss.vfs.VirtualFile")) {
                File physicalFile = readJBossPhysicalFile(content);
                readFile(urlsphysicalFile);
                readFile(urlsphysicalFile.getParentFile());
            } else {
                urls.add(url);
            }
        } catch (Exception e) {
            .warn("Error calling getPhysicalFile() on JBoss VirtualFile."e);
        }
        return urls;
    }
    private static File readJBossPhysicalFile(Object contentthrows Exception {
        Method method = content.getClass().getDeclaredMethod("getPhysicalFile");
        return (Filemethod.invoke(content);
    }
    private static void readFile(List<URLurlsFile physicalFilethrows MalformedURLException {
        if (physicalFile.isDirectory()) {
            for (File file : physicalFile.listFiles()) {
                if (file.isFile()) {
                    addIfAbsent(urlsfile.toURI().toURL());
                } else if (file.isDirectory()) {
                    readFile(urlsfile);
                }
            }
        }
    }
    private static void addIfAbsent(List<URLurlsURL fileUrl) {
        if (!urls.contains(fileUrl)) {
            urls.add(fileUrl);
        }
    }
New to GrepCode? Check out our FAQ X