Search in sources :

Example 51 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project musiccabinet by hakko.

the class AbstractWSGetClient method getURI.

/*
	 * Assemble URI for the Last.fm web service.
	 */
protected URI getURI(List<NameValuePair> params) throws ApplicationException {
    URI uri = null;
    try {
        URIBuilder uriBuilder = new URIBuilder();
        uriBuilder.setScheme(HTTP);
        uriBuilder.setHost(HOST);
        uriBuilder.setPath(PATH);
        for (NameValuePair param : params) {
            uriBuilder.addParameter(param.getName(), param.getValue());
        }
        uri = uriBuilder.build();
    } catch (URISyntaxException e) {
        throw new ApplicationException("Could not create Last.fm URI!", e);
    }
    return uri;
}
Also used : NameValuePair(org.apache.http.NameValuePair) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 52 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project musiccabinet by hakko.

the class AbstractWSPostClient method getURI.

/*
	 * Assemble URI for the Last.fm web service.
	 */
protected URI getURI(List<NameValuePair> params) throws ApplicationException {
    URI uri = null;
    try {
        URIBuilder uriBuilder = new URIBuilder();
        uriBuilder.setScheme(HTTP);
        uriBuilder.setHost(HOST);
        uriBuilder.setPath(PATH);
        uri = uriBuilder.build();
    } catch (URISyntaxException e) {
        throw new ApplicationException("Could not create Last.fm URI!", e);
    }
    return uri;
}
Also used : ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 53 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project platformlayer by platformlayer.

the class MetricClientImpl method getMetrics.

@Override
public MetricServiceData getMetrics(MetricQuery query) throws RestClientException {
    URI url = metricBaseUrl.resolve("api/metric/").resolve(project + "/");
    URIBuilder uriBuilder = new URIBuilder(url);
    if (query != null) {
        for (String filter : query.filters) {
            int firstEquals = filter.indexOf('=');
            if (firstEquals == -1) {
                uriBuilder.addParameter("has." + filter, "");
            } else {
                String key = filter.substring(0, firstEquals);
                String value = filter.substring(firstEquals + 1);
                uriBuilder.addParameter("filter." + key, value);
            }
        }
        for (String projection : query.projections) {
            int firstEquals = projection.indexOf('=');
            if (firstEquals == -1) {
                uriBuilder.addParameter("select." + projection, "");
            } else {
                throw new IllegalArgumentException();
            }
        }
        if (query.flatten) {
            uriBuilder.addParameter("flatten", "true");
        }
    }
    try {
        url = uriBuilder.build();
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Error building URI", e);
    }
    HttpGet request = new HttpGet(url);
    HttpResponse response = null;
    try {
        response = httpClient.execute(request);
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() != 200) {
            log.info("Error reading from metrics service: " + statusLine);
            throw new RestClientException("Error reading from metrics service", null, statusLine.getStatusCode());
        } else {
            MetricServiceData ret = new MetricServiceData(request, response);
            // Don't close yet
            response = null;
            return ret;
        }
    } catch (IOException e) {
        throw new RestClientException("Error reading from metrics service", e);
    } finally {
        if (response != null) {
            try {
                EntityUtils.consume(response.getEntity());
            } catch (IOException e) {
                log.warn("Error consuming response", e);
            }
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpGet(org.apache.http.client.methods.HttpGet) RestClientException(org.platformlayer.rest.RestClientException) HttpResponse(org.apache.http.HttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 54 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project wechat-mp-sdk by usc.

the class AccessServletRealTest method testGet.

private static void testGet() throws Exception {
    String echoStr = RandomStringUtils.randomAlphabetic(36);
    URI uri = new URIBuilder("http://localhost/wechat/mp/access").addParameter("echostr", echoStr).addParameter("nonce", "1366260493").addParameter("timestamp", "1366238018").addParameter("signature", "9498eeaaca0b8a35f1b027498e0c0fb37fec5fa0").build();
    String rtn = Request.Get(uri).execute().returnContent().asString();
    System.out.println(echoStr + " ==> " + rtn);
}
Also used : URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 55 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project wechat-mp-sdk by usc.

the class AccessServletRealTest method testPost.

private static void testPost() throws Exception {
    URI uri = new URIBuilder("http://localhost/wechat/mp/access").addParameter("nonce", "1366260493").addParameter("timestamp", "1366238018").addParameter("signature", "9498eeaaca0b8a35f1b027498e0c0fb37fec5fa0").build();
    File parent = new File(AccessServletRealTest.class.getClassLoader().getResource("push").toURI());
    for (File pushFile : parent.listFiles()) {
        String body = FileUtils.readFileToString(pushFile);
        String reply = Request.Post(uri).bodyString(body, ContentType.create("text/html", Consts.UTF_8)).execute().returnContent().asString();
        System.out.println(pushFile + "\n" + body + "\n" + reply + "\n");
    }
}
Also used : URI(java.net.URI) File(java.io.File) URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

URIBuilder (org.apache.http.client.utils.URIBuilder)107 URISyntaxException (java.net.URISyntaxException)42 URI (java.net.URI)37 HttpGet (org.apache.http.client.methods.HttpGet)22 IOException (java.io.IOException)21 NameValuePair (org.apache.http.NameValuePair)13 HttpEntity (org.apache.http.HttpEntity)10 NotNull (org.jetbrains.annotations.NotNull)9 Map (java.util.Map)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 HttpResponse (org.apache.http.HttpResponse)7 List (java.util.List)6 HttpClient (org.apache.http.client.HttpClient)5 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 Gson (com.google.gson.Gson)4 URL (java.net.URL)4 RequestConfig (org.apache.http.client.config.RequestConfig)4 HttpPost (org.apache.http.client.methods.HttpPost)4