Example usage for java.net URL getProtocol

List of usage examples for java.net URL getProtocol

Introduction

In this page you can find the example usage for java.net URL getProtocol.

Prototype

public String getProtocol() 

Source Link

Document

Gets the protocol name of this URL .

Usage

From source file:de.tudarmstadt.ukp.dkpro.core.api.resources.ResourceUtils.java

/**
 * Make the given URL available as a file. A temporary file is created and deleted upon a
 * regular shutdown of the JVM. If the parameter {@code aCache} is {@code true}, the temporary
 * file is remembered in a cache and if a file is requested for the same URL at a later time,
 * the same file is returned again. If the previously created file has been deleted meanwhile,
 * it is recreated from the URL. This method should not be used for creating executable
 * binaries. For this purpose, getUrlAsExecutable should be used.
 *
 * @param aUrl//www . jav a2  s. c  om
 *            the URL.
 * @param aCache
 *            use the cache or not.
 * @param aForceTemp
 *            always create a temporary file, even if the URL is already a file.
 * @return a file created from the given URL.
 * @throws IOException
 *             if the URL cannot be accessed to (re)create the file.
 */
public static synchronized File getUrlAsFile(URL aUrl, boolean aCache, boolean aForceTemp) throws IOException {
    // If the URL already points to a file, there is not really much to do.
    if (!aForceTemp && "file".equalsIgnoreCase(aUrl.getProtocol())) {
        try {
            return new File(aUrl.toURI());
        } catch (URISyntaxException e) {
            throw new IOException(e);
        }
    }

    synchronized (urlFileCache) {
        // Lets see if we already have a file for this URL in our cache. Maybe
        // the file has been deleted meanwhile, so we also check if the file
        // actually still exists on disk.
        File file = urlFileCache.get(aUrl.toString());
        if (!aCache || (file == null) || !file.exists()) {
            // Create a temporary file and try to preserve the file extension
            String suffix = FilenameUtils.getExtension(aUrl.getPath());
            if (suffix.length() == 0) {
                suffix = "temp";
            }
            String name = FilenameUtils.getBaseName(aUrl.getPath());

            // Get a temporary file which will be deleted when the JVM shuts
            // down.
            file = File.createTempFile(name, "." + suffix);
            file.deleteOnExit();

            // Now copy the file from the URL to the file.

            InputStream is = null;
            OutputStream os = null;
            try {
                is = aUrl.openStream();
                os = new FileOutputStream(file);
                copy(is, os);
            } finally {
                closeQuietly(is);
                closeQuietly(os);
            }

            // Remember the file
            if (aCache) {
                urlFileCache.put(aUrl.toString(), file);
            }
        }

        return file;
    }
}

From source file:org.hawkular.wildfly.agent.itest.util.AbstractITest.java

public static void writeNode(Class<?> caller, ModelNode node, String nodeFileName)
        throws UnsupportedEncodingException, FileNotFoundException {
    URL callerUrl = caller.getResource(caller.getSimpleName() + ".class");
    if (!callerUrl.getProtocol().equals("file")) {
        throw new IllegalStateException(AbstractITest.class.getName()
                + ".store() works only if the caller's class file is loaded using a file:// URL.");
    }/*  w  w  w. jav  a 2s.  c o m*/
    String callerUrlPath = callerUrl.getPath();

    String nodePath = callerUrlPath.replaceAll("\\.class$", "." + nodeFileName);
    nodePath = nodePath.replace("/target/test-classes/", "/src/test/resources/");
    System.out.println("Storing a node to [" + nodePath + "]");

    File outputFile = new File(nodePath);
    if (!outputFile.getParentFile().exists()) {
        outputFile.getParentFile().mkdirs();
    }

    try (PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "utf-8"))) {
        node.writeString(out, false);
    }
}

From source file:edu.du.penrose.systems.fedoraApp.util.FedoraAppUtil.java

/**
 * Returns strings of the type 'institution'_'batchSet' ie 'codu_ectd' to show the institution and batch set for batch sets 
 * that have been configured for background tasks ingest's, with a institution_batchSet_TASK.properties file, they may or not be 
 * enabled in the taskEnable.properties file.
 * // www.  j a va2  s . c  om
 * @return string of type 'codu_ectd'
 * @throws Exception
 */
public static Set<String> getTaskConfiguredInstitutions() throws Exception {
    Set<String> taskEnabledInstutions = new HashSet<String>();

    File institutionDirectory = null;
    String institutionDirectoryPath = ProgramProperties
            .getInstance(FedoraAppConstants.getServletContextListener().getProgramPropertiesURL())
            .getProperty(FedoraAppConstants.BATCH_INGEST_TOP_FOLDER_URL_PROPERTY);
    URL institutionURL = new URL(institutionDirectoryPath);
    if (institutionURL.getProtocol().toLowerCase().equals("file")) { // TBD only file protocol currently supported.

        institutionDirectory = new File(institutionURL.getFile());
    } else {
        throw new FatalException("Unsupported Protocol for top batch ingest folder (institution directory");
    }

    File[] dirNames = institutionDirectory.listFiles();
    for (int i = 0; i < dirNames.length; i++) {
        if (dirNames[i].isDirectory()) {
            File dirTemp = dirNames[i];
            File[] batchSetList = dirTemp.listFiles();
            for (int j = 0; j < batchSetList.length; j++) {
                if (batchSetList[j].isDirectory()) {
                    File configFile = new File(batchSetList[j] + "/" + batchSetList[j].getName()
                            + FedoraAppConstants.BACKGROUND_TASK_NAME_SUFFIX + ".properties");
                    if (configFile.exists()) {
                        // this will create a worker name of type 'codu_ectd_REMOTE'
                        String workerName = dirNames[i].getName() + "_" + batchSetList[j].getName()
                                + FedoraAppConstants.BACKGROUND_TASK_NAME_SUFFIX;
                        taskEnabledInstutions.add(workerName);
                    }
                }
            }
        }
    }

    return taskEnabledInstutions;
}

From source file:com.emcopentechnologies.viprcloudstorage.WAStorageClient.java

/**
 * Generates SAS URL for blob in Cloud storage account
 * @param storageAccountName/*from w  w w.  ja  va 2s .c  o m*/
 * @param storageAccountKey
 * @param containerName
 * @param strBlobURL
 * @return SAS URL
 * @throws Exception
 */
public static String generateSASURL(String storageAccountName, String storageAccountKey, String containerName,
        String saBlobEndPoint) throws Exception {
    StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey(storageAccountName,
            storageAccountKey);
    URL blobURL = new URL(saBlobEndPoint);
    String saBlobURI = new StringBuilder().append(blobURL.getProtocol()).append("://")
            .append(storageAccountName).append(".").append(blobURL.getHost()).append("/").toString();
    CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(credentials, new URI(saBlobURI),
            new URI(getCustomURI(storageAccountName, QUEUE, saBlobURI)),
            new URI(getCustomURI(storageAccountName, TABLE, saBlobURI)));
    // Create the blob client.
    CloudBlobClient blobClient = cloudStorageAccount.createCloudBlobClient();
    CloudBlobContainer container = blobClient.getContainerReference(containerName);

    // At this point need to throw an error back since container itself did not exist.
    if (!container.exists()) {
        throw new Exception("WAStorageClient: generateSASURL: Container " + containerName
                + " does not exist in storage account " + storageAccountName);
    }

    SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy();
    GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    calendar.setTime(new Date());

    //policy.setSharedAccessStartTime(calendar.getTime());
    calendar.add(Calendar.HOUR, 1);
    policy.setSharedAccessExpiryTime(calendar.getTime());
    policy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ));

    BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
    containerPermissions.getSharedAccessPolicies().put("jenkins" + System.currentTimeMillis(), policy);
    container.uploadPermissions(containerPermissions);

    // Create a shared access signature for the container.
    String sas = container.generateSharedAccessSignature(policy, null);

    return sas;
}

From source file:edu.du.penrose.systems.fedoraApp.util.FedoraAppUtil.java

/**
 * Returns strings of the type 'institution'_'batchSet' ie 'codu_ectd' to show the institution and batch set for batch sets 
 * that have been configured for remote ingest's, with a institution_batchSet_REMOTE.properties file, they may or not be 
 * enabled in the taskEnable.properties file.
 * //from   www  .  j a va2 s.com
 * @return string of type 'codu_ectd'
 * @throws Exception
 */
public static Set<String> getRemoteConfiguredInstitutions() throws Exception {
    Set<String> remoteEnabledInstutions = new HashSet<String>();

    File institutionDirectory = null;
    String institutionDirectoryPath = ProgramProperties
            .getInstance(FedoraAppConstants.getServletContextListener().getProgramPropertiesURL())
            .getProperty(FedoraAppConstants.BATCH_INGEST_TOP_FOLDER_URL_PROPERTY);
    //   logger.info( "topInstitutionPath=" + institutionDirectoryPath );

    URL institutionURL = new URL(institutionDirectoryPath);
    if (institutionURL.getProtocol().toLowerCase().equals("file")) { // TBD only file protocol currently supported.

        institutionDirectory = new File(institutionURL.getFile());
    } else {
        throw new FatalException("Unsupported Protocol for top batch ingest folder (institution directory");
    }

    File[] dirNames = institutionDirectory.listFiles();
    for (int i = 0; i < dirNames.length; i++) {
        //   logger.info( "processFileOrDirectory=" + dirNames[i] );
        if (dirNames[i].isDirectory()) {
            //   logger.info( "processDirectory=" + dirNames[i] );
            File dirTemp = dirNames[i];
            File[] batchSetList = dirTemp.listFiles();
            if (batchSetList == null) {
                continue;
            }
            for (int j = 0; j < batchSetList.length; j++) {
                //   logger.info( "processFileOrDirectory=" + batchSetList[j] );
                if (batchSetList[j].isDirectory()) {
                    //      logger.info( "processDirectory=" + batchSetList[j] );
                    File configFile = new File(batchSetList[j] + "/" + batchSetList[j].getName()
                            + FedoraAppConstants.REMOTE_TASK_NAME_SUFFIX + ".properties");
                    if (configFile.exists()) {
                        //      logger.info( "Add worker for "+configFile.getName() );
                        // this will create a worker name of type 'codu_ectd_REMOTE'
                        String workerName = dirNames[i].getName() + "_" + batchSetList[j].getName()
                                + FedoraAppConstants.REMOTE_TASK_NAME_SUFFIX;
                        remoteEnabledInstutions.add(workerName);
                    }
                }
            }
        }
    }

    return remoteEnabledInstutions;
}

From source file:com.mber.client.MberClient.java

public static String baseUrlWithPath(final String url, String path) throws MalformedURLException {
    if (!path.endsWith("/")) {
        path += "/";
    }/*from   www.j a  va  2  s .  c o m*/
    URL base = new URL(url);
    URL baseUrl = new URL(base.getProtocol() + "://" + base.getAuthority());
    URL resolvedUrl = new URL(baseUrl, path);
    return resolvedUrl.toString();
}

From source file:org.apache.metron.dataloads.taxii.TaxiiHandler.java

private static HttpClient buildClient(URL proxy, String username, String password) throws Exception {
    HttpClient client = new HttpClient(); // Start with a default TAXII HTTP client.

    // Create an Apache HttpClientBuilder to be customized by the command line arguments.
    HttpClientBuilder builder = HttpClientBuilder.create().useSystemProperties();

    // Proxy/*from w ww  .j  a  va2 s  .  c  o  m*/
    if (proxy != null) {
        HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getProtocol());
        builder.setProxy(proxyHost);
    }

    // Basic authentication. User & Password
    if (username != null ^ password != null) {
        throw new Exception("'username' and 'password' arguments are required to appear together.");
    }

    // from:  http://stackoverflow.com/questions/19517538/ignoring-ssl-certificate-in-apache-httpclient-4-3
    SSLContextBuilder ssbldr = new SSLContextBuilder();
    ssbldr.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(ssbldr.build(),
            SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", new PlainConnectionSocketFactory()).register("https", sslsf).build();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
    cm.setMaxTotal(20);//max connection

    System.setProperty("jsse.enableSNIExtension", "false"); //""
    CloseableHttpClient httpClient = builder.setSSLSocketFactory(sslsf).setConnectionManager(cm).build();

    client.setHttpclient(httpClient);
    return client;
}

From source file:com.eucalyptus.imaging.manifest.DownloadManifestFactory.java

/**
 * Generates download manifest based on bundle manifest and puts in into
 * system owned bucket/*from w w  w  . j  a v  a 2 s .  c  o m*/
 * 
 * @param baseManifest
 *          the base manifest
 * @param keyToUse
 *          public key that used for encryption
 * @param manifestName
 *          name for generated manifest file
 * @param expirationHours
 *          expiration policy in hours for pre-signed URLs
 * @param urlForNc
 *          indicates if urs are constructed for NC use
 * @return pre-signed URL that can be used to download generated manifest
 * @throws DownloadManifestException
 */
public static String generateDownloadManifest(final ImageManifestFile baseManifest, final PublicKey keyToUse,
        final String manifestName, int expirationHours, boolean urlForNc) throws DownloadManifestException {
    EucaS3Client s3Client = null;
    try {
        try {
            s3Client = s3ClientsPool.borrowObject();
        } catch (Exception ex) {
            throw new DownloadManifestException("Can't borrow s3Client from the pool");
        }
        // prepare to do pre-signed urls
        if (!urlForNc)
            s3Client.refreshEndpoint(true);
        else
            s3Client.refreshEndpoint();

        Date expiration = new Date();
        long msec = expiration.getTime() + 1000 * 60 * 60 * expirationHours;
        expiration.setTime(msec);

        // check if download-manifest already exists
        if (objectExist(s3Client, DOWNLOAD_MANIFEST_BUCKET_NAME, DOWNLOAD_MANIFEST_PREFIX + manifestName)) {
            LOG.debug("Manifest '" + (DOWNLOAD_MANIFEST_PREFIX + manifestName)
                    + "' is already created and has not expired. Skipping creation");
            URL s = s3Client.generatePresignedUrl(DOWNLOAD_MANIFEST_BUCKET_NAME,
                    DOWNLOAD_MANIFEST_PREFIX + manifestName, expiration, HttpMethod.GET);
            return String.format("%s://imaging@%s%s?%s", s.getProtocol(), s.getAuthority(), s.getPath(),
                    s.getQuery());
        } else {
            LOG.debug("Manifest '" + (DOWNLOAD_MANIFEST_PREFIX + manifestName) + "' does not exist");
        }

        UrlValidator urlValidator = new UrlValidator();

        final String manifest = baseManifest.getManifest();
        if (manifest == null) {
            throw new DownloadManifestException("Can't generate download manifest from null base manifest");
        }
        final Document inputSource;
        final XPath xpath;
        Function<String, String> xpathHelper;
        DocumentBuilder builder = XMLParser.getDocBuilder();
        inputSource = builder.parse(new ByteArrayInputStream(manifest.getBytes()));
        if (!"manifest".equals(inputSource.getDocumentElement().getNodeName())) {
            LOG.error("Expected image manifest. Got " + nodeToString(inputSource, false));
            throw new InvalidBaseManifestException("Base manifest does not have manifest element");
        }

        StringBuilder signatureSrc = new StringBuilder();
        Document manifestDoc = builder.newDocument();
        Element root = (Element) manifestDoc.createElement("manifest");
        manifestDoc.appendChild(root);
        Element el = manifestDoc.createElement("version");
        el.appendChild(manifestDoc.createTextNode("2014-01-14"));
        signatureSrc.append(nodeToString(el, false));
        root.appendChild(el);
        el = manifestDoc.createElement("file-format");
        el.appendChild(manifestDoc.createTextNode(baseManifest.getManifestType().getFileType().toString()));
        root.appendChild(el);
        signatureSrc.append(nodeToString(el, false));

        xpath = XPathFactory.newInstance().newXPath();
        xpathHelper = new Function<String, String>() {
            @Override
            public String apply(String input) {
                try {
                    return (String) xpath.evaluate(input, inputSource, XPathConstants.STRING);
                } catch (XPathExpressionException ex) {
                    return null;
                }
            }
        };

        // extract keys
        // TODO: move this?
        if (baseManifest.getManifestType().getFileType() == FileType.BUNDLE) {
            String encryptedKey = xpathHelper.apply("/manifest/image/ec2_encrypted_key");
            String encryptedIV = xpathHelper.apply("/manifest/image/ec2_encrypted_iv");
            String size = xpathHelper.apply("/manifest/image/size");
            EncryptedKey encryptKey = reEncryptKey(new EncryptedKey(encryptedKey, encryptedIV), keyToUse);
            el = manifestDoc.createElement("bundle");
            Element key = manifestDoc.createElement("encrypted-key");
            key.appendChild(manifestDoc.createTextNode(encryptKey.getKey()));
            Element iv = manifestDoc.createElement("encrypted-iv");
            iv.appendChild(manifestDoc.createTextNode(encryptKey.getIV()));
            el.appendChild(key);
            el.appendChild(iv);
            Element sizeEl = manifestDoc.createElement("unbundled-size");
            sizeEl.appendChild(manifestDoc.createTextNode(size));
            el.appendChild(sizeEl);
            root.appendChild(el);
            signatureSrc.append(nodeToString(el, false));
        }

        el = manifestDoc.createElement("image");
        String bundleSize = xpathHelper.apply(baseManifest.getManifestType().getSizePath());
        if (bundleSize == null) {
            throw new InvalidBaseManifestException("Base manifest does not have size element");
        }
        Element size = manifestDoc.createElement("size");
        size.appendChild(manifestDoc.createTextNode(bundleSize));
        el.appendChild(size);

        Element partsEl = manifestDoc.createElement("parts");
        el.appendChild(partsEl);
        // parts
        NodeList parts = (NodeList) xpath.evaluate(baseManifest.getManifestType().getPartsPath(), inputSource,
                XPathConstants.NODESET);
        if (parts == null) {
            throw new InvalidBaseManifestException("Base manifest does not have parts");
        }

        for (int i = 0; i < parts.getLength(); i++) {
            Node part = parts.item(i);
            String partIndex = part.getAttributes().getNamedItem("index").getNodeValue();
            String partKey = ((Node) xpath.evaluate(baseManifest.getManifestType().getPartUrlElement(), part,
                    XPathConstants.NODE)).getTextContent();
            String partDownloadUrl = partKey;
            if (baseManifest.getManifestType().signPartUrl()) {
                GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(
                        baseManifest.getBaseBucket(), partKey, HttpMethod.GET);
                generatePresignedUrlRequest.setExpiration(expiration);
                URL s = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
                partDownloadUrl = s.toString();
            } else {
                // validate url per EUCA-9144
                if (!urlValidator.isEucalyptusUrl(partDownloadUrl))
                    throw new DownloadManifestException(
                            "Some parts in the manifest are not stored in the OS. Its location is outside Eucalyptus:"
                                    + partDownloadUrl);
            }
            Node digestNode = null;
            if (baseManifest.getManifestType().getDigestElement() != null)
                digestNode = ((Node) xpath.evaluate(baseManifest.getManifestType().getDigestElement(), part,
                        XPathConstants.NODE));
            Element aPart = manifestDoc.createElement("part");
            Element getUrl = manifestDoc.createElement("get-url");
            getUrl.appendChild(manifestDoc.createTextNode(partDownloadUrl));
            aPart.setAttribute("index", partIndex);
            aPart.appendChild(getUrl);
            if (digestNode != null) {
                NamedNodeMap nm = digestNode.getAttributes();
                if (nm == null)
                    throw new DownloadManifestException(
                            "Some parts in manifest don't have digest's verification algorithm");
                Element digest = manifestDoc.createElement("digest");
                digest.setAttribute("algorithm", nm.getNamedItem("algorithm").getTextContent());
                digest.appendChild(manifestDoc.createTextNode(digestNode.getTextContent()));
                aPart.appendChild(digest);
            }
            partsEl.appendChild(aPart);
        }
        root.appendChild(el);
        signatureSrc.append(nodeToString(el, false));
        String signatureData = signatureSrc.toString();
        Element signature = manifestDoc.createElement("signature");
        signature.setAttribute("algorithm", "RSA-SHA256");
        signature.appendChild(manifestDoc
                .createTextNode(Signatures.SHA256withRSA.trySign(Eucalyptus.class, signatureData.getBytes())));
        root.appendChild(signature);
        String downloadManifest = nodeToString(manifestDoc, true);
        // TODO: move this ?
        createManifestsBucketIfNeeded(s3Client);
        putManifestData(s3Client, DOWNLOAD_MANIFEST_BUCKET_NAME, DOWNLOAD_MANIFEST_PREFIX + manifestName,
                downloadManifest, expiration);
        // generate pre-sign url for download manifest
        URL s = s3Client.generatePresignedUrl(DOWNLOAD_MANIFEST_BUCKET_NAME,
                DOWNLOAD_MANIFEST_PREFIX + manifestName, expiration, HttpMethod.GET);
        return String.format("%s://imaging@%s%s?%s", s.getProtocol(), s.getAuthority(), s.getPath(),
                s.getQuery());
    } catch (Exception ex) {
        LOG.error("Got an error", ex);
        throw new DownloadManifestException("Can't generate download manifest");
    } finally {
        if (s3Client != null)
            try {
                s3ClientsPool.returnObject(s3Client);
            } catch (Exception e) {
                // sad, but let's not break instances run
                LOG.warn("Could not return s3Client to the pool");
            }
    }
}

From source file:com.nary.io.FileUtils.java

/**
 * This only works on WLS 9.x, which unpacks WARs to a temp directory, but still returns null from context.getRealPath() and request.getRealPath(). This method returns null if the resource does not exist, unlike context.getRealPath() and request.getRealPath()
 *///  w  ww. j  a va 2 s .c  o  m
private static String getWebLogicRealPath(String path) {
    try {
        java.net.URL url = cfEngine.thisServletContext.getResource(path);
        if ((url != null) && (url.getProtocol().equalsIgnoreCase("file"))) {
            return url.getPath();
        }
    } catch (java.net.MalformedURLException ignore) {
    }
    return null;
}

From source file:com.cloud.utils.SwiftUtil.java

public static URL generateTempUrl(SwiftClientCfg cfg, String container, String object, String tempKey,
        int urlExpirationInterval) {

    int currentTime = (int) (System.currentTimeMillis() / 1000L);
    int expirationSeconds = currentTime + urlExpirationInterval;

    try {/*from   ww  w.  j  ava  2 s  . c  o  m*/

        URL endpoint = new URL(cfg.getEndPoint());
        String method = "GET";
        String path = String.format("/v1/AUTH_%s/%s/%s", cfg.getAccount(), container, object);

        //sign the request
        String hmacBody = String.format("%s\n%d\n%s", method, expirationSeconds, path);
        String signature = calculateRFC2104HMAC(hmacBody, tempKey);
        path += String.format("?temp_url_sig=%s&temp_url_expires=%d", signature, expirationSeconds);

        //generate the temp url
        URL tempUrl = new URL(endpoint.getProtocol(), endpoint.getHost(), endpoint.getPort(), path);

        return tempUrl;

    } catch (Exception e) {
        logger.error(e.getMessage());
        throw new CloudRuntimeException(e.getMessage());
    }

}