Example usage for org.apache.http.client.utils URIBuilder build

List of usage examples for org.apache.http.client.utils URIBuilder build

Introduction

In this page you can find the example usage for org.apache.http.client.utils URIBuilder build.

Prototype

public URI build() throws URISyntaxException 

Source Link

Document

Builds a URI instance.

Usage

From source file:hackathon.Hackathon.java

/**
 * @param args the command line arguments
 *//*from   w  ww .j  ava2  s . c  om*/
public static void main(String[] args) {
    // TODO code application logic here
    HttpClient httpclient = HttpClients.createDefault();

    try {
        URIBuilder builder = new URIBuilder(
                "https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize");

        URI uri = builder.build();
        HttpPost request = new HttpPost(uri);
        request.setHeader("Content-Type", "application/json");
        request.setHeader("Ocp-Apim-Subscription-Key", "3532e4ff429c4cce9baa783451db8b3b");

        // Request body
        String url = "https://s-media-cache-ak0.pinimg.com/564x/af/8b/47/af8b47d56ded6952fa8ebfdcf7e87f43.jpg";
        StringEntity reqEntity = new StringEntity(
                "{'url' : 'https://s-media-cache-ak0.pinimg.com/564x/af/8b/47/af8b47d56ded6952fa8ebfdcf7e87f43.jpg'}");
        request.setEntity(reqEntity);

        HttpResponse response = httpclient.execute(request);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            System.out.println(EntityUtils.toString(entity));
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:com.mycompany.asyncreq.MainApp.java

public static void main(String[] args) throws InterruptedException, ExecutionException, IOException {
    URIBuilder builder = new URIBuilder();
    builder.setScheme("http").setHost("comtrade.un.org").setPath("/api/get").setParameter("max", "50000")
            .setParameter("type", "C").setParameter("freq", "M").setParameter("px", "HS")
            .setParameter("ps", "2014").setParameter("r", "804").setParameter("p", "112")
            .setParameter("rg", "All").setParameter("cc", "All").setParameter("fmt", "json");
    URI requestURL = null;//from  ww  w.j ava  2 s.  co  m
    try {
        requestURL = builder.build();
    } catch (URISyntaxException use) {
    }

    ExecutorService threadpool = Executors.newFixedThreadPool(2);
    Async async = Async.newInstance().use(threadpool);
    final Request request = Request.Get(requestURL);

    try {
        Future<Content> future = async.execute(request, new FutureCallback<Content>() {
            @Override
            public void failed(final Exception e) {
                System.out.println(e.getMessage() + ": " + request);
            }

            @Override
            public void completed(final Content content) {
                System.out.println("Request completed: " + request);
                System.out.println("Response:\n" + content.asString());
            }

            @Override
            public void cancelled() {
            }
        });
    } catch (Exception e) {
        System.out.println("Job threw exception: " + e.getCause());
    }

}

From source file:com.kookoo.outbound.OutboundTest.java

public static void main(String a[]) {
    try {/*from   w  w w . j  av a  2  s. co m*/
        String server_ip = "http://XXXX:8080"; /*change to your web server*/
        String phone_no1 = "09985XXXXX";/*change number to your numbers*/
        String phone_no2 = "09985XXXXX";/*change number to your numbers*/
        String api_key = "KKXXXXX";/*kookoo api key*/
        String kookoo_number = "91xxxxx";/*kookoo assigned number*/

        Date d = new Date();
        String trackId = "" + d.getTime();
        String url = "http://kookoo.in/outbound/outbound.php";
        URIBuilder uribuilder = new URIBuilder(url);
        uribuilder.addParameter("api_key", api_key);
        uribuilder.addParameter("phone_no", phone_no1);
        uribuilder.addParameter("caller_id", kookoo_number);
        /*assigned kookoo number*/
        uribuilder.addParameter("url",
                server_ip + "/kookoocall/outboundcall?number2=" + phone_no2 + "&trackId=" + trackId);
        uribuilder.addParameter("callback_url",
                server_ip + "/kookoocall/outbound_callstatus?number2=" + phone_no2 + "&trackId=" + trackId);

        URI uri = uribuilder.build();
        System.out.println("Final Outboud API url " + uri);
        HttpGet request = new HttpGet(uri);
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);

        String responseString = new BasicResponseHandler().handleResponse(response);
        System.out.println(responseString);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:net.oneandone.shared.artifactory.Utils.java

/**
 * Builds the URI, throws an {@link IllegalArgumentException} with errorMessage when something goes wrong.
 *
 * @param uriBuilder we want to convert to an URI.
 * @param errorMessage to include in the IllegalArgumentException.
 * @return the URI.//from  w  w  w .j  ava 2  s  . co m
 */
public static URI toUri(URIBuilder uriBuilder, String errorMessage) {
    try {
        return uriBuilder.build();
    } catch (URISyntaxException ex) {
        throw new IllegalArgumentException(errorMessage, ex);
    }
}

From source file:com.infullmobile.jenkins.plugin.restrictedregister.util.AppUrls.java

@Nonnull
public static String buildSignInUrl() throws InvalidUriException {
    String ret = null;// w  w  w .  j  a  v  a 2  s  .c  o  m
    final String path = String.format(Locale.US, FORMAT_LOGIN_PATH,
            PluginModule.getDefault().getJenkinsDescriptor().getLoginURI());
    try {
        final URIBuilder builder = new URIBuilder(getRootURL());
        builder.setPath(path);
        final URI uri = builder.build();
        ret = uri.toURL().toExternalForm();
    } catch (URISyntaxException | MalformedURLException e) {
        onError(e.getLocalizedMessage());
    }
    return ret;
}

From source file:com.vmware.identity.openidconnect.common.CommonUtils.java

public static String appendQueryParameter(URI uri, String parameterName, String parameterValue) {
    Validate.notNull(uri, "uri");
    Validate.notEmpty(parameterName, "parameterName");
    Validate.notEmpty(parameterValue, "parameterValue");

    String result;// w  w  w  . j a  v a  2s .c  om
    URIBuilder uriBuilder = new URIBuilder(uri);
    uriBuilder.addParameter(parameterName, parameterValue);
    try {
        result = uriBuilder.build().toString();
    } catch (URISyntaxException e) {
        String error = String.format("failed to add parameter [%s]=[%s] to uri [%s]", parameterName,
                parameterValue, uri);
        throw new IllegalArgumentException(error, e);
    }
    return result;
}

From source file:com.appdirect.sdk.support.HttpClientHelper.java

public static HttpGet get(String endpoint, String... params) throws URISyntaxException {
    if (params.length % 2 != 0) {
        throw new IllegalArgumentException("pass params in name=value form");
    }//w ww  .  ja v  a 2  s  .  c o m

    URIBuilder builder = new URIBuilder(endpoint);
    for (int i = 0; i < params.length; i = i + 2) {
        builder.addParameter(params[i], params[i + 1]).build();
    }
    return new HttpGet(builder.build());
}

From source file:com.evolveum.midpoint.report.impl.ReportNodeUtils.java

private static URI buildURI(String intraClusterHttpUrlPattern, String host, String fileName)
        throws URISyntaxException {
    String path = intraClusterHttpUrlPattern.replace("$host", host) + ENDPOINTURIPATH;
    URIBuilder uriBuilder = new URIBuilder(path);
    uriBuilder.setParameter(ReportTypeUtil.FILENAMEPARAMETER, fileName);
    return uriBuilder.build();
}

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

/**
 * Construct SSO URL to revoke SSO token
 *
 * @param url oVirt engine URL//from  w  w  w.jav  a  2s .  c  o  m
 * @return URI to be used to revoke token
 */
public static URI buildSsoRevokeUrl(String url) {
    try {
        URI uri = new URI(url);
        URIBuilder uriBuilder = new URIBuilder(String.format("%1$s://%2$s/ovirt-engine/services/sso-logout",
                uri.getScheme(), uri.getAuthority()));
        return uriBuilder.build();
    } catch (URISyntaxException ex) {
        throw new Error("Failed to build SSO revoke URL", ex);
    }
}

From source file:manager.computerVisionManager.java

private static void getJson(String path) {
    System.out.println("Get Description from https://westus.api.cognitive.microsoft.com/vision/v1.0/describe");
    try {//  ww  w . j a  va2 s . c om
        URIBuilder builder = new URIBuilder("https://westus.api.cognitive.microsoft.com/vision/v1.0/describe");
        builder.setParameter("maxCandidates", "1");
        URI uri = builder.build();
        HttpPost request = new HttpPost(uri);
        request.setHeader("Content-Type", "application/json");
        request.setHeader("Ocp-Apim-Subscription-Key", "d7f6ef12e41c4f8c8e72d12a890fa703");
        // Request body
        StringEntity reqEntity = new StringEntity("{\"url\":\"" + path + "\"}");
        System.out.println("Request String: " + reqEntity.toString());
        request.setEntity(reqEntity);
        HttpResponse response = httpclient.execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String respuesta = EntityUtils.toString(entity);
            JSONParser lector = new JSONParser();
            StringReader SR = new StringReader(respuesta);
            try {
                JSONObject temp = (JSONObject) lector.parse(SR);
                json = (JSONObject) temp.get("description");
            } catch (org.json.simple.parser.ParseException e) {
                System.err.println(e.getMessage());
            }
        }
    } catch (IOException | URISyntaxException | ParseException e) {
        System.err.println(e.getMessage());
    }
}