List of usage examples for java.io File toURL
@Deprecated public URL toURL() throws MalformedURLException
file:
URL. From source file:org.apache.xml.security.test.interop.InteropTest.java
/** * Method verify/*ww w. j a va2s. c o m*/ * * @param filename * @param resolver * * @throws Exception */ public boolean verify(String filename, ResourceResolverSpi resolver, boolean followManifests) throws Exception { File f = new File(filename); javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder(); org.w3c.dom.Document doc = db.parse(f); Element nscontext = TestUtils.createDSctx(doc, "ds", Constants.SignatureSpecNS); Element sigElement = (Element) XPathAPI.selectSingleNode(doc, "//ds:Signature[1]", nscontext); XMLSignature signature = new XMLSignature(sigElement, f.toURL().toString()); if (resolver != null) { signature.addResourceResolver(resolver); } signature.setFollowNestedManifests(followManifests); KeyInfo ki = signature.getKeyInfo(); boolean result = false; if (ki != null) { X509Certificate cert = ki.getX509Certificate(); if (cert != null) { result = signature.checkSignatureValue(cert); } else { PublicKey pk = ki.getPublicKey(); if (pk != null) { result = signature.checkSignatureValue(pk); } else { throw new RuntimeException("Did not find a public key, so I can't check the signature"); } } } else { throw new RuntimeException("Did not find a KeyInfo"); } if (!result) { for (int i = 0; i < signature.getSignedInfo().getLength(); i++) { boolean refVerify = signature.getSignedInfo().getVerificationResult(i); if (refVerify) { log.debug("Reference " + i + " was OK"); } else { // JavaUtils.writeBytesToFilename(filename + i + ".apache.txt", signature.getSignedInfo().item(i).getContentsAfterTransformation().getBytes()); log.debug("Reference " + i + " was not OK"); } } //throw new RuntimeException("Falle:"+sb.toString()); } return result; }
From source file:com.moss.appkeep.tools.MavenUploader.java
public List<Component> upload(File launchSpec, AppkeepService appkeep, IdProover idProver, boolean forceUpload) throws IdProovingException, SecurityException { // 1) select launch-spec Object o;//from w w w . j a v a2s .co m try { JAXBContext jaxb = JAXBContext.newInstance(JavaAppSpec.class, JavaAppletSpec.class); o = jaxb.createUnmarshaller().unmarshal(launchSpec.toURL()); } catch (Throwable e) { throw new RuntimeException("Error reading file \"" + launchSpec + "\"", e); } final List<Component> components = new LinkedList<Component>(); if (o instanceof JavaAppSpec) { JavaAppSpec spec = (JavaAppSpec) o; { components.addAll(spec.components()); for (BundleSpec b : spec.bundles()) { components.addAll(b.components()); } for (AppProfile p : spec.profiles()) { components.addAll(p.components()); } } } else if (o instanceof JavaAppletSpec) { JavaAppletSpec spec = (JavaAppletSpec) o; { components.addAll(spec.components()); for (AppletProfile p : spec.profiles()) { components.addAll(p.components()); } } } // 2) for each component (in each profile): for (Component c : components) { // a) locate the component in the maven repo (local, remote?) MavenCoordinatesHandle m = MavenArtifactGrabber.getMavenArtifact(c.artifactHandles()); if (m == null) { throw new RuntimeException("Component has no maven handle: " + c); } final String logPrefix = c.type() + ": " + m; ComponentInfo info = appkeep.getInfo(new ComponentHandlesSelector(c.artifactHandles()), new UserAccountDownloadToken(idProver.giveProof())); // List<ComponentInfo> matches = appkeep.listByMavenInfo(m.groupId(), m.artifactId(), m.version(), idProver.giveProof()); if (forceUpload || info == null) { if (forceUpload) { log.info("[FORCING] " + logPrefix + " "); } else { log.info("[NOT IN KEEP] " + logPrefix + " "); } // b) download it SimpleArtifactFinder finder = new SimpleArtifactFinder(); File location = finder.findLocal(m.groupId(), m.artifactId(), m.version()); if (!location.exists()) { throw new RuntimeException("Cannot find " + m + " in local repository. I was expecting to find it at " + location.getAbsolutePath()); } log.info(" found in local repository: " + location.getAbsoluteFile()); InputStream data; try { data = new FileInputStream(location); } catch (FileNotFoundException e) { throw new RuntimeException(e); } try { log.info("uploading"); // c) upload it if necessary appkeep.post(c.artifactHandles().toArray(new ComponentHandle[] {}), c.type(), idProver.giveProof(), data); log.info("done"); } catch (SecurityException e) { throw new RuntimeException(e); } finally { try { data.close(); } catch (IOException e) { throw new RuntimeException(e); } } } else { log.info("[ALREADY IN KEEP] " + logPrefix + " "); } } /* * 3) upload the launch-spec itself? */ return components; }
From source file:com.t3.client.ui.AddResourceDialog.java
@Override public boolean commit() { if (!super.commit()) { return false; }//from w w w .jav a 2 s . c o m // Add the resource final List<LibraryRow> rowList = new ArrayList<LibraryRow>(); switch (model.getTab()) { case LOCAL: if (StringUtils.isEmpty(model.getLocalDirectory())) { TabletopTool.showMessage("dialog.addresource.warn.filenotfound", "Error", JOptionPane.ERROR_MESSAGE, model.getLocalDirectory()); return false; } File root = new File(model.getLocalDirectory()); if (!root.exists()) { TabletopTool.showMessage("dialog.addresource.warn.filenotfound", "Error", JOptionPane.ERROR_MESSAGE, model.getLocalDirectory()); return false; } if (!root.isDirectory()) { TabletopTool.showMessage("dialog.addresource.warn.directoryrequired", "Error", JOptionPane.ERROR_MESSAGE, model.getLocalDirectory()); return false; } AppSetup.installLibrary(FileUtil.getNameWithoutExtension(root), root); return true; case WEB: if (StringUtils.isEmpty(model.getUrlName())) { TabletopTool.showMessage("dialog.addresource.warn.musthavename", "Error", JOptionPane.ERROR_MESSAGE, model.getLocalDirectory()); return false; } // validate the url format so that we don't hit it later try { new URL(model.getUrl()); } catch (MalformedURLException e) { TabletopTool.showMessage("dialog.addresource.warn.invalidurl", "Error", JOptionPane.ERROR_MESSAGE, model.getUrl()); return false; } rowList.add(new LibraryRow(model.getUrlName(), model.getUrl(), -1)); break; case TABLETOPTOOL_SITE: List<LibraryRow> selectedRows = getLibraryList().getSelectedValuesList(); if (selectedRows == null || selectedRows.isEmpty()) { TabletopTool.showMessage("dialog.addresource.warn.mustselectone", "Error", JOptionPane.ERROR_MESSAGE); return false; } for (LibraryRow row : selectedRows) { //validate the url format row.path = LIBRARY_URL + "/" + row.path; try { new URL(row.path); } catch (MalformedURLException e) { TabletopTool.showMessage("dialog.addresource.warn.invalidurl", "Error", JOptionPane.ERROR_MESSAGE, row.path); return false; } rowList.add(row); } break; } new SwingWorker<Object, Object>() { @Override protected Object doInBackground() throws Exception { for (LibraryRow row : rowList) { try { RemoteFileDownloader downloader = new RemoteFileDownloader(new URL(row.path)); File tmpFile = downloader.read(); AppSetup.installLibrary(row.name, tmpFile.toURL()); tmpFile.delete(); } catch (IOException e) { log.error("Error downloading library: " + e, e); TabletopTool.showInformation("dialog.addresource.warn.couldnotload"); } } return null; } }.execute(); return true; }
From source file:com.crosstreelabs.cognitio.common.ExtensionLoader.java
public void addJar(String path) throws MalformedURLException { File file; if (!path.endsWith(".jar")) { file = new File(path + ".jar"); } else {/*www.ja v a 2 s . c o m*/ file = new File(path); } if (!file.exists()) { LOGGER.warn("Could not find jar at {}", file); return; } if (!file.canRead()) { LOGGER.warn("Could not read jar at {}", file); return; } addURL(file.toURL()); }
From source file:org.apache.axis2.jaxws.description.impl.URIResolverImpl.java
/** * Gets input stream from the uri given. If we cannot find the stream, <code>null</code> is * returned.//from ww w.j a v a 2s . co m * * @param uri * @return */ protected InputStream getInputStreamForURI(String uri) { URL streamURL = null; InputStream is = null; URI pathURI = null; try { streamURL = new URL(uri); is = openStream_doPriv(streamURL); } catch (Throwable t) { //Exception handling not needed if (log.isDebugEnabled()) { log.debug("Exception occured in getInputStreamForURI, ignoring exception continuing processing: " + t.getMessage()); } } if (is == null) { try { pathURI = new URI(uri); streamURL = pathURI.toURL(); is = openStream_doPriv(streamURL); } catch (Throwable t) { //Exception handling not needed if (log.isDebugEnabled()) { log.debug( "Exception occured in getInputStreamForURI, ignoring exception continuing processing: " + t.getMessage()); } } } if (is == null) { try { final File file = new File(uri); streamURL = (URL) AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws MalformedURLException { return file.toURL(); } }); is = openStream_doPriv(streamURL); } catch (Throwable t) { //Exception handling not needed if (log.isDebugEnabled()) { log.debug( "Exception occured in getInputStreamForURI, ignoring exception continuing processing: " + t.getMessage()); } } } return is; }
From source file:net.rptools.maptool.client.ui.AddResourceDialog.java
@Override public boolean commit() { if (!super.commit()) { return false; }// ww w . ja v a2 s.c om // Add the resource final List<LibraryRow> rowList = new ArrayList<LibraryRow>(); switch (model.getTab()) { case LOCAL: if (StringUtils.isEmpty(model.getLocalDirectory())) { MapTool.showMessage("dialog.addresource.warn.filenotfound", "Error", JOptionPane.ERROR_MESSAGE, model.getLocalDirectory()); return false; } File root = new File(model.getLocalDirectory()); if (!root.exists()) { MapTool.showMessage("dialog.addresource.warn.filenotfound", "Error", JOptionPane.ERROR_MESSAGE, model.getLocalDirectory()); return false; } if (!root.isDirectory()) { MapTool.showMessage("dialog.addresource.warn.directoryrequired", "Error", JOptionPane.ERROR_MESSAGE, model.getLocalDirectory()); return false; } try { AppSetup.installLibrary(FileUtil.getNameWithoutExtension(root), root); } catch (MalformedURLException e) { log.error("Bad path url: " + root.getPath(), e); MapTool.showMessage("dialog.addresource.warn.badpath", "Error", JOptionPane.ERROR_MESSAGE, model.getLocalDirectory()); return false; } catch (IOException e) { log.error("IOException adding local root: " + root.getPath(), e); MapTool.showMessage("dialog.addresource.warn.badpath", "Error", JOptionPane.ERROR_MESSAGE, model.getLocalDirectory()); return false; } return true; case WEB: if (StringUtils.isEmpty(model.getUrlName())) { MapTool.showMessage("dialog.addresource.warn.musthavename", "Error", JOptionPane.ERROR_MESSAGE, model.getLocalDirectory()); return false; } // validate the url format so that we don't hit it later try { new URL(model.getUrl()); } catch (MalformedURLException e) { MapTool.showMessage("dialog.addresource.warn.invalidurl", "Error", JOptionPane.ERROR_MESSAGE, model.getUrl()); return false; } rowList.add(new LibraryRow(model.getUrlName(), model.getUrl(), -1)); break; case RPTOOLS: Object[] selectedRows = getLibraryList().getSelectedValues(); if (selectedRows == null || selectedRows.length == 0) { MapTool.showMessage("dialog.addresource.warn.mustselectone", "Error", JOptionPane.ERROR_MESSAGE); return false; } for (Object obj : selectedRows) { LibraryRow row = (LibraryRow) obj; //validate the url format row.path = LIBRARY_URL + "/" + row.path; try { new URL(row.path); } catch (MalformedURLException e) { MapTool.showMessage("dialog.addresource.warn.invalidurl", "Error", JOptionPane.ERROR_MESSAGE, row.path); return false; } rowList.add(row); } break; } new SwingWorker<Object, Object>() { @Override protected Object doInBackground() throws Exception { for (LibraryRow row : rowList) { try { RemoteFileDownloader downloader = new RemoteFileDownloader(new URL(row.path)); File tmpFile = downloader.read(); AppSetup.installLibrary(row.name, tmpFile.toURL()); tmpFile.delete(); } catch (IOException e) { log.error("Error downloading library: " + e, e); MapTool.showInformation("dialog.addresource.warn.couldnotload"); } } return null; } }.execute(); return true; }
From source file:org.dataconservancy.ui.dcpmap.DataFileMapperTest.java
private DataFile createDataFile(String dataFileBusinessId, File source) throws MalformedURLException { final DataFile df = new DataFile(); df.setName(source.getName());// w ww. j a va 2 s .c o m df.setPath(source.getPath()); df.setSize(source.length()); df.setSource(source.toURL().toExternalForm()); df.setId(dataFileBusinessId); return df; }
From source file:org.mule.module.launcher.DefaultMuleDeployer.java
public Application installFromAppDir(String packedMuleAppFileName) throws IOException { final File appsDir = MuleContainerBootstrapUtils.getMuleAppsDir(); File appFile = new File(appsDir, packedMuleAppFileName); // basic security measure: outside apps dir use installFrom(url) and go through any // restrictions applied to it if (!appFile.getParentFile().equals(appsDir)) { throw new SecurityException( "installFromAppDir() can only deploy from $MULE_HOME/apps. Use installFrom(url) instead."); }// w ww .j a va2 s . c om return installFrom(appFile.toURL()); }
From source file:org.basket3.filesystem.FileS3ObjectTest.java
/** * Test the content length property./*from ww w .j av a2 s .c om*/ */ public void test_contentLength() { File file; GAES3Object s3Object; try { file = File.createTempFile("unitTest", null); file.deleteOnExit(); FileOutputStream out = new FileOutputStream(file); out.write("12345".getBytes()); out.close(); s3Object = new GAES3Object("bucket", "key", file.toURL()); assertEquals("Unexpected value", 5, s3Object.getContentLength()); } catch (IOException e) { e.printStackTrace(); fail("Unexpected exception"); return; } }
From source file:org.basket3.filesystem.FileS3ObjectTest.java
/** * Test the last modified property./*from ww w . j a v a2 s . c om*/ */ public void test_lastModified() { File file; GAES3Object s3Object; try { file = File.createTempFile("unitTest", null); file.deleteOnExit(); FileOutputStream out = new FileOutputStream(file); out.write("12345".getBytes()); out.close(); s3Object = new GAES3Object("bucket", "key", file.toURL()); assertEquals("Unexpected value", file.lastModified(), s3Object.getLastModified()); } catch (IOException e) { e.printStackTrace(); fail("Unexpected exception"); return; } }