Example usage for java.net URL toURI

List of usage examples for java.net URL toURI

Introduction

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

Prototype

public URI toURI() throws URISyntaxException 

Source Link

Document

Returns a java.net.URI equivalent to this URL.

Usage

From source file:it.cloudicaro.disit.kb.rdf.HttpUtil.java

public static String post(URL url, String data, String contentType, String user, String passwd)
        throws Exception {
    //System.out.println("POST "+url);
    HttpClient client = HttpClients.createDefault();
    HttpPost request = new HttpPost(url.toURI());
    request.setEntity(new StringEntity(data, ContentType.create(contentType, "UTF-8")));

    HttpClientContext context = HttpClientContext.create();
    if (user != null && passwd != null) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, passwd));
        context.setCredentialsProvider(credsProvider);
    }//from  www .j a  v a  2s. co  m

    HttpResponse response = client.execute(request, context);

    StatusLine s = response.getStatusLine();
    int code = s.getStatusCode();
    //System.out.println(code);
    if (code == 204)
        return "";
    if (code != 200)
        throw new Exception(
                "failed access to " + url.toString() + " code: " + code + " " + s.getReasonPhrase());

    Reader reader = null;
    try {
        reader = new InputStreamReader(response.getEntity().getContent());

        StringBuilder sb = new StringBuilder();
        {
            int read;
            char[] cbuf = new char[1024];
            while ((read = reader.read(cbuf)) != -1) {
                sb.append(cbuf, 0, read);
            }
        }

        return sb.toString();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.clank.launcher.swing.SwingHelper.java

/**
 * Opens a system web browser for the given URL.
 *
 * @param url the URL/*from   www. j av a2 s .  c o  m*/
 * @param parentComponent the component from which to show any errors
 */
public static void openURL(URL url, Component parentComponent) {
    try {
        Desktop.getDesktop().browse(url.toURI());
    } catch (IOException e) {
        showErrorDialog(parentComponent, _("errors.openUrlError", url.toString()), _("errorTitle"));
    } catch (URISyntaxException e) {
    }
}

From source file:it.govpay.web.console.utils.HttpClientUtils.java

public static HttpResponse sendRichiestaPagamento(String urlToInvoke, RichiestaPagamento richiestaPagamento,
        Logger log) throws Exception {
    HttpResponse responseGET = null;//from w  w  w  . j  av a 2 s. c  o  m
    try {
        log.debug("Invio del pagamento in corso...");

        URL urlObj = new URL(urlToInvoke);
        HttpHost target = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());
        CloseableHttpClient client = HttpClientBuilder.create().disableRedirectHandling().build();
        HttpPost richiestaPost = new HttpPost();

        richiestaPost.setURI(urlObj.toURI());

        log.debug("Serializzazione pagamento in corso...");
        byte[] bufferPagamento = JaxbUtils.toBytes(richiestaPagamento);
        log.debug("Serializzazione pagamento completata.");

        HttpEntity bodyEntity = new InputStreamEntity(new ByteArrayInputStream(bufferPagamento),
                ContentType.APPLICATION_XML);
        richiestaPost.setEntity(bodyEntity);
        richiestaPost.setHeader("Content-Type", ContentType.APPLICATION_XML.getMimeType());

        log.debug("Invio tramite client Http in corso...");
        responseGET = client.execute(target, richiestaPost);

        if (responseGET == null)
            throw new NullPointerException("La Response HTTP e' null");

        log.debug("Invio tramite client Http completato.");
        return responseGET;
    } catch (Exception e) {
        log.error("Errore durante l'invio della richiesta di pagamento: " + e.getMessage(), e);
        throw e;
    }
}

From source file:no.norrs.projects.andronary.service.utils.HttpUtil.java

public static HttpResponse GET(URL url, UsernamePasswordCredentials creds)
        throws IOException, URISyntaxException {

    DefaultHttpClient httpClient = new DefaultHttpClient();

    httpClient.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), url.getPort()), creds);

    HttpGet httpGet = new HttpGet(url.toURI());

    httpGet.addHeader("Accept", "application/json");
    httpGet.addHeader("User-Agent", "Andronary/0.1");
    HttpResponse response;/*  w w w  .j  a  v a  2s.  co  m*/

    return httpClient.execute(httpGet);
}

From source file:it.geosolutions.geostore.core.security.password.URLMasterPasswordProvider.java

/**
 * Writes the master password in the file
 * @param url/*from  ww  w . jav a2 s . c o  m*/
 * @param configDir
 * @return
 * @throws IOException
 */
static OutputStream output(URL url, File configDir) throws IOException {
    //check for file URL
    if ("file".equalsIgnoreCase(url.getProtocol())) {
        File f;
        try {
            f = new File(url.toURI());
        } catch (URISyntaxException e) {
            f = new File(url.getPath());
        }
        if (!f.isAbsolute()) {
            //make relative to config dir
            f = new File(configDir, f.getPath());
        }
        return new FileOutputStream(f);
    } else {
        URLConnection cx = url.openConnection();
        cx.setDoOutput(true);
        return cx.getOutputStream();
    }
}

From source file:org.apache.hadoop.gateway.GatewayLdapDynamicGroupFuncTest.java

public static int setupLdap() throws Exception {
    URL usersUrl = getResourceUrl("users.ldif");
    ldapTransport = new TcpTransport(0);
    ldap = new SimpleLdapDirectoryServer("dc=hadoop,dc=apache,dc=org", new File(usersUrl.toURI()),
            ldapTransport);//from  w  w w  .  j  a  v  a2  s .com
    ldap.start();
    LOG.info("LDAP port = " + ldapTransport.getAcceptor().getLocalAddress().getPort());
    return ldapTransport.getAcceptor().getLocalAddress().getPort();
}

From source file:com.adobe.aem.demomachine.gui.AemDemoUtils.java

public static void openWebpage(URL url) {
    try {//from w  w  w .j a va2  s .c o m
        openWebpage(url.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

From source file:edu.internet2.middleware.openid.message.encoding.EncodingUtils.java

/**
 * Append the URL encoded OpenID message parameters to the query string of the provided URL.
 * /*from  w ww.j  a  v  a2s.  co  m*/
 * @param url URL to append OpenID message parameter to
 * @param message URL encoded OpenID message parameters
 * @return URL with message parameters appended
 */
public static URL appendMessageParameters(URL url, String message) {
    try {
        return appendMessageParameters(url.toURI(), message).toURL();
    } catch (MalformedURLException e) {
        log.error("Unable to append message parameters to URL: {}", e);
    } catch (URISyntaxException e) {
        log.error("Unable to append message parameters to URL: {}", e);
    }

    return null;
}

From source file:no.kantega.commons.util.XMLHelper.java

public static Document openDocument(URL url) throws SystemException {
    Document doc = null;//from   w  w w. java2  s. co  m
    CloseableHttpClient httpClient = getHttpClient();
    try (CloseableHttpResponse execute = httpClient.execute(new HttpGet(url.toURI()))) {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = docFactory.newDocumentBuilder();

        doc = builder.parse(execute.getEntity().getContent());
    } catch (Exception e) {
        log.error("Error opening XML document from URL", e);
        throw new SystemException("Error opening XML document from URL", e);
    }

    return doc;
}

From source file:com.qualogy.qafe.web.ContextLoader.java

public static String getContextPath(ServletContext servletContext)
        throws MalformedURLException, URISyntaxException {
    // The default way: works on JBoss/Tomcat/Jetty
    String contextPath = servletContext.getRealPath("/WEB-INF/");

    // This is how a weblogic explicitly wants the fetching of resources.
    if (contextPath == null) {
        URL url = servletContext.getResource("/WEB-INF/");
        logger.log(Level.INFO, "Fallback scenario " + url.toString());
        if (url != null) {
            logger.log(Level.INFO, "URL to config file " + url.toString());
            contextPath = url.toURI().toString();
        } else {// w ww .j a  va 2  s  . com
            throw new RuntimeException(
                    "Strange Error: /WEB-INF/ cannot be found. Check the appserver or installation directory.");
        }
    }
    return contextPath;
}