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:com.whatsthatlight.teamcity.hipchat.test.SimpleServerTest.java

@Test
public void testTestServer() throws Exception {
    // Test parameters
    String expectedResponse = "<h1>Hello World</h1>";
    int expectedStatusCode = HttpServletResponse.SC_OK;
    int port = 8080;
    URI uri = new URI(String.format("http://localhost:%s/", port));

    // Setup/*  w w  w . java2 s.co m*/
    SimpleServer server = new SimpleServer(port, new SimpleHandler(expectedResponse, expectedStatusCode));
    server.start();

    // Make request
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet getRequest = new HttpGet(uri.toString());
    HttpResponse getResponse = client.execute(getRequest);
    int actualStatusCode = getResponse.getStatusLine().getStatusCode();
    String actualResponse = EntityUtils.toString(getResponse.getEntity());

    // Clean up
    server.stop();

    // Test
    assertEquals(expectedStatusCode, actualStatusCode);
    assertEquals(expectedResponse, actualResponse);
}

From source file:com.mindquarry.desktop.workspace.SVNProxyHandler.java

public static void applyProxySettings(List<Profile> profiles, String proxyURL, String proxyLogin,
        String proxyPwd) throws Exception {
    removeProxySettings();//from  ww  w  . j  a v  a 2 s .co m

    Ini ini = getParser();
    Ini.Section groups = ini.get("groups");

    int number = 0;
    for (Profile profile : profiles) {
        URI uri = new URI(profile.getServerURL());

        String groupID = GROUP_PREFIX + number++;
        groups.put(groupID, uri.getHost());

        uri = new URI(proxyURL);

        Ini.Section section = ini.add(groupID);
        section.put(PROXY_HOST, uri.getHost());
        if (uri.getPort() != -1) {
            section.put(PROXY_PORT, String.valueOf(uri.getPort()));
        }
        if (!proxyLogin.equals("")) {
            section.put(PROXY_USERNAME, proxyLogin);
        }
        if (!proxyPwd.equals("")) {
            section.put(PROXY_PASSWORD, proxyPwd);
        }
    }
    storeConfig(ini);
}

From source file:org.dojotoolkit.zazl.internal.XMLHttpRequestUtils.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static String xhrRequest(String shrDataString) {
    InputStream is = null;//from  w w w.ja va 2s  .c  om
    String json = null;

    try {
        logger.logp(Level.FINER, XMLHttpRequestUtils.class.getName(), "xhrRequest",
                "shrDataString [" + shrDataString + "]");

        Map<String, Object> xhrData = (Map<String, Object>) JSONParser.parse(new StringReader(shrDataString));
        String url = (String) xhrData.get("url");
        String method = (String) xhrData.get("method");
        List headers = (List) xhrData.get("headers");
        URL requestURL = createURL(url);
        URI uri = new URI(requestURL.toString());

        HashMap httpMethods = new HashMap(7);
        httpMethods.put("DELETE", new HttpDelete(uri));
        httpMethods.put("GET", new HttpGet(uri));
        httpMethods.put("HEAD", new HttpHead(uri));
        httpMethods.put("OPTIONS", new HttpOptions(uri));
        httpMethods.put("POST", new HttpPost(uri));
        httpMethods.put("PUT", new HttpPut(uri));
        httpMethods.put("TRACE", new HttpTrace(uri));
        HttpUriRequest request = (HttpUriRequest) httpMethods.get(method.toUpperCase());

        if (request.equals(null)) {
            throw new Error("SYNTAX_ERR");
        }

        for (Object header : headers) {
            StringTokenizer st = new StringTokenizer((String) header, ":");
            String name = st.nextToken();
            String value = st.nextToken();
            request.addHeader(name, value);
        }

        HttpClient client = new DefaultHttpClient();

        HttpResponse response = client.execute(request);
        Map headerMap = new HashMap();

        HeaderIterator headerIter = response.headerIterator();

        while (headerIter.hasNext()) {
            Header header = headerIter.nextHeader();
            headerMap.put(header.getName(), header.getValue());
        }

        int status = response.getStatusLine().getStatusCode();
        String statusText = response.getStatusLine().toString();

        is = response.getEntity().getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        StringBuffer sb = new StringBuffer();

        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append('\n');
        }
        Map m = new HashMap();
        m.put("status", new Integer(status));
        m.put("statusText", statusText);
        m.put("responseText", sb.toString());
        m.put("headers", headerMap.toString());
        StringWriter w = new StringWriter();
        JSONSerializer.serialize(w, m);
        json = w.toString();
        logger.logp(Level.FINER, XMLHttpRequestUtils.class.getName(), "xhrRequest", "json [" + json + "]");
    } catch (Throwable e) {
        logger.logp(Level.SEVERE, XMLHttpRequestUtils.class.getName(), "xhrRequest",
                "Failed request for [" + shrDataString + "]", e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
    return json;
}

From source file:ru.prbb.activeagent.services.AbstractChecker.java

protected AbstractChecker(String host) throws URISyntaxException {
    uri = new URI(host);
    mapper = new ObjectMapper();
    //mapper.setDateFormat(new SimpleDateFormat("yyyyMMdd"));
    exec = Executors.newSingleThreadScheduledExecutor();
}

From source file:cop.raml.processor.SimpleJavaFileObjectImpl.java

public SimpleJavaFileObjectImpl(File file, Kind kind) throws URISyntaxException {
    super(new URI("string:///" + file.getName()), kind);
    this.file = file;
}

From source file:org.altchain.neo4j.database.Database.java

public static void addMetadataToProperty(URI relationshipUri, String name, String value)
        throws URISyntaxException {
    URI propertyUri = new URI(relationshipUri.toString() + "/properties");
    String entity = toJsonNameValuePairCollection(name, value);
    WebResource resource = Client.create().resource(propertyUri);
    ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
            .entity(entity).put(ClientResponse.class);

    logger.debug(/*from   w ww . j  a  va  2 s . c  o m*/
            String.format("PUT [%s] to [%s], status code [%d]", entity, propertyUri, response.getStatus()));
    response.close();
}

From source file:org.eclipse.thym.core.internal.util.HttpUtil.java

/**
 * Set the proxy settings from ProxyService.
 * This method sets a {@link HttpRoutePlanner} to the client
 * /*ww  w  .ja v a  2 s . c o  m*/
 * @param client
 */
public static void setupProxy(final DefaultHttpClient client) {
    client.setRoutePlanner(new HttpRoutePlanner() {

        @Override
        public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context)
                throws HttpException {

            //use forced route if one exists
            HttpRoute route = ConnRouteParams.getForcedRoute(request.getParams());
            if (route != null)
                return route;

            // if layered, is it secure?
            final Scheme scheme = client.getConnectionManager().getSchemeRegistry().getScheme(target);
            final boolean secure = scheme.isLayered();

            HttpHost host = null;
            try {
                IProxyData[] proxyDatas = getEclipseProxyData(new URI(target.toURI()));
                for (IProxyData data : proxyDatas) {
                    if (data.getType().equals(IProxyData.HTTP_PROXY_TYPE)) {
                        host = new HttpHost(data.getHost(), data.getPort());
                        break;
                    }
                }
            } catch (URISyntaxException e) {
                HybridCore.log(IStatus.ERROR, "Incorrect URI", e);
            }
            if (host == null) {
                return new HttpRoute(target, null, secure);
            }
            return new HttpRoute(target, null, host, secure);
        }
    });

}

From source file:eu.over9000.skadi.util.HttpUtil.java

public static String getAPIResponse(final String apiUrl) throws URISyntaxException, IOException {
    final URI URL = new URI(apiUrl);
    final HttpGet request = new HttpGet(URL);
    request.setHeader("Client-ID", CLIENT_ID);
    final HttpResponse response = HTTP_CLIENT.execute(request);
    return EntityUtils.toString(response.getEntity(), "UTF-8");
}

From source file:fr.pingtimeout.ConnectionUtils.java

public static boolean pingServer(String serverHostname) {
    String pingUrlAsString = String.format("http://%s:8080/ping", serverHostname);
    Log.d(loggerName, "Pinging server " + pingUrlAsString);

    BufferedReader bufferedReader = null;
    try {/*from  w  w w. j  a  v  a2 s . co m*/
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(new URI(pingUrlAsString));

        HttpResponse response = client.execute(request);

        bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        while (bufferedReader.readLine() != null) {
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException ignored) {
            }
        }
    }

    return true;
}

From source file:com.ettrema.httpclient.LockMethod.java

public LockMethod(String uri) throws URISyntaxException {
    setURI(new URI(uri));
}