List of usage examples for java.net URL toURI
public URI toURI() throws URISyntaxException
From source file:de.unikassel.android.sdcframework.transmission.SimpleHttpProtocol.java
/** * Method for an HTTP upload of file stream * // w w w .j av a2s . c om * @param file * the input file * * @return true if successful, false otherwise */ private final boolean httpUpload(File file) { DefaultHttpClient client = new DefaultHttpClient(); try { String fileName = FileUtils.fileNameFromPath(file.getName()); String contentType = getContentType(fileName); URL url = getURL(); configureForAuthentication(client, url); HttpPost httpPost = new HttpPost(url.toURI()); FileEntity fileEntity = new FileEntity(file, contentType); fileEntity.setContentType(contentType); fileEntity.setChunked(true); httpPost.setEntity(fileEntity); httpPost.addHeader("filename", fileName); httpPost.addHeader("uuid", getUuid().toString()); HttpResponse response = client.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); boolean success = statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_NO_CONTENT; Logger.getInstance().debug(this, "Server returned: " + statusCode); // clean up if necessary HttpEntity resEntity = response.getEntity(); if (resEntity != null) { resEntity.consumeContent(); } if (!success) { doHandleError("Unexpected server response: " + response.getStatusLine()); success = false; } return success; } catch (ClientProtocolException e) { e.printStackTrace(); doHandleError(PROTOCOL_EXCEPTION + ": " + e.getMessage()); } catch (IOException e) { e.printStackTrace(); doHandleError(IO_EXCEPTION + ": " + e.getMessage()); } catch (URISyntaxException e) { setURL(null); doHandleError(INVALID_URL); } finally { client.getConnectionManager().shutdown(); } return false; }
From source file:com.heliosdecompiler.helios.controller.UpdateController.java
public File getHeliosLocation() { ProtectionDomain pd = getClass().getProtectionDomain(); if (pd != null) { CodeSource cs = pd.getCodeSource(); if (cs != null) { URL location = cs.getLocation(); if (location != null) { try { File file = new File(location.toURI().getPath()); if (file.isFile()) { return file; }//from w ww . java2 s . co m } catch (URISyntaxException e) { e.printStackTrace(); return null; } } } } return null; }
From source file:com.omertron.omdbapi.OmdbApi.java
private String requestWebPage(URL url) throws OMDBException { LOG.trace("Requesting: {}", url.toString()); try {//w w w . j a v a 2s . com final HttpGet httpGet = new HttpGet(url.toURI()); httpGet.addHeader("accept", "application/json"); httpGet.addHeader(HTTP.USER_AGENT, UserAgentSelector.randomUserAgent()); final DigestedResponse response = DigestedResponseReader.requestContent(httpClient, httpGet, charset); if (response.getStatusCode() >= HTTP_STATUS_500) { throw new OMDBException(ApiExceptionType.HTTP_503_ERROR, response.getContent(), response.getStatusCode(), url); } else if (response.getStatusCode() >= HTTP_STATUS_300) { throw new OMDBException(ApiExceptionType.HTTP_404_ERROR, response.getContent(), response.getStatusCode(), url); } return response.getContent(); } catch (URISyntaxException ex) { throw new OMDBException(ApiExceptionType.INVALID_URL, "Invalid URL", url, ex); } catch (IOException ex) { throw new OMDBException(ApiExceptionType.CONNECTION_ERROR, "Error retrieving URL", url, ex); } }
From source file:com.cognifide.qa.bb.config.YamlConfig.java
private Map<String, Map<String, String>> readAdditionalContexts() { Map<String, Map<String, String>> additionalContexts = new HashMap<>(); URL contextsResource = getClass().getResource(ADDITIONAL_CONTEXTS_FOLDER); if (contextsResource != null) { try (Stream<Path> files = Files.walk(Paths.get(contextsResource.toURI()))) { additionalContexts.putAll(// ww w . j a v a 2 s. c o m files.filter(Files::isRegularFile).filter(path -> path.toString().endsWith(YamlReader.YAML)) .map(this::getContextsFromYaml).map(Map::entrySet).flatMap(Collection::stream) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); } catch (IOException e) { LOG.error("Could not read the additional contexts", e); } catch (URISyntaxException e) { LOG.error("Could not parse the additional contexts folder path", e); } } return additionalContexts; }
From source file:io.cloudslang.lang.compiler.CompileDecisionTest.java
@Test public void testDecision1() throws Exception { URL decision = getClass().getResource("/decision/decision_1.sl"); CompilationArtifact compilationArtifact = compiler.compile(fromFile(decision.toURI()), emptySetSlangSource); validateCompilationArtifact(compilationArtifact, inputs1, outputs1, results1, emptySetSystemProperties); }
From source file:de.smartics.maven.plugin.jboss.modules.util.classpath.ClassPathDirectoryListing.java
private List<String> handleFile(final String resourcePath, final URL resourcePathUrl) throws IllegalArgumentException { try {/*from w ww .j a v a 2 s. co m*/ final String[] contentsArray = new File(resourcePathUrl.toURI()).list(); return Arrays.asList(contentsArray); } catch (final URISyntaxException e) { throw new IllegalArgumentException( "Cannot read URL derived from resource '" + resourcePath + "' on the class path.", e); } }
From source file:edu.kit.dama.ui.repo.staging.DataZipCreator.java
@Override public void performPostTransferProcessing(TransferTaskContainer pContainer) throws StagingProcessorException { LOGGER.debug("Zipping data"); URL generatedFolder = pContainer.getGeneratedUrl(); LOGGER.debug("Using target folder: {}", generatedFolder); try {// w ww. j a va2 s. co m String zipFileName = CryptUtil.stringToSHA1(pContainer.getTransferInformation().getDigitalObjectId()) + ".zip"; URL targetZip = URLCreator.appendToURL(generatedFolder, zipFileName); LOGGER.debug("Zipping all data to file {}", targetZip); File targetFile = new File(targetZip.toURI()); ITransferInformation info = pContainer.getTransferInformation(); LOGGER.debug("Obtaining local folder for transfer with id {}", info.getTransferId()); File localFolder = StagingService.getSingleton().getLocalStagingFolder(info, StagingService.getSingleton().getContext(info)); File dataFolder = new File( FilenameUtils.concat(localFolder.getAbsolutePath(), Constants.STAGING_DATA_FOLDER_NAME)); if (!dataFolder.exists()) { throw new IOException( "Data folder " + dataFolder.getAbsolutePath() + " does not exist. Aborting zip operation."); } LOGGER.debug("Start zip operation using data input folder URL {}", dataFolder); ZipUtils.zip(new File(dataFolder.toURI()), targetFile); LOGGER.debug("Adding zip file {} to container.", targetFile); pContainer.addGeneratedFile(targetFile); LOGGER.debug("Zip operation successfully finished."); } catch (IOException | URISyntaxException ex) { throw new StagingProcessorException("Failed to zip data", ex); } }
From source file:io.cloudslang.lang.compiler.CompileDecisionTest.java
@Test public void testDecision2() throws Exception { URL decision = getClass().getResource("/decision/decision_2.sl"); CompilationArtifact compilationArtifact = compiler.compile(fromFile(decision.toURI()), emptySetSlangSource); validateCompilationArtifact(compilationArtifact, inputs2, emptyListOutputs, results1, emptySetSystemProperties);/*from ww w.j a v a 2 s . co m*/ }
From source file:com.vaadin.sass.testcases.scss.AbstractDirectoryScanningSassTests.java
private File getSassLangResourceFile(String resourceName) throws IOException, URISyntaxException { String base = "/scss/"; String fullResourceName = base + resourceName; URL res = getResourceURL(fullResourceName); if (res == null) { throw new FileNotFoundException( "Resource " + resourceName + " not found (tried " + fullResourceName + ")"); }/*w w w. ja v a2 s.co m*/ return new File(res.toURI()); }
From source file:com.arangodb.example.ssl.SslExample.java
@Test public void sslPeerUnverifiedExceptionTest() throws ArangoException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, URISyntaxException { // create a sslContext for the self signed certificate URL resource = this.getClass().getResource(SSL_TRUSTSTORE); SSLContext sslContext = SSLContexts.custom() .loadTrustMaterial(Paths.get(resource.toURI()).toFile(), SSL_TRUSTSTORE_PASSWORD.toCharArray()) .build();// w w w .jav a 2 s . com ArangoConfigure configuration = new ArangoConfigure("/ssl-arangodb.properties"); // 127.0.0.1 is the wrong name configuration.getArangoHost().setHost("127.0.0.1"); configuration.setSslContext(sslContext); configuration.init(); ArangoDriver arangoDriver = new ArangoDriver(configuration); try { arangoDriver.getVersion(); Assert.fail("this should fail"); } catch (ArangoException e) { Throwable cause = e.getCause(); Assert.assertTrue(cause instanceof javax.net.ssl.SSLPeerUnverifiedException); } }