Example usage for java.net URI URI

List of usage examples for java.net URI URI

Introduction

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

Prototype

public URI(String str) throws URISyntaxException 

Source Link

Document

Constructs a URI by parsing the given string.

Usage

From source file:net.modelbased.proasense.storage.registry.RegisterSensorSSN.java

public static String postSensor(Sensor sensor) throws RequestErrorException {
    String content = JsonPrinter.sensorToJson(sensor);
    URI target;//  w  w  w .  ja  v  a  2 s .c  o  m
    try {
        target = new URI(sensor.getUri().toString() + SENSOR_PATH);
    } catch (URISyntaxException e1) {
        e1.printStackTrace();
        throw new RequestErrorException(e1.getMessage());
    }
    HttpClient client = new DefaultHttpClient();
    HttpPost request = new HttpPost(target);
    request.setHeader("Content-type", "application/json");
    String response = null;
    try {
        StringEntity seContent = new StringEntity(content);
        seContent.setContentType("text/json");
        request.setEntity(seContent);
        response = resolveResponse(client.execute(request));
    } catch (Exception e) {
        throw new RequestErrorException(e.getMessage());
    }
    return response;
}

From source file:com.xx_dev.apn.proxy.utils.HostNamePortUtil.java

public static String getHostName(HttpRequest httpRequest) {
    String originalHostHeader = httpRequest.headers().get(HttpHeaders.Names.HOST);

    if (StringUtils.isBlank(originalHostHeader) && httpRequest.getMethod().equals(HttpMethod.CONNECT)) {
        originalHostHeader = httpRequest.getUri();
    }//from   w w  w .ja va 2  s.c o m

    if (StringUtils.isNotBlank(originalHostHeader)) {
        String originalHost = StringUtils.split(originalHostHeader, ": ")[0];
        return originalHost;
    } else {
        String uriStr = httpRequest.getUri();
        try {
            URI uri = new URI(uriStr);

            String schema = uri.getScheme();

            String originalHost = uri.getHost();

            return originalHost;
        } catch (URISyntaxException e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }
}

From source file:net.desgrange.pwad.service.UrlUtils.java

public static String getPathElement(final String url, final int elementPosition) {
    try {/* w  ww .  j av a  2  s.  c  om*/
        final String[] explodedPath = new URI(url).getPath().split("/");
        return StringUtils.defaultIfEmpty(explodedPath[elementPosition + 1], null);
    } catch (final IndexOutOfBoundsException e) {
        return null;
    } catch (final URISyntaxException e) {
        throw new BadUrlException(e);
    } catch (final NullPointerException e) {
        throw new BadUrlException(e);
    }
}

From source file:com.blackducksoftware.integration.hub.util.HubUrlParser.java

public static String getRelativeUrl(final String url) throws URISyntaxException {
    if (url == null) {
        return null;
    }//  w w w  .  j a  v a  2  s . c o m
    final String baseUrl = getBaseUrl(url);
    final URI baseUri = new URI(baseUrl);
    final URI origUri = new URI(url);
    final URI relativeUri = baseUri.relativize(origUri);
    return relativeUri.toString();
}

From source file:com.elasticbox.jenkins.k8s.repositories.api.charts.github.GitHubUrl.java

public GitHubUrl(String url) {
    this.baseUrl = normalize(url);
    try {//from  w w  w  .ja v  a  2 s . c  om
        this.parsedUrl = new URI(baseUrl);
    } catch (URISyntaxException e) {
        throw new RuntimeException("Malformed URL: " + url);
    }
}

From source file:com.zimbra.cs.httpclient.HttpProxyUtil.java

public static synchronized void configureProxy(HttpClient client) {
    try {/*from ww w .j  a va2s. c o  m*/
        String url = Provisioning.getInstance().getLocalServer().getAttr(Provisioning.A_zimbraHttpProxyURL,
                null);
        if (url == null)
            return;

        // need to initializae all the statics
        if (sProxyUrl == null || !sProxyUrl.equals(url)) {
            sProxyUrl = url;
            sProxyUri = new URI(url);
            sProxyAuthScope = null;
            sProxyCreds = null;
            String userInfo = sProxyUri.getUserInfo();
            if (userInfo != null) {
                int i = userInfo.indexOf(':');
                if (i != -1) {
                    sProxyAuthScope = new AuthScope(sProxyUri.getHost(), sProxyUri.getPort(), null);
                    sProxyCreds = new UsernamePasswordCredentials(userInfo.substring(0, i),
                            userInfo.substring(i + 1));
                }
            }
        }
        if (ZimbraLog.misc.isDebugEnabled()) {
            ZimbraLog.misc.debug("setting proxy: " + url);
        }
        client.getHostConfiguration().setProxy(sProxyUri.getHost(), sProxyUri.getPort());
        if (sProxyAuthScope != null && sProxyCreds != null)
            client.getState().setProxyCredentials(sProxyAuthScope, sProxyCreds);
    } catch (ServiceException e) {
        ZimbraLog.misc.warn("Unable to configureProxy: " + e.getMessage(), e);
    } catch (URISyntaxException e) {
        ZimbraLog.misc.warn("Unable to configureProxy: " + e.getMessage(), e);
    }
}

From source file:kuona.jenkins.analyser.JenkinsProcessor.java

public JenkinsProcessor(BuildServerSpec spec) {
    try {//from  w  w w  .j  a v a  2 s.co m
        final URI uri = new URI(spec.getUrl());
        final Project project = new Project(uri);
        this.client = new JenkinsClient(project, uri, spec.getUsername(), spec.getPassword());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.flutter.server.vmService.VmOpenSourceLocationListener.java

/**
 * Connect to the VM observatory service via the specified URI
 *
 * @return an API object for interacting with the VM service (not {@code null}).
 *///from ww w  .java  2  s  . c  o  m
public static VmOpenSourceLocationListener connect(@NotNull final String url) throws IOException {

    // Validate URL
    final URI uri;
    try {
        uri = new URI(url);
    } catch (URISyntaxException e) {
        throw new IOException("Invalid URL: " + url, e);
    }
    final String wsScheme = uri.getScheme();
    if (!"ws".equals(wsScheme) && !"wss".equals(wsScheme)) {
        throw new IOException("Unsupported URL scheme: " + wsScheme);
    }

    // Create web socket and observatory
    final WebSocket webSocket;
    try {
        webSocket = new WebSocket(uri);
    } catch (WebSocketException e) {
        throw new IOException("Failed to create websocket: " + url, e);
    }
    final VmOpenSourceLocationListener listener = new VmOpenSourceLocationListener(new MessageSender() {
        @Override
        public void sendMessage(JsonObject message) {
            try {
                webSocket.send(message.toString());
            } catch (WebSocketException e) {
                LOG.warn(e);
            }
        }

        @Override
        public void close() {
            try {
                webSocket.close();
            } catch (WebSocketException e) {
                LOG.warn(e);
            }
        }
    });

    webSocket.setEventHandler(new WebSocketEventHandler() {
        final JsonParser parser = new JsonParser();

        @Override
        public void onClose() {
            // ignore
        }

        @Override
        public void onMessage(WebSocketMessage message) {
            listener.onMessage(parser.parse(message.getText()).getAsJsonObject());
        }

        @Override
        public void onOpen() {
            listener.onOpen();
        }

        @Override
        public void onPing() {
            // ignore
        }

        @Override
        public void onPong() {
            // ignore
        }
    });

    // Establish WebSocket Connection
    try {
        webSocket.connect();
    } catch (WebSocketException e) {
        throw new IOException("Failed to connect: " + url, e);
    }
    return listener;
}

From source file:org.ovirt.engine.sdk4.internal.SsoUtils.java

/**
 * Construct SSO URL to obtain token from kerberos authentication.
 *
 * @param url oVirt engine URL//from w ww.  j  a v a  2 s .  c o m
 * @return URI to be used to obtain token
 */
public static URI buildSsoUrlKerberos(String url) {
    try {
        URI uri = new URI(url);
        URIBuilder uriBuilder = new URIBuilder(String.format("%1$s://%2$s/ovirt-engine/sso/oauth/%3$s",
                uri.getScheme(), uri.getAuthority(), ENTRY_POINT_HTTP));
        return uriBuilder.build();
    } catch (URISyntaxException ex) {
        throw new Error("Failed to build SSO authentication URL", ex);
    }
}

From source file:cz.muni.fi.pa165.rentalofconstructionmachinery.restclient.MachineRestController.java

public static List<MachineDTO> listMachines() throws URISyntaxException {
    HttpEntity re = rt.exchange(new URI(MACHINE_URL), HttpMethod.GET, new HttpEntity<>(httpHeaders),
            MachineDTO[].class);
    return Arrays.asList((MachineDTO[]) re.getBody());
}