Example usage for org.apache.http.client.utils URLEncodedUtils format

List of usage examples for org.apache.http.client.utils URLEncodedUtils format

Introduction

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

Prototype

public static String format(final Iterable<? extends NameValuePair> parameters, final Charset charset) 

Source Link

Document

Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

Usage

From source file:org.dataconservancy.dcs.integration.lineage.HttpLineageServiceIT.java

/**
 * Builds an HttpGet to use in the test given the argument and the id. 
        /* w w w.  j a  v a  2  s.c o  m*/
 * @param argument should be one of latest, original, search.
 * @param id The lineage or entity id of the object
 * @param optionalArgument can be if-modified-since or accept
 * @return The completed http request that can be passed to HttpClient
 * @throws UnsupportedEncodingException 
 * @throws URISyntaxException 
 */
private HttpGet buildRequest(String argument, String id, String... optionalArguments)
        throws UnsupportedEncodingException, URISyntaxException {
    HttpGet request = null;

    String arguments = "/lineage";

    if (argument != null && !argument.isEmpty()) {
        arguments += "/" + argument;
    }
    if (id != null && !id.isEmpty()) {
        arguments += "/" + id;
    }

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    String headerName = "";
    String headerValue = "";

    for (int i = 0; i < optionalArguments.length; i = i + 2) {
        if (!optionalArguments[i].isEmpty()) {
            if (optionalArguments[i].equalsIgnoreCase("if-modified-since")) {
                headerName = optionalArguments[i];
                headerValue = optionalArguments[i + 1];
            } else {
                params.add(new BasicNameValuePair(optionalArguments[i], optionalArguments[i + 1]));
            }
        }
    }

    URI uri = URIUtils.createURI("http", "localhost", 8080, arguments, URLEncodedUtils.format(params, "UTF-8"),
            null);

    request = new HttpGet(uri);
    if (headerName != null && !headerName.isEmpty() && headerValue != null && !headerValue.isEmpty()) {
        headerValue = DateUtility.toRfc822(Long.valueOf(headerValue));
        request.setHeader(headerName, headerValue);
    }
    return request;
}

From source file:tw.com.sti.store.api.android.AndroidApiService.java

/**
 * ??appstore(26)/*w  ww  . j a v a 2 s  .  c  o  m*/
 */
public ApiInvoker<CheckAppStoreRet> checkAppStoreList(String pkg, int ver) {
    String[] paramNames = new String[] { "ver" };
    String[] paramValues = new String[] { "" + ver };
    String url = apiUrl.getCheckAppStoreListUrl(pkg);
    ApiDataParseHandler<CheckAppStoreRet> handler = ApiDataParseHandler.APP_STORE_PARSE_HANDLER;
    List<NameValuePair> nvps = createRequestParams(paramNames, paramValues, false);
    if (Logger.DEBUG) {
        L.d(url + "?" + URLEncodedUtils.format(nvps, "UTF-8"));
    }
    return new ApiInvoker<CheckAppStoreRet>(this.config, handler, url, nvps);
}

From source file:tw.com.sti.store.api.android.AndroidApiService.java

/**
 * ??appfor app sdk(27)/*w ww.  ja  va2s.  c o  m*/
 */
public ApiInvoker<SdkAppInfoRet> getSdkAppInfo(String pkg, int ver, String dlog, String first) {
    String[] paramNames = new String[] { "ver", "dlog", "first" };
    String[] paramValues = new String[] { "" + ver, dlog, "" + first };
    String url = apiUrl.getSdkAppInfoUrl(pkg);
    ApiDataParseHandler<SdkAppInfoRet> handler = ApiDataParseHandler.SDK_APP_INFO_PARSE_HANDLER;
    List<NameValuePair> nvps = createRequestParams(paramNames, paramValues, true);
    if (Logger.DEBUG) {
        L.d(url + "?" + URLEncodedUtils.format(nvps, "UTF-8"));
    }
    return new ApiInvoker<SdkAppInfoRet>(this.config, handler, url, nvps);
}

From source file:tw.com.sti.store.api.android.AndroidApiService.java

public ApiInvoker<CommonRet> sendLogs(List<? extends LogMessage> logs) {
    StringBuilder sb = new StringBuilder("<log>");
    for (int i = 0; i < logs.size(); i++) {
        sb.append(logs.get(i).toXML());//from w  w  w  . ja  va 2  s.c  om
    }
    sb.append("</log>");
    String[] paramNames = new String[] { "log" };
    String[] paramValues = new String[] { sb.toString() };
    String url = apiUrl.getSendLogUrl();
    ApiDataParseHandler<CommonRet> handler = ApiDataParseHandler.COMMON_RET_PARSE_HANDLER;
    List<NameValuePair> nvps = createRequestParams(paramNames, paramValues, true);
    if (Logger.DEBUG) {
        L.d(url + "?" + URLEncodedUtils.format(nvps, "UTF-8"));
    }
    return new ApiInvoker<CommonRet>(this.config, handler, url, nvps);
}

From source file:com.catchnotes.api.CatchAPI.java

private HttpResponse performGET(String method, List<NameValuePair> httpParams, boolean useToken) {
    HttpGet httpget;//from   www  .j  av a2s .  c  o  m
    String uri = method;

    if (!uri.startsWith("http")) {
        // method isn't a fully-qualified URI
        uri = catchBaseUrl + uri;
    }

    if (httpParams == null || httpParams.isEmpty()) {
        httpget = new HttpGet(uri);
    } else {
        httpget = new HttpGet(uri + '?' + URLEncodedUtils.format(httpParams, "UTF-8"));
    }

    HttpResponse response = null;

    try {
        response = useToken ? getHttpClient().execute(httpget) : getHttpClientNoToken().execute(httpget);
    } catch (ClientProtocolException e) {
        log("caught ClientProtocolException performing GET " + httpget.getURI(), e);
        return null;
    } catch (UnknownHostException e) {
        log("caught UnknownHostException performing GET " + httpget.getURI(), null);
        return null;
    } catch (IOException e) {
        log("caught IOException performing GET " + httpget.getURI(), e);
        return null;
    }

    sync_trace("GET " + httpget.getURI() + " returned " + response.getStatusLine().getStatusCode() + ' '
            + response.getStatusLine().getReasonPhrase());
    return response;
}

From source file:org.auraframework.integration.test.util.WebDriverTestCase.java

/**
 * Add additional parameters to the URL. These paremeters will be added after the query string, and before a hash
 * (if present)./*  w w  w  .  j  a va 2s .  com*/
 */
public String addUrlParams(String url, Map<String, String> params) {
    // save any fragment
    int hashLoc = url.indexOf('#');
    String hash = "";
    if (hashLoc >= 0) {
        hash = url.substring(hashLoc);
        url = url.substring(0, hashLoc);
    }

    // strip query string
    int qLoc = url.indexOf('?');
    String qs = "";
    if (qLoc >= 0) {
        qs = url.substring(qLoc + 1);
        url = url.substring(0, qLoc);
    }

    // add any additional params
    List<NameValuePair> newParams = Lists.newArrayList();
    URLEncodedUtils.parse(newParams, new Scanner(qs), "UTF-8");
    for (String key : params.keySet()) {
        newParams.add(new BasicNameValuePair(key, params.get(key)));
    }

    return url + "?" + URLEncodedUtils.format(newParams, "UTF-8") + hash;
}

From source file:tw.com.sti.store.api.android.AndroidApiService.java

public ApiInvoker<OrderRet> getOrderStatus(String pkg) {
    String url = apiUrl.getOrderStatusUrl(pkg);
    ApiDataParseHandler<OrderRet> handler = ApiDataParseHandler.G_PAYMENT_RET_PARSE_HANDLER;
    List<NameValuePair> nvps = createRequestParams(null, null, true);
    if (Logger.DEBUG) {
        L.d(url + "?" + URLEncodedUtils.format(nvps, "UTF-8"));
    }/*ww  w.j  ava  2  s  .  co  m*/
    return new ApiInvoker<OrderRet>(this.config, handler, url, nvps);
}

From source file:tw.com.sti.store.api.android.AndroidApiService.java

public ApiInvoker<CommonRet> orderRefund(String orderNo, String reason) {
    String[] paramNames = new String[] { "orderNo", "reason" };
    String[] paramValues = new String[] { orderNo, reason };
    String url = apiUrl.getOrderRefundUrl();
    ApiDataParseHandler<CommonRet> handler = ApiDataParseHandler.COMMON_RET_PARSE_HANDLER;
    List<NameValuePair> nvps = createRequestParams(paramNames, paramValues, true);
    if (Logger.DEBUG) {
        L.d(url + "?" + URLEncodedUtils.format(nvps, "UTF-8"));
    }/*from  w ww.  ja v  a  2  s  .c o m*/
    return new ApiInvoker<CommonRet>(this.config, handler, url, nvps);
}