Example usage for java.io File toURL

List of usage examples for java.io File toURL

Introduction

In this page you can find the example usage for java.io File toURL.

Prototype

@Deprecated
public URL toURL() throws MalformedURLException 

Source Link

Document

Converts this abstract pathname into a file: URL.

Usage

From source file:org.roda.core.util.ClassLoaderUtility.java

/**
 * Add file to CLASSPATH/*from w  w  w .  j a v  a  2  s  . c o  m*/
 * 
 * @param f
 *          File object
 * @throws IOException
 *           IOException
 */
public static void addFile(File f) throws IOException {
    addURL(f.toURL());
}

From source file:org.tizzit.core.classloading.ExternalLibClassLoaderManager.java

public static List<URL> getURLList(String path) throws Exception {
    List<URL> retVal = null;

    List<File> files = getFileList(path);
    if (files != null && files.size() > 0) {
        retVal = new ArrayList<URL>(files.size());
        for (File file : files) {
            retVal.add(file.toURL());
        }//  w w w .jav  a  2  s. c  o m
    }
    return retVal;
}

From source file:org.apache.xml.security.samples.signature.CreateDonaldsAdditionalURISignature.java

/**
 * Method signAndWrite//  w  ww .j av  a  2 s  .c o  m
 *
 * @param db
 * @param privk
 * @param pubkey
 * @param SignatureURI
 * @param DigestURI
 * @param filename
 * @throws Exception
 */
public static void signAndWrite(DocumentBuilder db, PrivateKey privk, PublicKey pubkey, String SignatureURI,
        String DigestURI, String filename) throws Exception {

    Document doc = createDocument(db);
    Element root = doc.getDocumentElement();

    File f = new File(filename);
    XMLSignature signature = new XMLSignature(doc, f.toURL().toString(), SignatureURI);
    Transforms transforms = new Transforms(doc);

    transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
    signature.addDocument("", transforms, DigestURI);
    signature.addKeyInfo(pubkey);
    root.appendChild(signature.getElement());
    XMLUtils.addReturnToElement(root);
    signature.sign(privk);

    FileOutputStream fos = new FileOutputStream(f);

    XMLUtils.outputDOMc14nWithComments(doc, fos);

    // System.out.println(new String(signature.getSignedInfo().getReferencedContentAfterTransformsItem(0).getBytes()));
}

From source file:org.apache.xml.security.samples.signature.CreateDonaldsAdditionalURISignature.java

/**
 * Method macAndWrite/*from   w  ww .  j a  v a2s  . co m*/
 *
 * @param db
 * @param mackey
 * @param SignatureURI
 * @param DigestURI
 * @param filename
 * @throws Exception
 */
public static void macAndWrite(DocumentBuilder db, byte[] mackey, String SignatureURI, String DigestURI,
        String filename) throws Exception {
    System.out.println(SignatureURI + "  ---   " + DigestURI);

    Document doc = createDocument(db);
    Element root = doc.getDocumentElement();

    File f = new File(filename);
    XMLSignature signature = new XMLSignature(doc, f.toURL().toString(), SignatureURI);
    Transforms transforms = new Transforms(doc);

    transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
    signature.addDocument("", transforms, DigestURI);

    SecretKey secretKey = signature.createSecretKey(mackey);

    root.appendChild(signature.getElement());
    XMLUtils.addReturnToElement(root);
    signature.sign(secretKey);

    FileOutputStream fos = new FileOutputStream(f);

    XMLUtils.outputDOMc14nWithComments(doc, fos);

    // System.out.println(new String(signature.getSignedInfo().getReferencedContentAfterTransformsItem(0).getBytes()));
}

From source file:org.apache.xml.security.samples.iaik.IAIKInterOp.java

public static void verifyAnonymous(String gregorsDir, DocumentBuilderFactory dbf) {
    String filename = gregorsDir + "coreFeatures/signatures/anonymousReferenceSignature.xml";
    try {//from   ww  w. j  a va  2  s.co  m
        String anonymousRef = gregorsDir + "coreFeatures/samples/anonymousReferenceContent.xml";
        ResourceResolverSpi resolver = new ResolverAnonymous(anonymousRef);
        File f = new File(filename);

        System.out.println("Try to verify " + f.toURL().toString());

        javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
        org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));
        Element nscontext = SampleUtils.createDSctx(doc, "ds", Constants.SignatureSpecNS);
        Element sigElement = (Element) XPathAPI.selectSingleNode(doc, "//ds:Signature[1]", nscontext);
        XMLSignature signature = new XMLSignature(sigElement, f.toURL().toString());

        signature.setFollowNestedManifests(false);
        signature.addResourceResolver(resolver);

        KeyInfo ki = signature.getKeyInfo();

        if (ki != null) {
            X509Certificate cert = signature.getKeyInfo().getX509Certificate();

            if (cert != null) {
                System.out.println("The XML signature in file " + f.toURL().toString() + " is "
                        + (signature.checkSignatureValue(cert) ? "valid (good)" : "invalid !!!!! (bad)"));
            } else {
                PublicKey pk = signature.getKeyInfo().getPublicKey();

                if (pk != null) {
                    System.out.println("The XML signature in file " + f.toURL().toString() + " is "
                            + (signature.checkSignatureValue(pk) ? "valid (good)" : "invalid !!!!! (bad)"));
                } else {
                    System.out.println("Did not find a public key, so I can't check the signature");
                }
            }
        } else {
            System.out.println("Did not find a KeyInfo");
        }
    } catch (Exception ex) {
        System.out.println("The XML signature in file " + filename + " crashed the application (bad)");
        ex.printStackTrace();
        System.out.println();
    }
}

From source file:org.apache.geode.internal.statistics.StatUtils.java

private static byte[] readBytes(File file) throws IOException {
    int byteCount = (int) file.length();

    byte[] input = new byte[byteCount];

    URL url = file.toURL();
    assertThat(url).isNotNull();/*  w  ww .  j  a  v a2  s  . c o  m*/

    InputStream is = url.openStream();
    assertThat(is).isNotNull();

    BufferedInputStream bis = new BufferedInputStream(is);
    int bytesRead = bis.read(input);
    bis.close();

    assertThat(bytesRead).isEqualTo(byteCount);
    return input;
}

From source file:org.nuxeo.runtime.jboss.adapter.ComponentAdapter.java

protected static void undeployComponent(RegistrationInfo ri)
        throws MBeanProxyCreationException, MalformedObjectNameException {
    ComponentName name = ri.getName();/*ww  w  . j  ava  2s. c o  m*/
    log.debug("Unregistering Mbean for service: " + name);
    RuntimeAdapterMBean rad = (RuntimeAdapterMBean) ServiceLocator.getService(RuntimeAdapterMBean.class,
            RuntimeAdapter.NAME);
    File file = new File(rad.getTempDeployDir(), name + "-service.xml");
    try {
        DeploymentHelper.undeploy(file.toURL());
    } catch (Exception e) {
        log.error("Failed to un-register Mbean for service " + ri.getName(), e);
    }
}

From source file:org.apache.xml.security.samples.signature.VerifyMerlinsExamplesSixteen.java

/**
 * Method verify/*from  www .j a v  a 2 s . co  m*/
 *
 * @param dbf
 * @param filename
 * @throws Exception
 */
public static void verify(DocumentBuilderFactory dbf, String filename) throws Exception {

    File f = new File(filename);

    System.out.println("Try to verify " + f.toURL().toString());

    javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();

    if (VerifyMerlinsExamplesSixteen.schemaValidate) {
        db.setErrorHandler(new org.apache.xml.security.utils.IgnoreAllErrorHandler());
        db.setEntityResolver(new org.xml.sax.EntityResolver() {

            public org.xml.sax.InputSource resolveEntity(String publicId, String systemId)
                    throws org.xml.sax.SAXException {

                if (systemId.endsWith("xmldsig-core-schema.xsd")) {
                    try {
                        return new org.xml.sax.InputSource(new FileInputStream(signatureSchemaFile));
                    } catch (FileNotFoundException ex) {
                        throw new org.xml.sax.SAXException(ex);
                    }
                } else {
                    return null;
                }
            }
        });
    }

    org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));

    Element nscontext = SampleUtils.createDSctx(doc, "ds", Constants.SignatureSpecNS);
    Element sigElement = (Element) XPathAPI.selectSingleNode(doc, "//ds:Signature[1]", nscontext);
    XMLSignature signature = new XMLSignature(sigElement, f.toURL().toString());

    signature.getSignedInfo()
            .addResourceResolver(new org.apache.xml.security.samples.utils.resolver.OfflineResolver());

    signature.setFollowNestedManifests(false);

    // signature.addResourceResolver(new OfflineResolver());

    // XMLUtils.outputDOMc14nWithComments(signature.getElement(), System.out);
    KeyInfo ki = signature.getKeyInfo();

    if (ki != null) {
        /*
        if (ki.containsX509Data()) {
           System.out.println("Could find a X509Data element in the KeyInfo");
        }
        */

        X509Certificate cert = signature.getKeyInfo().getX509Certificate();

        if (cert != null) {
            /*
            System.out.println(
               "I try to verify the signature using the X509 Certificate: "
               + cert);
            */
            System.out.println("The XML signature in file " + f.toURL().toString() + " is "
                    + (signature.checkSignatureValue(cert) ? "valid (good)" : "invalid !!!!! (bad)"));
        } else {
            // System.out.println("Did not find a Certificate");

            PublicKey pk = signature.getKeyInfo().getPublicKey();

            if (pk != null) {
                // System.out.println("I try to verify the signature using the public key: " + pk);
                System.out.println("The XML signature in file " + f.toURL().toString() + " is "
                        + (signature.checkSignatureValue(pk) ? "valid (good)" : "invalid !!!!! (bad)"));
            } else {
                System.out.println("Did not find a public key, so I can't check the signature");
            }
        }
    } else {
        System.out.println("Did not find a KeyInfo");
    }

    /*
    SignedInfo s = signature.getSignedInfo();
    for (int i=0; i<s.getSignedContentLength(); i++) {
       System.out.println("################ Signed Resource " + i + " ################");
       FileOutputStream f2 = new FileOutputStream(filename + "." + i + ".input");
       byte[] data = s.getSignedContentItem(i);
       f2.write(data);
       f2.close();
            
       System.out.println(new String(data));
       System.out.println();
    }
    */
}

From source file:org.apache.xml.security.samples.signature.VerifyMerlinsExamplesTwentyThree.java

/**
 * Method verify/*from   w ww.  j a  va  2  s . c  o  m*/
 *
 * @param dbf
 * @param filename
 * @throws Exception
 */
public static void verify(DocumentBuilderFactory dbf, String filename) throws Exception {

    File f = new File(filename);

    System.out.println("Try to verify " + f.toURL().toString());

    javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();

    if (VerifyMerlinsExamplesTwentyThree.schemaValidate) {
        db.setErrorHandler(new org.apache.xml.security.utils.IgnoreAllErrorHandler());
        db.setEntityResolver(new org.xml.sax.EntityResolver() {

            public org.xml.sax.InputSource resolveEntity(String publicId, String systemId)
                    throws org.xml.sax.SAXException {

                if (systemId.endsWith("xmldsig-core-schema.xsd")) {
                    try {
                        return new org.xml.sax.InputSource(new FileInputStream(signatureSchemaFile));
                    } catch (FileNotFoundException ex) {
                        throw new org.xml.sax.SAXException(ex);
                    }
                } else {
                    return null;
                }
            }
        });
    }

    org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));

    Element nscontext = SampleUtils.createDSctx(doc, "ds", Constants.SignatureSpecNS);
    Element sigElement = (Element) XPathAPI.selectSingleNode(doc, "//ds:Signature[1]", nscontext);
    XMLSignature signature = new XMLSignature(sigElement, f.toURL().toString());

    signature.getSignedInfo()
            .addResourceResolver(new org.apache.xml.security.samples.utils.resolver.OfflineResolver());

    signature.setFollowNestedManifests(false);

    // signature.addResourceResolver(new OfflineResolver());

    // XMLUtils.outputDOMc14nWithComments(signature.getElement(), System.out);
    KeyInfo ki = signature.getKeyInfo();

    if (ki != null) {
        /*
        if (ki.containsX509Data()) {
           System.out.println("Could find a X509Data element in the KeyInfo");
        }
        */

        X509Certificate cert = signature.getKeyInfo().getX509Certificate();

        if (cert != null) {
            /*
            System.out.println(
               "I try to verify the signature using the X509 Certificate: "
               + cert);
            */
            System.out.println("The XML signature in file " + f.toURL().toString() + " is "
                    + (signature.checkSignatureValue(cert) ? "valid (good)" : "invalid !!!!! (bad)"));
        } else {
            // System.out.println("Did not find a Certificate");

            PublicKey pk = signature.getKeyInfo().getPublicKey();

            if (pk != null) {
                // System.out.println("I try to verify the signature using the public key: " + pk);
                System.out.println("The XML signature in file " + f.toURL().toString() + " is "
                        + (signature.checkSignatureValue(pk) ? "valid (good)" : "invalid !!!!! (bad)"));
            } else {
                System.out.println("Did not find a public key, so I can't check the signature");
            }
        }
    } else {
        System.out.println("Did not find a KeyInfo");
    }

    /*
    SignedInfo s = signature.getSignedInfo();
    for (int i=0; i<s.getSignedContentLength(); i++) {
       System.out.println("################ Signed Resource " + i + " ################");
       FileOutputStream f2 = new FileOutputStream(filename + "." + i + ".input");
       byte[] data = s.getSignedContentItem(i);
       f2.write(data);
       f2.close();
            
       System.out.println(new String(data));
       System.out.println();
    }
    */
}

From source file:org.apache.axis2.metadata.registry.MetadataFactoryRegistry.java

/**
 * This method will load a file, if it exists, that contains a list
 * of interfaces and custom implementations. This allows for non-
 * programmatic registration of custom interface implementations
 * with the MDQ layer.//  w  w  w. j a va 2  s.c  o  m
 */
private static void loadConfigFromFile() {
    String pairSeparator = "|";
    try {
        ClassLoader classLoader = getContextClassLoader(null);
        URL url = null;
        url = classLoader.getResource(configurationFileLoc);
        if (url == null) {
            File file = new File(configurationFileLoc);
            url = file.toURL();
        }
        // the presence of this file is optional
        if (url != null) {
            if (log.isDebugEnabled()) {
                log.debug("Found URL to MetadataFactoryRegistry configuration file: " + configurationFileLoc);
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            String line = reader.readLine();

            // the separator of the file is the '|' character
            // to the left of the separator will be the interface and
            // to the right will be the custom implementation
            if (line != null && line.indexOf("|") != -1) {
                String interfaceName = line.substring(0, line.indexOf(pairSeparator));
                String implName = line.substring(line.indexOf(pairSeparator) + 1, line.length());
                if (log.isDebugEnabled()) {
                    log.debug("For registered class: " + interfaceName + " the "
                            + "following implementation was found: " + implName);
                }
                Class intf = classLoader.loadClass(interfaceName);
                Class impl = classLoader.loadClass(implName);

                // if we could load both we need to register them with our
                // internal registry
                if (intf != null && impl != null) {
                    if (log.isDebugEnabled()) {
                        log.debug("Loaded both interface and implementation class: " + interfaceName + ":"
                                + implName);
                    }
                    if (impl.getEnclosingClass() == null) {
                        table.put(intf, impl.newInstance());
                    } else {
                        if (log.isWarnEnabled()) {
                            log.warn("The implementation class: " + impl.getClass().getName()
                                    + " could not be lregistered because it is an inner class. "
                                    + "In order to register file-based overrides, implementations "
                                    + "must be public outer classes.");
                        }
                    }
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Could not load both interface and implementation class: " + interfaceName
                                + ":" + implName);
                    }
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Did not find File for MetadataFactoryRegistry configuration " + "file: "
                            + configurationFileLoc);
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Did not find URL for MetadataFactoryRegistry configuration " + "file: "
                        + configurationFileLoc);
            }
        }
    } catch (Throwable t) {
        if (log.isDebugEnabled()) {
            log.debug("The MetadataFactoryRegistry could not process the configuration file: "
                    + configurationFileLoc + " because of the following error: " + t.toString());
        }
    }
}