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

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

Introduction

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

Prototype

public URIBuilder setParameter(final String param, final String value) 

Source Link

Document

Sets parameter of URI query overriding existing value if set.

Usage

From source file:azkaban.utils.RestfulApiClient.java

/** helper function to build a valid URI.
 *  @param host   host name.//from  w  w  w. j  a v  a  2s  .c om
 *  @param port   host port.
 *  @param path   extra path after host.
 *  @param isHttp indicates if whether Http or HTTPS should be used.
 *  @param params extra query parameters.
 *  @return the URI built from the inputs.
 *  @throws IOException
 * */
public static URI buildUri(String host, int port, String path, boolean isHttp, Pair<String, String>... params)
        throws IOException {
    URIBuilder builder = new URIBuilder();
    builder.setScheme(isHttp ? "http" : "https").setHost(host).setPort(port);

    if (null != path && path.length() > 0) {
        builder.setPath(path);
    }

    if (params != null) {
        for (Pair<String, String> pair : params) {
            builder.setParameter(pair.getFirst(), pair.getSecond());
        }
    }

    URI uri = null;
    try {
        uri = builder.build();
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }

    return uri;
}

From source file:com.vmware.identity.rest.core.client.URIFactory.java

@SuppressWarnings("unchecked")
public static URI buildURI(HostRetriever host, String path, Object... args) throws ClientException {
    Map<String, Object> parameters = null;

    if (args != null) {
        if (args[args.length - 1] instanceof Map) {
            parameters = (Map<String, Object>) args[args.length - 1];
            args = Arrays.copyOf(args, args.length - 1);
        }//from  www.j av  a  2 s .c o  m
    }

    URIBuilder builder = host.getURIBuilder().setPath(String.format(path, args));

    if (parameters != null && !parameters.isEmpty()) {
        for (Entry<String, Object> param : parameters.entrySet()) {
            if (param.getValue() instanceof Collection) {
                Collection<Object> list = (Collection<Object>) param.getValue();
                Iterator<Object> iter = list.iterator();

                while (iter.hasNext()) {
                    builder.setParameter(param.getKey(), iter.next().toString());
                }

            } else {
                builder.setParameter(param.getKey(), param.getValue().toString());
            }
        }
    }

    try {
        return builder.build();
    } catch (URISyntaxException e) {
        throw new ClientException("An error occurred while building the URI", e);
    }
}

From source file:com.teradata.tempto.internal.hadoop.hdfs.SimpleHttpRequestsExecutor.java

private URI appendUsername(URI originalUri) {
    URIBuilder uriBuilder = new URIBuilder(originalUri);
    uriBuilder.setParameter("user.name", username);
    try {//from   w  w w  .  j  av  a 2s . c o  m
        return uriBuilder.build();
    } catch (URISyntaxException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.griddynamics.jagger.providers.creators.SimpleHttpQueryCreator.java

@Override
public HttpGet createObject(String... strings) {
    URIBuilder builder = new URIBuilder();
    if (paramName != null) {
        builder.setParameter(paramName, strings[0]);
    }/* ww  w . j  a  v a 2 s  .co m*/
    if (path != null) {
        builder.setPath(path);
    }
    if (fragment != null) {
        builder.setFragment(fragment);
    }
    try {
        return new HttpGet(builder.build());
    } catch (URISyntaxException e) {
        throw new RuntimeException("URIBuilder.build()", e);
    }
}

From source file:com.epam.ngb.cli.manager.command.handler.http.DatasetDeletionHandler.java

@Override
protected void runDeletion(Long id) {
    String url = String.format(getRequestUrl(), id);
    try {//from  w w  w.  j  a v a 2 s .c o  m
        URIBuilder requestBuilder = new URIBuilder(String.format(url, projectId));
        requestBuilder.setParameter("force", String.valueOf(force));
        HttpRequestBase request = getRequest(requestBuilder.build().toString());

        setDefaultHeader(request);
        if (isSecure()) {
            addAuthorizationToRequest(request);
        }

        String result = RequestManager.executeRequest(request);
        ResponseResult response = getMapper().readValue(result,
                getMapper().getTypeFactory().constructType(ResponseResult.class));
        LOGGER.info(response.getStatus() + "\t" + response.getMessage());
    } catch (IOException | URISyntaxException e) {
        throw new ApplicationException(e.getMessage(), e);
    }
}

From source file:ch.asadzia.cognitive.SituationAnalysis.java

public ServiceResult process() {

    HttpClient httpclient = HttpClients.createDefault();

    try {/*from   w ww  .j a  va2 s  .co m*/
        URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/vision/v1.0/analyze");

        builder.setParameter("visualFeatures", "Categories,Tags,Description,Faces,Adult");

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

        // Request body
        FileEntity reqEntity = new FileEntity(imageData);
        request.setEntity(reqEntity);

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

        if (entity != null) {
            String responseStr = EntityUtils.toString(entity);

            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                System.err.println(responseStr);
                return null;
            }

            ServiceResult result = translateSituation(responseStr);

            System.out.println(responseStr);

            return result;
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return null;
}

From source file:ninja.NinjaApiDocTest.java

private void addParametersToURI(Map<String, String> parameters, URIBuilder uriBuilder) {
    if (parameters != null) {
        for (Entry<String, String> param : parameters.entrySet()) {
            uriBuilder.setParameter(param.getKey(), param.getValue());
        }//  ww w . ja va  2  s  .  c  o  m
    }
}

From source file:eu.dime.userresolver.client.ResolverClient.java

public void searchAll(String token, String name) {
    HttpGet httpGet;//from  w  ww .j  a v  a 2 s  .c om
    try {
        URIBuilder builder = new URIBuilder(serviceEnpoint + "/search");
        builder.setParameter("like", name);
        httpGet = new HttpGet(builder.build());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }

    httpGet.setHeader("Authorization", "Bearer " + token);
    try {
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String jsonResponse = IOUtils.toString(entity.getContent());
            LOG.debug("Search response: {}", jsonResponse);
        }
    } catch (IOException e) {
        LOG.debug("Unable to search", e);
    }
}

From source file:org.metaservice.manager.blazegraph.FastRangeCountRequestBuilder.java

public MutationResult execute() throws ManagerException {
    try {//from   ww  w.  j a v a  2  s  . c om
        JAXBContext jaxbContext = JAXBContext.newInstance(MutationResult.class);

        URIBuilder uriBuilder = new URIBuilder(path);
        if (subject != null) {
            uriBuilder.setParameter("s", format(subject));
        }
        if (object != null) {
            uriBuilder.setParameter("o", format(object));
        }
        if (predicate != null) {
            uriBuilder.setParameter("p", format(predicate));
        }
        if (context != null) {
            uriBuilder.setParameter("c", format(context));
        }
        uriBuilder.addParameter("ESTCARD", null);
        URI uri = uriBuilder.build();
        LOGGER.debug("QUERY = " + uri.toString());
        String s = Request.Get(uri).connectTimeout(1000).socketTimeout(10000)
                .setHeader("Accept", "application/xml").execute().returnContent().asString();
        LOGGER.debug("RESULT = " + s);
        return (MutationResult) jaxbContext.createUnmarshaller().unmarshal(new StringReader(s));
    } catch (JAXBException | URISyntaxException | IOException e) {
        throw new ManagerException(e);
    }

}

From source file:at.yawk.buycraft.BuycraftApiImpl.java

@Override
public Set<Purchase> payments(String limit, Optional<String> user) throws IOException {
    URIBuilder builder = new URIBuilder();
    builder.setParameter("limit", limit);
    user.ifPresent(u -> builder.setParameter("ign", u));
    JsonObject object = get("payments", builder);
    Set<Purchase> purchases = new HashSet<>();
    object.getAsJsonArray("payload").forEach(ele -> {
        JsonObject entry = ele.getAsJsonObject();
        List<Integer> packages = new ArrayList<>();
        entry.getAsJsonArray("packages").forEach(pack -> packages.add(pack.getAsInt()));
        Purchase purchase = new Purchase(entry.get("time").getAsLong(), entry.get("humanTime").getAsString(),
                entry.get("ign").getAsString(), entry.get("uuid").getAsString(),
                entry.get("price").getAsString(), entry.get("currency").getAsString(),
                Collections.unmodifiableCollection(packages));
        purchases.add(purchase);//www .j a  v a  2s .c om
    });
    return purchases;
}