Example usage for java.net URLEncoder encode

List of usage examples for java.net URLEncoder encode

Introduction

In this page you can find the example usage for java.net URLEncoder encode.

Prototype

public static String encode(String s, Charset charset) 

Source Link

Document

Translates a string into application/x-www-form-urlencoded format using a specific java.nio.charset.Charset Charset .

Usage

From source file:com.github.khazrak.jdocker.utils.Filters.java

public static String encodeFilters(Map<String, String> filters) {
    ObjectMapper mapper = new ObjectMapper();
    String result = null;//from   w ww  . j av  a2 s  .  c  o  m
    ObjectNode objectNode = mapper.createObjectNode();
    if (!filters.keySet().isEmpty()) {
        for (Map.Entry<String, String> s : filters.entrySet()) {
            objectNode.putObject(s.getKey()).put(s.getValue(), true);
        }
    }

    try {
        result = URLEncoder.encode(objectNode.toString(), StandardCharsets.UTF_8.toString());
    } catch (UnsupportedEncodingException e) {
        logger.error("Error encoding filtersMap", e);
    }
    return result;
}

From source file:com.dubsar_dictionary.Dubsar.model.Search.java

/**
 * Instantiate a search for the specified term
 * @param term a search term, specified by the user
 *///w ww  .j av  a2 s . c  o  m
public Search(String term) {
    mTerm = new String(term);

    // DEBT: Take from strings file
    try {
        mPath = new String("/?term=") + URLEncoder.encode(mTerm, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        setErrorMessage(e.getMessage());
    }
}

From source file:com.github.thorqin.webapi.oauth2.OAuthServer.java

public static String makeAuthorizationFailedUri(String redirectionUri, OAuthError error,
        String errorDescription, String errorUri, String state) throws IOException {
    StringBuilder result = new StringBuilder(redirectionUri);
    if (redirectionUri.contains("?"))
        result.append("&error=");
    else//from   w  w w.  jav a2s . c  o m
        result.append("?error=");
    result.append(error.toString().toLowerCase());
    if (errorDescription != null)
        result.append("&error_description=").append(URLEncoder.encode(errorDescription, "utf-8"));
    if (errorUri != null)
        result.append("&error_uri=").append(URLEncoder.encode(errorUri, "utf-8"));
    if (state != null)
        result.append("&state=").append(URLEncoder.encode(state, "utf-8"));
    return result.toString();
}

From source file:com.adaptris.core.services.metadata.UrlEncodeMetadataService.java

@Override
public String reformat(String s, String msgCharset) throws Exception {
    return URLEncoder.encode(s, defaultIfEmpty(msgCharset, "UTF-8"));
}

From source file:com.symphony.jirabot.clients.QuandlClient.java

private static String urlEncodeUTF8(String string) {
    try {//from  w ww .ja va  2s  .c  om
        return URLEncoder.encode(string, "UTF-8");
    } catch (UnsupportedEncodingException error) {
        throw new UnsupportedOperationException(error);
    }
}

From source file:com.pinterest.deployservice.common.HTTPClient.java

private String generateUrlAndQuery(String url, Map<String, String> params) throws Exception {
    if (params == null || params.isEmpty()) {
        return url;
    }//from   w  ww .j a v a 2s . c o m
    StringBuilder sb = new StringBuilder();
    sb.append(url);
    String prefix = "?";
    for (Map.Entry<String, String> entry : params.entrySet()) {
        sb.append(prefix);
        prefix = "&";
        sb.append(String.format("%s=%s", entry.getKey(), URLEncoder.encode(entry.getValue(), "UTF-8")));
    }
    return sb.toString();
}

From source file:lk.appzone.client.MchoiceAventuraSmsSender.java

public MchoiceAventuraResponse sendMessage(String content, String address)
        throws MchoiceAventuraMessagingException {

    try {//from  w w w  .  j av a  2  s.com
        String urlParameters = "version=" + URLEncoder.encode("1.0", "UTF-8") + "&message="
                + URLEncoder.encode(content, "UTF-8") + "&address="
                + URLEncoder.encode("tel:" + address, "UTF-8");

        if (logger.isDebugEnabled()) {
            logger.debug("requesting - httpHost:" + this.httpHost + "auth" + this.authId + ":" + this.password
                    + "\nurlParams: " + urlParameters);
        }

        String response = excutePost(this.httpHost, this.authId, this.password, urlParameters);
        logger.info("response :" + response);

        return new MchoiceAventuraResponse(response);

    } catch (UnsupportedEncodingException e) {
        throw new MchoiceAventuraMessagingException("Encoding error in urlParams", e);
    }
}

From source file:edu.indiana.lib.twinpeaks.util.HttpTransactionUtils.java

/**
 * Format one HTTP parameter/*from  w w w.  j  a va 2s. c om*/
 * @param name Parameter name
 * @param value Parameter value (will be URLEncoded)
 * @param separator Character to separate parameters
 * @param cs Character set specification (utf-8, etc)
 * @return Parameter text (separator+name=url-encoded-value)
 */
public static String formatParameter(String name, String value, String separator, String cs) {
    StringBuilder parameter = new StringBuilder();

    if (!StringUtils.isNull(value)) {

        parameter.append(separator);
        parameter.append(name);
        parameter.append('=');

        try {
            parameter.append(URLEncoder.encode(value, cs));
        } catch (UnsupportedEncodingException exception) {
            throw new IllegalArgumentException("Invalid character set: \"" + cs + "\"");
        }
    }
    return parameter.toString();
}

From source file:me.bramhaag.discordselfbot.commands.fun.CommandLMGTFY.java

@Command(name = "lmgtfy", minArgs = 1)
public void execute(@NonNull Message message, @NonNull TextChannel channel, @NonNull String[] args) {
    String tinyURL = "http://tinyurl.com/api-create.php?url=";
    String lmgtfyURL = "http://lmgtfy.com?q=";

    String url;/*  w w  w.  j  ava2 s.c o  m*/

    try {
        if (args[0].equalsIgnoreCase("--expanded") || args[0].equalsIgnoreCase("-e") && args.length >= 2) {
            url = lmgtfyURL + URLEncoder.encode(StringUtils.join(Arrays.copyOfRange(args, 1, args.length), " "),
                    "UTF-8");
        } else {
            Document doc;
            try {
                doc = Jsoup
                        .connect(tinyURL + lmgtfyURL + URLEncoder.encode(StringUtils.join(args, " "), "UTF-8"))
                        .get();
            } catch (IOException e) {
                e.printStackTrace();

                Util.sendError(message, e.getMessage());
                return;
            }

            url = doc.body().text();
        }
    } catch (UnsupportedEncodingException e) {
        Util.sendError(message, e.getMessage());
        return;
    }

    message.editMessage("<" + url + ">").queue();
}

From source file:net.sourceforge.jwbf.actions.mw.util.PostModifyContent.java

/**
 * //from   w  w  w.j  ava  2 s .  co m
 * @param a the
 * @param tab internal value set
 * @param login a 
 */
public PostModifyContent(final ContentAccessable a, final Hashtable<String, String> tab, LoginData login) {

    String uS = "";
    try {
        uS = "/index.php?title=" + URLEncoder.encode(a.getLabel(), MediaWikiBot.CHARSET) + "&action=submit";
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    NameValuePair action = new NameValuePair("wpSave", "Save");
    NameValuePair wpStarttime = new NameValuePair("wpStarttime", tab.get("wpStarttime"));
    NameValuePair wpEditToken = new NameValuePair("wpEditToken", tab.get("wpEditToken"));
    NameValuePair wpEdittime = new NameValuePair("wpEdittime", tab.get("wpEdittime"));

    NameValuePair wpTextbox = new NameValuePair("wpTextbox1", a.getText());

    String editSummaryText = a.getEditSummary();
    if (editSummaryText != null && editSummaryText.length() > 200) {
        editSummaryText = editSummaryText.substring(0, 200);
    }

    NameValuePair wpSummary = new NameValuePair("wpSummary", editSummaryText);

    NameValuePair wpMinoredit = new NameValuePair();

    if (a.isMinorEdit()) {
        wpMinoredit.setValue("1");
        wpMinoredit.setName("wpMinoredit");
    }

    LOG.info("WRITE: " + a.getLabel());
    PostMethod pm = new PostMethod(uS);
    pm.getParams().setContentCharset(MediaWikiBot.CHARSET);

    pm.setRequestBody(new NameValuePair[] { action, wpStarttime, wpEditToken, wpEdittime, wpTextbox, wpSummary,
            wpMinoredit });
    msgs.add(pm);
}